gfxconversion/mifconv/src/mifconv_bitmapconverter.cpp
changeset 0 f453ebb75370
child 2 1f6339ced17d
equal deleted inserted replaced
-1:000000000000 0:f453ebb75370
       
     1 /*
       
     2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). 
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:  Mifconv bitmap converters class.
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include "mifconv.h"
       
    20 #include "mifconv_bitmapconverter.h"
       
    21 #include "mifconv_util.h"
       
    22 #include "mifconv_exception.h"
       
    23 #include "mifconv_argumentmanager.h"
       
    24 #include <stdio.h>
       
    25 
       
    26 const MifConvString BMCONV_DEFAULT_PATH(EPOC_TOOLS_PATH);
       
    27 
       
    28 /**
       
    29  *
       
    30  */
       
    31 MifConvBitmapConverter::MifConvBitmapConverter()
       
    32 {
       
    33     MifConvArgumentManager* argMgr = MifConvArgumentManager::Instance();
       
    34 	// Output file:
       
    35 	iTargetFilename = MifConvUtil::FilenameWithoutExtension(argMgr->TargetFile()) + "." + MifConvString(MBM_FILE_EXTENSION);
       
    36 }
       
    37 
       
    38 /**
       
    39  *
       
    40  */
       
    41 MifConvBitmapConverter::~MifConvBitmapConverter()
       
    42 {
       
    43 }
       
    44 
       
    45 /**
       
    46  *
       
    47  */
       
    48 void MifConvBitmapConverter::Init()
       
    49 {
       
    50     CleanupTargetFiles();
       
    51 }
       
    52 
       
    53 /**
       
    54  *
       
    55  */
       
    56 void MifConvBitmapConverter::CleanupTargetFiles()
       
    57 {
       
    58 	if( MifConvUtil::FileExists(iTargetFilename) )
       
    59 	{
       
    60         // Try to remove file MIFCONV_MAX_REMOVE_TRIES times, no exception in case of failure:
       
    61         MifConvUtil::RemoveFile(iTargetFilename, MIFCONV_MAX_REMOVE_TRIES, true);
       
    62 	}
       
    63 }
       
    64 
       
    65 /**
       
    66  *
       
    67  */
       
    68 void MifConvBitmapConverter::AppendFile( const MifConvSourceFile& sourcefile )
       
    69 {    
       
    70 	if( MifConvUtil::FileExtension( sourcefile.Filename() ) == BMP_FILE_EXTENSION )
       
    71 	{
       
    72 		iSourceFiles.push_back( sourcefile );
       
    73 	}
       
    74 }
       
    75 
       
    76 /**
       
    77  *
       
    78  */
       
    79 void MifConvBitmapConverter::Convert()
       
    80 {
       
    81     if( iSourceFiles.size() > 0 )
       
    82     {
       
    83 	    ConvertToMbm();
       
    84     }
       
    85 }
       
    86 
       
    87 /**
       
    88  *
       
    89  */
       
    90 void MifConvBitmapConverter::Cleanup(bool err)
       
    91 {
       
    92 	CleanupTempFiles();
       
    93 	if( err )
       
    94 	{
       
    95 	    CleanupTargetFiles();
       
    96 	}
       
    97 }
       
    98 
       
    99 /**
       
   100  *
       
   101  */
       
   102 void MifConvBitmapConverter::ConvertToMbm()
       
   103 {    
       
   104     RunBmconv();
       
   105 }
       
   106 
       
   107 /**
       
   108  *
       
   109  */
       
   110 void MifConvBitmapConverter::InitTempFile()
       
   111 {
       
   112     MifConvArgumentManager* argMgr = MifConvArgumentManager::Instance();
       
   113     // Construct temp file name
       
   114     iTempDir = MifConvUtil::DefaultTempDirectory();
       
   115     const MifConvString& tempDirArg = argMgr->StringValue(MifConvTempPathArg);
       
   116     if( tempDirArg.length() > 0 )
       
   117     {
       
   118         iTempDir = tempDirArg;
       
   119     }
       
   120 
       
   121     if( iTempDir.length() > 0 && iTempDir.at(iTempDir.length()-1) != DIR_SEPARATOR2 )
       
   122     {
       
   123         iTempDir.append(DIR_SEPARATOR);
       
   124     }
       
   125 
       
   126     // Generate new temp-filename:
       
   127     iTempDir.append(MifConvUtil::TemporaryFilename());
       
   128 
       
   129     // append tmp at as postfix
       
   130     // this is needed because the generated name can contain a single period '.'
       
   131     // character as the last character which is eaten away when the directory created.
       
   132     iTempDir.append(MifConvString("tmp"));
       
   133 
       
   134     MifConvUtil::EnsurePathExists(iTempDir);
       
   135 
       
   136     iTempDir.append(DIR_SEPARATOR);
       
   137 
       
   138     iTempFilename = iTempDir + MifConvUtil::FilenameWithoutExtension(MifConvUtil::FilenameWithoutPath(argMgr->TargetFile()));
       
   139     iTempFilename += BMCONV_TEMP_FILE_POSTFIX;
       
   140 
       
   141     // Create temp file
       
   142     fstream tempFile(iTempFilename.c_str(), ios::out|ios::binary|ios::trunc);
       
   143     if (!tempFile.is_open())
       
   144     {        
       
   145         throw MifConvException(MifConvString("Unable to create tmp file! ") + iTempFilename);        
       
   146     }
       
   147 
       
   148     try {
       
   149         // quiet mode        
       
   150         tempFile << BMCONV_OPTION_PREFIX << BMCONV_QUIET_PARAMETER << " ";
       
   151         // Palette argument
       
   152         const MifConvString& paletteArg = argMgr->StringValue(MifConvPaletteFileArg);
       
   153         if( paletteArg.length() > 0 )
       
   154         {
       
   155             tempFile << BMCONV_OPTION_PREFIX << BMCONV_PALETTE_PARAMETER;            
       
   156             tempFile << MifConvString(paletteArg + " ");
       
   157         }
       
   158 
       
   159         tempFile << iTargetFilename << " ";                
       
   160         // Add filenames to the temp file
       
   161         for( MifConvSourceFileList::iterator i = iSourceFiles.begin(); i != iSourceFiles.end(); ++i )
       
   162         {
       
   163             AppendBmpToTempFile(tempFile, *i);
       
   164         }
       
   165     }
       
   166     catch(...) {
       
   167         tempFile.close();
       
   168         throw;
       
   169     }        
       
   170 
       
   171     tempFile.close();
       
   172 }
       
   173 
       
   174 /**
       
   175  *
       
   176  */
       
   177 void MifConvBitmapConverter::RunBmconv()
       
   178 {
       
   179     MifConvArgumentManager* argMgr = MifConvArgumentManager::Instance();
       
   180     // Create and initialize the temp file:    
       
   181     InitTempFile();
       
   182 
       
   183     // Build bmconv command    
       
   184     MifConvString bmconvCommand("\""); // Open " mark
       
   185     
       
   186     const MifConvString& bmconvPath = argMgr->StringValue(MifConvBmconvPathArg);
       
   187     const MifConvString& defaultBmconvPath = GetDefaultBmConvPath();
       
   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:
       
   198     if( bmconvCommand.length() > 1 && bmconvCommand.at(bmconvCommand.length()-1) != DIR_SEPARATOR2 )
       
   199         bmconvCommand += DIR_SEPARATOR;
       
   200 
       
   201     // Then add bmconv executable call and close the " mark
       
   202     bmconvCommand += BMCONV_EXECUTABLE_NAME + MifConvString("\" ");  
       
   203     bmconvCommand += "\"" + iTempFilename + "\"";
       
   204         
       
   205     MifConvUtil::EnsurePathExists(iTargetFilename, true);
       
   206     
       
   207     cout << "Writing mbm: " << iTargetFilename << endl;           
       
   208     int err = 0;
       
   209     
       
   210 #ifdef __linux__
       
   211     if ((err = system (MifConvString(bmconvCommand).c_str())) != 0)   // Returns 0 if success
       
   212 #else
       
   213     if ((err = system (MifConvString("\""+bmconvCommand+"\"").c_str())) != 0)   // Returns 0 if success
       
   214 #endif
       
   215     {
       
   216     	THROW_ERROR_COMMON("Executing BMCONV failed", MifConvString(__FILE__), __LINE__);
       
   217     }
       
   218 }
       
   219 
       
   220 /**
       
   221  *
       
   222  */
       
   223 void MifConvBitmapConverter::CleanupTempFiles()
       
   224 {
       
   225     if( iTempFilename.length() > 0 && remove( iTempFilename.c_str() ) != 0 )
       
   226     {
       
   227         perror( "Error deleting temporary file (bitmap conversion)" );
       
   228     }
       
   229     
       
   230     if( iTempDir.length() > 0 && MifConvUtil::RemoveDirectory( iTempDir ) != 0 )
       
   231     {
       
   232         perror( "Error deleting temporary directory (bitmap conversion)" );
       
   233     }
       
   234 }
       
   235 
       
   236 /**
       
   237  *
       
   238  */
       
   239 const MifConvString& MifConvBitmapConverter::GetDefaultBmConvPath()
       
   240 {
       
   241     if( iDefaultBmConvPath.length() == 0 )
       
   242     {        
       
   243         // Check if the EPOCROOT is given
       
   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;
       
   263     aStream << bmpFile.DepthString();
       
   264     aStream << bmpFile.Filename();
       
   265     aStream << " ";
       
   266         
       
   267     // Prepare also for the case that mask is not used at all.
       
   268     const MifConvString& maskName = bmpFile.BmpMaskFilename();
       
   269     if (maskName.length() > 0 )
       
   270     {
       
   271         cout << "Loading file: " << maskName << endl;
       
   272         aStream << BMCONV_OPTION_PREFIX;
       
   273         aStream << bmpFile.MaskDepthString();
       
   274         aStream << maskName;        
       
   275     }
       
   276     aStream << " ";    
       
   277     }