mpxmusicplayer/commonui/src/mpximageutil.cpp
changeset 0 ff3acec5bc43
child 2 b70d77332e66
equal deleted inserted replaced
-1:000000000000 0:ff3acec5bc43
       
     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             iObserver.ExtractAlbumArtCompleted(iBitmap,iStatus.Int());
       
   244             break;
       
   245             }
       
   246         }
       
   247 
       
   248     // It's safe to destroy iImageData here
       
   249     delete iImageData;
       
   250     iImageData = NULL;
       
   251     
       
   252     if ( deleteDecoder )
       
   253         {
       
   254         delete iImageDecoder;
       
   255         iImageDecoder = NULL;
       
   256         }
       
   257     }
       
   258 
       
   259 // -----------------------------------------------------------------------------
       
   260 // Scales iBitmap by iSize
       
   261 // -----------------------------------------------------------------------------
       
   262 //
       
   263 void CMPXImageUtil::ScaleL()
       
   264     {
       
   265     iScaler->Scale(&iStatus, *iBitmap, iSize, EFalse);
       
   266     iState = EScaling;
       
   267     SetActive();
       
   268     }
       
   269 
       
   270 // -----------------------------------------------------------------------------
       
   271 // Starts to decode an image from a buffer. 
       
   272 // -----------------------------------------------------------------------------
       
   273 //
       
   274 void CMPXImageUtil::StartToDecodeL( const TSize& aSize,
       
   275         HBufC8* aAlbumArt, TDisplayMode aDisplayMode/*=EColor64K*/ )
       
   276     {
       
   277     if(iState)
       
   278         {
       
   279         User::Leave( KErrNotReady );
       
   280         }
       
   281                                
       
   282     delete iImageDecoder; 
       
   283     iImageDecoder = NULL;
       
   284     delete iBitmap; 
       
   285     iBitmap = NULL;
       
   286     delete iImageData;
       
   287     iImageData = NULL;
       
   288     // storing the pointer to aAlbumArt, ownership was transferred to us.
       
   289     iImageData = aAlbumArt;
       
   290     // create the decoder
       
   291     
       
   292     
       
   293     TRAPD( err, iImageDecoder = CExtJpegDecoder::DataNewL( 
       
   294             CExtJpegDecoder::EHwImplementation, 
       
   295             iFs, 
       
   296             *iImageData, 
       
   297             CImageDecoder::EOptionNone ) );
       
   298     if ( KErrNone != err )
       
   299         {
       
   300         TRAP(err,iImageDecoder = CExtJpegDecoder::DataNewL( 
       
   301                 CExtJpegDecoder::ESwImplementation, 
       
   302                 iFs, 
       
   303                 *iImageData, 
       
   304                 CImageDecoder::EOptionNone ) );
       
   305         if ( KErrNone != err )
       
   306             {
       
   307             iImageDecoder = CImageDecoder::DataNewL( 
       
   308                     iFs, 
       
   309                     *iImageData, 
       
   310                     CImageDecoder::EOptionNone );
       
   311             }
       
   312         }
       
   313     
       
   314     
       
   315 
       
   316     
       
   317     TSize bitmapSize = CalculateDecodeSize( aSize );
       
   318 
       
   319     // create the destination bitmap
       
   320     iBitmap = new ( ELeave ) CFbsBitmap();
       
   321     User::LeaveIfError( iBitmap->Create( bitmapSize, aDisplayMode ) );
       
   322     // start conversion to bitmap
       
   323     iState = EDecoding;
       
   324     iImageDecoder->Convert( &iStatus, *iBitmap );
       
   325     
       
   326     iObserver.ExtractAlbumArtStarted();
       
   327     SetActive();
       
   328     }
       
   329 // -----------------------------------------------------------------------------
       
   330 // Calculates the decode size and prepares members for scaling. 
       
   331 // -----------------------------------------------------------------------------
       
   332 //
       
   333 TSize CMPXImageUtil::CalculateDecodeSize(const TSize& aSize)
       
   334     {
       
   335     const TFrameInfo& frameInfo = iImageDecoder->FrameInfo();
       
   336     TSize bitmapSize = frameInfo.iOverallSizeInPixels;
       
   337     TReal sourceAspectRatio( TReal( bitmapSize.iWidth) / bitmapSize.iHeight );
       
   338     TReal destinationAspectRatio( TReal( aSize.iWidth ) / aSize.iHeight );
       
   339     TReal xScale = TReal( bitmapSize.iWidth ) / aSize.iWidth;
       
   340     TReal yScale = TReal( bitmapSize.iHeight ) / aSize.iHeight;
       
   341     TReal scale(0.0f);
       
   342     
       
   343     if ( sourceAspectRatio > destinationAspectRatio )
       
   344         {
       
   345         scale = xScale;
       
   346         iSize.iWidth = aSize.iWidth;
       
   347         iSize.iHeight = Stretch( TReal( bitmapSize.iHeight ) / scale , 
       
   348                                  aSize.iHeight );
       
   349         }
       
   350     else
       
   351         {
       
   352         scale = yScale;
       
   353         iSize.iWidth = Stretch( TReal( bitmapSize.iWidth ) / scale ,
       
   354                                 aSize.iWidth);
       
   355         iSize.iHeight = aSize.iHeight;
       
   356         }
       
   357      
       
   358     if ((frameInfo.iFlags & TFrameInfo::EFullyScaleable))
       
   359         {
       
   360         iScaleRquired = EFalse;
       
   361         bitmapSize = iSize;
       
   362         }
       
   363     else
       
   364         //Decoder only supports 2, 4 and 8 scallyng, the image will need 
       
   365         //to be reescaled after decoding.
       
   366         //Decoding to a scale that is just a bit bigger thant the target,
       
   367         //this will save memory and resources and we will get a sharp image 
       
   368         //after scaling.
       
   369         {
       
   370         TInt intscale = ( scale >= 8 ) ? 8 : 
       
   371                 ( scale >= 4 ) ? 4 :
       
   372                 ( scale >= 2 ) ? 2 : 1;
       
   373         TUint xCorrection = ( bitmapSize.iWidth % intscale ) ? 1 : 0;
       
   374         TUint yCorrection = ( bitmapSize.iHeight % intscale ) ? 1 : 0;
       
   375         bitmapSize.iWidth /= intscale;
       
   376         bitmapSize.iHeight /= intscale;
       
   377         bitmapSize += TSize( xCorrection, yCorrection );
       
   378         iScaleRquired = ETrue;
       
   379         }
       
   380     return bitmapSize;
       
   381     }
       
   382 
       
   383 // -----------------------------------------------------------------------------
       
   384 // CMPXImageUtil::CancelRequest
       
   385 // Cancel Asynch requests
       
   386 // -----------------------------------------------------------------------------
       
   387 //
       
   388 void CMPXImageUtil::CancelRequest()
       
   389     {
       
   390     Cancel();
       
   391     }
       
   392 
       
   393 //  End of File