uifw/AknGlobalUI/OldStyleNotif/Src/AknPrivateImageLoader.cpp
changeset 0 2f259fa3e83a
child 9 0aa5fbdfbc30
equal deleted inserted replaced
-1:000000000000 0:2f259fa3e83a
       
     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:  Private image loader to convert binary array into usable image
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 // INCLUDES
       
    20 #include "AknPrivateImageLoader.h"
       
    21 #include <imageconversion.h>
       
    22 #include <eikenv.h>
       
    23 #include <f32file.h>
       
    24 #include <e32debug.h>
       
    25 #include <eikimage.h>
       
    26 #include <AknIconUtils.h>
       
    27 #include <SVGEngineInterfaceImpl.h>
       
    28 
       
    29 /// Max bitmap dimension (heigth or width) which is allowed to be loaded
       
    30 const TInt KMaxDecodeSize   = 300;
       
    31 
       
    32 /// Debug print macro
       
    33 #ifdef _DEBUG
       
    34     #define RDEBUG( args... ) RDebug::Printf( args )
       
    35 #else
       
    36     #define RDEBUG( args... )  
       
    37 #endif
       
    38 
       
    39 // ======== MEMBER FUNCTIONS ========
       
    40 // ---------------------------------------------------------------------------
       
    41 // CAknPrivateImageLoader::CAknPrivateImageLoader
       
    42 // ---------------------------------------------------------------------------
       
    43 //
       
    44 CAknPrivateImageLoader::CAknPrivateImageLoader(
       
    45     RFs& aFs,
       
    46     MAknPrivateImageLoaderObserver& aObserver ) : 
       
    47     CActive( EPriorityStandard ),
       
    48     iObserver( aObserver ),
       
    49     iFs( aFs )
       
    50     {
       
    51     CActiveScheduler::Add( this );
       
    52     }
       
    53 
       
    54 // ---------------------------------------------------------------------------
       
    55 // CAknPrivateImageLoader::ConstructL
       
    56 // ---------------------------------------------------------------------------
       
    57 //
       
    58 void CAknPrivateImageLoader::ConstructL()
       
    59     {
       
    60     }
       
    61 
       
    62 // ---------------------------------------------------------------------------
       
    63 // CAknPrivateImageLoader::NewL
       
    64 // ---------------------------------------------------------------------------
       
    65 //
       
    66 CAknPrivateImageLoader* CAknPrivateImageLoader::NewL(
       
    67     RFs& aFs,
       
    68     MAknPrivateImageLoaderObserver& aObserver )
       
    69     {
       
    70     CAknPrivateImageLoader* self = 
       
    71         CAknPrivateImageLoader::NewLC( aFs, aObserver );
       
    72     CleanupStack::Pop( self );
       
    73     return self;
       
    74     }
       
    75 
       
    76 // ---------------------------------------------------------------------------
       
    77 // CAknPrivateImageLoader::NewLC
       
    78 // ---------------------------------------------------------------------------
       
    79 //
       
    80 CAknPrivateImageLoader* CAknPrivateImageLoader::NewLC(
       
    81     RFs& aFs,
       
    82     MAknPrivateImageLoaderObserver& aObserver )
       
    83     {
       
    84     CAknPrivateImageLoader* self = 
       
    85         new( ELeave ) CAknPrivateImageLoader( aFs, aObserver );
       
    86     CleanupStack::PushL( self );
       
    87     self->ConstructL();
       
    88     return self;
       
    89     }
       
    90 
       
    91 // ---------------------------------------------------------------------------
       
    92 // CAknPrivateImageLoader::~CAknPrivateImageLoader
       
    93 // ---------------------------------------------------------------------------
       
    94 //
       
    95 CAknPrivateImageLoader::~CAknPrivateImageLoader()
       
    96     {
       
    97     Cancel();
       
    98     delete iDecoder;
       
    99     delete iIcon;
       
   100     }
       
   101 
       
   102 // ---------------------------------------------------------------------------
       
   103 // CAknPrivateImageLoader::LoadIconL
       
   104 // ---------------------------------------------------------------------------
       
   105 //
       
   106 void CAknPrivateImageLoader::LoadIconL( 
       
   107     const TDesC8& aImageData, 
       
   108     TSize aSize )
       
   109     {
       
   110     Cancel();
       
   111 
       
   112     // try to load bitmap
       
   113     TRAPD( err, LoadL( aImageData, aSize ) );
       
   114     
       
   115     if( err == KErrTooBig )
       
   116         {
       
   117         User::Leave( err );
       
   118         }
       
   119     else if( err )
       
   120         {
       
   121         // Decoder can't open it -> try to load as SVG image
       
   122         LoadSVGImageL( aImageData, aSize );
       
   123         }
       
   124     }
       
   125 
       
   126 // ---------------------------------------------------------------------------
       
   127 // CAknPrivateImageLoader::RunL
       
   128 // ---------------------------------------------------------------------------
       
   129 //
       
   130 void CAknPrivateImageLoader::RunL()
       
   131     {
       
   132     // Compress the heap after image conversion as image decoder
       
   133     // seems to leave heap uncompressed
       
   134     if( iDecoder )
       
   135         {
       
   136         delete iDecoder;
       
   137         iDecoder = NULL;
       
   138         User::Heap().Compress();
       
   139         }
       
   140     
       
   141     // check errors
       
   142     TInt status = iStatus.Int();
       
   143     if( status < KErrNone )
       
   144         {
       
   145         // Image load failed
       
   146         RDEBUG( "CAknPrivateImageLoader::RunL: image load error %d", status );
       
   147 
       
   148         delete iIcon;
       
   149         iIcon = NULL;
       
   150 
       
   151         iObserver.ImageLoadError( status );
       
   152         // don't use member variables after callback, since this instance
       
   153         // might be deleted
       
   154         }
       
   155     else
       
   156         {
       
   157         // Image load success
       
   158         ASSERT( iIcon );
       
   159         
       
   160         // generate dummy mask if image didn't have one.
       
   161         CFbsBitmap* mask = iIcon->Mask();
       
   162         if( !mask )
       
   163             {
       
   164             CFbsBitmap* genMask = 
       
   165                 GenerateMaskLC( iIcon->Bitmap()->SizeInPixels() );
       
   166             iIcon->SetMask( genMask );
       
   167             CleanupStack::Pop( genMask );
       
   168             }
       
   169         
       
   170         // 1 bit masks needs to be inverted
       
   171         else if( mask->DisplayMode() == EGray2 ) 
       
   172             {
       
   173             InvertImageL( *mask );
       
   174             }
       
   175         
       
   176         // create scalable image
       
   177         CAknIcon* icon = iIcon;
       
   178         iIcon = NULL;
       
   179         iIcon = CreateIconL( icon ); // takes ownership (leave safe)
       
   180         
       
   181         CEikImage* image = new(ELeave) CEikImage;
       
   182         image->SetPicture( iIcon->Bitmap(), iIcon->Mask() );
       
   183         
       
   184         // remove ownership from iIcon
       
   185         iIcon->SetBitmap( NULL );
       
   186         iIcon->SetMask( NULL );
       
   187         
       
   188         delete iIcon;
       
   189         iIcon = NULL;
       
   190         
       
   191         iObserver.ImageLoadSuccess( image ); // ownership given
       
   192         // don't use member variables after callback, since this instance
       
   193         // might be deleted
       
   194         }
       
   195     }
       
   196 
       
   197 // ---------------------------------------------------------------------------
       
   198 // CAknPrivateImageLoader::RunError
       
   199 // ---------------------------------------------------------------------------
       
   200 //
       
   201 TInt CAknPrivateImageLoader::RunError( TInt aError )
       
   202     {
       
   203     RDEBUG( "CAknPrivateImageLoader::RunError: image load error %d", aError );
       
   204     
       
   205     delete iIcon;
       
   206     iIcon = NULL;
       
   207 
       
   208     iObserver.ImageLoadError( aError );
       
   209     // don't use member variables after callback, since this instance
       
   210     // might be deleted
       
   211 
       
   212     return KErrNone;
       
   213     }
       
   214 
       
   215 
       
   216 // ---------------------------------------------------------------------------
       
   217 // CAknPrivateImageLoader::DoCancel
       
   218 // ---------------------------------------------------------------------------
       
   219 //
       
   220 void CAknPrivateImageLoader::DoCancel()
       
   221     {
       
   222     if( iDecoder )
       
   223         {
       
   224         iDecoder->Cancel();
       
   225         delete iDecoder;
       
   226         iDecoder = NULL;
       
   227         }
       
   228     }
       
   229 
       
   230 // ---------------------------------------------------------------------------
       
   231 // CAknPrivateImageLoader::InvertImageL
       
   232 // ---------------------------------------------------------------------------
       
   233 //
       
   234 void CAknPrivateImageLoader::InvertImageL( CFbsBitmap& aBitmap )
       
   235     {
       
   236     TDisplayMode mode = aBitmap.DisplayMode();
       
   237     TInt width = aBitmap.SizeInPixels().iWidth;
       
   238     TInt height = aBitmap.SizeInPixels().iHeight;
       
   239 
       
   240     HBufC8* buf = HBufC8::NewLC( aBitmap.ScanLineLength( width, mode ) );
       
   241     TPtr8 bufPtr( buf->Des() );
       
   242     
       
   243     for( TInt i = 0; i < height; ++i )
       
   244         {
       
   245         aBitmap.GetScanLine( bufPtr, TPoint( 0, i ), width, mode );
       
   246         TInt len = bufPtr.Length();
       
   247         for( TInt j = 0; j < len; ++j )
       
   248             {
       
   249             bufPtr[j] ^= 0xff;
       
   250             }
       
   251         aBitmap.SetScanLine( bufPtr, i );
       
   252         }
       
   253     
       
   254     CleanupStack::PopAndDestroy( buf );
       
   255     }
       
   256 
       
   257 // ---------------------------------------------------------------------------
       
   258 // CAknPrivateImageLoader::CreateIconL
       
   259 // ---------------------------------------------------------------------------
       
   260 //
       
   261 CAknIcon* CAknPrivateImageLoader::CreateIconL( CAknIcon* aIcon )
       
   262     {
       
   263     CAknIcon* result = NULL;
       
   264     if( aIcon->Mask() )
       
   265         {
       
   266         result = AknIconUtils::CreateIconL( aIcon ); // deletes icon in case of leave
       
   267         }
       
   268     else
       
   269         {
       
   270         // aMask is NULL
       
   271         CFbsBitmap* image = aIcon->Bitmap();
       
   272         aIcon->SetBitmap( NULL );
       
   273         CleanupStack::PushL( aIcon );
       
   274         image = AknIconUtils::CreateIconL( image ); // deletes image in case of leave
       
   275         CleanupStack::Pop( aIcon );
       
   276         aIcon->SetBitmap( image );
       
   277         result = aIcon;
       
   278         }
       
   279     
       
   280     return result; // ownership given to caller
       
   281     }
       
   282 
       
   283 // ---------------------------------------------------------------------------
       
   284 // CAknPrivateImageLoader::LoadL
       
   285 // ---------------------------------------------------------------------------
       
   286 //
       
   287 void CAknPrivateImageLoader::LoadL( const TDesC8& aImageData, TSize aSize )
       
   288     {
       
   289     delete iDecoder;
       
   290     iDecoder = NULL;
       
   291     
       
   292     // default loader
       
   293     iDecoder = CImageDecoder::DataNewL( iFs, aImageData, 
       
   294         CImageDecoder::EAllowGeneratedMask );
       
   295 
       
   296     TFrameInfo info( iDecoder->FrameInfo() ); 
       
   297     TSize& frameSize( info.iFrameSizeInPixels );
       
   298     if( frameSize.iHeight > KMaxDecodeSize || 
       
   299         frameSize.iWidth > KMaxDecodeSize )
       
   300         {
       
   301         RDEBUG("CAknPrivateImageLoader::LoadLC err: image larger than %dx%d",
       
   302             KMaxDecodeSize, KMaxDecodeSize );
       
   303         User::Leave( KErrTooBig );
       
   304         }
       
   305     
       
   306     CAknIcon* icon = CAknIcon::NewL();
       
   307     CleanupStack::PushL( icon );
       
   308     icon->SetBitmap( new( ELeave ) CFbsBitmap() );
       
   309     
       
   310     TSize decodeSize( DecodeSize( info, aSize ) );
       
   311     
       
   312     User::LeaveIfError( icon->Bitmap()->Create( 
       
   313         decodeSize, info.iFrameDisplayMode ) );
       
   314 
       
   315     if ( info.iFlags & TFrameInfo::ETransparencyPossible )
       
   316         {
       
   317         // Transparency available -> use mask
       
   318         icon->SetMask( new( ELeave ) CFbsBitmap() );
       
   319         User::LeaveIfError( icon->Mask()->Create( decodeSize,
       
   320             info.iFlags & TFrameInfo::EAlphaChannel ? EGray256 : EGray2 ) );
       
   321         }
       
   322 
       
   323     // start processing
       
   324     if( icon->Mask() )
       
   325         {
       
   326         iDecoder->Convert( &iStatus, *icon->Bitmap(), *icon->Mask() );
       
   327         }
       
   328     else
       
   329         {
       
   330         iDecoder->Convert( &iStatus, *icon->Bitmap() );
       
   331         }
       
   332     
       
   333     CleanupStack::Pop( icon );
       
   334     delete iIcon;
       
   335     iIcon = icon;
       
   336 
       
   337     // wait for completion
       
   338     SetActive();
       
   339     }
       
   340 
       
   341 // ---------------------------------------------------------------------------
       
   342 // CAknPrivateImageLoader::LoadSVGImageL
       
   343 // ---------------------------------------------------------------------------
       
   344 //
       
   345 void CAknPrivateImageLoader::LoadSVGImageL(
       
   346     const TDesC8& aImageData, 
       
   347     TSize aSize )
       
   348     {
       
   349     CFbsBitmap* dummy = new(ELeave) CFbsBitmap();
       
   350     CleanupStack::PushL( dummy );
       
   351     TFontSpec spec;
       
   352     CSvgEngineInterfaceImpl* svgEngine = 
       
   353         CSvgEngineInterfaceImpl::NewL( dummy, NULL, spec );
       
   354     CleanupStack::PushL( svgEngine );
       
   355 
       
   356     TInt handle = KErrNotFound;
       
   357     LeaveIfErrorL( svgEngine->PrepareDom( aImageData, handle ) );
       
   358     
       
   359     CAknIcon* icon = CAknIcon::NewL();
       
   360     CleanupStack::PushL( icon );
       
   361     icon->SetBitmap( new(ELeave) CFbsBitmap() );
       
   362     
       
   363     // create image bitmap
       
   364     TDisplayMode mode( EColor64K ); // default value
       
   365     CEikonEnv* eikon = CEikonEnv::Static();
       
   366     if( eikon )
       
   367         {
       
   368         mode = eikon->ScreenDevice()->DisplayMode();
       
   369         }
       
   370     User::LeaveIfError( icon->Bitmap()->Create( aSize, mode ) ); 
       
   371     
       
   372     // create soft mask
       
   373     icon->SetMask( new(ELeave) CFbsBitmap() );
       
   374     User::LeaveIfError( icon->Mask()->Create( aSize, EGray256 ) );
       
   375 
       
   376     // render svg image
       
   377     LeaveIfErrorL( 
       
   378         svgEngine->RenderDom( handle, icon->Bitmap(), icon->Mask() ) );
       
   379 
       
   380     CleanupStack::Pop( icon );
       
   381     CleanupStack::PopAndDestroy( 2, dummy ); // svgEngine
       
   382     
       
   383     delete iIcon;
       
   384     iIcon = icon;
       
   385 
       
   386     // handle image in RunL
       
   387     TRequestStatus* status = &iStatus;
       
   388     User::RequestComplete( status, KErrNone );
       
   389     SetActive();
       
   390     }
       
   391 
       
   392 // ---------------------------------------------------------------------------
       
   393 // CAknPrivateImageLoader::LeaveIfErrorL
       
   394 // ---------------------------------------------------------------------------
       
   395 //
       
   396 void CAknPrivateImageLoader::LeaveIfErrorL( MSvgError* aError )
       
   397     {
       
   398     if( aError )
       
   399         {
       
   400         if( aError->HasError() && !aError->IsWarning() )
       
   401             {
       
   402             RDEBUG( "CAknPrivateImageLoader::LeaveIfErrorL: "
       
   403                 "SVG loading error: %d", aError->ErrorCode() );
       
   404             User::Leave( KErrCorrupt );
       
   405             }
       
   406         }
       
   407     }
       
   408 
       
   409 // ---------------------------------------------------------------------------
       
   410 // CAknPrivateImageLoader::GenerateMaskLC
       
   411 // ---------------------------------------------------------------------------
       
   412 //
       
   413 CFbsBitmap* CAknPrivateImageLoader::GenerateMaskLC( TSize aSize )
       
   414     {
       
   415     CFbsBitmap* mask = new(ELeave) CFbsBitmap;
       
   416     CleanupStack::PushL( mask );
       
   417     User::LeaveIfError( mask->Create( aSize, EGray2 ) );
       
   418 
       
   419     TInt width = aSize.iWidth;
       
   420     TInt height = aSize.iHeight;
       
   421     HBufC8* buf = HBufC8::NewLC( mask->ScanLineLength( width, EGray2 ) );
       
   422     TPtr8 bufPtr( buf->Des() );
       
   423     
       
   424     for( TInt i = 0; i < height; ++i )
       
   425         {
       
   426         mask->GetScanLine( bufPtr, TPoint( 0, i ), width, EGray2 );
       
   427         bufPtr.FillZ();
       
   428         mask->SetScanLine( bufPtr, i );
       
   429         }
       
   430     
       
   431     CleanupStack::PopAndDestroy( buf );
       
   432     User::LeaveIfError( mask->Compress() );
       
   433     return mask;
       
   434     }
       
   435 
       
   436 // ---------------------------------------------------------------------------
       
   437 // CAknPrivateImageLoader::DecodeSize
       
   438 // ---------------------------------------------------------------------------
       
   439 //
       
   440 TSize CAknPrivateImageLoader::DecodeSize( 
       
   441     const TFrameInfo& aFrameInfo, 
       
   442     const TSize& aTargetSize )
       
   443     {
       
   444     TSize imageSize( aFrameInfo.iOverallSizeInPixels );
       
   445     
       
   446     if( aFrameInfo.iFlags & TFrameInfo::EFullyScaleable || 
       
   447         aTargetSize == imageSize )
       
   448         {
       
   449         // decoder can scale to any ratio or the size is already correct
       
   450         return aTargetSize;
       
   451         }
       
   452     
       
   453     if( imageSize.iWidth < aTargetSize.iWidth ||
       
   454         imageSize.iHeight < aTargetSize.iHeight )
       
   455         {
       
   456         // image is smaller than targetsize
       
   457         return imageSize;
       
   458         }
       
   459     
       
   460     // Scale ratio limits
       
   461     const TInt KScaleRatioMin = 2;
       
   462     const TInt KScaleRatioMax = 8;
       
   463     
       
   464     // 1:1 is always valid ratio for decode scaling
       
   465     TInt lastValidRatio( 1 );
       
   466     for( TInt ratio = KScaleRatioMin; ratio <= KScaleRatioMax; ratio <<= 1 )
       
   467         {
       
   468         if( imageSize.iWidth % ratio + imageSize.iHeight % ratio == 0 )
       
   469             {
       
   470             // this ratio is valid
       
   471             if( imageSize.iWidth / ratio < aTargetSize.iWidth || 
       
   472                 imageSize.iHeight / ratio < aTargetSize.iHeight )
       
   473                 {
       
   474                 // the decoded size was smaller in some dimension, 
       
   475                 // the last valid ratio should be used
       
   476                 break;
       
   477                 }
       
   478                 
       
   479             // this scale ratio results to greater or equal size
       
   480             lastValidRatio = ratio;
       
   481             }
       
   482         }
       
   483     
       
   484     // return the size scaled with correct ratio
       
   485     return TSize( imageSize.iWidth / lastValidRatio, 
       
   486                   imageSize.iHeight / lastValidRatio );
       
   487     }
       
   488 
       
   489 // End of file