camerauis/cameraapp/generic/src/CamBurstThumbnailGridSizer.cpp
changeset 0 1ddebce53859
equal deleted inserted replaced
-1:000000000000 0:1ddebce53859
       
     1 /*
       
     2 * Copyright (c) 2007-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:  Class used to for resizing burst mode thumbnails*
       
    15 */
       
    16 
       
    17 
       
    18 
       
    19 // INCLUDE FILES
       
    20 #include <e32base.h>
       
    21 #include <e32std.h>
       
    22 
       
    23 #include "CamBurstThumbnailGridSizer.h"
       
    24 #include "camlogging.h"
       
    25 
       
    26 
       
    27 
       
    28 
       
    29 
       
    30 // EXTERNAL DATA STRUCTURES
       
    31 
       
    32 // EXTERNAL FUNCTION PROTOTYPES  
       
    33 
       
    34 // CONSTANTS
       
    35 
       
    36 // MACROS
       
    37 
       
    38 // LOCAL CONSTANTS AND MACROS
       
    39 
       
    40 // MODULE DATA STRUCTURES
       
    41 
       
    42 // LOCAL FUNCTION PROTOTYPES
       
    43 
       
    44 // FORWARD DECLARATIONS
       
    45 
       
    46 // ============================= LOCAL FUNCTIONS ===============================
       
    47 
       
    48 
       
    49 // ============================ MEMBER FUNCTIONS ===============================
       
    50 
       
    51 // -----------------------------------------------------------------------------
       
    52 // Symbian 2 phase constructor <<static>>
       
    53 // -----------------------------------------------------------------------------
       
    54 //
       
    55 CScaleTask* 
       
    56 CScaleTask::NewL( const CFbsBitmap& aBitmap, 
       
    57                   const TSize&      aSize,
       
    58                   TInt32            aInfo )
       
    59   {
       
    60   CScaleTask* self = new (ELeave) CScaleTask( aSize, aInfo );
       
    61   CleanupStack::PushL( self );
       
    62   self->ConstructL( aBitmap );
       
    63   CleanupStack::Pop( self );
       
    64   return self;
       
    65   }
       
    66 
       
    67 // -----------------------------------------------------------------------------
       
    68 // CScaleTask destructor <<virtual>>
       
    69 // -----------------------------------------------------------------------------
       
    70 //
       
    71 CScaleTask::~CScaleTask()
       
    72   {
       
    73   delete iSrc;
       
    74   iSrc = NULL;
       
    75   }
       
    76 
       
    77 // -----------------------------------------------------------------------------
       
    78 // CScaleTask constructor <<virtual>>
       
    79 // -----------------------------------------------------------------------------
       
    80 //
       
    81 CScaleTask::CScaleTask( const TSize&      aSize,
       
    82                         TInt32            aInfo )
       
    83   : iSize ( aSize ),
       
    84     iInfo ( aInfo )
       
    85   {
       
    86   }
       
    87 
       
    88 // -----------------------------------------------------------------------------
       
    89 // CScaleTask::ConstructL
       
    90 // -----------------------------------------------------------------------------
       
    91 //
       
    92 void 
       
    93 CScaleTask::ConstructL( const CFbsBitmap& aBitmap )
       
    94   {
       
    95   iSrc = new (ELeave) CFbsBitmap;
       
    96   User::LeaveIfError( iSrc->Duplicate( aBitmap.Handle() ) );
       
    97   }
       
    98 
       
    99 
       
   100 
       
   101 // -----------------------------------------------------------------------------
       
   102 // CCamThumbnailGridSizer::CCamThumbnailGridSizer
       
   103 // C++ default constructor can NOT contain any code, that
       
   104 // might leave.
       
   105 // -----------------------------------------------------------------------------
       
   106 //
       
   107 CCamThumbnailGridSizer::CCamThumbnailGridSizer( MBitmapScaler* aObserver, TSize& aSize ) 
       
   108         : CActive( EPriorityStandard ),
       
   109           iThumbSize( aSize )
       
   110     {
       
   111     iObserver = aObserver;
       
   112     CActiveScheduler::Add(this);
       
   113     }
       
   114 
       
   115 // -----------------------------------------------------------------------------
       
   116 // CCamThumbnailGridSizer::ConstructL
       
   117 // Symbian 2nd phase constructor can leave.
       
   118 // -----------------------------------------------------------------------------
       
   119 //
       
   120 void CCamThumbnailGridSizer::ConstructL()
       
   121     {
       
   122     iScaler = CBitmapScaler::NewL();
       
   123     }
       
   124 
       
   125 // -----------------------------------------------------------------------------
       
   126 // CCamThumbnailGridSizer::NewL
       
   127 // Two-phased constructor.
       
   128 // -----------------------------------------------------------------------------
       
   129 //
       
   130 CCamThumbnailGridSizer* CCamThumbnailGridSizer::NewL( MBitmapScaler* aObserver, TSize& aSize )
       
   131     {
       
   132     CCamThumbnailGridSizer* self = new( ELeave ) CCamThumbnailGridSizer( aObserver, aSize );
       
   133     
       
   134     CleanupStack::PushL( self );
       
   135     self->ConstructL();
       
   136     CleanupStack::Pop();
       
   137 
       
   138     return self;
       
   139     }
       
   140 
       
   141     
       
   142 // -----------------------------------------------------------------------------
       
   143 // Destructor
       
   144 // -----------------------------------------------------------------------------
       
   145 //
       
   146 CCamThumbnailGridSizer::~CCamThumbnailGridSizer()
       
   147   {
       
   148   PRINT( _L("Camera => ~CCamThumbnailGridSizer") );
       
   149   Cancel();
       
   150   delete iScaler;
       
   151   
       
   152   // Delete any intermediate temporary bitmap
       
   153   delete iTmpBitmap;
       
   154   
       
   155   // Cancel all pending scaling operations
       
   156   iScaleTaskArray.ResetAndDestroy();            
       
   157   PRINT( _L("Camera <= ~CCamThumbnailGridSizer") );
       
   158   }
       
   159 
       
   160 // -----------------------------------------------------------------------------
       
   161 // CCamThumbnailGridSizer::SetSize
       
   162 // -----------------------------------------------------------------------------
       
   163 //
       
   164 void CCamThumbnailGridSizer::SetSize( const TSize aThumbSize )
       
   165     {
       
   166     PRINT2( _L("Camera == CCamThumbnailGridSizer::SetSize width = %d, height = %d"), aThumbSize.iWidth, aThumbSize.iHeight );
       
   167     iThumbSize.iWidth = aThumbSize.iWidth;
       
   168     iThumbSize.iHeight = aThumbSize.iHeight;
       
   169     }
       
   170 
       
   171 // -----------------------------------------------------------------------------
       
   172 // CCamThumbnailGridSizer::StartScaleL
       
   173 // -----------------------------------------------------------------------------
       
   174 //
       
   175 void CCamThumbnailGridSizer::StartScaleL( const CFbsBitmap& aSrc )
       
   176     {
       
   177     PRINT( _L("Camera == CCamThumbnailGridSizer::StartScaleL") );
       
   178     // Forward request on with default destination size
       
   179     StartScaleL( aSrc, iThumbSize, 0 );
       
   180     }
       
   181     
       
   182     
       
   183 // -----------------------------------------------------------------------------
       
   184 // CCamThumbnailGridSizer::StartScaleL
       
   185 // Allows specifying of specific size to go to where required.
       
   186 // -----------------------------------------------------------------------------
       
   187 //
       
   188 void CCamThumbnailGridSizer::StartScaleL( const CFbsBitmap& aSrc, TSize& aSize, TInt32 aInfo )
       
   189     {
       
   190     PRINT3(_L("Camera => CCamThumbnailGridSizer::StartScaleL size %d x %d info 0x%x"), aSize.iWidth, aSize.iHeight, aInfo );
       
   191 
       
   192     // Add it to the queue of things to scale
       
   193     CScaleTask* task = CScaleTask::NewL( aSrc, aSize, aInfo );    
       
   194     CleanupStack::PushL( task );    
       
   195     User::LeaveIfError( iScaleTaskArray.Append( task ) );    // takes ownership of "task"
       
   196     CleanupStack::Pop( task );
       
   197 
       
   198     StartScaleL( task );    
       
   199     PRINT ( _L("Camera <= CCamThumbnailGridSizer::StartScaleL") );
       
   200     }
       
   201     
       
   202 // -----------------------------------------------------------------------------
       
   203 // CCamThumbnailGridSizer::StartScaleL
       
   204 // Allows specifying of specific sizing task to start
       
   205 // -----------------------------------------------------------------------------
       
   206 //
       
   207 void CCamThumbnailGridSizer::StartScaleL( CScaleTask* aTask )
       
   208     {    
       
   209     PRINT( _L("Camera => CCamThumbnailGridSizer::StartScaleL task") );
       
   210     // If scaling not currently underway, start it.
       
   211     // If scaler is busy, it will be started when current operation completes.            
       
   212     if ( !IsActive() )
       
   213         {
       
   214         // Check that we don't have any temporary images floating around
       
   215         if ( iTmpBitmap )
       
   216             {
       
   217             delete iTmpBitmap;
       
   218             iTmpBitmap = NULL;
       
   219             }    
       
   220         
       
   221         TSize        trgSize( aTask->Size()                   );
       
   222         const TSize& bmpSize( aTask->Bitmap()->SizeInPixels() );
       
   223         TRect        srcRect( bmpSize                         );
       
   224 
       
   225         TReal bmpRatio( TReal( bmpSize.iWidth ) / TReal( bmpSize.iHeight ) );
       
   226         TReal trgRatio( TReal( trgSize.iWidth ) / TReal( trgSize.iHeight ) );
       
   227 
       
   228         PRINT2( _L("Camera <> CCamThumbnailGridSizer - source size(%d,%d)"), 
       
   229                 bmpSize.iWidth, bmpSize.iHeight );
       
   230 
       
   231         PRINT2( _L("Camera <> CCamThumbnailGridSizer - target size(%d,%d)"), 
       
   232                 trgSize.iWidth, trgSize.iHeight );
       
   233 
       
   234         if( bmpRatio > trgRatio )
       
   235           {
       
   236           // Thumbnail aspect ratio is wider than the target size.
       
   237           // Scale initially to a size wider than target and then crop.
       
   238           trgSize.iWidth = Max( trgSize.iHeight * bmpRatio, 1 );
       
   239           }
       
   240         if( bmpRatio < trgRatio )
       
   241           {
       
   242           // Thumbnail aspect ratio is wider than the target size.
       
   243           // Scale initially to a size higher than target and then crop.
       
   244           trgSize.iHeight = Max( trgSize.iWidth * bmpRatio, 1 );
       
   245           }
       
   246 
       
   247         PRINT2( _L("Camera <> CCamThumbnailGridSizer - intermediate size(%d,%d)"), 
       
   248                 trgSize.iWidth, trgSize.iHeight );
       
   249 
       
   250 
       
   251         // Create a temporary bitmap
       
   252         iTmpBitmap = new ( ELeave ) CFbsBitmap();
       
   253         iTmpBitmap->Create( trgSize, aTask->Bitmap()->DisplayMode() );        
       
   254        
       
   255 
       
   256         // Start the scaling operation
       
   257         iScaler->Scale( &iStatus, 
       
   258                         const_cast<CFbsBitmap&>(*aTask->Bitmap()), 
       
   259                         *iTmpBitmap, 
       
   260                         ETrue );
       
   261         SetActive();
       
   262         }
       
   263     PRINT( _L("Camera <= CCamThumbnailGridSizer::StartScaleL task") );
       
   264     }    
       
   265 
       
   266 // -----------------------------------------------------------------------------
       
   267 // CCamThumbnailGridSizer::DoCancel
       
   268 // -----------------------------------------------------------------------------
       
   269 //
       
   270 void CCamThumbnailGridSizer::DoCancel()
       
   271     {    
       
   272     PRINT( _L("Camera => CCamThumbnailGridSizer::DoCancel") );
       
   273     iScaler->Cancel();    
       
   274         
       
   275     // Delete any intermediate temporary bitmap
       
   276     if ( iTmpBitmap )
       
   277         {
       
   278         delete iTmpBitmap;
       
   279         iTmpBitmap = NULL;
       
   280         }
       
   281     
       
   282     // Cancel all pending scaling operations
       
   283     iScaleTaskArray.ResetAndDestroy();
       
   284     PRINT( _L("Camera <= CCamThumbnailGridSizer::DoCancel") );
       
   285     }
       
   286 
       
   287 // -----------------------------------------------------------------------------
       
   288 // CCamThumbnailGridSizer::RunL
       
   289 // -----------------------------------------------------------------------------
       
   290 //
       
   291 void CCamThumbnailGridSizer::RunL()
       
   292     {
       
   293     PRINT2( _L("Camera => CCamThumbnailGridSizer::RunL .. observer 0x%x err %d"), iObserver, iStatus.Int() );
       
   294     // If there is an observer, notify them of the scaling completion
       
   295     if ( iObserver )
       
   296         {
       
   297         PRINT1( _L("Camera <> CCamThumbnailGridSizer::RunL .. scale array count %d"), iScaleTaskArray.Count() );
       
   298         // Note; passing iTmpBitmap in this call is passing ownership of the 
       
   299         // bitmap to the observer.  This also places the restriction that there
       
   300         // can only be one observer to an implementation of the GridSizer, which
       
   301         // is fine for our requirements
       
   302         iObserver->BitmapScaleCompleteL( iStatus.Int(), iTmpBitmap, iScaleTaskArray[0]->Info() );
       
   303         iTmpBitmap = NULL;
       
   304         }
       
   305         
       
   306     CScaleTask* task = NULL;
       
   307     if ( iScaleTaskArray.Count() > 0 )
       
   308         {
       
   309         // Remove from the array and delete this (completed) task
       
   310         task = iScaleTaskArray[0];        
       
   311         iScaleTaskArray.Remove(0);        
       
   312         delete task;
       
   313         task = NULL;        
       
   314         }
       
   315         
       
   316     // If completed successfully...     
       
   317     // ... and there are more tasks on the queue...
       
   318     // ... and the task is valid, then do next task    
       
   319     if ( iStatus.Int() == KErrNone && 
       
   320          iScaleTaskArray.Count() > 0 )
       
   321         {
       
   322         task = iScaleTaskArray[0];        
       
   323         if ( task )
       
   324             {
       
   325             StartScaleL( task );    
       
   326             }        
       
   327         }
       
   328     PRINT ( _L("Camera <= CCamThumbnailGridSizer::RunL") );
       
   329     }
       
   330 
       
   331 
       
   332 //  End of File