engine/src/ImageHandler.cpp
changeset 2 29cda98b007e
child 32 26a3f2dfba08
equal deleted inserted replaced
1:5f8e5adbbed9 2:29cda98b007e
       
     1 /*
       
     2 * Copyright (c) 2007-2010 Sebastian Brannstrom, Lars Persson, EmbedDev AB
       
     3 *
       
     4 * All rights reserved.
       
     5 * This component and the accompanying materials are made available
       
     6 * under the terms of the License "Eclipse Public License v1.0"
       
     7 * which accompanies this distribution, and is available
       
     8 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     9 *
       
    10 * Initial Contributors:
       
    11 * EmbedDev AB - initial contribution.
       
    12 *
       
    13 * Contributors:
       
    14 * Example code from OcrExample Copyright (c) 2006 Nokia Corporation.
       
    15 * Description:
       
    16 *
       
    17 */
       
    18 
       
    19 #include "ImageHandler.h"
       
    20 
       
    21 // =============================================================================
       
    22 // CImageHandler, a utility class for loading images.
       
    23 // =============================================================================
       
    24 
       
    25 // ============================= MEMBER FUNCTIONS ===============================
       
    26 
       
    27 // -----------------------------------------------------------------------------
       
    28 // CImageHandler::CImageHandler
       
    29 // C++ constructor can NOT contain any code, that
       
    30 // might leave.
       
    31 // -----------------------------------------------------------------------------
       
    32 //
       
    33 CImageHandler::CImageHandler( RFs& aFs )
       
    34     : CActive(CActive::EPriorityStandard)
       
    35     , iDecoder(NULL)
       
    36     , iScaler(NULL)
       
    37     , iFs(aFs)
       
    38     , iSize(0,0)
       
    39     {
       
    40     }
       
    41 
       
    42 // -----------------------------------------------------------------------------
       
    43 // CImageHandler::ConstructL
       
    44 // Symbian 2nd phase constructor can leave.
       
    45 // -----------------------------------------------------------------------------
       
    46 //
       
    47 void CImageHandler::ConstructL()
       
    48     {
       
    49     CActiveScheduler::Add(this);
       
    50     iBitmap = new (ELeave) CFbsBitmap;
       
    51     }
       
    52 
       
    53 // -----------------------------------------------------------------------------
       
    54 // CImageHandler::NewLC
       
    55 // Two-phased constructor.
       
    56 // -----------------------------------------------------------------------------
       
    57 //
       
    58 EXPORT_C CImageHandler* CImageHandler::NewL(RFs& aFs)
       
    59     {
       
    60     CImageHandler* self = NewLC(aFs);
       
    61     CleanupStack::Pop();
       
    62     return self;
       
    63     }
       
    64 
       
    65 // -----------------------------------------------------------------------------
       
    66 // CImageHandler::NewLC
       
    67 // Two-phased constructor.
       
    68 // -----------------------------------------------------------------------------
       
    69 //
       
    70 EXPORT_C CImageHandler* CImageHandler::NewLC(RFs& aFs)
       
    71     {
       
    72     CImageHandler* self = new (ELeave) CImageHandler(aFs);
       
    73     CleanupStack::PushL( self );
       
    74     self->ConstructL();
       
    75 
       
    76     return self;
       
    77     }
       
    78 
       
    79 // Destructor
       
    80 EXPORT_C CImageHandler::~CImageHandler()
       
    81     {
       
    82 	// Cancel any outstanding request
       
    83 	Cancel();
       
    84 	Deque();
       
    85     delete iDecoder;
       
    86     delete iScaler;
       
    87 	delete iBitmap;
       
    88     iCallbackQue.Close(); 
       
    89     }
       
    90 
       
    91 // -----------------------------------------------------------------------------
       
    92 // CImageHandler::LoadFileL
       
    93 // Loads a selected frame from a named file
       
    94 // -----------------------------------------------------------------------------
       
    95 //
       
    96 void CImageHandler::LoadFileL(const TFileName& aFileName, TInt aSelectedFrame)
       
    97     {
       
    98     __ASSERT_ALWAYS(!IsActive(),User::Invariant());
       
    99     if ( iDecoder )
       
   100         {
       
   101         delete iDecoder;
       
   102         iDecoder = NULL;
       
   103         }
       
   104    
       
   105     iDecoder = CImageDecoder::FileNewL(iFs, aFileName);
       
   106     // Get image information
       
   107     iFrameInfo = iDecoder->FrameInfo(aSelectedFrame);
       
   108     // Resize to fit.
       
   109     TRect bitmap = iFrameInfo.iFrameCoordsInPixels;
       
   110 
       
   111     // release possible previous image
       
   112     iBitmap->Reset();
       
   113     iBitmap->Create(bitmap.Size(), EColor16M);
       
   114 
       
   115     // Decode as bitmap.
       
   116     iDecoder->Convert(&iStatus, *iBitmap, aSelectedFrame);
       
   117     SetActive();
       
   118     }
       
   119 
       
   120 EXPORT_C void CImageHandler::LoadFileAndScaleL(CFbsBitmap* aScaledBitmap, 
       
   121 								      const TFileName& aFileName,
       
   122                                       const TSize &aSize,
       
   123                                       MImageHandlerCallback& aCallback,
       
   124                                       TInt aSelectedFrame)
       
   125     {        
       
   126     if(!IsActive())
       
   127     	{
       
   128     	__ASSERT_ALWAYS(!IsActive(),User::Invariant());
       
   129     	iSize = aSize;
       
   130     	iScaledBitmap = aScaledBitmap;
       
   131     	iScaledBitmap->Reset();
       
   132     	iScaledBitmap->Create(aSize, EColor16M);
       
   133     	iCallback = &aCallback; 
       
   134     	LoadFileL(aFileName, aSelectedFrame);
       
   135     	}
       
   136     else
       
   137     	{
       
   138     	TImageStruct imageStruct;
       
   139     	imageStruct.iCallBack = &aCallback;
       
   140     	imageStruct.iScaledImage = aScaledBitmap;
       
   141     	imageStruct.iScaledSize = aSize;    
       
   142     	imageStruct.iFileName = aFileName;
       
   143     	iCallbackQue.Append(imageStruct);
       
   144     	}
       
   145     }
       
   146 
       
   147 EXPORT_C CFbsBitmap* CImageHandler::ScaledBitmap()
       
   148 	{
       
   149 	return iScaledBitmap;
       
   150 	}
       
   151 
       
   152 void CImageHandler::ScaleL()
       
   153     {
       
   154     __ASSERT_ALWAYS(!IsActive(),User::Invariant());
       
   155     if ( iScaler )
       
   156         {
       
   157         delete iScaler;
       
   158         }
       
   159     iScaler = NULL;
       
   160     iScaler = CBitmapScaler::NewL();
       
   161     iScaler->Scale(&iStatus, *iBitmap, *iScaledBitmap, ETrue);
       
   162     SetActive();
       
   163     }
       
   164 
       
   165 // -----------------------------------------------------------------------------
       
   166 // CImageHandler::FrameInfo
       
   167 // Get the current frame information.
       
   168 // -----------------------------------------------------------------------------
       
   169 //
       
   170 const TFrameInfo& CImageHandler::FrameInfo() const
       
   171     {
       
   172     return iFrameInfo;
       
   173     }
       
   174 
       
   175 // -----------------------------------------------------------------------------
       
   176 // CImageHandler::RunL
       
   177 // CActive::RunL() implementation. Called on image load success/failure.
       
   178 // -----------------------------------------------------------------------------
       
   179 //
       
   180 void CImageHandler::RunL()
       
   181     {
       
   182     if ((! iStatus.Int()) && (iSize.iWidth != 0) && (iSize.iHeight != 0))
       
   183         {
       
   184         ScaleL();
       
   185         iSize.SetSize(0,0);
       
   186         }
       
   187     else
       
   188         {
       
   189         // Invoke callback.
       
   190         iCallback->ImageOperationCompleteL(iStatus.Int());        
       
   191         if(iCallbackQue.Count())
       
   192         	{
       
   193         	TInt loaderror = KErrNotFound;
       
   194         	while(loaderror != KErrNone && iCallbackQue.Count())
       
   195         		{
       
   196         		TImageStruct imageStruct= iCallbackQue[0];
       
   197         		iCallbackQue.Remove(0);
       
   198         		TRAP(loaderror, LoadFileAndScaleL(imageStruct.iScaledImage, imageStruct.iFileName, imageStruct.iScaledSize, *imageStruct.iCallBack));
       
   199         		}
       
   200         	}
       
   201         }
       
   202  
       
   203     }
       
   204 
       
   205 // -----------------------------------------------------------------------------
       
   206 // CImageHandler::DoCancel
       
   207 // CActive::Cancel() implementation. Stops decoding.
       
   208 // -----------------------------------------------------------------------------
       
   209 //
       
   210 void CImageHandler::DoCancel()
       
   211     {
       
   212     if ( iDecoder )
       
   213         {
       
   214         iDecoder->Cancel();
       
   215         }
       
   216     if ( iScaler )
       
   217         {
       
   218         iScaler->Cancel();
       
   219         }
       
   220     }
       
   221 
       
   222 
       
   223