imagehandlinglib/Src/CIHLFileImage.cpp
changeset 54 48dd0f169f0d
parent 42 2e2a89493e2b
equal deleted inserted replaced
42:2e2a89493e2b 54:48dd0f169f0d
     1 /*
       
     2 * Copyright (c) 2004 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 Image class.
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 // INCLUDE FILES
       
    20 #include "CIHLFileImage.h"
       
    21 
       
    22 #include "CIHLBitmap.h"
       
    23 #include "IHLImplementationIds.h"
       
    24 #include "IHLDebugPrint.h" // Debug print
       
    25 #include <IHLInterfaceIds.h>
       
    26 
       
    27 // Private namespace for constants and functions
       
    28 namespace
       
    29 	{
       
    30 	// Fixed scale factors
       
    31 	enum TScaleFactors
       
    32 		{
       
    33 		EFull		= 1,
       
    34 		EHalf		= 2,
       
    35 		EQuarter	= 4,
       
    36 		EEighth		= 8,
       
    37 		};
       
    38 
       
    39 	// Panic function
       
    40     _LIT( KIHLPanicString, "IHLImage" );
       
    41     void Panic( TInt aPanicCode ) { User::Panic( KIHLPanicString, aPanicCode ); }
       
    42 	}
       
    43 
       
    44 // ============================ MEMBER FUNCTIONS ===============================
       
    45 // -----------------------------------------------------------------------------
       
    46 //
       
    47 // C++ default constructor can NOT contain any code, that
       
    48 // might leave.
       
    49 // -----------------------------------------------------------------------------
       
    50 CIHLFileImage::CIHLFileImage( TInt aImageIndex )
       
    51 :CActive( CActive::EPriorityStandard ),
       
    52 iImageIndex( aImageIndex )
       
    53     {
       
    54 	CActiveScheduler::Add( this );
       
    55     }
       
    56 
       
    57 // -----------------------------------------------------------------------------
       
    58 //
       
    59 // Two-phased constructor.
       
    60 // -----------------------------------------------------------------------------
       
    61 CIHLFileImage* CIHLFileImage::NewL( RFile& aFile, TInt aImageIndex, const TUint32 aOptions )
       
    62 	{
       
    63     CIHLFileImage* self = new (ELeave) CIHLFileImage( aImageIndex );
       
    64 	CleanupStack::PushL( self );
       
    65 	self->ConstructL( aFile, aOptions );
       
    66 	CleanupStack::Pop(); // self
       
    67     return self;
       
    68 	}
       
    69 
       
    70 CIHLFileImage* CIHLFileImage::NewL( RFs& aFs, const TDesC8& aDataBuf,
       
    71                                     TInt aImageIndex, const TUint32 aOptions )
       
    72     {
       
    73     CIHLFileImage* self = new (ELeave) CIHLFileImage( aImageIndex );
       
    74 	CleanupStack::PushL( self );
       
    75 	self->ConstructL( aFs, aDataBuf, aOptions );
       
    76 	CleanupStack::Pop(); // self
       
    77     return self;
       
    78     }
       
    79 
       
    80 // -----------------------------------------------------------------------------
       
    81 //
       
    82 // Symbian constructor can leave.
       
    83 // -----------------------------------------------------------------------------
       
    84 void CIHLFileImage::ConstructL( RFile& aFile, const TUint32 aOptions )
       
    85 	{
       
    86     TInt decoderOptions( CImageDecoder::EOptionNoDither | CImageDecoder::EOptionUseFrameSizeInPixels );
       
    87 
       
    88 	// Open decoder
       
    89     IHL_DEBUG1( KIHLDebug1, "IHL - CIHLFileImage - Start create ICL image decoder" );
       
    90 	if( aOptions & MIHLFileImage::EOptionNoDRMConsume )
       
    91 	    {
       
    92 	    iDecoder = CImageDecoder::FileNewL( aFile, ContentAccess::EPeek,
       
    93 	                             (CImageDecoder::TOptions)decoderOptions );
       
    94 	    }
       
    95 	else
       
    96 	    {
       
    97 	    iDecoder = CImageDecoder::FileNewL( aFile, ContentAccess::EView,
       
    98 	                             (CImageDecoder::TOptions)decoderOptions );
       
    99 	    }
       
   100 	ConstructCommonL();
       
   101     IHL_DEBUG1( KIHLDebug2, "IHL - CIHLFileImage - ICL image decoder ready!" );
       
   102 	}
       
   103 
       
   104 void CIHLFileImage::ConstructL( RFs& aFs, const TDesC8& aDataBuf, const TUint32 /*aOptions*/ )
       
   105     {
       
   106     TInt decoderOptions( CImageDecoder::EOptionNoDither | CImageDecoder::EOptionUseFrameSizeInPixels );
       
   107 
       
   108     IHL_DEBUG1( KIHLDebug1, "IHL - CIHLFileImage - Start create buffered ICL image decoder" );
       
   109 
       
   110     iDecoder = CImageDecoder::DataNewL( aFs, aDataBuf, (CImageDecoder::TOptions)decoderOptions );
       
   111 	ConstructCommonL();
       
   112 
       
   113     IHL_DEBUG1( KIHLDebug2, "IHL - CIHLFileImage - Buffered ICL image decoder ready!" );
       
   114     }
       
   115 
       
   116 
       
   117 // -----------------------------------------------------------------------------
       
   118 // CIHLFileImage::ConstructCommonL
       
   119 // -----------------------------------------------------------------------------
       
   120 
       
   121 void CIHLFileImage::ConstructCommonL()
       
   122     {
       
   123 	// Check frame count and image index
       
   124 	iImageCount = iDecoder->FrameCount();
       
   125 	__ASSERT_ALWAYS( iImageCount > 0, User::Leave( KErrCorrupt ) );
       
   126 
       
   127 	// Get image types
       
   128 	iDecoder->ImageType( iImageIndex, iImageType, iImageSubType );
       
   129 
       
   130 	if( KImageTypeGIFUid == iImageType )
       
   131 		{
       
   132 		iGif = ETrue;
       
   133 		if( iImageCount > 1 )
       
   134 		    {
       
   135     		iAnimation = ETrue;
       
   136     		iAnimationFrameCount = iImageCount;
       
   137     		iImageCount = 1; // Handled as one animated image
       
   138 		    }
       
   139 		}
       
   140 	__ASSERT_ALWAYS( iImageIndex >= 0 && iImageIndex < iImageCount, User::Leave( KErrArgument ) );
       
   141 
       
   142 	// cache frame info and set scale sizes
       
   143 	iFrameInfo = iDecoder->FrameInfo( iImageIndex );
       
   144 	if( !iAnimation )
       
   145 		{
       
   146         // Animation must always be loaded 1:1
       
   147 		if( !iGif &&
       
   148 		    iFrameInfo.iFlags & TFrameInfo::EFullyScaleable )
       
   149 			{
       
   150 			// Gif cannot be fully scaleable
       
   151 			iFullyScaleable = ETrue;
       
   152 			}
       
   153 		else
       
   154 			{
       
   155 			TSize size( iFrameInfo.iOverallSizeInPixels );
       
   156 			iLoadSizeArray.AppendL( ScaledLoadSize( size, EEighth ) );
       
   157 			iLoadSizeArray.AppendL( ScaledLoadSize( size, EQuarter ) );
       
   158 			iLoadSizeArray.AppendL( ScaledLoadSize( size, EHalf ) );
       
   159 			}
       
   160 		}
       
   161     }
       
   162 
       
   163 // -----------------------------------------------------------------------------
       
   164 // Destructor
       
   165 // -----------------------------------------------------------------------------
       
   166 CIHLFileImage::~CIHLFileImage()
       
   167     {
       
   168 	Cancel();
       
   169 	delete iPrevAnimationFrame;
       
   170 	delete iSubFrameBitmap;
       
   171 	delete iDecoder;
       
   172 	iLoadSizeArray.Reset();
       
   173     }
       
   174 
       
   175 // -----------------------------------------------------------------------------
       
   176 // CIHLFileImage::Type
       
   177 // -----------------------------------------------------------------------------
       
   178 TIHLInterfaceType CIHLFileImage::Type() const
       
   179 	{
       
   180 	return TIHLInterfaceType( KIHLInterfaceIdFileImage,
       
   181 							  KIHLImplementationIdFileImage );
       
   182 	}
       
   183 
       
   184 // -----------------------------------------------------------------------------
       
   185 // CIHLFileImage::ImageType
       
   186 // -----------------------------------------------------------------------------
       
   187 const TUid& CIHLFileImage::ImageType() const
       
   188 	{
       
   189 	return iImageType;
       
   190 	}
       
   191 
       
   192 // -----------------------------------------------------------------------------
       
   193 // CIHLFileImage::ImageSubType
       
   194 // -----------------------------------------------------------------------------
       
   195 const TUid& CIHLFileImage::ImageSubType() const
       
   196 	{
       
   197 	return iImageSubType;
       
   198 	}
       
   199 
       
   200 // -----------------------------------------------------------------------------
       
   201 // CIHLFileImage::ImageIndex
       
   202 // -----------------------------------------------------------------------------
       
   203 TInt CIHLFileImage::ImageIndex() const
       
   204 	{
       
   205 	return iImageIndex;
       
   206 	}
       
   207 
       
   208 // -----------------------------------------------------------------------------
       
   209 // CIHLFileImage::ImageCount
       
   210 // -----------------------------------------------------------------------------
       
   211 TInt CIHLFileImage::ImageCount() const
       
   212 	{
       
   213 	return iImageCount;
       
   214 	}
       
   215 
       
   216 // -----------------------------------------------------------------------------
       
   217 // CIHLFileImage::Size
       
   218 // -----------------------------------------------------------------------------
       
   219 TSize CIHLFileImage::Size() const
       
   220 	{
       
   221 	return iFrameInfo.iOverallSizeInPixels;
       
   222 	}
       
   223 
       
   224 // -----------------------------------------------------------------------------
       
   225 // CIHLFileImage::DisplayMode
       
   226 // -----------------------------------------------------------------------------
       
   227 TDisplayMode CIHLFileImage::DisplayMode() const
       
   228 	{
       
   229 	if( iGif )
       
   230 		{
       
   231 		// We cannot trust iFrameDisplayMode for GIF images. It always return EColor256.
       
   232 		// This is error because palette sure holds only 256 colors but these colors can
       
   233 		// be still any RGB values and so for cannot be directly put to 8 bit bitmap (EColor256).
       
   234 		// To decrypt image correctly and without color dithering, we must use 24 bit (EColor16M)
       
   235 		// destination bitmap. Note that CFbsBitmap has palette methods but they are
       
   236 		// not supported currently.
       
   237 		// Return maximum color mode to ensure best image quality.
       
   238 		return EColor16MU;
       
   239 		}
       
   240 	else if( iFrameInfo.iFrameDisplayMode < EColor16MU ||
       
   241 	         iFrameInfo.iFrameDisplayMode == EColor4K )
       
   242 		{
       
   243 		return EColor64K;
       
   244 		}
       
   245 	return EColor16MU;
       
   246 	}
       
   247 
       
   248 // -----------------------------------------------------------------------------
       
   249 // CIHLFileImage::MaskDisplayMode
       
   250 // -----------------------------------------------------------------------------
       
   251 TDisplayMode CIHLFileImage::MaskDisplayMode() const
       
   252 	{
       
   253 	if( iFrameInfo.iFlags & TFrameInfo::ETransparencyPossible )
       
   254 		{
       
   255 		if( iFrameInfo.iFlags & TFrameInfo::EAlphaChannel )
       
   256 			{
       
   257 			return EGray256;
       
   258 			}
       
   259 		return EGray2;
       
   260 		}
       
   261 	return ENone;
       
   262 	}
       
   263 
       
   264 // -----------------------------------------------------------------------------
       
   265 // CIHLFileImage::BackgroundColor
       
   266 // -----------------------------------------------------------------------------
       
   267 TRgb CIHLFileImage::BackgroundColor() const
       
   268 	{
       
   269 	return iFrameInfo.iBackgroundColor;
       
   270 	}
       
   271 
       
   272 // -----------------------------------------------------------------------------
       
   273 // CIHLFileImage::CustomLoadSizeArray
       
   274 // -----------------------------------------------------------------------------
       
   275 const RArray<TSize>& CIHLFileImage::CustomLoadSizeArray() const
       
   276 	{
       
   277 	return iLoadSizeArray;
       
   278 	}
       
   279 
       
   280 // -----------------------------------------------------------------------------
       
   281 // CIHLFileImage::CustomLoadSizeArray
       
   282 // -----------------------------------------------------------------------------
       
   283 TBool CIHLFileImage::IsFullyScaleable() const
       
   284 	{
       
   285 	return iFullyScaleable;
       
   286 	}
       
   287 
       
   288 // -----------------------------------------------------------------------------
       
   289 // CIHLFileImage::IsAnimation
       
   290 // -----------------------------------------------------------------------------
       
   291 TBool CIHLFileImage::IsAnimation() const
       
   292 	{
       
   293 	return iAnimation;
       
   294 	}
       
   295 
       
   296 // -----------------------------------------------------------------------------
       
   297 // CIHLFileImage::AnimationFrameCount
       
   298 // -----------------------------------------------------------------------------
       
   299 TInt CIHLFileImage::AnimationFrameCount() const
       
   300 	{
       
   301 	return iAnimationFrameCount;
       
   302 	}
       
   303 
       
   304 // -----------------------------------------------------------------------------
       
   305 // CIHLFileImage::AnimationFrameDelay
       
   306 // -----------------------------------------------------------------------------
       
   307 TTimeIntervalMicroSeconds32 CIHLFileImage::AnimationFrameDelay( TInt aAnimationFrameIndex ) const
       
   308 	{
       
   309 	__ASSERT_ALWAYS( aAnimationFrameIndex >= 0 &&
       
   310 		aAnimationFrameIndex < iAnimationFrameCount, Panic( KErrArgument ) );
       
   311 
       
   312 	return I64INT( iDecoder->FrameInfo( aAnimationFrameIndex ).iDelay.Int64() );
       
   313 	}
       
   314 
       
   315 // ------------------------------------------------------------------------------
       
   316 // CIHLFileImage::Load
       
   317 // ------------------------------------------------------------------------------
       
   318 
       
   319 TInt CIHLFileImage::Load( TRequestStatus& aStatus, MIHLBitmap& aDestination, TInt aFrameIndex )
       
   320 	{
       
   321 	iImageIndex = aFrameIndex;	
       
   322 	return LoadRequest( aStatus, aDestination, iImageIndex );
       
   323 	}
       
   324 
       
   325 // -----------------------------------------------------------------------------
       
   326 // CIHLFileImage::LoadAnimation
       
   327 // -----------------------------------------------------------------------------
       
   328 TInt CIHLFileImage::LoadAnimation( TRequestStatus& aStatus, MIHLBitmap& aDestination,
       
   329 							  TInt aAnimationFrameIndex )
       
   330 	{
       
   331 	__ASSERT_ALWAYS( aAnimationFrameIndex >= 0 &&
       
   332 		aAnimationFrameIndex < iAnimationFrameCount, Panic( KErrArgument ) );
       
   333 
       
   334 	return LoadRequest( aStatus, aDestination, aAnimationFrameIndex );
       
   335 	}
       
   336 
       
   337 // -----------------------------------------------------------------------------
       
   338 // CIHLFileImage::IsBusy
       
   339 // -----------------------------------------------------------------------------
       
   340 TBool CIHLFileImage::IsBusy() const
       
   341 	{
       
   342 	return ( iImageState != EInactive );
       
   343 	}
       
   344 
       
   345 // -----------------------------------------------------------------------------
       
   346 // CIHLFileImage::CancelLoad
       
   347 // -----------------------------------------------------------------------------
       
   348 void CIHLFileImage::CancelLoad()
       
   349 	{
       
   350 	Cancel();
       
   351 	}
       
   352 
       
   353 // -----------------------------------------------------------------------------
       
   354 // CIHLFileImage::SetFilter
       
   355 // -----------------------------------------------------------------------------
       
   356 void CIHLFileImage::SetFilter( MIHLFilter* /*aFilter*/ )
       
   357 	{
       
   358 	// Not in use
       
   359 	}
       
   360 
       
   361 
       
   362 // -----------------------------------------------------------------------------
       
   363 // CIHLFileImage::Decoder
       
   364 // -----------------------------------------------------------------------------
       
   365 const CImageDecoder& CIHLFileImage::Decoder() const
       
   366 	{
       
   367 	return *iDecoder;
       
   368 	}
       
   369 
       
   370 // -----------------------------------------------------------------------------
       
   371 // CIHLFileImage::DoCancel
       
   372 // -----------------------------------------------------------------------------
       
   373 void CIHLFileImage::DoCancel()
       
   374 	{
       
   375 	iDecoder->Cancel();
       
   376 
       
   377 	// Delete all processed bitmaps
       
   378 	ErrorCleanup();
       
   379 
       
   380 	// Complete with cancel
       
   381 	iImageState = EInactive;
       
   382 	RequestComplete( KErrCancel );
       
   383 	}
       
   384 
       
   385 // -----------------------------------------------------------------------------
       
   386 // CIHLFileImage::RunL
       
   387 // -----------------------------------------------------------------------------
       
   388 void CIHLFileImage::RunL()
       
   389 	{
       
   390 	__ASSERT_DEBUG( iDestination, Panic( KErrGeneral ) );
       
   391 	User::LeaveIfError( iStatus.Int() );
       
   392 
       
   393 	switch( iImageState )
       
   394 		{
       
   395 		case EStartLoad:
       
   396 			{
       
   397 			// start loading the bitmaps
       
   398 			StartLoadL();
       
   399 			break;
       
   400 			}
       
   401 		case ECompleteLoad:
       
   402 			{
       
   403 			// complete loading the bitmaps
       
   404 			CompleteLoadL();
       
   405 			break;
       
   406 			}
       
   407 		default:
       
   408 			{
       
   409 			Panic( KErrTotalLossOfPrecision );
       
   410 			}
       
   411 		}
       
   412 	}
       
   413 
       
   414 // -----------------------------------------------------------------------------
       
   415 // CIHLFileImage::RunError
       
   416 // -----------------------------------------------------------------------------
       
   417 TInt CIHLFileImage::RunError( TInt aError )
       
   418 	{
       
   419     IHL_DEBUG2( KIHLDebug, "IHL - CIHLFileImage - Loading error: %d", aError );
       
   420 
       
   421 	// Delete all processed bitmaps
       
   422 	ErrorCleanup();
       
   423 
       
   424 	// Complete with error
       
   425 	iImageState = EInactive;
       
   426 	RequestComplete( aError );
       
   427 	return KErrNone;
       
   428 	}
       
   429 
       
   430 // -----------------------------------------------------------------------------
       
   431 // CIHLFileImage::LoadRequest
       
   432 // -----------------------------------------------------------------------------
       
   433 TInt CIHLFileImage::LoadRequest( TRequestStatus& aStatus,
       
   434                                  MIHLBitmap& aDestination,
       
   435                                  TInt aFrameIndex )
       
   436 	{
       
   437 	if( IsBusy() )
       
   438 		{
       
   439 		return KErrNotReady;
       
   440 		}
       
   441 
       
   442 	if( aFrameIndex < 0 || aFrameIndex >= iDecoder->FrameCount() )
       
   443 		{
       
   444 		return KErrArgument;
       
   445 		}
       
   446 
       
   447 	const CFbsBitmap& dstBitmap = aDestination.Bitmap();
       
   448 	if( !dstBitmap.Handle() )
       
   449 		{
       
   450 		return KErrArgument;
       
   451 		}
       
   452 
       
   453 	TSize dstSize( dstBitmap.SizeInPixels() );
       
   454 	if( dstSize != Size() &&
       
   455 		!iFullyScaleable )
       
   456 		{
       
   457 		TBool sizeFound( EFalse );
       
   458 		const TInt count( iLoadSizeArray.Count() );
       
   459 		for( TInt i( 0 ); i < count; ++i )
       
   460 			{
       
   461 			if( dstSize == iLoadSizeArray[ i ] )
       
   462 				{
       
   463 				sizeFound = ETrue;
       
   464 				}
       
   465 			}
       
   466 		if( !sizeFound )
       
   467 			{
       
   468 			return KErrArgument;
       
   469 			}
       
   470 		}
       
   471 
       
   472     IHL_DEBUG1( KIHLDebug, "IHL - CIHLFileImage - Frame loading requested" );
       
   473 
       
   474 	iImageStatus = &aStatus;
       
   475 	*iImageStatus = KRequestPending;
       
   476 
       
   477 	iDestination = static_cast<CIHLBitmap*>( &aDestination ); //lint !e826
       
   478 	iFrameIndex = aFrameIndex;
       
   479 
       
   480 	// Start the active object
       
   481 	iImageState = EStartLoad;
       
   482 	SelfComplete();
       
   483 	return KErrNone;
       
   484 	}
       
   485 
       
   486 // -----------------------------------------------------------------------------
       
   487 // CIHLFileImage::StartLoadL
       
   488 // -----------------------------------------------------------------------------
       
   489 void CIHLFileImage::StartLoadL()
       
   490 	{
       
   491 	__ASSERT_DEBUG( !iSubFrameBitmap, Panic( KErrGeneral ) );
       
   492 
       
   493     IHL_DEBUG1( KIHLDebug, "IHL - CIHLFileImage - Start ICL convert" );
       
   494 
       
   495 	if( iAnimation )
       
   496 		{
       
   497 		// Start animation from first frame by default
       
   498 		iSubFrameIndex = 0;
       
   499 
       
   500 		// Check is animation can be continued on top of destination bitmap
       
   501 		if( iDestination->IsCreated() &&
       
   502 			iDestination->EditorPtr() == this &&
       
   503 			iDestination->EditorValue() < iFrameIndex )
       
   504 			{
       
   505 			// Editor value means frame index
       
   506 			iSubFrameIndex = iDestination->EditorValue() + 1;
       
   507 			}
       
   508 
       
   509 		StartLoadSubFrameL( iSubFrameIndex, ETrue );
       
   510 		}
       
   511     else if( iGif )
       
   512         {
       
   513 		StartLoadSubFrameL( iFrameIndex, EFalse );
       
   514         }
       
   515 	else
       
   516 		{
       
   517         // Frame fills the whole image -> normal load
       
   518 	    StartLoadNormalFrame( iFrameIndex );
       
   519 		}
       
   520 
       
   521 	iImageState = ECompleteLoad;
       
   522 	SetActive();
       
   523 	}
       
   524 
       
   525 // -----------------------------------------------------------------------------
       
   526 // CIHLFileImage::StartLoadNormalFrame
       
   527 // -----------------------------------------------------------------------------
       
   528 void CIHLFileImage::StartLoadNormalFrame( TInt aFrameIndex )
       
   529 	{
       
   530 	CFbsBitmap& dstBitmap = iDestination->BitmapModifyable();
       
   531 	CFbsBitmap& dstMask = iDestination->MaskModifyable();
       
   532 
       
   533 	if( MaskDisplayMode() && dstMask.Handle() )
       
   534 		{
       
   535 		iDecoder->Convert( &iStatus, dstBitmap, dstMask, aFrameIndex );
       
   536 		}
       
   537 	else
       
   538 		{
       
   539 		dstMask.Reset();
       
   540 		iDecoder->Convert( &iStatus, dstBitmap, aFrameIndex );
       
   541 		}
       
   542 	}
       
   543 
       
   544 // -----------------------------------------------------------------------------
       
   545 // CIHLFileImage::StartLoadSubFrameL
       
   546 // -----------------------------------------------------------------------------
       
   547 void CIHLFileImage::StartLoadSubFrameL( TInt aFrameIndex, TBool aAnimation )
       
   548 	{
       
   549 	__ASSERT_DEBUG( !iSubFrameBitmap, Panic( KErrGeneral ) );
       
   550 
       
   551 	// Create animation bitmap
       
   552 	iSubFrameBitmap = CIHLBitmap::NewL();
       
   553 	CFbsBitmap& subBitmap = iSubFrameBitmap->BitmapModifyable();
       
   554 	CFbsBitmap& subMask = iSubFrameBitmap->MaskModifyable();
       
   555 
       
   556     TSize dstSize( iDestination->Bitmap().SizeInPixels() );
       
   557 	TFrameInfo subFrameInfo( iDecoder->FrameInfo( aFrameIndex ) );
       
   558 
       
   559     // Check is client uses downscaling (not used in animations)
       
   560 	TSize loadSize( subFrameInfo.iFrameSizeInPixels );   
       
   561     iSubFrameScaleFactor = EFull;
       
   562     if( !aAnimation &&
       
   563         dstSize != iFrameInfo.iOverallSizeInPixels )
       
   564         {
       
   565         if( dstSize == ScaledLoadSize( iFrameInfo.iOverallSizeInPixels, EHalf ) )
       
   566             {
       
   567             iSubFrameScaleFactor = EHalf;
       
   568             loadSize = ScaledLoadSize( loadSize, EHalf );
       
   569             }
       
   570         else if( dstSize == ScaledLoadSize( iFrameInfo.iOverallSizeInPixels, EQuarter ) )
       
   571             {
       
   572             iSubFrameScaleFactor = EQuarter;
       
   573             loadSize = ScaledLoadSize( loadSize, EQuarter );
       
   574             }
       
   575         else if( dstSize == ScaledLoadSize( iFrameInfo.iOverallSizeInPixels, EEighth ) )
       
   576             {
       
   577             iSubFrameScaleFactor = EEighth;
       
   578             loadSize = ScaledLoadSize( loadSize, EEighth );
       
   579             }
       
   580         }
       
   581     User::LeaveIfError( subBitmap.Create( loadSize, EColor16M ) );
       
   582 
       
   583 	if( subFrameInfo.iFlags & TFrameInfo::ETransparencyPossible )
       
   584 		{
       
   585 		User::LeaveIfError( subMask.Create( loadSize,
       
   586 			subFrameInfo.iFlags & TFrameInfo::EAlphaChannel ? EGray256 : EGray2 ) );
       
   587 		iDecoder->Convert( &iStatus, subBitmap, subMask, aFrameIndex );
       
   588 		}
       
   589 	else
       
   590 		{
       
   591 		iDecoder->Convert( &iStatus, subBitmap, aFrameIndex );
       
   592 		}
       
   593 	}
       
   594 
       
   595 // -----------------------------------------------------------------------------
       
   596 // CIHLFileImage::CompleteLoadL
       
   597 // -----------------------------------------------------------------------------
       
   598 void CIHLFileImage::CompleteLoadL()
       
   599 	{
       
   600     IHL_DEBUG1( KIHLDebug1, "IHL - CIHLFileImage - ICL convert complete!" );
       
   601 
       
   602 	if( iSubFrameBitmap )
       
   603 		{
       
   604         IHL_DEBUG1( KIHLDebug2, "IHL - CIHLFileImage - Start build animation frame" );
       
   605 
       
   606 		// Copy animation bitmap to destination
       
   607 		BuildSubFrameL();
       
   608 		delete iSubFrameBitmap;
       
   609 		iSubFrameBitmap = NULL;
       
   610 
       
   611         IHL_DEBUG1( KIHLDebug3, "IHL - CIHLFileImage - Animation frame complete!" );
       
   612 
       
   613 		// Save source info destination
       
   614 		iDestination->SetEditorPtr( this );
       
   615 		iDestination->SetEditorValue( iSubFrameIndex );
       
   616 
       
   617 		if( iSubFrameIndex < iFrameIndex )
       
   618 			{
       
   619 			// re-start the active object and load next subframe
       
   620 			iSubFrameIndex++;
       
   621 			iImageState = EStartLoad;
       
   622 			SelfComplete();
       
   623 			}
       
   624 		else
       
   625 			{
       
   626 			// Animation/subframe image ready
       
   627 			iDestination = NULL;
       
   628 			iImageState = EInactive;
       
   629 			RequestComplete( KErrNone );
       
   630 			}
       
   631 		}
       
   632 	else
       
   633 		{
       
   634 		// Save source info destination
       
   635 		iDestination->SetEditorPtr( this );
       
   636 		iDestination->SetEditorValue( iFrameIndex );
       
   637 
       
   638 		// Normal image ready
       
   639 		iDestination = NULL;
       
   640 		iImageState = EInactive;
       
   641 		RequestComplete( KErrNone );
       
   642 		}
       
   643 
       
   644     IHL_DEBUG1( KIHLDebug4, "IHL - CIHLFileImage - Frame loading request complete!" );
       
   645 	}
       
   646 
       
   647 // -----------------------------------------------------------------------------
       
   648 // CIHLFileImage::BuildSubFrameL
       
   649 // -----------------------------------------------------------------------------
       
   650 void CIHLFileImage::BuildSubFrameL()
       
   651 	{
       
   652 	__ASSERT_DEBUG( iSubFrameBitmap, Panic( KErrGeneral ) );
       
   653 	const CFbsBitmap& subBitmap = iSubFrameBitmap->Bitmap();
       
   654 	const CFbsBitmap& subMask = iSubFrameBitmap->Mask();
       
   655 	__ASSERT_DEBUG( subBitmap.Handle(), Panic( KErrGeneral ) );
       
   656 
       
   657 	if( !iAnimation ||
       
   658 	    ( iAnimation && iSubFrameIndex == 0 ) )
       
   659 		{
       
   660 		TFrameInfo frameInfo( iDecoder->FrameInfo( iSubFrameIndex ) );
       
   661         if( iDestination->Bitmap().SizeInPixels() == subBitmap.SizeInPixels() &&
       
   662             frameInfo.iFrameCoordsInPixels.iTl == TPoint(0,0) )
       
   663             {
       
   664     		// Sub frame is same size as destination image and has no offset
       
   665     		// -> put directly into destination
       
   666     		User::LeaveIfError( iDestination->Copy( subBitmap, subMask, ETrue ) );
       
   667             }
       
   668         else
       
   669             {
       
   670             // Sub frame size differs from destination image size
       
   671     		CFbsBitmap& desBitmap = iDestination->BitmapModifyable();
       
   672     		CFbsBitmap& desMask = iDestination->MaskModifyable();
       
   673 
       
   674     		// Other frames must be build on top of previous frames
       
   675     		__ASSERT_DEBUG( desBitmap.Handle(), Panic( KErrGeneral ) );
       
   676 
       
   677             // Fill destination using background color
       
   678 		    FillL( desBitmap, frameInfo.iBackgroundColor );
       
   679 
       
   680     		// Copy loaded frame on top of background
       
   681     		CFbsBitGc* bitGc;
       
   682     		CFbsBitmapDevice* bitDevice = CFbsBitmapDevice::NewL( &desBitmap );
       
   683     		CleanupStack::PushL( bitDevice );
       
   684     		User::LeaveIfError( bitDevice->CreateContext( bitGc ) );
       
   685     		CleanupStack::PushL( bitGc );
       
   686 
       
   687             TPoint framePos( ScaledFramePosition(
       
   688                     frameInfo.iFrameCoordsInPixels.iTl, iSubFrameScaleFactor ) );
       
   689     		if( subMask.Handle() )
       
   690     			{
       
   691     			bitGc->BitBltMasked( framePos, &subBitmap,
       
   692     								 subBitmap.SizeInPixels(), &subMask, EFalse );
       
   693     			}
       
   694     		else
       
   695     			{
       
   696     			bitGc->BitBlt( framePos, &subBitmap, subBitmap.SizeInPixels() );
       
   697     			}
       
   698     		CleanupStack::PopAndDestroy( 2, bitDevice ); // bitGc, bitDevice
       
   699 
       
   700             if( desMask.Handle() )
       
   701                 {
       
   702                 // Fill mask to transparent
       
   703     		    FillL( desMask, KRgbBlack );
       
   704 
       
   705                 // Fill bg mask with transparency (=black)
       
   706         		CFbsBitmapDevice* maskDev = CFbsBitmapDevice::NewL( &desMask );
       
   707         		CleanupStack::PushL( maskDev );
       
   708     		    CFbsBitGc* maskGc;
       
   709         		User::LeaveIfError( maskDev->CreateContext( maskGc ) );
       
   710         		CleanupStack::PushL( maskGc );
       
   711 
       
   712                 // Combine bg mask with first frame mask
       
   713 				maskGc->BitBlt( framePos, &subMask, subMask.SizeInPixels() );
       
   714 
       
   715     		    CleanupStack::PopAndDestroy( 2, maskDev ); // maskGc, maskDev
       
   716                 }
       
   717             }
       
   718 
       
   719 		// Create "previous frame" if animation
       
   720 		if( iAnimation )
       
   721 		    {
       
   722     		if( !iPrevAnimationFrame )
       
   723     		    {
       
   724     		    iPrevAnimationFrame = CIHLBitmap::NewL();
       
   725     		    }
       
   726     		CFbsBitmap& desBitmap = iDestination->BitmapModifyable();
       
   727     		CFbsBitmap& desMask = iDestination->MaskModifyable();
       
   728     		if( iSubFrameBitmap->HasMask() )
       
   729     		    {
       
   730         		User::LeaveIfError(
       
   731         		    iPrevAnimationFrame->Create( desBitmap.SizeInPixels(),
       
   732         		        desBitmap.DisplayMode(), desMask.DisplayMode() ) );
       
   733         		FillL( iPrevAnimationFrame->BitmapModifyable(), frameInfo.iBackgroundColor );
       
   734     		    FillL( iPrevAnimationFrame->MaskModifyable(), KRgbBlack );
       
   735     		    }
       
   736     		else
       
   737     		    {
       
   738         		User::LeaveIfError(
       
   739         		    iPrevAnimationFrame->Create( desBitmap.SizeInPixels(),
       
   740         		                                 desBitmap.DisplayMode() ) );
       
   741         		FillL( iPrevAnimationFrame->BitmapModifyable(), frameInfo.iBackgroundColor );
       
   742     		    }
       
   743 		    }
       
   744 		}
       
   745 	else // same as iAnimation && iSubFrameIndex > 0
       
   746 		{
       
   747 		TFrameInfo prevFrameInfo( iDecoder->FrameInfo( iSubFrameIndex - 1 ) );
       
   748 	    if ( prevFrameInfo.iFlags & TFrameInfo::ERestoreToPrevious )
       
   749 	        {
       
   750             // Restore destination to "previous frame"
       
   751             User::LeaveIfError( iDestination->Copy( *iPrevAnimationFrame, EFalse ) );
       
   752 	        }
       
   753 
       
   754 		CFbsBitmap& prevBitmap = iDestination->BitmapModifyable();
       
   755 		CFbsBitmap& prevMask = iDestination->MaskModifyable();
       
   756 
       
   757 		// Other frames must be build on top of previous frames
       
   758 		__ASSERT_DEBUG( prevBitmap.Handle(), Panic( KErrGeneral ) );
       
   759 
       
   760 		// Restore area in destination bitmap if needed
       
   761 		TRect restoreRect;
       
   762 		TBool restoreToBackground( EFalse );
       
   763 		if( prevFrameInfo.iFlags & TFrameInfo::ERestoreToBackground )
       
   764 			{
       
   765 			restoreToBackground = ETrue;
       
   766 			restoreRect = prevFrameInfo.iFrameCoordsInPixels;
       
   767 		    FillRectL( prevBitmap, restoreRect, prevFrameInfo.iBackgroundColor );
       
   768 
       
   769 			// Cache new "previous frame"
       
   770 			User::LeaveIfError( iPrevAnimationFrame->Copy( *iDestination, EFalse ) );
       
   771 			}
       
   772 	    else if( prevFrameInfo.iFlags & TFrameInfo::ELeaveInPlace )
       
   773 	        {
       
   774 			// Cache new "previous frame"
       
   775 			User::LeaveIfError( iPrevAnimationFrame->Copy( *iDestination, EFalse ) );
       
   776 	        }
       
   777 
       
   778 		// Copy animation frame to destination bitmap
       
   779 		TFrameInfo frameInfo( iDecoder->FrameInfo( iSubFrameIndex ) );
       
   780 		CFbsBitGc* bitGc;
       
   781 		CFbsBitmapDevice* bitDevice = CFbsBitmapDevice::NewL( &prevBitmap );
       
   782 		CleanupStack::PushL( bitDevice );
       
   783 		User::LeaveIfError( bitDevice->CreateContext( bitGc ) );
       
   784 		CleanupStack::PushL( bitGc );
       
   785 		if( subMask.Handle() )
       
   786 			{
       
   787 			bitGc->BitBltMasked( frameInfo.iFrameCoordsInPixels.iTl, &subBitmap,
       
   788 									subBitmap.SizeInPixels(), &subMask, EFalse );
       
   789 			}
       
   790 		else
       
   791 			{
       
   792 			bitGc->BitBlt( frameInfo.iFrameCoordsInPixels.iTl, &subBitmap,
       
   793 									subBitmap.SizeInPixels() );
       
   794 			}
       
   795 		CleanupStack::PopAndDestroy( 2 ); // bitGc, bitDevice
       
   796 
       
   797 		// Combine masks if any
       
   798 		if( prevMask.Handle() && subMask.Handle() )
       
   799 			{
       
   800 			//////////////////////////////////////////////////////////////////////////
       
   801 			// ALTERNATIVE WAY TO COMBINE MASKS!
       
   802 			// Current solution doesn't combine soft masks.
       
   803 			// Following code could be used if soft masks are enabled in animations.
       
   804 			// Do not delete!
       
   805 			//////////////////////////////////////////////////////////////////////////
       
   806 			/*
       
   807 			if( restoreToBackground )
       
   808 				{
       
   809 				bitDevice = CFbsBitmapDevice::NewL( &prevMask );
       
   810 				CleanupStack::PushL( bitDevice );
       
   811 				User::LeaveIfError( bitDevice->CreateContext( bitGc ) );
       
   812 				CleanupStack::PushL( bitGc );
       
   813 
       
   814 				bitGc->SetBrushColor( KRgbBlack );
       
   815 				bitGc->SetBrushStyle( CGraphicsContext::ESolidBrush );
       
   816 				bitGc->DrawRect( restoreRect );
       
   817 				bitGc->SetBrushStyle( CGraphicsContext::ENullBrush );
       
   818 
       
   819 				CleanupStack::PopAndDestroy( 2 ); // bitDevice, bitGc
       
   820 				}
       
   821 
       
   822 			prevMask.LockHeap();
       
   823 
       
   824 			TUint8* srcAddress = reinterpret_cast<TUint8*>( animMask.DataAddress() );
       
   825 			TSize srcSize( animMask.SizeInPixels() );
       
   826 			TPoint srcPos( 0,0 );
       
   827 			TInt srcScanLen8 = CFbsBitmap::ScanLineLength( srcSize.iWidth, EGray256 );
       
   828 
       
   829 			TUint8* dstAddress = reinterpret_cast<TUint8*>( prevMask.DataAddress() );
       
   830 			TSize dstSize( prevMask.SizeInPixels() );
       
   831 			TPoint dstPos( frameInfo.iFrameCoordsInPixels.iTl );
       
   832 			TPoint dstEndPos( frameInfo.iFrameCoordsInPixels.iBr );
       
   833 			TInt dstScanLen8 = CFbsBitmap::ScanLineLength( dstSize.iWidth, EGray256 );
       
   834 
       
   835 			while( dstPos.iY < dstEndPos.iY )
       
   836 				{
       
   837 				TUint8* srcAddressCur = srcAddress + ( srcPos.iY * srcScanLen8 );
       
   838 				TUint8* dstAddressCur = dstAddress + ( dstPos.iY * dstScanLen8 );
       
   839 				while( dstPos.iX < dstEndPos.iX )
       
   840 					{
       
   841 					TUint8& srcPixel = srcAddressCur[ srcPos.iX++ ];
       
   842 					TUint8& dstPixel = dstAddressCur[ dstPos.iX++ ];
       
   843 					if( srcPixel > dstPixel )
       
   844 						{
       
   845 						dstPixel = srcPixel;
       
   846 						}
       
   847 					}
       
   848 
       
   849 				srcPos.iX = 0;
       
   850 				srcPos.iY++;
       
   851 				dstPos.iX = frameInfo.iFrameCoordsInPixels.iTl.iX;
       
   852 				dstPos.iY++;
       
   853 				}
       
   854 
       
   855 			animMask.UnlockHeap();
       
   856 			*/
       
   857 
       
   858 			if( restoreToBackground )
       
   859 				{
       
   860 		        FillRectL( prevMask, restoreRect, KRgbBlack );
       
   861 				}
       
   862 
       
   863 			bitDevice = CFbsBitmapDevice::NewL( &prevMask );
       
   864 			CleanupStack::PushL( bitDevice );
       
   865 			User::LeaveIfError( bitDevice->CreateContext( bitGc ) );
       
   866 			CleanupStack::PushL( bitGc );
       
   867 
       
   868 			CFbsBitmap* tmpMask = new(ELeave) CFbsBitmap;
       
   869 			CleanupStack::PushL( tmpMask );
       
   870 			User::LeaveIfError( tmpMask->Create( prevMask.SizeInPixels(), prevMask.DisplayMode() ) );
       
   871 			CFbsBitmapDevice* tmpMaskDev = CFbsBitmapDevice::NewL( tmpMask );
       
   872 			CleanupStack::PushL( tmpMaskDev );
       
   873 			CFbsBitGc* tmpMaskGc;
       
   874 			User::LeaveIfError( tmpMaskDev->CreateContext( tmpMaskGc ) );
       
   875 			CleanupStack::PushL( tmpMaskGc );
       
   876 
       
   877 			tmpMaskGc->BitBlt( TPoint( 0, 0 ), &prevMask, frameInfo.iFrameCoordsInPixels );
       
   878 
       
   879 			bitGc->BitBltMasked( frameInfo.iFrameCoordsInPixels.iTl, &subMask,
       
   880 								 subMask.SizeInPixels(), tmpMask, ETrue );
       
   881 
       
   882 			CleanupStack::PopAndDestroy( 5 ); // tmpMaskGc,tmpMaskDev,tmpMask,bitGc,bitDevice
       
   883 			}
       
   884 		else
       
   885 			{
       
   886 			prevMask.Reset(); // Mask not valid anymore -> reset
       
   887 			}
       
   888 		}
       
   889 	}
       
   890 
       
   891 // -----------------------------------------------------------------------------
       
   892 // CIHLFileImage::FillL
       
   893 // -----------------------------------------------------------------------------
       
   894 void CIHLFileImage::FillL( CFbsBitmap& aBitmap, const TRgb& aColor )
       
   895     {
       
   896 	CFbsBitGc* bitGc;
       
   897 	CFbsBitmapDevice* bitDevice = CFbsBitmapDevice::NewL( &aBitmap );
       
   898 	CleanupStack::PushL( bitDevice );
       
   899 	User::LeaveIfError( bitDevice->CreateContext( bitGc ) );
       
   900 	CleanupStack::PushL( bitGc );
       
   901 
       
   902 	bitGc->SetBrushColor( aColor );
       
   903 	bitGc->SetPenColor( aColor );
       
   904 	bitGc->SetBrushStyle( CGraphicsContext::ESolidBrush );
       
   905 	bitGc->Clear();
       
   906 	bitGc->SetBrushStyle( CGraphicsContext::ENullBrush );
       
   907 
       
   908 	CleanupStack::PopAndDestroy( 2 ); // bitGc, bitDevice
       
   909     }
       
   910 
       
   911 // -----------------------------------------------------------------------------
       
   912 // CIHLFileImage::FillRectL
       
   913 // -----------------------------------------------------------------------------
       
   914 void CIHLFileImage::FillRectL( CFbsBitmap& aBitmap, const TRect& aRect,
       
   915                                const TRgb& aColor )
       
   916     {
       
   917 	CFbsBitGc* bitGc;
       
   918 	CFbsBitmapDevice* bitDevice = CFbsBitmapDevice::NewL( &aBitmap );
       
   919 	CleanupStack::PushL( bitDevice );
       
   920 	User::LeaveIfError( bitDevice->CreateContext( bitGc ) );
       
   921 	CleanupStack::PushL( bitGc );
       
   922 
       
   923 	bitGc->SetBrushColor( aColor );
       
   924 	bitGc->SetPenColor( aColor );
       
   925 	bitGc->SetBrushStyle( CGraphicsContext::ESolidBrush );
       
   926 	bitGc->DrawRect( aRect );
       
   927 	bitGc->SetBrushStyle( CGraphicsContext::ENullBrush );
       
   928 
       
   929 	CleanupStack::PopAndDestroy( 2 ); // bitGc, bitDevice
       
   930     }
       
   931 
       
   932 // -----------------------------------------------------------------------------
       
   933 // CIHLFileImage::ErrorCleanup
       
   934 // -----------------------------------------------------------------------------
       
   935 void CIHLFileImage::ErrorCleanup()
       
   936 	{
       
   937 	if( iSubFrameBitmap )
       
   938 		{
       
   939 		delete iSubFrameBitmap;
       
   940 		iSubFrameBitmap = NULL;
       
   941 		}
       
   942 
       
   943 	if( iDestination )
       
   944 		{
       
   945 		iDestination = NULL;
       
   946 		}
       
   947 	}
       
   948 
       
   949 // -----------------------------------------------------------------------------
       
   950 // CIHLFileImage::SelfComplete
       
   951 // -----------------------------------------------------------------------------
       
   952 void CIHLFileImage::SelfComplete()
       
   953 	{
       
   954 	SetActive();
       
   955 	iStatus = KRequestPending;
       
   956 	TRequestStatus* status = &iStatus;
       
   957 	User::RequestComplete( status, KErrNone );
       
   958 	}
       
   959 
       
   960 // -----------------------------------------------------------------------------
       
   961 // CIHLFileImage::RequestComplete
       
   962 // -----------------------------------------------------------------------------
       
   963 void CIHLFileImage::RequestComplete( TInt aReason )
       
   964 	{
       
   965 	ASSERT( iImageStatus );
       
   966 	User::RequestComplete( iImageStatus, aReason );
       
   967 	}
       
   968 
       
   969 // -----------------------------------------------------------------------------
       
   970 // CIHLFileImage::ScaledLoadSize
       
   971 // -----------------------------------------------------------------------------
       
   972 TSize CIHLFileImage::ScaledLoadSize( const TSize& aOriginalSize, TInt aScaleFactor )
       
   973 	{
       
   974 	// Divide original size with scalefactor
       
   975 	// Round always up if result is not integer
       
   976 	return ( aScaleFactor == EFull ) ? aOriginalSize :
       
   977 		TSize( aOriginalSize.iWidth / aScaleFactor + ( aOriginalSize.iWidth % aScaleFactor ? 1 : 0 ),
       
   978 			   aOriginalSize.iHeight / aScaleFactor + ( aOriginalSize.iHeight % aScaleFactor ? 1 : 0 ) );
       
   979 	}
       
   980 
       
   981 // -----------------------------------------------------------------------------
       
   982 // CIHLFileImage::ScaledLoadSize
       
   983 // -----------------------------------------------------------------------------
       
   984 TPoint CIHLFileImage::ScaledFramePosition( const TPoint& aOriginalPos, TInt aScaleFactor )
       
   985 	{
       
   986 	// Divide original position with scalefactor
       
   987 	// Round always up if result is not integer
       
   988 	return ( aScaleFactor == EFull ) ? aOriginalPos :
       
   989 		TPoint( aOriginalPos.iX / aScaleFactor + ( aOriginalPos.iX % aScaleFactor ? 1 : 0 ),
       
   990 			    aOriginalPos.iY / aScaleFactor + ( aOriginalPos.iY % aScaleFactor ? 1 : 0 ) );
       
   991 	}
       
   992 //  End of File