gfxconversion/mifconv/src/mifconv_bitmapconverter.cpp
changeset 12 9b222d6b819d
parent 0 f453ebb75370
equal deleted inserted replaced
0:f453ebb75370 12:9b222d6b819d
    19 #include "mifconv.h"
    19 #include "mifconv.h"
    20 #include "mifconv_bitmapconverter.h"
    20 #include "mifconv_bitmapconverter.h"
    21 #include "mifconv_util.h"
    21 #include "mifconv_util.h"
    22 #include "mifconv_exception.h"
    22 #include "mifconv_exception.h"
    23 #include "mifconv_argumentmanager.h"
    23 #include "mifconv_argumentmanager.h"
       
    24 #include "mifconv_mbmgenerator.h"
       
    25 #include "mifconv_mbmgenerator_pbmcomp.h"
    24 #include <stdio.h>
    26 #include <stdio.h>
    25 
    27 #include <sys/stat.h>
    26 const MifConvString BMCONV_DEFAULT_PATH(EPOC_TOOLS_PATH);
    28 
       
    29 using namespace std;
       
    30 
       
    31 #ifdef __linux__
       
    32 const char* DefaultPaletteFileSearchLocations[] =
       
    33 {
       
    34     "/epoc32/include/mw/ThirdPartyBitmap.pal",
       
    35     "/epoc32/include/middleware/ThirdPartyBitmap.pal",
       
    36     "/epoc32/include/ThirdPartyBitmap.pal"
       
    37 };
       
    38 #else
       
    39 const char* DefaultPaletteFileSearchLocations[] =
       
    40 {
       
    41     "\\epoc32\\include\\mw\\ThirdPartyBitmap.pal",
       
    42     "\\epoc32\\include\\middleware\\ThirdPartyBitmap.pal",
       
    43     "\\epoc32\\include\\ThirdPartyBitmap.pal"
       
    44 };
       
    45 #endif
    27 
    46 
    28 /**
    47 /**
    29  *
    48  *
    30  */
    49  */
    31 MifConvBitmapConverter::MifConvBitmapConverter()
    50 MifConvBitmapConverter::MifConvBitmapConverter()
    32 {
    51 {
    33     MifConvArgumentManager* argMgr = MifConvArgumentManager::Instance();
    52     MifConvArgumentManager* argMgr = MifConvArgumentManager::Instance();
    34 	// Output file:
    53     // Output file:
    35 	iTargetFilename = MifConvUtil::FilenameWithoutExtension(argMgr->TargetFile()) + "." + MifConvString(MBM_FILE_EXTENSION);
    54     iTargetFilename = MifConvUtil::FilenameWithoutExtension(argMgr->TargetFile()) + "." + MifConvString(MBM_FILE_EXTENSION);
    36 }
    55 }
    37 
    56 
    38 /**
    57 /**
    39  *
    58  *
    40  */
    59  */
    53 /**
    72 /**
    54  *
    73  *
    55  */
    74  */
    56 void MifConvBitmapConverter::CleanupTargetFiles()
    75 void MifConvBitmapConverter::CleanupTargetFiles()
    57 {
    76 {
    58 	if( MifConvUtil::FileExists(iTargetFilename) )
    77     if( MifConvUtil::FileExists(iTargetFilename) )
    59 	{
    78     {
    60         // Try to remove file MIFCONV_MAX_REMOVE_TRIES times, no exception in case of failure:
    79         // Try to remove file MIFCONV_MAX_REMOVE_TRIES times, no exception in case of failure:
    61         MifConvUtil::RemoveFile(iTargetFilename, MIFCONV_MAX_REMOVE_TRIES, true);
    80         MifConvUtil::RemoveFile(iTargetFilename, MIFCONV_MAX_REMOVE_TRIES, true);
    62 	}
    81     }
    63 }
    82 }
    64 
    83 
    65 /**
    84 /**
    66  *
    85  *
    67  */
    86  */
    68 void MifConvBitmapConverter::AppendFile( const MifConvSourceFile& sourcefile )
    87 void MifConvBitmapConverter::AppendFile( const MifConvSourceFile& aSourcefile )
    69 {    
    88 {    
    70 	if( MifConvUtil::FileExtension( sourcefile.Filename() ) == BMP_FILE_EXTENSION )
    89     if( MifConvUtil::FileExtension( aSourcefile.Filename() ) == BMP_FILE_EXTENSION )
    71 	{
    90     {
    72 		iSourceFiles.push_back( sourcefile );
    91         iSourceFiles.push_back( aSourcefile );
    73 	}
    92     }
    74 }
    93 }
    75 
    94 
    76 /**
    95 /**
    77  *
    96  *
    78  */
    97  */
    79 void MifConvBitmapConverter::Convert()
    98 void MifConvBitmapConverter::Convert()
    80 {
    99 {
    81     if( iSourceFiles.size() > 0 )
   100     if( iSourceFiles.size() > 0 )
    82     {
   101     {
    83 	    ConvertToMbm();
   102         ConvertToMbm();
    84     }
   103     }
    85 }
   104 }
    86 
   105 
    87 /**
   106 /**
    88  *
   107  *
    89  */
   108  */
    90 void MifConvBitmapConverter::Cleanup(bool err)
   109 void MifConvBitmapConverter::Cleanup(bool err)
    91 {
   110 {
    92 	CleanupTempFiles();
   111     CleanupTempFiles();
    93 	if( err )
   112     if( err )
    94 	{
   113     {
    95 	    CleanupTargetFiles();
   114         CleanupTargetFiles();
    96 	}
   115     }
    97 }
   116 }
    98 
   117 
    99 /**
   118 /**
   100  *
   119  *
   101  */
   120  */
   102 void MifConvBitmapConverter::ConvertToMbm()
   121 void MifConvBitmapConverter::ConvertToMbm()
   103 {    
   122 {   
   104     RunBmconv();
   123     MifConvArgumentManager* argMgr = MifConvArgumentManager::Instance();
   105 }
   124     const MifConvString& bmconvPath = argMgr->StringValue(MifConvBmconvPathArg);
       
   125     
       
   126     // If bmconv.exe's location is given.
       
   127     if( bmconvPath.length() > 0 ) 
       
   128     {
       
   129         // Create and initialize the temp file.  
       
   130         InitTempFile();
       
   131     
       
   132         // Run external bmconv.
       
   133         RunExternalBmconv(bmconvPath);
       
   134     }
       
   135     else // Use internal logic.
       
   136     {
       
   137         // Create the path if not existing.
       
   138         MifConvUtil::EnsurePathExists(iTargetFilename, true);
       
   139         
       
   140         cout << "Writing mbm: " << iTargetFilename << endl; 
       
   141     
       
   142         // Variable for source files.
       
   143         MifConvSourceFileList sourceFiles;
       
   144         
       
   145         // Get palette file name from either arguments or default values.
       
   146         MifConvString palettefilename = GetPaletteFileName();
       
   147         
       
   148         // Add filenames to the temp file.
       
   149         // Sourcefiles and their mask files are considered as same.
       
   150         for( MifConvSourceFileList::iterator i = iSourceFiles.begin(); i != iSourceFiles.end(); ++i )
       
   151             {               
       
   152             cout << "Loading file: " << i->Filename() << endl;
       
   153             sourceFiles.push_back( *i );              
       
   154             // Prepare also for the case that mask is not used at all.
       
   155             if (i->BmpMaskFilename().length() > 0 )
       
   156                 {
       
   157                 cout << "Loading file: " << i->BmpMaskFilename() << endl;
       
   158                 MifConvSourceFile maskFile;
       
   159                 maskFile.SetFilename(i->BmpMaskFilename());
       
   160                 maskFile.SetDepthString(i->MaskDepthString());
       
   161                 sourceFiles.push_back( maskFile );
       
   162                 }
       
   163             }
       
   164        
       
   165         // Create internal bmconv to handle compiling.
       
   166         BmConv bmConv = BmConv(sourceFiles);
       
   167         int ret = bmConv.Compile(iTargetFilename.c_str(), palettefilename); 
       
   168         if (ret) {
       
   169             THROW_ERROR_COMMON("BMP to MBM conversion failed", MifConvString(__FILE__), __LINE__);
       
   170         }
       
   171 
       
   172     }
       
   173 }
       
   174 
       
   175 const MifConvString MifConvBitmapConverter::GetPaletteFileName() 
       
   176     {
       
   177     
       
   178     // Get palette argument.
       
   179     MifConvArgumentManager* argMgr = MifConvArgumentManager::Instance();
       
   180     const MifConvString& paletteArg = argMgr->StringValue(MifConvPaletteFileArg);
       
   181     
       
   182     MifConvString palettefilename;       
       
   183     MifConvString paletteBuf;
       
   184     
       
   185     // If palette argument has been given.
       
   186     if( paletteArg.length() == 0 )
       
   187         {
       
   188         // Default palette file.
       
   189         const MifConvString& defaultPalettefile = DefaultPaletteFileName(paletteBuf);
       
   190         
       
   191         // Palette file not given in arguments. If the default palette file
       
   192         // was found, use it, otherwise throw error.
       
   193         if( defaultPalettefile.length() > 0 )
       
   194             {
       
   195             palettefilename = defaultPalettefile;           
       
   196             }
       
   197         else
       
   198             {
       
   199             // Palettefile not given in arguments nor found from default locations:
       
   200             THROW_ERROR_COMMON("Unable to open palette file from default locations! ", MifConvString(__FILE__), __LINE__ );
       
   201             }
       
   202         } else {
       
   203         palettefilename = paletteArg;           
       
   204         }   
       
   205     
       
   206     // Check that palette file exists.
       
   207     if( !MifConvUtil::FileExists(palettefilename) )
       
   208     {
       
   209         THROW_ERROR_COMMON("Unable to open palette file! " + paletteArg, MifConvString(__FILE__), __LINE__ );
       
   210     }
       
   211     
       
   212     return palettefilename;
       
   213     }
       
   214 
       
   215 const MifConvString& MifConvBitmapConverter::DefaultPaletteFileName(MifConvString& aBuf)
       
   216     {
       
   217     int numOfSearchLocations = sizeof(DefaultPaletteFileSearchLocations)/sizeof(char*);
       
   218     
       
   219     // Get epocroot.
       
   220     MifConvString epocRoot(MifConvArgumentManager::Instance()->EpocRoot());
       
   221     
       
   222     for( int i = 0; i < numOfSearchLocations; ++i )
       
   223         {
       
   224         aBuf = epocRoot;
       
   225         aBuf += DefaultPaletteFileSearchLocations[i];
       
   226         
       
   227         // Remove possible double-\.
       
   228         if ((aBuf.at(1) == '\\') && (aBuf.at(0) == '\\')) aBuf = aBuf.substr(1, aBuf.length()-1);
       
   229         if( MifConvUtil::FileExists(aBuf))
       
   230             {
       
   231             return aBuf;
       
   232             }
       
   233         }
       
   234     aBuf = "";
       
   235     return aBuf;
       
   236     }
   106 
   237 
   107 /**
   238 /**
   108  *
   239  *
   109  */
   240  */
   110 void MifConvBitmapConverter::InitTempFile()
   241 void MifConvBitmapConverter::InitTempFile()
   111 {
   242 {
       
   243     // Take an instance from the argument manager.
   112     MifConvArgumentManager* argMgr = MifConvArgumentManager::Instance();
   244     MifConvArgumentManager* argMgr = MifConvArgumentManager::Instance();
   113     // Construct temp file name
   245 
       
   246     // Construct temp file name.
   114     iTempDir = MifConvUtil::DefaultTempDirectory();
   247     iTempDir = MifConvUtil::DefaultTempDirectory();
       
   248     
   115     const MifConvString& tempDirArg = argMgr->StringValue(MifConvTempPathArg);
   249     const MifConvString& tempDirArg = argMgr->StringValue(MifConvTempPathArg);
   116     if( tempDirArg.length() > 0 )
   250     if( tempDirArg.length() > 0 )
   117     {
   251     {
   118         iTempDir = tempDirArg;
   252         iTempDir = tempDirArg;
   119     }
   253     }
   135 
   269 
   136     iTempDir.append(DIR_SEPARATOR);
   270     iTempDir.append(DIR_SEPARATOR);
   137 
   271 
   138     iTempFilename = iTempDir + MifConvUtil::FilenameWithoutExtension(MifConvUtil::FilenameWithoutPath(argMgr->TargetFile()));
   272     iTempFilename = iTempDir + MifConvUtil::FilenameWithoutExtension(MifConvUtil::FilenameWithoutPath(argMgr->TargetFile()));
   139     iTempFilename += BMCONV_TEMP_FILE_POSTFIX;
   273     iTempFilename += BMCONV_TEMP_FILE_POSTFIX;
   140 
   274     
   141     // Create temp file
   275     // Create temp file
   142     fstream tempFile(iTempFilename.c_str(), ios::out|ios::binary|ios::trunc);
   276     fstream tempFile(iTempFilename.c_str(), ios::out|ios::binary|ios::trunc);
   143     if (!tempFile.is_open())
   277     if (!tempFile.is_open())
   144     {        
   278     {        
   145         throw MifConvException(MifConvString("Unable to create tmp file! ") + iTempFilename);        
   279         throw MifConvException(MifConvString("Unable to create tmp file! ") + iTempFilename);    
   146     }
   280     }
   147 
   281 
   148     try {
   282     try {
   149         // quiet mode        
   283         // quiet mode
       
   284         // ex. "-q"
   150         tempFile << BMCONV_OPTION_PREFIX << BMCONV_QUIET_PARAMETER << " ";
   285         tempFile << BMCONV_OPTION_PREFIX << BMCONV_QUIET_PARAMETER << " ";
       
   286         
   151         // Palette argument
   287         // Palette argument
   152         const MifConvString& paletteArg = argMgr->StringValue(MifConvPaletteFileArg);
   288         const MifConvString& paletteArg = argMgr->StringValue(MifConvPaletteFileArg);
   153         if( paletteArg.length() > 0 )
   289         if( paletteArg.length() > 0 )
   154         {
   290         {
       
   291             // ex. "-p"
   155             tempFile << BMCONV_OPTION_PREFIX << BMCONV_PALETTE_PARAMETER;            
   292             tempFile << BMCONV_OPTION_PREFIX << BMCONV_PALETTE_PARAMETER;            
   156             tempFile << MifConvString(paletteArg + " ");
   293             tempFile << MifConvString(paletteArg + " ");
   157         }
   294         }
   158 
   295 
   159         tempFile << iTargetFilename << " ";                
   296         tempFile << iTargetFilename << " ";
       
   297         
   160         // Add filenames to the temp file
   298         // Add filenames to the temp file
   161         for( MifConvSourceFileList::iterator i = iSourceFiles.begin(); i != iSourceFiles.end(); ++i )
   299         for( MifConvSourceFileList::iterator i = iSourceFiles.begin(); i != iSourceFiles.end(); ++i )
   162         {
   300         {
   163             AppendBmpToTempFile(tempFile, *i);
   301             AppendBmpToTempFile(tempFile, *i);
   164         }
   302         }
   165     }
   303     }
   166     catch(...) {
   304     catch(...) {
   167         tempFile.close();
   305         tempFile.close();
   168         throw;
   306         throw;
   169     }        
   307     }    
   170 
       
   171     tempFile.close();
   308     tempFile.close();
   172 }
   309 }
   173 
   310 
   174 /**
   311 /**
   175  *
   312  * Run external bmconv from given path.
   176  */
   313  */
   177 void MifConvBitmapConverter::RunBmconv()
   314 void MifConvBitmapConverter::RunExternalBmconv( const MifConvString& aBmconvPath )
   178 {
   315 {    
   179     MifConvArgumentManager* argMgr = MifConvArgumentManager::Instance();
       
   180     // Create and initialize the temp file:    
       
   181     InitTempFile();
       
   182 
       
   183     // Build bmconv command    
   316     // Build bmconv command    
   184     MifConvString bmconvCommand("\""); // Open " mark
   317     MifConvString bmconvCommand("\""); // Open " mark
   185     
   318     
   186     const MifConvString& bmconvPath = argMgr->StringValue(MifConvBmconvPathArg);
   319     bmconvCommand += aBmconvPath; // If the path is given, use it.
   187     const MifConvString& defaultBmconvPath = GetDefaultBmConvPath();
   320         
   188     if( bmconvPath.length() > 0 )
       
   189     {
       
   190         bmconvCommand += bmconvPath; // If the path is given, use it.
       
   191     }
       
   192     else
       
   193     {
       
   194         bmconvCommand += defaultBmconvPath; // Use default path
       
   195     }
       
   196 
       
   197     // Ensure that the last char of the path is dir-separator:
   321     // Ensure that the last char of the path is dir-separator:
   198     if( bmconvCommand.length() > 1 && bmconvCommand.at(bmconvCommand.length()-1) != DIR_SEPARATOR2 )
   322     if( bmconvCommand.length() > 1 && bmconvCommand.at(bmconvCommand.length()-1) != DIR_SEPARATOR2 )
   199         bmconvCommand += DIR_SEPARATOR;
   323         bmconvCommand += DIR_SEPARATOR;
   200 
   324 
   201     // Then add bmconv executable call and close the " mark
   325     // Then add bmconv executable call and close the " mark
   202     bmconvCommand += BMCONV_EXECUTABLE_NAME + MifConvString("\" ");  
   326     bmconvCommand += BMCONV_EXECUTABLE_NAME + MifConvString("\" ");  
   203     bmconvCommand += "\"" + iTempFilename + "\"";
   327     bmconvCommand += "\"" + iTempFilename + "\"";
   204         
   328             
   205     MifConvUtil::EnsurePathExists(iTargetFilename, true);
   329     MifConvUtil::EnsurePathExists(iTargetFilename, true);
   206     
   330         
   207     cout << "Writing mbm: " << iTargetFilename << endl;           
   331     cout << "Writing mbm: " << iTargetFilename << endl;           
   208     int err = 0;
   332     int err = 0;
   209     
   333       
   210 #ifdef __linux__
   334 #ifdef __linux__
   211     if ((err = system (MifConvString(bmconvCommand).c_str())) != 0)   // Returns 0 if success
   335     if ((err = system (MifConvString(bmconvCommand).c_str())) != 0)   // Returns 0 if success
   212 #else
   336 #else
   213     if ((err = system (MifConvString("\""+bmconvCommand+"\"").c_str())) != 0)   // Returns 0 if success
   337     if ((err = system (MifConvString("\""+bmconvCommand+"\"").c_str())) != 0)   // Returns 0 if success
   214 #endif
   338 #endif
   215     {
   339     {
   216     	THROW_ERROR_COMMON("Executing BMCONV failed", MifConvString(__FILE__), __LINE__);
   340             THROW_ERROR_COMMON("Executing BMCONV failed", MifConvString(__FILE__), __LINE__);
   217     }
   341     }
   218 }
   342 }
   219 
   343 
   220 /**
   344 /**
   221  *
   345  *
   234 }
   358 }
   235 
   359 
   236 /**
   360 /**
   237  *
   361  *
   238  */
   362  */
   239 const MifConvString& MifConvBitmapConverter::GetDefaultBmConvPath()
   363 void MifConvBitmapConverter::AppendBmpToTempFile(fstream& aStream, const MifConvSourceFile& aBmpFile)
   240 {
   364     {
   241     if( iDefaultBmConvPath.length() == 0 )
   365     cout << "Loading file: " << aBmpFile.Filename() << endl;
   242     {        
   366     
   243         // Check if the EPOCROOT is given
   367     // ex. "-8..\..\bitmaps\mifconv_test_bitmap_01.bmp"
   244         MifConvString epocRoot(MifConvArgumentManager::Instance()->EpocRoot());
       
   245         if( epocRoot.length() > 0 )
       
   246         {
       
   247             // EPOCROOT environment variable defined.
       
   248             iDefaultBmConvPath = epocRoot + BMCONV_DEFAULT_PATH;
       
   249         }        
       
   250     }
       
   251 
       
   252     return iDefaultBmConvPath;
       
   253 }
       
   254 
       
   255 /**
       
   256  *
       
   257  */
       
   258 void MifConvBitmapConverter::AppendBmpToTempFile(fstream& aStream, const MifConvSourceFile& bmpFile)
       
   259     {
       
   260     cout << "Loading file: " << bmpFile.Filename() << endl;
       
   261 
       
   262     aStream << BMCONV_OPTION_PREFIX;
   368     aStream << BMCONV_OPTION_PREFIX;
   263     aStream << bmpFile.DepthString();
   369     aStream << aBmpFile.DepthString();
   264     aStream << bmpFile.Filename();
   370     aStream << aBmpFile.Filename();
   265     aStream << " ";
   371     aStream << " ";
   266         
   372         
   267     // Prepare also for the case that mask is not used at all.
   373     // Prepare also for the case that mask is not used at all.
   268     const MifConvString& maskName = bmpFile.BmpMaskFilename();
   374     const MifConvString& maskName = aBmpFile.BmpMaskFilename();
   269     if (maskName.length() > 0 )
   375     if (maskName.length() > 0 )
   270     {
   376     {
   271         cout << "Loading file: " << maskName << endl;
   377         cout << "Loading file: " << maskName << endl;
       
   378         
       
   379         // ex. -8..\..\bitmaps\mifconv_test_bitmap_01_mask_soft.bmp 
   272         aStream << BMCONV_OPTION_PREFIX;
   380         aStream << BMCONV_OPTION_PREFIX;
   273         aStream << bmpFile.MaskDepthString();
   381         aStream << aBmpFile.MaskDepthString();
   274         aStream << maskName;        
   382         aStream << maskName;      
   275     }
   383     }
   276     aStream << " ";    
   384     aStream << " ";    
   277     }
   385     }