camappengine/Engine/Src/CaeStillBurst.cpp
branchRCL_3
changeset 34 27fe719c32e6
parent 0 9b3e960ffc8a
equal deleted inserted replaced
33:e3cdd00b5ae3 34:27fe719c32e6
       
     1 /*
       
     2 * Copyright (c) 2003,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:  Camera Application Engine still image burst class
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 // INCLUDE FILES
       
    21 
       
    22 #include "CaeEngineImp.h"                   // For LOGTEXT
       
    23 #include <fbs.h>                            // For CFbsBitmap
       
    24 #include <hal.h>                            // For HAL
       
    25 
       
    26 #include "CaeStillBurst.h"                  // Still image burst capturing class.
       
    27 #include "CaeImageItem.h"                   // Still image item class.
       
    28 
       
    29 
       
    30 // CONSTANTS
       
    31 const TInt KCaeStillBurstImageQueueGranularity = 6;
       
    32 const TInt KCaeMinImageHeapSize = 0; 
       
    33 
       
    34 // ============================ MEMBER FUNCTIONS ===============================
       
    35 
       
    36 
       
    37 // -----------------------------------------------------------------------------
       
    38 // CCaeStillBurst::NewL
       
    39 // -----------------------------------------------------------------------------
       
    40 //
       
    41 CCaeStillBurst* CCaeStillBurst::NewL()
       
    42     {
       
    43     LOGTEXT( _L( "Cae: CCaeStillBurst::NewL() entering" ) );
       
    44 
       
    45     CCaeStillBurst* self = new( ELeave ) CCaeStillBurst;
       
    46 
       
    47     CleanupStack::PushL( self );
       
    48     self->ConstructL();
       
    49     CleanupStack::Pop( self );
       
    50 
       
    51     LOGTEXT( _L( "Cae: CCaeStillBurst::NewL() returning" ) );
       
    52 
       
    53     return self;
       
    54     }
       
    55 
       
    56     
       
    57 // -----------------------------------------------------------------------------
       
    58 // CCaeStillBurst::~CCaeStillBurst
       
    59 // Destroys image queue.
       
    60 // -----------------------------------------------------------------------------
       
    61 //
       
    62 CCaeStillBurst::~CCaeStillBurst()
       
    63     {
       
    64     LOGTEXT( _L( "Cae: CCaeStillBurst::~CCaeStillBurst() entering" ) );
       
    65 
       
    66     ResetAndDestroyImages();
       
    67 
       
    68     if ( iImageHeap )
       
    69         {
       
    70         iImageHeap->Close();
       
    71         }
       
    72 
       
    73     delete iImageQueue;
       
    74 
       
    75     LOGTEXT( _L( "Cae: CCaeStillBurst::~CCaeStillBurst() returning" ) );
       
    76     }
       
    77 
       
    78 
       
    79 // -----------------------------------------------------------------------------
       
    80 // CCaeStillBurst::CCaeStillBurst
       
    81 // -----------------------------------------------------------------------------
       
    82 //
       
    83 CCaeStillBurst::CCaeStillBurst() 
       
    84     {
       
    85     }
       
    86 
       
    87 
       
    88 // -----------------------------------------------------------------------------
       
    89 // CCaeStillBurst::ConstructL
       
    90 // Allocates image queue.
       
    91 // -----------------------------------------------------------------------------
       
    92 //
       
    93 void CCaeStillBurst::ConstructL()
       
    94     {
       
    95     LOGTEXT2( _L( "Cae: CCaeStillBurst::ConstructL() entering, min heap: %d" ), KCaeMinImageHeapSize );
       
    96 
       
    97     iImageQueue = new( ELeave ) RPointerArray<CCaeImageItem>( 
       
    98         KCaeStillBurstImageQueueGranularity );
       
    99 
       
   100     TInt maxMem;
       
   101     User::LeaveIfError( HAL::Get( HAL::EMemoryRAM, maxMem ) );
       
   102     #if (defined (__WINS__) || defined (__WINSCW__))
       
   103     maxMem /= 2; // Fix emulator -4 error
       
   104     #endif
       
   105 
       
   106     iImageHeap = User::ChunkHeap( NULL, KCaeMinImageHeapSize, maxMem );
       
   107 
       
   108     #ifdef _DEBUG
       
   109     if ( !iImageHeap )
       
   110         {
       
   111         LOGTEXT( _L( "Cae: CCaeStillBurst::ConstructL() Error: Cannot create iImageHeap !" ) );
       
   112         }
       
   113     #endif
       
   114 
       
   115     User::LeaveIfNull( iImageHeap );
       
   116 
       
   117     LOGTEXT( _L( "Cae: CCaeStillBurst::ConstructL() returning" ) );
       
   118     }
       
   119 
       
   120 
       
   121 
       
   122 // -----------------------------------------------------------------------------
       
   123 // CCaeStillBurst::SetLengthL
       
   124 // -----------------------------------------------------------------------------
       
   125 //
       
   126 void CCaeStillBurst::SetLengthL( 
       
   127     TInt aLength )
       
   128     {
       
   129     LOGTEXT2( _L( "Cae: CCaeStillBurst::SetLengthL(): length: %d" ), aLength );
       
   130 
       
   131     // Leave if not supported.
       
   132     if ( aLength < 1 )
       
   133         {
       
   134         LOGTEXT( _L( "Cae: CCaeStillBurst::SetLengthL(): leaving KErrArgument" ));
       
   135         User::Leave( KErrArgument );
       
   136         }
       
   137 
       
   138     iDesiredLength = aLength;
       
   139     }
       
   140 
       
   141 
       
   142 // -----------------------------------------------------------------------------
       
   143 // CCaeStillBurst::Length
       
   144 // -----------------------------------------------------------------------------
       
   145 //
       
   146 TInt CCaeStillBurst::Length() const
       
   147     {
       
   148     LOGTEXT2( _L( "Cae: CCaeStillBurst::Length(): length: %d" ), iDesiredLength );
       
   149 
       
   150     return iDesiredLength;
       
   151     }
       
   152 
       
   153 
       
   154 // -----------------------------------------------------------------------------
       
   155 // CCaeStillBurst::AppendImage
       
   156 // Appends an image item object to image queue.
       
   157 // -----------------------------------------------------------------------------
       
   158 //
       
   159 TInt CCaeStillBurst::AppendImage( 
       
   160     CFbsBitmap*  aBitmap, 
       
   161     HBufC8*      aImageData, 
       
   162     TInt         aError )
       
   163     {
       
   164     LOGTEXT2( _L( "Cae: CCaeStillBurst::AppendImage() entering: %d" ), aError );
       
   165 
       
   166     TInt result( KErrNone );
       
   167 
       
   168     CCaeImageItem* imageItem = new CCaeImageItem( *iImageHeap );
       
   169     if ( imageItem ) 
       
   170         {
       
   171         if (aImageData)
       
   172             {
       
   173             // Copy image data from HBufC8 to image heap
       
   174             imageItem->iImageData = iImageHeap->Alloc( aImageData->Size() );
       
   175             if ( imageItem->iImageData )
       
   176                 {
       
   177                 imageItem->iImageDataSize = aImageData->Size();
       
   178                 Mem::Copy( imageItem->iImageData, aImageData->Ptr(), 
       
   179                     imageItem->iImageDataSize );
       
   180                 }
       
   181             else
       
   182                 {
       
   183                 result = KErrNoMemory;
       
   184                 }
       
   185             }
       
   186         if ( result == KErrNone ) 
       
   187             {
       
   188             imageItem->iBitmap = aBitmap;
       
   189             imageItem->iError  = aError;
       
   190             // Add image to the queue.
       
   191             result = iImageQueue->Append( imageItem );
       
   192             }
       
   193         if ( result == KErrNone ) 
       
   194             {
       
   195             iCountOfBurstAppends++;
       
   196             LOGTEXT2( _L( "Cae: CCaeStillBurst::AppendImage(): count of burst appends = %d" ), iCountOfBurstAppends );
       
   197 
       
   198             // Given aImageData can be deleted now as it has successfully 
       
   199             // been copied to imageItem->iImageData
       
   200             delete aImageData;
       
   201             }
       
   202         else
       
   203             {
       
   204             // Do not delete the bitmap if error, delete just imageItem 
       
   205             // which deletes also iImageData.
       
   206             imageItem->iBitmap = NULL;
       
   207             delete imageItem;
       
   208             imageItem = NULL;
       
   209             }
       
   210         }
       
   211     else 
       
   212         {
       
   213         result = KErrNoMemory;
       
   214         }
       
   215 
       
   216     LOGTEXT( _L( "Cae: CCaeStillBurst::AppendImage() returning" ) );
       
   217 
       
   218     return result;
       
   219     }
       
   220 
       
   221 
       
   222 // -----------------------------------------------------------------------------
       
   223 // CCaeStillBurst::CaptureCount
       
   224 // -----------------------------------------------------------------------------
       
   225 //
       
   226 TInt CCaeStillBurst::CaptureCount() const
       
   227     {
       
   228     // Return the count of appends (captures) made.
       
   229     return iCountOfBurstAppends;
       
   230     }
       
   231 
       
   232 
       
   233 // -----------------------------------------------------------------------------
       
   234 // CCaeStillBurst::ImageCount
       
   235 // -----------------------------------------------------------------------------
       
   236 //
       
   237 TInt CCaeStillBurst::ImageCount() const
       
   238     {
       
   239     // Return the count of image items.
       
   240     return iImageQueue->Count();
       
   241     }
       
   242 
       
   243 
       
   244 // -----------------------------------------------------------------------------
       
   245 // CCaeStillBurst::GetNextImage
       
   246 // Fetches and deletes the next image from image queue.
       
   247 // -----------------------------------------------------------------------------
       
   248 //
       
   249 TInt CCaeStillBurst::GetNextImage( 
       
   250     CFbsBitmap*&   aBitmap,     // output
       
   251     HBufC8*&       aImageData,  // output
       
   252     TInt&          aError )     // output
       
   253     {
       
   254     LOGTEXT2( _L( "Cae: CCaeStillBurst::GetNextImage() entering, %d images in queue" ), iImageQueue->Count() );
       
   255 
       
   256     TInt error( KErrNone );
       
   257 
       
   258     if ( iImageQueue->Count() > 0 )
       
   259         {
       
   260 
       
   261         if ( (*iImageQueue)[0]->iImageData )
       
   262             {
       
   263             // Copy image data from queue image item.
       
   264             aImageData = HBufC8::New( (*iImageQueue)[0]->iImageDataSize );
       
   265             if ( aImageData )
       
   266                 {
       
   267                 aImageData->Des().Copy( (TUint8*)(*iImageQueue)[0]->iImageData, 
       
   268                     (*iImageQueue)[0]->iImageDataSize );
       
   269                 }
       
   270             else
       
   271                 {
       
   272                 error = KErrNoMemory;
       
   273                 }
       
   274             }
       
   275 
       
   276         if ( error == KErrNone )
       
   277             {
       
   278             // Get bitmap pointer and image error value from the queue image item.
       
   279             aBitmap     = (*iImageQueue)[0]->iBitmap;
       
   280             aError      = (*iImageQueue)[0]->iError;
       
   281 
       
   282             #ifdef _DEBUG
       
   283             if ( aError )
       
   284                 {
       
   285                 LOGTEXT2( _L( "Cae: CCaeStillBurst::GetNextImage(): This image has error: %d" ), aError );
       
   286                 }
       
   287             #endif
       
   288 
       
   289             // Do not delete the bitmap. Delete the image item which deletes iImageData also.
       
   290             (*iImageQueue)[0]->iBitmap = NULL;
       
   291             delete (*iImageQueue)[0];
       
   292             iImageQueue->Remove( 0 );
       
   293             }
       
   294 
       
   295         if ( ( iImageQueue->Count() == 0 ) || error ) 
       
   296             {
       
   297             // Give free memory to system from the top of heap.
       
   298             iImageHeap->Compress(); 
       
   299             LOGTEXT( _L( "Cae: CCaeStillBurst::GetNextImage(): last image fetched, compressed the heap" ) );
       
   300             MEM();
       
   301             }
       
   302         }
       
   303     else 
       
   304         {
       
   305         aBitmap = NULL;
       
   306         aImageData = NULL;
       
   307         error = KErrUnderflow;
       
   308         }
       
   309 
       
   310     LOGTEXT2( _L( "Cae: CCaeStillBurst::GetNextImage() returning: %d" ), error );
       
   311 
       
   312     return error;
       
   313     }
       
   314 
       
   315 
       
   316 // -----------------------------------------------------------------------------
       
   317 // CCaeStillBurst::IsBurstCaptured
       
   318 // -----------------------------------------------------------------------------
       
   319 //
       
   320 TBool CCaeStillBurst::IsBurstCaptured() const
       
   321     {
       
   322     TBool isComplete( EFalse );
       
   323     if ( iCountOfBurstAppends >= iDesiredLength )
       
   324         {
       
   325         isComplete = ETrue;
       
   326         }    
       
   327     return isComplete;
       
   328     }
       
   329 
       
   330 
       
   331 // -----------------------------------------------------------------------------
       
   332 // CCaeStillBurst::ResetAndDestroyImages()
       
   333 // Reset and destroy image queue.
       
   334 // -----------------------------------------------------------------------------
       
   335 //
       
   336 void CCaeStillBurst::ResetAndDestroyImages()
       
   337     {
       
   338     LOGTEXT( _L( "Cae: CCaeStillBurst::ResetAndDestroyImages()" ) );
       
   339 
       
   340     if ( iImageQueue ) 
       
   341         {
       
   342         // Reset and destroy the image items from queue.
       
   343         iImageQueue->ResetAndDestroy();
       
   344         }
       
   345     if ( iImageHeap )
       
   346         {
       
   347         iImageHeap->Compress(); // Give free memory to system from the top of heap
       
   348         }
       
   349     iCountOfBurstAppends = 0;
       
   350     }
       
   351 
       
   352 
       
   353 //  End of File