imagehandlingutilities/thumbnailmanager/plugins/audio/src/thumbnailimagedecoderv3.cpp
changeset 54 48dd0f169f0d
parent 42 2e2a89493e2b
equal deleted inserted replaced
42:2e2a89493e2b 54:48dd0f169f0d
     1 /*
       
     2 * Copyright (c) 2006-2007 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 thumbnail decoder
       
    15  *
       
    16 */
       
    17 
       
    18 
       
    19 //INCLUDE FILES
       
    20 #include <e32base.h>
       
    21 #include <imageconversion.h>
       
    22 #include <ExifRead.h>
       
    23 
       
    24 #include <IclExtJpegApi.h>
       
    25 #include "thumbnailimagedecoderv3.h"
       
    26 #include "thumbnaillog.h"
       
    27 #include "thumbnailpanic.h"
       
    28 #include "OstTraceDefinitions.h"
       
    29 #ifdef OST_TRACE_COMPILER_IN_USE
       
    30 #include "thumbnailimagedecoderv3Traces.h"
       
    31 #endif
       
    32 
       
    33 
       
    34 
       
    35 // CImageDecoder supports up to 1/8 size reduction if EFullyScaleable is
       
    36 // not set.
       
    37 const TInt KMaximumReductionFactor = 8;
       
    38 
       
    39 
       
    40 // ============================ MEMBER FUNCTIONS ===============================
       
    41 
       
    42 // ---------------------------------------------------------------------------
       
    43 // CThumbnailImageDecoder::CThumbnailImageDecoder()
       
    44 // C++ default constructor can NOT contain any code, that might leave.
       
    45 // ---------------------------------------------------------------------------
       
    46 //
       
    47 CThumbnailImageDecoderv3::CThumbnailImageDecoderv3( RFs& aFs ): 
       
    48     CActive(EPriorityStandard ), iBitmap( NULL ), iFs( aFs ), iBuffer( NULL )
       
    49     {
       
    50     CActiveScheduler::Add( this );
       
    51     }
       
    52 
       
    53 
       
    54 // ---------------------------------------------------------------------------
       
    55 // CThumbnailImageDecoderv3::~CThumbnailImageDecoderv3()
       
    56 // Destructor.
       
    57 // ---------------------------------------------------------------------------
       
    58 //
       
    59 CThumbnailImageDecoderv3::~CThumbnailImageDecoderv3()
       
    60     {
       
    61     Release();
       
    62     }
       
    63 
       
    64 
       
    65 // -----------------------------------------------------------------------------
       
    66 // CThumbnailImageDecoderv3::CreateL()
       
    67 // Creates thumbnail of image
       
    68 // -----------------------------------------------------------------------------
       
    69 //
       
    70 void CThumbnailImageDecoderv3::CreateL( const TDesC8* aBuffer, MThumbnailProviderObserver&
       
    71     aObserver, const CThumbnailManager::TThumbnailFlags /*aFlags*/, const
       
    72     TDataType& aMimeType, const TSize& aSize )
       
    73     {
       
    74 
       
    75     TN_DEBUG1( "CCThumbnailImageDecoderv3::CreateL() called" );
       
    76     OstTrace0( TRACE_NORMAL, CTHUMBNAILIMAGEDECODERV3_CREATEL, "CThumbnailImageDecoderv3::CreateL" );
       
    77 
       
    78     iSize = aSize;
       
    79     iMimeType = aMimeType;
       
    80     iObserver = &aObserver;
       
    81     iBuffer = aBuffer;
       
    82 
       
    83     CreateDecoderL();
       
    84 
       
    85     const TFrameInfo info( iDecoder->FrameInfo());
       
    86     if (( info.iOverallSizeInPixels.iWidth < 1 ) || (
       
    87         info.iOverallSizeInPixels.iHeight < 1 ))
       
    88         {
       
    89         User::Leave( KErrCorrupt );
       
    90         }
       
    91     iFrameInfoFlags = info.iFlags;
       
    92     iOriginalSize = info.iOverallSizeInPixels; 
       
    93     }
       
    94 
       
    95 // -----------------------------------------------------------------------------
       
    96 // CThumbnailImageDecoderv3::DecodeL()
       
    97 // Decode the thumbnail image
       
    98 // -----------------------------------------------------------------------------
       
    99 //
       
   100 void CThumbnailImageDecoderv3::DecodeL( const TDisplayMode aDisplayMode )
       
   101     {
       
   102     TN_DEBUG1( "CThumbnailImageDecoderv3::DecodeL() start" );
       
   103     OstTrace0( TRACE_NORMAL, CTHUMBNAILIMAGEDECODERV3_DECODEL, "CThumbnailImageDecoderv3::DecodeL - start" );
       
   104     
       
   105     // Create the bitmap
       
   106     if ( !iBitmap )
       
   107         {
       
   108         iBitmap = new( ELeave )CFbsBitmap();
       
   109         }
       
   110 
       
   111     //Size in both x and y dimension must be non-zero, positive value
       
   112     TSize loadSize( iOriginalSize) ;
       
   113 
       
   114     // If EFullyScaleable flag is not set, we need to figure out a size
       
   115     // reduction factor. 1/1, 1/2, 1/4, and 1/8 are possible values for all
       
   116     // plug-ins. SVG graphics can be rendered at any size even though
       
   117     // EFullyScaleable is not set.
       
   118     if ( !( iFrameInfoFlags& TFrameInfo::EFullyScaleable ) )
       
   119         {
       
   120         loadSize = iOriginalSize;
       
   121         TInt reductionFactor = 1;
       
   122         while ( reductionFactor < KMaximumReductionFactor && ( iSize.iWidth <
       
   123             loadSize.iWidth / 2 ) && ( iSize.iHeight < loadSize.iHeight / 2 ))
       
   124             {
       
   125             // magic: use loadSize that is half of previous size
       
   126             loadSize.iWidth /= 2;
       
   127             loadSize.iHeight /= 2;
       
   128             reductionFactor *= 2;
       
   129             }
       
   130         // If original size is not an exact multiple of reduction factor,
       
   131         // we need to round loadSize up
       
   132         if ( reductionFactor && iOriginalSize.iWidth % reductionFactor )
       
   133             {
       
   134             loadSize.iWidth++;
       
   135             }
       
   136         if ( reductionFactor && iOriginalSize.iHeight % reductionFactor )
       
   137             {
       
   138             loadSize.iHeight++;
       
   139             }
       
   140         TN_DEBUG4( 
       
   141             "EFullyScaleable not set for image - loadSize=(%d,%d) reduction=1/%d ", loadSize.iWidth, loadSize.iHeight, reductionFactor );
       
   142         OstTraceExt3( TRACE_NORMAL, DUP1_CTHUMBNAILIMAGEDECODERV3_DECODEL, "CThumbnailImageDecoderv3::DecodeL;loadSize.iWidth=%d;loadSize.iHeight=%d;reductionFactor=%d", loadSize.iWidth, loadSize.iHeight, reductionFactor );
       
   143         }
       
   144 
       
   145     TInt err = iBitmap->Create( loadSize, aDisplayMode );
       
   146     if (err != KErrNone)
       
   147         {
       
   148         Release();
       
   149         User::Leave(err);
       
   150         }
       
   151     
       
   152     iDecoder->Convert( &iStatus, * iBitmap );
       
   153 
       
   154     SetActive();
       
   155     
       
   156     TN_DEBUG1( "CThumbnailImageDecoderv3::DecodeL() end" );
       
   157     OstTrace0( TRACE_NORMAL, DUP2_CTHUMBNAILIMAGEDECODERV3_DECODEL, "CThumbnailImageDecoderv3::DecodeL - end" );
       
   158     }
       
   159 
       
   160 
       
   161 // -----------------------------------------------------------------------------
       
   162 // CThumbnailImageDecoderv3::Release()
       
   163 // Releases resources
       
   164 // -----------------------------------------------------------------------------
       
   165 //
       
   166 void CThumbnailImageDecoderv3::Release()
       
   167     {
       
   168     Cancel();
       
   169     
       
   170     delete iDecoder;
       
   171     iDecoder = NULL;
       
   172     
       
   173     delete iBitmap;
       
   174     iBitmap = NULL;
       
   175     delete iBuffer; // we own the buffer
       
   176     iBuffer = NULL;
       
   177     }
       
   178 
       
   179 
       
   180 // -----------------------------------------------------------------------------
       
   181 // CThumbnailImageDecoderv3::DoCancel()
       
   182 // -----------------------------------------------------------------------------
       
   183 //
       
   184 void CThumbnailImageDecoderv3::DoCancel()
       
   185     {
       
   186     if ( iDecoder )
       
   187         {
       
   188         iDecoder->Cancel();
       
   189         delete iDecoder;
       
   190         iDecoder = NULL;
       
   191         }
       
   192     
       
   193     delete iBitmap;
       
   194     iBitmap = NULL;
       
   195     delete iBuffer; // we own the buffer
       
   196     iBuffer = NULL;
       
   197     }
       
   198 
       
   199 
       
   200 // -----------------------------------------------------------------------------
       
   201 // CThumbnailImageDecoderv3::RunL()
       
   202 // -----------------------------------------------------------------------------
       
   203 //
       
   204 void CThumbnailImageDecoderv3::RunL()
       
   205     {
       
   206     // This call takes ownership of iBitmap
       
   207     iObserver->ThumbnailProviderReady( iStatus.Int(), iBitmap, iOriginalSize, EFalse, EFalse );
       
   208 
       
   209     iBitmap = NULL; // owned by server now
       
   210     delete iBuffer; // we own the buffer
       
   211     iBuffer = NULL;
       
   212     
       
   213     Release();
       
   214     }
       
   215 
       
   216 
       
   217 // -----------------------------------------------------------------------------
       
   218 // CThumbnailImageDecoderv3::CreateDecoderL
       
   219 // Creates image decoder
       
   220 // -----------------------------------------------------------------------------
       
   221 //
       
   222 void CThumbnailImageDecoderv3::CreateDecoderL()
       
   223     {
       
   224     delete iDecoder;
       
   225     iDecoder = NULL;
       
   226 	
       
   227     CImageDecoder::TOptions options = ( CImageDecoder::TOptions )( 
       
   228             CImageDecoder::EOptionNoDither );
       
   229  
       
   230     TRAPD( decErr, iDecoder = CExtJpegDecoder::DataNewL(
       
   231             CExtJpegDecoder::EHwImplementation, iFs, *iBuffer, options ));
       
   232     
       
   233     if ( decErr != KErrNone )
       
   234         {
       
   235         TN_DEBUG2( "CThumbnailImageDecoderv3::CreateDecoderL() - HW CExtJpegDecoder failed %d", decErr);
       
   236         OstTrace1( TRACE_NORMAL, CTHUMBNAILIMAGEDECODERV3_CREATEDECODERL, "CThumbnailImageDecoderv3::CreateDecoderL - HW CExtJpegDecoder failed;decErr=%d", decErr );
       
   237         
       
   238         LeaveIfCorruptL( decErr );
       
   239         
       
   240         TRAP( decErr, iDecoder = CExtJpegDecoder::DataNewL(
       
   241                 CExtJpegDecoder::ESwImplementation, iFs, *iBuffer, options ));
       
   242         
       
   243         if ( decErr != KErrNone )
       
   244             {                             
       
   245             TN_DEBUG2( "CThumbnailImageDecoderv3::CreateDecoderL() - SW CExtJpegDecoder failed %d", decErr);
       
   246             OstTrace1( TRACE_NORMAL, DUP1_CTHUMBNAILIMAGEDECODERV3_CREATEDECODERL, "CThumbnailImageDecoderv3::CreateDecoderL - SW CExtJpegDecoder failed;decErr=%d", decErr );
       
   247             
       
   248             LeaveIfCorruptL( decErr );
       
   249             // don't force any mime type
       
   250             TRAPD( decErr, iDecoder = CImageDecoder::DataNewL( iFs, *iBuffer, options ) );
       
   251             if ( decErr != KErrNone )
       
   252                 {
       
   253                 Release();
       
   254                 TN_DEBUG2( "CThumbnailImageDecoderv3::CreateDecoderL() - CImageDecoder error %d", decErr );
       
   255                 OstTrace1( TRACE_NORMAL, DUP2_CTHUMBNAILIMAGEDECODERV3_CREATEDECODERL, "CThumbnailImageDecoderv3::CreateDecoderL - CImageDecoder error;decErr=%d", decErr );
       
   256                 
       
   257                 User::Leave( decErr );
       
   258                 }     
       
   259             
       
   260             TN_DEBUG1( "CThumbnailImageDecoderv3::CreateDecoderL() - CImageDecoder created" );
       
   261             OstTrace0( TRACE_NORMAL, DUP3_CTHUMBNAILIMAGEDECODERV3_CREATEDECODERL, "CThumbnailImageDecoderv3::CreateDecoderL - CImageDecoder created" );
       
   262             }
       
   263         else
       
   264             {
       
   265             TN_DEBUG1( "CThumbnailImageDecoderv3::CreateDecoderL() - SW CExtJpegDecoder created" );
       
   266             OstTrace0( TRACE_NORMAL, DUP4_CTHUMBNAILIMAGEDECODERV3_CREATEDECODERL, "CThumbnailImageDecoderv3::CreateDecoderL - SW CExtJpegDecoder created" );
       
   267             }               
       
   268         }
       
   269     else
       
   270         {
       
   271         TN_DEBUG1( "CThumbnailImageDecoderv3::CreateDecoderL() - HW CExtJpegDecoder created" );
       
   272         OstTrace0( TRACE_NORMAL, DUP5_CTHUMBNAILIMAGEDECODERV3_CREATEDECODERL, "CThumbnailImageDecoderv3::CreateDecoderL - HW CExtJpegDecoder created" );
       
   273         }    
       
   274     }
       
   275 
       
   276 
       
   277 // -----------------------------------------------------------------------------
       
   278 // CThumbnailImageDecoderv3::CreateExifDecoderL()
       
   279 // Returns size of original image
       
   280 // -----------------------------------------------------------------------------
       
   281 //
       
   282 const TSize& CThumbnailImageDecoderv3::OriginalSize()const
       
   283     {
       
   284     return iOriginalSize;
       
   285     }
       
   286 
       
   287 // -----------------------------------------------------------------------------
       
   288 // CThumbnailImageDecoder3::LeaveIfCorruptL()
       
   289 // Leave is image is corrupted
       
   290 // -----------------------------------------------------------------------------
       
   291 //
       
   292 void CThumbnailImageDecoderv3::LeaveIfCorruptL(const TInt aError )
       
   293     {
       
   294     //no sense to try other codecs if image is corrupted
       
   295     if( aError == KErrCorrupt || aError == KErrUnderflow)
       
   296         {
       
   297         Release();
       
   298         User::Leave( aError );
       
   299         }
       
   300     }
       
   301 
       
   302 //End of file