mpxmusicplayer/commonui/src/mpximageutil.cpp
branchRCL_3
changeset 53 3de6c4cf6b67
equal deleted inserted replaced
52:14979e23cb5e 53:3de6c4cf6b67
       
     1 /*
       
     2 * Copyright (c) 2006 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:  Implementation of CMPXImageUtil.
       
    15 *
       
    16 */
       
    17 
       
    18 // INCLUDE FILES
       
    19 #include <e32std.h>
       
    20 #include <e32base.h>
       
    21 #include <bitmaptransforms.h>
       
    22 #include <mda/common/video.h>
       
    23 #include <eikenv.h>
       
    24 #include <icl/imagedata.h>
       
    25 #include <imageconversion.h>
       
    26 #include <IclExtJpegApi.h>
       
    27 
       
    28 #include "mpximageutil.h"
       
    29 
       
    30 //This value is used to stretch so the image fills the container.
       
    31 const TReal MaxStretchRatio = 1.1f;
       
    32 
       
    33 LOCAL_C TInt Stretch (TInt aValue, TInt aLimit)
       
    34     {
       
    35     TInt ret( aValue * MaxStretchRatio );
       
    36     ret = (ret>aLimit) ? aLimit : ret;
       
    37     return ret;
       
    38     }
       
    39 
       
    40 // ============================ MEMBER FUNCTIONS ===============================
       
    41 
       
    42 
       
    43 // -----------------------------------------------------------------------------
       
    44 // Two-phased constructor.
       
    45 // -----------------------------------------------------------------------------
       
    46 //
       
    47 CMPXImageUtil* CMPXImageUtil::NewL(
       
    48                                     MMPXAlbumArtUtilObserver& aObserver)
       
    49     {
       
    50     CMPXImageUtil* self = new ( ELeave ) CMPXImageUtil(aObserver);
       
    51     CleanupStack::PushL( self );
       
    52     self->ConstructL();
       
    53     CleanupStack::Pop(); // self
       
    54     return self; 
       
    55     }
       
    56 
       
    57 // -----------------------------------------------------------------------------
       
    58 // C++ default constructor can NOT contain any code, that
       
    59 // might leave.
       
    60 // -----------------------------------------------------------------------------
       
    61 //
       
    62 CMPXImageUtil::CMPXImageUtil(MMPXAlbumArtUtilObserver& aObserver) 
       
    63 :   CActive(EPriorityStandard), iObserver(aObserver) 
       
    64     {
       
    65     iBitmap = NULL;
       
    66     iState = EIdle;
       
    67     }
       
    68 
       
    69 // -----------------------------------------------------------------------------
       
    70 // Symbian 2nd phase constructor can leave.
       
    71 // -----------------------------------------------------------------------------
       
    72 //
       
    73 void CMPXImageUtil::ConstructL()
       
    74     {
       
    75     iScaler = CBitmapScaler::NewL();
       
    76     User::LeaveIfError(iFs.Connect());
       
    77     CActiveScheduler::Add( this );
       
    78     }
       
    79 
       
    80 // -----------------------------------------------------------------------------
       
    81 // Destructor
       
    82 // -----------------------------------------------------------------------------
       
    83 //
       
    84 CMPXImageUtil::~CMPXImageUtil()
       
    85     {
       
    86     Cancel();
       
    87     delete iImageDecoder; // CImageDecoder must be deleted before the 
       
    88     delete iScaler;
       
    89     iFs.Close();
       
    90     delete iImageData;
       
    91     }
       
    92 
       
    93 // -----------------------------------------------------------------------------
       
    94 // Starts to decode an image from a file. 
       
    95 // -----------------------------------------------------------------------------
       
    96 //
       
    97 void CMPXImageUtil::StartToDecodeL(const TDesC& aFileName, 
       
    98                                       const TSize& aSize, 
       
    99                                       TDisplayMode aDisplayMode /*=EColor64K*/)
       
   100     {
       
   101     if(iState)
       
   102         {
       
   103         User::Leave(KErrNotReady);
       
   104         }
       
   105 
       
   106     delete iImageDecoder; 
       
   107     iImageDecoder = NULL;
       
   108     delete iBitmap; 
       
   109     iBitmap = NULL;
       
   110     delete iImageData;
       
   111     iImageData = NULL;
       
   112     
       
   113     // create the decoder
       
   114 
       
   115     TRAPD( err, iImageDecoder = CExtJpegDecoder::FileNewL( 
       
   116             CExtJpegDecoder::EHwImplementation, 
       
   117             iFs, 
       
   118             aFileName, 
       
   119             CImageDecoder::EOptionNone ) );
       
   120     if (KErrNone != err)
       
   121         {
       
   122             TRAP(err,iImageDecoder = CExtJpegDecoder::FileNewL( 
       
   123                     CExtJpegDecoder::ESwImplementation, 
       
   124                     iFs, 
       
   125                     aFileName, 
       
   126                     CImageDecoder::EOptionNone ) );
       
   127         if (KErrNone != err)
       
   128             {
       
   129             iImageDecoder = CImageDecoder::FileNewL(
       
   130                     iFs, 
       
   131                     aFileName,
       
   132                     CImageDecoder::EOptionNone);
       
   133             }
       
   134         }
       
   135 
       
   136     // Get image size
       
   137     TSize bitmapSize = CalculateDecodeSize(aSize);
       
   138     // create the destination bitmap
       
   139     iBitmap = new (ELeave) CFbsBitmap();
       
   140     User::LeaveIfError(iBitmap->Create(bitmapSize, aDisplayMode)); 
       
   141 
       
   142     // start conversion to bitmap
       
   143     iState = EDecoding;
       
   144     iImageDecoder->Convert(&iStatus, *iBitmap);
       
   145     
       
   146     iObserver.ExtractAlbumArtStarted();
       
   147     SetActive();
       
   148     }
       
   149 
       
   150 // -----------------------------------------------------------------------------
       
   151 // CMPXImageUtil::DoCancel
       
   152 // Implementation of CActive
       
   153 // -----------------------------------------------------------------------------
       
   154 //
       
   155 void CMPXImageUtil::DoCancel()
       
   156     {
       
   157     switch ( iState )
       
   158         {
       
   159         case EDecoding:
       
   160             {
       
   161             iImageDecoder->Cancel();
       
   162             // need to delete bitmap as we have are still the owner until the 
       
   163             // operation completes
       
   164             if ( iBitmap )
       
   165                 {
       
   166                 delete iBitmap;
       
   167                 iBitmap = NULL;
       
   168                 }
       
   169             break;
       
   170             }
       
   171         case EScaling:
       
   172             {
       
   173             iScaler->Cancel();
       
   174             if ( iBitmap )
       
   175                 {
       
   176                 delete iBitmap;
       
   177                 iBitmap = NULL;
       
   178                 }
       
   179             break;
       
   180             }
       
   181         default: // No Asynchronous events are taking place, do nothing.
       
   182             {
       
   183             break;
       
   184             }
       
   185         }
       
   186 
       
   187     delete iImageData;
       
   188     iImageData = NULL;
       
   189     delete iImageDecoder;
       
   190     iImageDecoder = NULL;
       
   191     }
       
   192 
       
   193 // -----------------------------------------------------------------------------
       
   194 // Implementation of CActive
       
   195 // -----------------------------------------------------------------------------
       
   196 //
       
   197 void CMPXImageUtil::RunL()
       
   198     {
       
   199     TInt deleteDecoder( ETrue );
       
   200     switch( iState ) 
       
   201         {
       
   202         case EDecoding:
       
   203             {
       
   204             if( iStatus == KErrNone ) 
       
   205                 {
       
   206                 if ( !iScaleRquired )
       
   207                     {   
       
   208                     iState = EIdle;
       
   209                     iObserver.ExtractAlbumArtCompleted(iBitmap,KErrNone);
       
   210                     iBitmap = NULL;
       
   211                     }
       
   212                 else 
       
   213                     {
       
   214                     deleteDecoder = EFalse;
       
   215                     ScaleL();
       
   216                     }
       
   217                 break;
       
   218                 }
       
   219             else
       
   220                 {
       
   221                 // some error
       
   222                 if ( iBitmap )
       
   223                     {
       
   224                     delete iBitmap;
       
   225                     iBitmap = NULL;                        
       
   226                     }
       
   227                 iState = EIdle;
       
   228                 iObserver.ExtractAlbumArtCompleted(iBitmap, iStatus.Int());
       
   229                 break;   
       
   230                 }
       
   231             }
       
   232         case EScaling:
       
   233             {
       
   234             iState = EIdle;
       
   235             iObserver.ExtractAlbumArtCompleted(iBitmap,iStatus.Int());
       
   236             iBitmap = NULL; 
       
   237             }
       
   238             break;
       
   239 
       
   240         default: // some error
       
   241             {
       
   242             iState = EIdle;
       
   243             if ( iBitmap )
       
   244                 {
       
   245                 delete iBitmap;
       
   246                 iBitmap = NULL;                        
       
   247                 }
       
   248             iObserver.ExtractAlbumArtCompleted(iBitmap,iStatus.Int());
       
   249             break;
       
   250             }
       
   251         }
       
   252 
       
   253     // It's safe to destroy iImageData here
       
   254     delete iImageData;
       
   255     iImageData = NULL;
       
   256     
       
   257     if ( deleteDecoder )
       
   258         {
       
   259         delete iImageDecoder;
       
   260         iImageDecoder = NULL;
       
   261         }
       
   262     }
       
   263 
       
   264 // -----------------------------------------------------------------------------
       
   265 // Scales iBitmap by iSize
       
   266 // -----------------------------------------------------------------------------
       
   267 //
       
   268 void CMPXImageUtil::ScaleL()
       
   269     {
       
   270     iScaler->Scale(&iStatus, *iBitmap, iSize, EFalse);
       
   271     iState = EScaling;
       
   272     SetActive();
       
   273     }
       
   274 
       
   275 // -----------------------------------------------------------------------------
       
   276 // Starts to decode an image from a buffer. 
       
   277 // -----------------------------------------------------------------------------
       
   278 //
       
   279 void CMPXImageUtil::StartToDecodeL( const TSize& aSize,
       
   280         HBufC8* aAlbumArt, TDisplayMode aDisplayMode/*=EColor64K*/ )
       
   281     {
       
   282     if(iState)
       
   283         {
       
   284         User::Leave( KErrNotReady );
       
   285         }
       
   286                                
       
   287     delete iImageDecoder; 
       
   288     iImageDecoder = NULL;
       
   289     delete iBitmap; 
       
   290     iBitmap = NULL;
       
   291     delete iImageData;
       
   292     iImageData = NULL;
       
   293     // storing the pointer to aAlbumArt, ownership was transferred to us.
       
   294     iImageData = aAlbumArt;
       
   295     // create the decoder
       
   296     
       
   297     
       
   298     TRAPD( err, iImageDecoder = CExtJpegDecoder::DataNewL( 
       
   299             CExtJpegDecoder::EHwImplementation, 
       
   300             iFs, 
       
   301             *iImageData, 
       
   302             CImageDecoder::EOptionNone ) );
       
   303     if ( KErrNone != err )
       
   304         {
       
   305         TRAP(err,iImageDecoder = CExtJpegDecoder::DataNewL( 
       
   306                 CExtJpegDecoder::ESwImplementation, 
       
   307                 iFs, 
       
   308                 *iImageData, 
       
   309                 CImageDecoder::EOptionNone ) );
       
   310         if ( KErrNone != err )
       
   311             {
       
   312             iImageDecoder = CImageDecoder::DataNewL( 
       
   313                     iFs, 
       
   314                     *iImageData, 
       
   315                     CImageDecoder::EOptionNone );
       
   316             }
       
   317         }
       
   318     
       
   319     
       
   320 
       
   321     
       
   322     TSize bitmapSize = CalculateDecodeSize( aSize );
       
   323 
       
   324     // create the destination bitmap
       
   325     iBitmap = new ( ELeave ) CFbsBitmap();
       
   326     User::LeaveIfError( iBitmap->Create( bitmapSize, aDisplayMode ) );
       
   327     // start conversion to bitmap
       
   328     iState = EDecoding;
       
   329     iImageDecoder->Convert( &iStatus, *iBitmap );
       
   330     
       
   331     iObserver.ExtractAlbumArtStarted();
       
   332     SetActive();
       
   333     }
       
   334 // -----------------------------------------------------------------------------
       
   335 // Calculates the decode size and prepares members for scaling. 
       
   336 // -----------------------------------------------------------------------------
       
   337 //
       
   338 TSize CMPXImageUtil::CalculateDecodeSize(const TSize& aSize)
       
   339     {
       
   340     const TFrameInfo& frameInfo = iImageDecoder->FrameInfo();
       
   341     TSize bitmapSize = frameInfo.iOverallSizeInPixels;
       
   342     TReal sourceAspectRatio( TReal( bitmapSize.iWidth) / bitmapSize.iHeight );
       
   343     TReal destinationAspectRatio( TReal( aSize.iWidth ) / aSize.iHeight );
       
   344     TReal xScale = TReal( bitmapSize.iWidth ) / aSize.iWidth;
       
   345     TReal yScale = TReal( bitmapSize.iHeight ) / aSize.iHeight;
       
   346     TReal scale(0.0f);
       
   347     
       
   348     if ( sourceAspectRatio > destinationAspectRatio )
       
   349         {
       
   350         scale = xScale;
       
   351         iSize.iWidth = aSize.iWidth;
       
   352         iSize.iHeight = Stretch( TReal( bitmapSize.iHeight ) / scale , 
       
   353                                  aSize.iHeight );
       
   354         }
       
   355     else
       
   356         {
       
   357         scale = yScale;
       
   358         iSize.iWidth = Stretch( TReal( bitmapSize.iWidth ) / scale ,
       
   359                                 aSize.iWidth);
       
   360         iSize.iHeight = aSize.iHeight;
       
   361         }
       
   362      
       
   363     if ((frameInfo.iFlags & TFrameInfo::EFullyScaleable))
       
   364         {
       
   365         iScaleRquired = EFalse;
       
   366         bitmapSize = iSize;
       
   367         }
       
   368     else
       
   369         //Decoder only supports 2, 4 and 8 scallyng, the image will need 
       
   370         //to be reescaled after decoding.
       
   371         //Decoding to a scale that is just a bit bigger thant the target,
       
   372         //this will save memory and resources and we will get a sharp image 
       
   373         //after scaling.
       
   374         {
       
   375         TInt intscale = ( scale >= 8 ) ? 8 : 
       
   376                 ( scale >= 4 ) ? 4 :
       
   377                 ( scale >= 2 ) ? 2 : 1;
       
   378         TUint xCorrection = ( bitmapSize.iWidth % intscale ) ? 1 : 0;
       
   379         TUint yCorrection = ( bitmapSize.iHeight % intscale ) ? 1 : 0;
       
   380         bitmapSize.iWidth /= intscale;
       
   381         bitmapSize.iHeight /= intscale;
       
   382         bitmapSize += TSize( xCorrection, yCorrection );
       
   383         iScaleRquired = ETrue;
       
   384         }
       
   385     return bitmapSize;
       
   386     }
       
   387 
       
   388 // -----------------------------------------------------------------------------
       
   389 // CMPXImageUtil::CancelRequest
       
   390 // Cancel Asynch requests
       
   391 // -----------------------------------------------------------------------------
       
   392 //
       
   393 void CMPXImageUtil::CancelRequest()
       
   394     {
       
   395     Cancel();
       
   396     }
       
   397 
       
   398 //  End of File