homesync/contentmanager/cmserver/cmmemorymanager/src/cmmmimagemetadataresolver.cpp
branchIOP_Improvements
changeset 40 08b5eae9f9ff
parent 39 6369bfd1b60d
child 41 b4d83ea1d6e2
equal deleted inserted replaced
39:6369bfd1b60d 40:08b5eae9f9ff
     1 /*
       
     2 * Copyright (c) 2008 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:  Image metadata resolver class in Memory manager component
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include <e32std.h>
       
    20 #include <ExifModify.h>
       
    21 #include <ExifTag.h> 
       
    22 #include <exifutility.h>
       
    23 #include <ExifRead.h> 
       
    24 #include "msdebug.h"
       
    25 #include "cmmmimagemetadataresolver.h"
       
    26 
       
    27 const TUint16 KIdFNumber = 0x829D;
       
    28 const TUint16 KIdFocalLength = 0x920A;
       
    29 const TUint16 KIdFocalLength35mmFilm = 0xA405;
       
    30 const TUint16 KIdFocalPlaneResolutionUnit = 0xA210;
       
    31 const TUint16 KIdFocalPlaneXResolution = 0xA20E;
       
    32 const TUint16 KIdFocalPlaneYResolution = 0xA20F;
       
    33 const TUint32 KDefaultPixelsPerResolutionUnitNumerator = 99;
       
    34 const TUint32 KDefaultPixelsPerResolutionUnitDenominator = 66;
       
    35 const TUint16 KDefaultYCbCrPositioningCentered = 1;
       
    36 const TUint16 KDefaultColorSpace_sRGB = 1;
       
    37 
       
    38 // ---------------------------------------------------------------------------
       
    39 // CCmMmImageMetadataResolver::NewL
       
    40 // ---------------------------------------------------------------------------
       
    41 //
       
    42 CCmMmImageMetadataResolver* CCmMmImageMetadataResolver::NewL( 
       
    43     RFs& aFileSession )
       
    44     {
       
    45     LOG(_L("[MEMORY MNGR]\t CCmMmImageMetadataResolver::NewL() start"));
       
    46     CCmMmImageMetadataResolver* self = 
       
    47         CCmMmImageMetadataResolver::NewLC( aFileSession );
       
    48     CleanupStack::Pop( self );
       
    49     LOG(_L("[MEMORY MNGR]\t CCmMmImageMetadataResolver::NewL() end"));
       
    50     return self;
       
    51     }
       
    52 
       
    53 // ---------------------------------------------------------------------------
       
    54 // CCmMmImageMetadataResolver::NewLC
       
    55 // ---------------------------------------------------------------------------
       
    56 //
       
    57 CCmMmImageMetadataResolver* CCmMmImageMetadataResolver::NewLC( 
       
    58     RFs& aFileSession )
       
    59     {
       
    60     LOG(_L("[MEMORY MNGR]\t CCmMmImageMetadataResolver::NewLC() start"));
       
    61     CCmMmImageMetadataResolver* self = 
       
    62         new ( ELeave ) CCmMmImageMetadataResolver( aFileSession );
       
    63     CleanupStack::PushL( self );
       
    64     self->ConstructL();
       
    65     LOG(_L("[MEMORY MNGR]\t CCmMmImageMetadataResolver::NewLC() end"));
       
    66     return self;
       
    67     }
       
    68 
       
    69 // ---------------------------------------------------------------------------
       
    70 // C++ destructor
       
    71 // ---------------------------------------------------------------------------
       
    72 //
       
    73 CCmMmImageMetadataResolver::~CCmMmImageMetadataResolver()
       
    74     {
       
    75     LOG(_L("[MEMORY MNGR]\t CCmMmImageMetadataResolver::~CCmMmImageMetadataResolver() start"));
       
    76     delete iExifOrig;
       
    77     LOG(_L("[MEMORY MNGR]\t CCmMmImageMetadataResolver::~CCmMmImageMetadataResolver() end"));
       
    78     }
       
    79 
       
    80 // ---------------------------------------------------------------------------
       
    81 // CCmMmImageMetadataResolver::CaptureOrginalMetadataL
       
    82 // ---------------------------------------------------------------------------
       
    83 //
       
    84 void CCmMmImageMetadataResolver::CaptureOrginalMetadataL( 
       
    85     const TDesC& aOrigFileName )
       
    86     {
       
    87     RFile orig;
       
    88     User::LeaveIfError( orig.Open( iFileSession, aOrigFileName, EFileRead ) );    
       
    89     CleanupClosePushL( orig );        
       
    90     TInt size = 0;
       
    91     orig.Size(size);
       
    92     delete iExifOrig;
       
    93     iExifOrig = NULL;
       
    94     iExifOrig = HBufC8::NewL( size );
       
    95     TPtr8 bufferDes( iExifOrig->Des() );
       
    96     User::LeaveIfError( orig.Read( bufferDes ) );
       
    97     CleanupStack::PopAndDestroy();    
       
    98     }
       
    99     
       
   100 // ---------------------------------------------------------------------------
       
   101 // CCmMmImageMetadataResolver::ResolveMetadataL
       
   102 // ---------------------------------------------------------------------------
       
   103 //
       
   104 void CCmMmImageMetadataResolver::ResolveMetadataL(
       
   105     const TDesC& aShrinkedFileName )
       
   106     {
       
   107     RFile shrinked;
       
   108     User::LeaveIfError( 
       
   109         shrinked.Open( iFileSession, aShrinkedFileName, EFileRead|EFileWrite ) );
       
   110     
       
   111     CleanupClosePushL( shrinked );         
       
   112     
       
   113     // 1. Read Exif image from the file to a buffer...
       
   114     TInt sizeS = 0;
       
   115     shrinked.Size(sizeS);
       
   116     HBufC8* exifShrinked = HBufC8::NewL( sizeS );
       
   117     CleanupStack::PushL( exifShrinked );
       
   118     TPtr8 bufferDesS( exifShrinked->Des() );
       
   119     User::LeaveIfError( shrinked.Read( bufferDesS ) );
       
   120     
       
   121     CExifModify* modify = NULL;
       
   122     CExifRead* read = NULL;
       
   123     TRAPD( err1, read = CExifRead::NewL( iExifOrig->Des() ) );
       
   124     User::LeaveIfError( err1 );
       
   125     // 2. Instantiate Exif modifier in EModify mode...
       
   126     TRAPD( err, modify = CExifModify::NewL( exifShrinked->Des() ) );
       
   127     if(err == KErrCorrupt)
       
   128         {
       
   129         modify = CExifModify::NewL( exifShrinked->Des(), CExifModify::ECreate );
       
   130         }
       
   131     CleanupStack::PushL(modify);
       
   132     // If exif info fails, there is nothing we can do
       
   133     TRAP_IGNORE( FillExifL( *read, *modify ) );         
       
   134     
       
   135     // 4. Get the modified Exif image...
       
   136     // If zero length descriptor is given instead of exif->Des(), then only the
       
   137     // Exif meta data is returned
       
   138     TInt pos = 0;
       
   139     User::LeaveIfError( shrinked.Seek( ESeekStart, pos ) );    
       
   140     HBufC8* modifiedExif = NULL;
       
   141     TRAPD( err2, modifiedExif = modify->WriteDataL( exifShrinked->Des() ) );
       
   142     /* Process the modified Exif data */
       
   143     if( modifiedExif && !err2 )
       
   144         {
       
   145         shrinked.Write( *modifiedExif, modifiedExif->Des().Length() );
       
   146         }    
       
   147     
       
   148     delete modifiedExif;
       
   149     modifiedExif = NULL;
       
   150 
       
   151     // 5. Delete the modifier instance...
       
   152     CleanupStack::PopAndDestroy( modify );
       
   153     CleanupStack::PopAndDestroy( exifShrinked );
       
   154     CleanupStack::PopAndDestroy(); 
       
   155     delete iExifOrig;
       
   156     iExifOrig = NULL; 
       
   157     
       
   158     }
       
   159     
       
   160 // ---------------------------------------------------------------------------
       
   161 // C++ constructor
       
   162 // ---------------------------------------------------------------------------
       
   163 //
       
   164 CCmMmImageMetadataResolver::CCmMmImageMetadataResolver( RFs& aFileSession ) 
       
   165     : iFileSession( aFileSession )
       
   166     {
       
   167     LOG(_L("[MEMORY MNGR]\t CCmMmImageMetadataResolver::CCmMmImageMetadataResolver() start"));
       
   168     LOG(_L("[MEMORY MNGR]\t CCmMmImageMetadataResolver::CCmMmImageMetadataResolver() end"));
       
   169     }
       
   170 
       
   171 // ---------------------------------------------------------------------------
       
   172 // CCmMmImageMetadataResolver::ConstructL
       
   173 // ---------------------------------------------------------------------------
       
   174 //
       
   175 void CCmMmImageMetadataResolver::ConstructL()
       
   176     {
       
   177     LOG(_L("[MEMORY MNGR]\t CCmMmImageMetadataResolver::ConstructL() start"));
       
   178     LOG(_L("[MEMORY MNGR]\t CCmMmImageMetadataResolver::ConstructL() end"));
       
   179     }
       
   180 
       
   181 // ---------------------------------------------------------------------------
       
   182 // CCmMmImageMetadataResolver::FillExifL
       
   183 // ---------------------------------------------------------------------------
       
   184 //
       
   185 void CCmMmImageMetadataResolver::FillExifL( CExifRead& aRead, CExifModify& aModify )
       
   186     {    
       
   187     TRAP_IGNORE( aModify.SetImageDescriptionL( *aRead.GetImageDescriptionL() ) );
       
   188     TRAP_IGNORE( aModify.SetMakeL( *aRead.GetMakeL() ) );
       
   189     TRAP_IGNORE( aModify.SetModelL( *aRead.GetModelL() ) ); 
       
   190     
       
   191     TRAP_IGNORE( aModify.SetThumbnailL( *aRead.GetThumbnailL() ) );
       
   192     TRAP_IGNORE( aModify.SetTransferFunctionL( *aRead.GetTransferFunctionL() ) );
       
   193     TRAP_IGNORE( aModify.SetDateTimeL( *aRead.GetDateTimeL() ) );         
       
   194     
       
   195     TRAP_IGNORE( aModify.SetSoftwareL( *aRead.GetSoftwareL() ) );
       
   196     TRAP_IGNORE( aModify.SetCopyrightL( *aRead.GetCopyrightL() ) );
       
   197 
       
   198     aModify.SetXResolutionL(
       
   199         KDefaultPixelsPerResolutionUnitNumerator,
       
   200         KDefaultPixelsPerResolutionUnitDenominator );
       
   201     aModify.SetYResolutionL( 
       
   202         KDefaultPixelsPerResolutionUnitNumerator, 
       
   203         KDefaultPixelsPerResolutionUnitDenominator );
       
   204 
       
   205     aModify.SetYCbCrPositioningL( KDefaultYCbCrPositioningCentered );
       
   206 
       
   207     aModify.SetColorSpaceL( KDefaultColorSpace_sRGB );
       
   208     
       
   209     TUint32 uint32Value = 0;
       
   210     TUint16 uint16Value = 0;
       
   211     HBufC8* textValue = NULL;
       
   212     const CExifTag* exifTagInfo = NULL;
       
   213     TInt exifError( KErrNone );
       
   214     exifError = aRead.GetPixelXDimension( uint32Value );
       
   215     if( exifError == KErrNone )
       
   216         { 
       
   217         TRAP_IGNORE( aModify.SetPixelXDimensionL( uint32Value ) );
       
   218         }
       
   219     exifError = aRead.GetPixelYDimension( uint32Value );
       
   220     if( exifError == KErrNone )
       
   221         { 
       
   222         TRAP_IGNORE( aModify.SetPixelYDimensionL( uint32Value ) );
       
   223         }
       
   224     
       
   225     exifError = aRead.GetWhiteBalance( uint16Value );
       
   226     if( exifError == KErrNone )
       
   227         {
       
   228         aModify.SetWhiteBalanceL( uint16Value );
       
   229         }
       
   230     exifError = aRead.GetFlash( uint16Value );
       
   231     if( exifError == KErrNone )
       
   232         {
       
   233         aModify.SetFlashL( uint16Value );        
       
   234         }
       
   235 
       
   236     exifError = aRead.GetExposureProgram( uint16Value );
       
   237     if( exifError == KErrNone )
       
   238         {
       
   239         aModify.SetExposureProgramL( uint16Value );
       
   240         }
       
   241 
       
   242 
       
   243     TRAP( exifError, textValue = aRead.GetUserCommentL() );
       
   244 
       
   245     if ( exifError == KErrNone )
       
   246         {
       
   247         aModify.SetUserCommentL( *textValue );
       
   248         }
       
   249 
       
   250     TRAP( exifError, textValue = aRead.GetDateTimeOriginalL() );
       
   251     if ( exifError == KErrNone )
       
   252         {
       
   253         aModify.SetDateTimeOriginalL( *textValue );
       
   254         }
       
   255 
       
   256     TRAP( exifError, textValue = aRead.GetDateTimeDigitizedL() );
       
   257     if ( exifError == KErrNone )
       
   258         {
       
   259         aModify.SetDateTimeDigitizedL( *textValue );
       
   260         }
       
   261 
       
   262     exifError = aRead.GetOrientation( uint16Value );
       
   263     if ( exifError == KErrNone )
       
   264         {
       
   265         aModify.SetOrientationL( uint16Value );
       
   266         }
       
   267 
       
   268     exifError = aRead.GetYCbCrPositioning( uint16Value );
       
   269     if ( exifError == KErrNone )
       
   270         {
       
   271         aModify.SetYCbCrPositioningL( uint16Value );
       
   272         }
       
   273 
       
   274     exifError = aRead.GetResolutionUnit( uint16Value );
       
   275     if ( exifError == KErrNone )
       
   276         {
       
   277         aModify.SetResolutionUnitL( uint16Value );
       
   278         }
       
   279 
       
   280     TRAP( exifError, textValue = aRead.GetIsoSpeedRatingsL() );
       
   281     if ( exifError == KErrNone )
       
   282         {
       
   283         aModify.SetIsoSpeedRatingsL( *textValue );
       
   284         }
       
   285 
       
   286     TRAP( exifError, textValue = aRead.GetRelatedSoundFileL() );
       
   287     if ( exifError == KErrNone )
       
   288         {
       
   289         aModify.SetRelatedSoundFileL( *textValue );
       
   290         }
       
   291                 
       
   292     TUint32 exifDenominator = 0;
       
   293     exifError = aRead.GetExposureTime( uint32Value, exifDenominator );
       
   294     if ( exifError == KErrNone )
       
   295         {
       
   296         aModify.SetExposureTimeL( uint32Value, exifDenominator );
       
   297         }
       
   298         
       
   299     exifDenominator = 0;
       
   300     exifError = aRead.GetApertureValue( uint32Value, exifDenominator );
       
   301     if ( exifError == KErrNone )
       
   302         {
       
   303         aModify.SetApertureValueL( uint32Value, exifDenominator );
       
   304         }        
       
   305 
       
   306     exifError = aRead.GetColorSpace( uint16Value );
       
   307     if ( exifError == KErrNone )
       
   308         {
       
   309         aModify.SetColorSpaceL( uint16Value );
       
   310         }
       
   311 
       
   312     TInt32 exifExposureB = 0;
       
   313     TInt32 exifDenominatorB = 0;
       
   314     exifError = aRead.GetExposureBiasValue( exifExposureB, exifDenominatorB );
       
   315     if ( exifError == KErrNone )
       
   316         {
       
   317         aModify.SetExposureBiasValueL( exifExposureB, exifDenominatorB );
       
   318         }
       
   319 
       
   320     exifError = aRead.GetMeteringMode( uint16Value );
       
   321     if ( exifError == KErrNone )
       
   322         {               
       
   323         aModify.SetMeteringModeL( uint16Value );
       
   324         }
       
   325 
       
   326     exifDenominator = 0;
       
   327     exifError = aRead.GetThumbnailXResolution( uint32Value, exifDenominator );
       
   328     if ( exifError == KErrNone )
       
   329         {        
       
   330         aModify.SetThumbnailXResolutionL( uint32Value, exifDenominator );
       
   331         }
       
   332     
       
   333     exifDenominator = 0;
       
   334     exifError = aRead.GetThumbnailYResolution( uint32Value, exifDenominator );
       
   335     if ( exifError == KErrNone )
       
   336         {        
       
   337         aModify.SetThumbnailYResolutionL( uint32Value, exifDenominator );
       
   338         } 
       
   339        
       
   340     exifError = aRead.GetThumbnailResolutionUnit( uint16Value );
       
   341     if ( exifError == KErrNone )
       
   342         {
       
   343         aModify.SetThumbnailResolutionUnitL( uint16Value );
       
   344         }
       
   345 
       
   346 
       
   347     TInt32 exifShutterSpeedValue = 0;
       
   348     TInt32 exifDenominatorS = 0;
       
   349     exifError = aRead.GetShutterSpeedValue( exifShutterSpeedValue, exifDenominatorS );
       
   350     if ( exifError == KErrNone )
       
   351         {
       
   352         aModify.SetShutterSpeedValueL( exifShutterSpeedValue, exifDenominatorS );
       
   353         }
       
   354 
       
   355     TUint8 exifComponent4th( 0 );
       
   356     TUint8 exifComponent3rd( 0 );
       
   357     TUint8 exifComponent2nd( 0 );
       
   358     TUint8 exifComponent1st( 0 );
       
   359 
       
   360     exifError = aRead.GetComponentsConfiguration(
       
   361     		exifComponent1st, exifComponent2nd, exifComponent3rd, exifComponent4th );
       
   362     if ( exifError == KErrNone )
       
   363         {
       
   364         aModify.SetComponentsConfigurationL(
       
   365         		exifComponent1st, exifComponent2nd, 
       
   366         		exifComponent3rd, exifComponent4th );
       
   367         }
       
   368         
       
   369     TRAP( exifError, exifTagInfo = aRead.GetTagL( EIfdExif, KIdFNumber ) );
       
   370     if ( exifError == KErrNone )
       
   371         {
       
   372         TPtrC8 exifFNumberBuf( exifTagInfo->Data() );
       
   373         TExifTagInfo tagInfo( KIdFNumber, CExifTag::ETagRational, 1 );
       
   374         aModify.SetTagL( EIfdExif, tagInfo, exifFNumberBuf );
       
   375         }
       
   376         
       
   377 
       
   378     TRAP( exifError, exifTagInfo = aRead.GetTagL( EIfdExif, KIdFocalLength ) );
       
   379     if ( exifError == KErrNone )
       
   380         {
       
   381         TPtrC8 exifFocalLengthBuf( exifTagInfo->Data() );
       
   382         TExifTagInfo tagInfo( KIdFocalLength, CExifTag::ETagRational, 1 );
       
   383         TRAP_IGNORE( aModify.SetTagL( EIfdExif, tagInfo, exifFocalLengthBuf ) );
       
   384         }
       
   385         
       
   386     TRAP( exifError, exifTagInfo = aRead.GetTagL( EIfdExif, KIdFocalLength35mmFilm ) );
       
   387     if ( exifError == KErrNone )
       
   388         {
       
   389         TPtrC8 exifFocal35mmBuf( exifTagInfo->Data() );
       
   390         TExifTagInfo tagInfo( KIdFocalLength35mmFilm, CExifTag::ETagRational, 1 );
       
   391         TRAP_IGNORE( aModify.SetTagL( EIfdExif, tagInfo, exifFocal35mmBuf ) );
       
   392         }                
       
   393 
       
   394     TRAP( exifError, exifTagInfo = aRead.GetTagL( EIfdExif, KIdFocalPlaneResolutionUnit ) );
       
   395     if ( exifError == KErrNone )
       
   396         {
       
   397         TPtrC8 exifFocalPlaneResolutionUnitBuf( exifTagInfo->Data() );
       
   398         TExifTagInfo tagInfo( KIdFocalPlaneResolutionUnit, CExifTag::ETagShort, 1 );
       
   399         TRAP_IGNORE( aModify.SetTagL( EIfdExif, tagInfo, exifFocalPlaneResolutionUnitBuf ) );
       
   400         }        
       
   401 
       
   402     TRAP( exifError, exifTagInfo = aRead.GetTagL( EIfdExif, KIdFocalPlaneXResolution ) );
       
   403     if ( exifError == KErrNone )
       
   404         {
       
   405         TPtrC8 exifFocalPlaneXResBuf( exifTagInfo->Data() );
       
   406         TExifTagInfo tagInfo( KIdFocalPlaneXResolution, CExifTag::ETagRational, 1 );
       
   407         TRAP_IGNORE( aModify.SetTagL( EIfdExif, tagInfo, exifFocalPlaneXResBuf ) );
       
   408         }
       
   409         
       
   410     TRAP( exifError, exifTagInfo = aRead.GetTagL( EIfdExif, KIdFocalPlaneYResolution ) );
       
   411     if ( exifError == KErrNone )
       
   412         {
       
   413         TPtrC8 exifFocalPlaneYResBuf( exifTagInfo->Data() );
       
   414         TExifTagInfo tagInfo( KIdFocalPlaneYResolution, CExifTag::ETagRational, 1 );
       
   415         TRAP_IGNORE( aModify.SetTagL( EIfdExif, tagInfo, exifFocalPlaneYResBuf ) );
       
   416         }
       
   417          
       
   418     exifError = aRead.GetLightSource( uint16Value );
       
   419     if ( exifError == KErrNone )
       
   420         {
       
   421         TRAP_IGNORE( aModify.SetLightSourceL( uint16Value ) );
       
   422         }
       
   423                                                                                                                         
       
   424     }
       
   425 
       
   426 // End of file
       
   427