gssettingsuis/Gs/GSBackgroundImage/Src/GsAsyncImageHandling.cpp
branchRCL_3
changeset 24 8ee96d21d9bf
parent 23 8bda91a87a00
child 25 7e0eff37aedb
equal deleted inserted replaced
23:8bda91a87a00 24:8ee96d21d9bf
     1 /*
       
     2 * Copyright (c) 2005-2007 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description: 
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 // INCLUDE FILES
       
    20 #include "gsasyncimagehandling.h"
       
    21 #include <aknappui.h>
       
    22 #include <AknUtils.h>
       
    23 #include <applayout.cdl.h>
       
    24 #include <aknlayoutscalable_apps.cdl.h>
       
    25 #include <startupdomaincrkeys.h>
       
    26 
       
    27 #include "mgsasyncimagehandlingobserver.h"
       
    28 #include "GsLogger.h"
       
    29 
       
    30 #include <CPhCltImageHandler.h>
       
    31 #include <CPhCltImageParams.h>
       
    32 #include <CPhCltBaseImageParams.h>
       
    33 
       
    34 //_LIT( KGSWelcomeNoteImgPath, "c:\\private\\100058ec\\welcomeimage.mbm");
       
    35 
       
    36 const   TInt    KGSVTStillImageWidth = 176;
       
    37 const   TInt    KGSVTStillImageHeight = 144;
       
    38 
       
    39 
       
    40 // ================= MEMBER FUNCTIONS =======================
       
    41 
       
    42 // ---------------------------------------------------------------------------
       
    43 // CGSAsyncImageHandling::CGSAsyncImageHandling
       
    44 // C++ constructor.
       
    45 //
       
    46 // ---------------------------------------------------------------------------
       
    47 //
       
    48 CGSAsyncImageHandling::CGSAsyncImageHandling( RFs& aFs,
       
    49                                   MGSAsyncImageHandlingObserver* aObserver,
       
    50                                   const TDesC& aDestinationPath )
       
    51         : CActive( EPriorityNormal ),
       
    52           iObserver( aObserver ),
       
    53           iFs( aFs ),
       
    54           iDestinationPath( aDestinationPath )
       
    55           
       
    56     {
       
    57     CActiveScheduler::Add( this );
       
    58     }
       
    59 
       
    60 
       
    61 // ---------------------------------------------------------------------------
       
    62 // CGSAsyncImageHandling::NewL()
       
    63 // C++ constructor.
       
    64 //
       
    65 // ---------------------------------------------------------------------------
       
    66 //
       
    67 EXPORT_C CGSAsyncImageHandling* CGSAsyncImageHandling::NewL( RFs& aFs,
       
    68                                    MGSAsyncImageHandlingObserver* aObserver,
       
    69                                    const TDesC& aDestinationPath )
       
    70     {
       
    71     CGSAsyncImageHandling* self =
       
    72         new( ELeave ) CGSAsyncImageHandling( aFs, aObserver,
       
    73                                                   aDestinationPath );
       
    74     CleanupStack::PushL( self );                                              
       
    75     self->ConstructL();
       
    76     CleanupStack::Pop();
       
    77     
       
    78     return self;
       
    79     }
       
    80 
       
    81 
       
    82 // -----------------------------------------------------------------------------
       
    83 // CGSAsyncImageHandling::ConstructL
       
    84 // Symbian 2nd phase constructor can leave.
       
    85 //
       
    86 // -----------------------------------------------------------------------------
       
    87 //
       
    88 void CGSAsyncImageHandling::ConstructL()
       
    89     {
       
    90     iStartupRepository = CRepository::NewL( KCRUidStartupConf );
       
    91     iBitmapScaler = CBitmapScaler::NewL();
       
    92     }
       
    93 
       
    94 
       
    95 // ---------------------------------------------------------------------------
       
    96 // CGSAsyncImageHandling::~CGSAsyncImageHandling
       
    97 // destructor.
       
    98 //
       
    99 // ---------------------------------------------------------------------------
       
   100 //
       
   101 CGSAsyncImageHandling::~CGSAsyncImageHandling()
       
   102     {
       
   103     Cancel();
       
   104 
       
   105     if ( iStartupRepository )
       
   106         {
       
   107         delete iStartupRepository;
       
   108         }
       
   109 
       
   110     if( iDecoder )
       
   111         {
       
   112         delete iDecoder;
       
   113         }
       
   114 
       
   115     if ( iBitmapScaler )
       
   116         {
       
   117         delete iBitmapScaler;
       
   118         }
       
   119 
       
   120     if ( iBitmap )
       
   121         {
       
   122         delete iBitmap;
       
   123         }
       
   124     }
       
   125 
       
   126 // ---------------------------------------------------------------------------
       
   127 // CGSAsyncImageHandling::ProcessImageL
       
   128 //
       
   129 // Processing the image asynchronously
       
   130 // ---------------------------------------------------------------------------
       
   131 //
       
   132 void CGSAsyncImageHandling::ProcessImageL( const TDesC& aFileName,
       
   133                                            TInt aScreenWidth,
       
   134                                            TInt aScreenHeight,
       
   135                                            TGSBgImageType aBgImageType )
       
   136     {
       
   137 __GSLOGSTRING1( "[CGSAsyncImageHandling] process image %S", &aFileName );
       
   138 
       
   139     TInt frameNumber = 0; //for JPEG & bmp images
       
   140     iBgImageType = aBgImageType;
       
   141 
       
   142     // make sure there is no memory leaks because of iBitmap and iDimmedBmp
       
   143     if (iBitmap)
       
   144         {
       
   145         delete iBitmap;
       
   146         iBitmap = NULL;
       
   147         }
       
   148     iBitmap = new(ELeave) CFbsBitmap;
       
   149 
       
   150     // Loading image from file into CFbsBitmap
       
   151     CImageDecoder::GetMimeTypeFileL( iFs, aFileName, iMimeString );
       
   152     iDecoder = CImageDecoder::FileNewL( iFs, aFileName,
       
   153                                         CImageDecoder::EOptionNone );
       
   154 
       
   155     TFrameInfo frameInfo = iDecoder->FrameInfo();
       
   156     TSize imgSize = frameInfo.iOverallSizeInPixels;
       
   157 
       
   158 
       
   159     TDisplayMode displayMode = GetDisplayMode( frameInfo );
       
   160 
       
   161     TSize screenSize( aScreenWidth, aScreenHeight );
       
   162     TSize loadSize( CalculateLoadSize( frameInfo, screenSize ) );
       
   163     iScaleSize = loadSize;
       
   164 
       
   165     iBitmap->Reset();
       
   166     User::LeaveIfError( iBitmap->Create( imgSize, displayMode ) );
       
   167     iDecoder->Convert( &iStatus, *iBitmap, frameNumber );
       
   168 
       
   169     if ( aBgImageType == EGSWelcomeNoteImage )
       
   170       {
       
   171       iState = EGSWelcomeConversion;
       
   172       }
       
   173     else if ( aBgImageType == EGSVtStillImage )
       
   174       {
       
   175       iState = EGSVTConversion;
       
   176       }
       
   177 
       
   178     SetActive();
       
   179     }
       
   180 
       
   181 // ---------------------------------------------------------------------------
       
   182 // CGSAsyncImageHandling::GetDisplayMode
       
   183 //
       
   184 // Dithers the image to attain the required dithering according to the display
       
   185 // mode settings
       
   186 // ---------------------------------------------------------------------------
       
   187 //
       
   188 TDisplayMode CGSAsyncImageHandling::GetDisplayMode( const TFrameInfo& aFrameInfo )
       
   189     {
       
   190     if( aFrameInfo.iFlags & TFrameInfo::ECanDither )
       
   191         {
       
   192         if( aFrameInfo.iFrameDisplayMode < EColor64K )
       
   193             {
       
   194             return aFrameInfo.iFrameDisplayMode;
       
   195             }
       
   196         else
       
   197             {
       
   198             return EColor64K;
       
   199             }
       
   200         }
       
   201     else
       
   202         {
       
   203         return aFrameInfo.iFrameDisplayMode;
       
   204         }
       
   205     }
       
   206 
       
   207 
       
   208 // -----------------------------------------------------------------------------
       
   209 // CGSAsyncImageHandling::RunL
       
   210 //
       
   211 //
       
   212 // ---------------------------------------------------------------------------
       
   213 //
       
   214 void CGSAsyncImageHandling::RunL()
       
   215     {
       
   216     if( iStatus.Int() == KErrNone )
       
   217       {
       
   218       switch( iState )
       
   219         {
       
   220         case EGSWelcomeConversion:
       
   221           iBitmapScaler->Scale(&iStatus, *iBitmap, iScaleSize, ETrue );
       
   222           iState = EGSWelcomeScaling;
       
   223           SetActive();
       
   224           break;
       
   225 
       
   226         case EGSWelcomeScaling:
       
   227             {
       
   228             TInt err = iBitmap->Save( iDestinationPath );
       
   229             iStartupRepository->Set( KStartupWelcomeNoteImage,
       
   230                                      iDestinationPath );
       
   231 
       
   232             //raising completion event
       
   233             iObserver->ImageHandlingCompleteL( iStatus.Int() );
       
   234             Cancel();
       
   235             }
       
   236           break;
       
   237 
       
   238         case EGSVTConversion:
       
   239           iBitmapScaler->Scale(&iStatus, *iBitmap, iScaleSize, ETrue );
       
   240           iState = EGSVTScaling;
       
   241           SetActive();
       
   242           break;
       
   243 
       
   244         case EGSVTScaling:
       
   245           SaveVTStillImageL();
       
   246           iObserver->ImageHandlingCompleteL( iStatus.Int() );
       
   247           Cancel();
       
   248           break;
       
   249 
       
   250         default:
       
   251           break;
       
   252         }
       
   253       }
       
   254     else
       
   255       {
       
   256       iObserver->ImageHandlingCompleteL( iStatus.Int() );
       
   257       Cancel();
       
   258       }
       
   259     }
       
   260 
       
   261 // -----------------------------------------------------------------------------
       
   262 // CGSAsyncImageHandling::DoCancel
       
   263 //
       
   264 //
       
   265 // ---------------------------------------------------------------------------
       
   266 //
       
   267 void CGSAsyncImageHandling::DoCancel()
       
   268     {
       
   269     if ( iDecoder )
       
   270         {
       
   271         iDecoder->Cancel();
       
   272         }
       
   273 
       
   274     if ( iBitmapScaler )
       
   275         {
       
   276         iBitmapScaler->Cancel();
       
   277         }
       
   278 
       
   279     delete iDecoder;
       
   280     iDecoder = NULL;
       
   281     }
       
   282 
       
   283 // -----------------------------------------------------------------------------
       
   284 // CGSAsyncImageHandling::RunError
       
   285 //
       
   286 //
       
   287 // ---------------------------------------------------------------------------
       
   288 //
       
   289 TInt CGSAsyncImageHandling::RunError( TInt aError )
       
   290     {
       
   291     TRAP_IGNORE( iObserver->ImageHandlingCompleteL( aError ) );
       
   292     DoCancel();
       
   293     return aError;
       
   294     }
       
   295 
       
   296 // ----------------------------------------------------------------------------
       
   297 // TSize CGSAsyncImageHandling::CalculateLoadSize
       
   298 //
       
   299 // Calculates the load size
       
   300 // ----------------------------------------------------------------------------
       
   301 //
       
   302 TSize CGSAsyncImageHandling::CalculateLoadSize( TFrameInfo& aFrameInfo,
       
   303                                                const TSize aScreenSize )
       
   304     {
       
   305     TReal perfectAspect = ( TReal )(
       
   306     		( TReal )aScreenSize.iWidth/
       
   307     		( TReal )aScreenSize.iHeight );
       
   308     TReal aspect = ( TReal )(
       
   309         ( TReal )aFrameInfo.iFrameCoordsInPixels.Width()/
       
   310         ( TReal )aFrameInfo.iFrameCoordsInPixels.Height() );
       
   311     TSize size( aScreenSize.iWidth, aScreenSize.iHeight );
       
   312     TSize cropsize( aScreenSize.iWidth, aScreenSize.iHeight );
       
   313 
       
   314     TAknWindowLineLayout layout = AknLayout::wallpaper_pane();
       
   315     if( aFrameInfo.iFrameCoordsInPixels.Width() > aScreenSize.iWidth ||
       
   316         aFrameInfo.iFrameCoordsInPixels.Height() > aScreenSize.iHeight )
       
   317         {
       
   318         //calculating dynamically range for image aspect ratio close to screen
       
   319         TReal maxAspect = ((TReal)aScreenSize.iWidth/(TReal)aScreenSize.iHeight) + 0.12;
       
   320         TReal minAspect = ((TReal)aScreenSize.iWidth/(TReal)aScreenSize.iHeight) - 0.12;
       
   321 
       
   322         if (aspect >= minAspect && aspect <= maxAspect )
       
   323             {
       
   324             if( aspect < perfectAspect )
       
   325                 {
       
   326                 size.iWidth = aScreenSize.iWidth;
       
   327                 size.iHeight = (TInt)((TReal)aScreenSize.iWidth/
       
   328                     (TReal)aFrameInfo.iFrameCoordsInPixels.Width()*
       
   329                     (TReal)aFrameInfo.iFrameCoordsInPixels.Height());
       
   330                 }
       
   331             else
       
   332                 {
       
   333                 size.iWidth = (TInt)((TReal)aScreenSize.iHeight/
       
   334                     (TReal)aFrameInfo.iFrameCoordsInPixels.Height()*
       
   335                     (TReal)aFrameInfo.iFrameCoordsInPixels.Width());
       
   336                 size.iHeight = aScreenSize.iHeight;
       
   337                 }
       
   338             if (size.iWidth > aScreenSize.iWidth)
       
   339                 {
       
   340                 cropsize.iWidth = aScreenSize.iWidth;
       
   341                 }
       
   342             else
       
   343                 {
       
   344                 cropsize.iWidth = size.iWidth;
       
   345                 }
       
   346             if (size.iHeight > aScreenSize.iHeight)
       
   347                 {
       
   348                 cropsize.iHeight = aScreenSize.iHeight;
       
   349                 }
       
   350             else
       
   351                 {
       
   352                 cropsize.iHeight = size.iHeight;
       
   353                 }
       
   354             }
       
   355         else
       
   356             {
       
   357             // Scale and maintain aspect ratio
       
   358             if( aspect < perfectAspect )
       
   359                 {
       
   360                 if (aFrameInfo.iFrameCoordsInPixels.Height() > aScreenSize.iHeight)
       
   361                     {
       
   362                     size.iWidth = (TInt)((TReal)aScreenSize.iHeight/
       
   363                         (TReal)aFrameInfo.iFrameCoordsInPixels.Height()*
       
   364                         (TReal)aFrameInfo.iFrameCoordsInPixels.Width());
       
   365                     size.iHeight = aScreenSize.iHeight;
       
   366                     }
       
   367                 }
       
   368             else
       
   369                 {
       
   370                 if (aFrameInfo.iFrameCoordsInPixels.Width() > aScreenSize.iWidth)
       
   371                     {
       
   372                     size.iWidth = aScreenSize.iWidth;
       
   373                     size.iHeight = (TInt)((TReal)aScreenSize.iWidth/
       
   374                         (TReal)aFrameInfo.iFrameCoordsInPixels.Width()*
       
   375                         (TReal)aFrameInfo.iFrameCoordsInPixels.Height());
       
   376                     }
       
   377                 }
       
   378             }
       
   379         }
       
   380 
       
   381     return size;
       
   382     }
       
   383 
       
   384 // -----------------------------------------------------------------------------
       
   385 // CGSAsyncImageHandling::SaveVTStillImageL
       
   386 //
       
   387 //
       
   388 // ---------------------------------------------------------------------------
       
   389 //
       
   390 void CGSAsyncImageHandling::SaveVTStillImageL()
       
   391     {
       
   392     //creating new and merging
       
   393     const TSize KStillImageSize( KGSVTStillImageWidth, KGSVTStillImageHeight );
       
   394 
       
   395     CFbsBitmap* newBitmap = new ( ELeave ) CFbsBitmap;
       
   396     CleanupStack::PushL( newBitmap );
       
   397 
       
   398     User::LeaveIfError(
       
   399         newBitmap->Create( KStillImageSize, iBitmap->DisplayMode() ) );
       
   400 
       
   401     CFbsBitmapDevice* device = CFbsBitmapDevice::NewL( newBitmap );
       
   402     CleanupStack::PushL( device );
       
   403 
       
   404     CFbsBitGc* context = NULL;
       
   405     User::LeaveIfError( device->CreateContext( context ) );
       
   406     User::LeaveIfNull( context );
       
   407 
       
   408     context->SetPenStyle( CGraphicsContext::ENullPen );
       
   409     context->SetBrushColor( TRgb( 0, 0, 0 ) ); // black color
       
   410     context->SetBrushStyle( CGraphicsContext::ESolidBrush );
       
   411     context->Clear();
       
   412 
       
   413     const TSize scaledImageSize( iBitmap->SizeInPixels() );
       
   414 
       
   415     TInt xPos = ( KStillImageSize.iWidth - scaledImageSize.iWidth ) / 2;
       
   416     TInt yPos = ( KStillImageSize.iHeight - scaledImageSize.iHeight ) / 2;
       
   417 
       
   418     context->BitBlt( TPoint( xPos, yPos ), iBitmap );
       
   419 
       
   420     delete context;
       
   421     CleanupStack::PopAndDestroy( device );
       
   422 
       
   423     //saving bitmap
       
   424     CPhCltImageHandler* phCltImageHandler = CPhCltImageHandler::NewL();
       
   425     CleanupStack::PushL( phCltImageHandler );
       
   426 
       
   427     // Acquires ownership of *imageParams
       
   428     // Bad naming for create-function CPhCltBaseImageParamsL().
       
   429     CPhCltImageParams* imageParams = phCltImageHandler->
       
   430                        CPhCltBaseImageParamsL( EPhCltTypeVTStill );
       
   431     CleanupStack::PushL( imageParams );
       
   432     TInt ret = newBitmap->Handle();
       
   433     imageParams->AddImageL( ret );
       
   434     phCltImageHandler->SaveImages( *imageParams );
       
   435     CleanupStack::PopAndDestroy( imageParams );
       
   436     CleanupStack::PopAndDestroy( phCltImageHandler );
       
   437 
       
   438     delete iBitmap;
       
   439     iBitmap = NULL;
       
   440 
       
   441     CleanupStack::PopAndDestroy( newBitmap );
       
   442     }
       
   443 
       
   444 
       
   445 // End of File