javauis/mmapi_akn/baseline/src.nga/cmmasnapshot.cpp
branchRCL_3
changeset 60 6c158198356e
equal deleted inserted replaced
59:e5618cc85d74 60:6c158198356e
       
     1 /*
       
     2 * Copyright (c) 2002-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:  This class takes snapshot and resizes bitmap if needed.
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 //  INCLUDE FILES
       
    20 #include <jdebug.h>
       
    21 
       
    22 #include "cmmasnapshot.h"
       
    23 #include "mmmaguiplayer.h"
       
    24 #include "mmmasnapshotreadycallback.h"
       
    25 #include "mmmasnapshot.h"
       
    26 
       
    27 // CONSTANTS
       
    28 const TInt KIgnoreSize = -1;
       
    29 
       
    30 
       
    31 // CONSTRUCTION
       
    32 CMMASnapshot* CMMASnapshot::NewL(MMMAGuiPlayer* aGuiPlayer,
       
    33                                  MMMASnapshotReadyCallback& aCallBack)
       
    34 {
       
    35     CMMASnapshot* self = new(ELeave) CMMASnapshot(aGuiPlayer,
       
    36             aCallBack);
       
    37     CleanupStack::PushL(self);
       
    38     self->ConstructL();
       
    39     CleanupStack::Pop(); // self
       
    40     return self;
       
    41 }
       
    42 
       
    43 CMMASnapshot::~CMMASnapshot()
       
    44 {
       
    45     if (iScaler)
       
    46     {
       
    47         iScaler->Cancel();
       
    48         delete iScaler;
       
    49     }
       
    50     if (iEncoder)
       
    51     {
       
    52         iEncoder->Cancel();
       
    53         delete iEncoder;
       
    54     }
       
    55     delete iBitmap;
       
    56     delete iBuffer;
       
    57     delete iSettings;
       
    58 }
       
    59 
       
    60 void CMMASnapshot::ConstructL()
       
    61 {
       
    62     CActiveScheduler::Add(this);
       
    63     iScaler = CBitmapScaler::NewL();
       
    64 }
       
    65 
       
    66 CMMASnapshot::CMMASnapshot(MMMAGuiPlayer* aGuiPlayer,
       
    67                            MMMASnapshotReadyCallback& aCallBack):
       
    68         CActive(EPriorityStandard),
       
    69         iEncoding(MMMASnapshot::EBitmap),
       
    70         iCallBack(aCallBack),
       
    71         iState(EIdle)
       
    72 {
       
    73     iGUIPlayer = aGuiPlayer;
       
    74 }
       
    75 
       
    76 void CMMASnapshot::TakeSnapShotL(const TDesC& aProperties)
       
    77 {
       
    78     // snapshot sequence is not finished
       
    79     __ASSERT_DEBUG(iState == EIdle, User::Invariant());
       
    80     // image buffer must be taken with ImageBuffer before taking new snapshot
       
    81     __ASSERT_DEBUG(iBuffer == NULL, User::Invariant());
       
    82 
       
    83     CMMAImageSettings* settings
       
    84     = TMMAParameterValidator::ValidateImagePropertiesL(aProperties);
       
    85     CleanupStack::PushL(settings);
       
    86 
       
    87     delete iSettings;
       
    88     CleanupStack::Pop(settings);
       
    89     iSettings = settings;
       
    90 
       
    91     // take snapshot from player. RunL is called when image is ready
       
    92     // or error occures
       
    93     iState = ETakingSnapshot;
       
    94     TSize snapshotSize(iSettings->iWidth, iSettings->iHeight);
       
    95     iEncoding = iGUIPlayer->SnapshoterL()->TakeSnapshotL(&iStatus,
       
    96                 snapshotSize,
       
    97                 *iSettings);
       
    98     SetActive();
       
    99 }
       
   100 
       
   101 HBufC8* CMMASnapshot::ImageBuffer()
       
   102 {
       
   103     // this must not be called when snapshot sequence is running
       
   104     __ASSERT_DEBUG(iState == EIdle, User::Invariant());
       
   105     HBufC8* buffer = iBuffer;
       
   106     // caller takes ownership of the buffer
       
   107     iBuffer = NULL;
       
   108     return buffer;
       
   109 }
       
   110 
       
   111 void CMMASnapshot::ResizeL()
       
   112 {
       
   113     iState = EResizing;
       
   114     TSize imageSize(iBitmap->SizeInPixels());
       
   115     if (iSettings->iWidth != KIgnoreSize)
       
   116     {
       
   117         imageSize.iWidth = iSettings->iWidth;
       
   118     }
       
   119     if (iSettings->iHeight != KIgnoreSize)
       
   120     {
       
   121         imageSize.iHeight = iSettings->iHeight;
       
   122     }
       
   123     if (imageSize == iBitmap->SizeInPixels())
       
   124     {
       
   125         // no user resizing needed, continue sequence
       
   126         EncodeL();
       
   127     }
       
   128     else
       
   129     {
       
   130         if(iScalingFactor)
       
   131             {
       
   132                 iScalingFactor = EFalse;
       
   133                 iScaler->Scale(&iStatus, *iBitmap, imageSize, EFalse);
       
   134             }
       
   135         else
       
   136             {
       
   137                 iScaler->Scale(&iStatus, *iBitmap, imageSize, ETrue);
       
   138             }
       
   139         SetActive();
       
   140     }
       
   141 }
       
   142 
       
   143 void CMMASnapshot::EncodeL()
       
   144 {
       
   145     // CImageEncoder cannot be reused, so have to make it every time
       
   146     CImageEncoder* encoder = CImageEncoder::DataNewL(iBuffer, *iSettings->iMimeType);
       
   147     delete iEncoder;
       
   148     iEncoder = encoder;
       
   149 
       
   150     iState = EEncoding;
       
   151     iEncoder->Convert(&iStatus, *iBitmap, iSettings->iImageData);
       
   152     SetActive();
       
   153 }
       
   154 
       
   155 void CMMASnapshot::Completed(TInt aError)
       
   156 {
       
   157     iStatus = aError;
       
   158     iCallBack.SnapshotReady();
       
   159 }
       
   160 
       
   161 void CMMASnapshot::RunL()
       
   162 {
       
   163     if (iStatus != KErrNone)
       
   164     {
       
   165         // Error has occured, inform java side and change state
       
   166         iState = EIdle;
       
   167         Completed(iStatus.Int());
       
   168         return;
       
   169     }
       
   170 
       
   171     switch (iState)
       
   172     {
       
   173     case ETakingSnapshot:
       
   174     {
       
   175         DEBUG_INT("MMA::CMMASnapshot::RunL: iEncoding = %d", iEncoding);
       
   176         if (iEncoding == MMMASnapshot::EEncoded)
       
   177         {
       
   178             // take encoded image from player.
       
   179             // Ownership transfers to this class.
       
   180             iBuffer = iGUIPlayer->SnapshoterL()->SnapshotEncoded();
       
   181             if (!iBuffer)
       
   182             {
       
   183                 // error has occured with taking image
       
   184                 Completed(KErrNotFound);
       
   185             }
       
   186             // Image is ready, update internal state and inform listener
       
   187             // Encoded images are not resized
       
   188             iState = EIdle;
       
   189             Completed(KErrNone);
       
   190         }
       
   191         else
       
   192         {
       
   193             // take bitmap from player.
       
   194             // Ownership transfers to this class.
       
   195             iBitmap = iGUIPlayer->SnapshoterL()->SnapshotBitmap();
       
   196             if (!iBitmap)
       
   197             {
       
   198                 // error has occured with taking image
       
   199                 Completed(KErrNotFound);
       
   200             }
       
   201             else
       
   202             {
       
   203                 TInt nativeArea = iBitmap->SizeInPixels().iWidth * iBitmap->SizeInPixels().iHeight;
       
   204                 TInt requestArea = iSettings->iWidth * iSettings->iHeight;
       
   205                 if(requestArea  <  nativeArea && (((TReal)iSettings->iWidth/iSettings->iHeight != (4.0/3.0)) && ((TReal)iSettings->iWidth/iSettings->iHeight != (16.0/9.0))))
       
   206                 {
       
   207                     DEBUG_INT2("CMMASnapshot::EncodeL() nativeBitmap->iWidth = %d, nativeBitmap->iHeight %d",iBitmap->SizeInPixels().iWidth,iBitmap->SizeInPixels().iHeight);
       
   208                     if(iBitmap)
       
   209                     {
       
   210                         iBitmap->Resize(TSize(iSettings->iWidth,iSettings->iHeight));
       
   211                     }
       
   212                 }
       
   213                 else if(requestArea  >  nativeArea && (((TReal)iSettings->iWidth/iSettings->iHeight != (4.0/3.0))))
       
   214                 {
       
   215                     if(iBitmap)
       
   216                     {
       
   217                         iScalingFactor = ETrue;
       
   218                     }
       
   219                 }
       
   220             }
       
   221             // Continue to next statecd
       
   222             ResizeL();
       
   223         }
       
   224         break;
       
   225     }
       
   226     case EResizing:
       
   227     {
       
   228         // Continue to next state
       
   229         EncodeL();
       
   230         break;
       
   231     }
       
   232     case EEncoding:
       
   233     {
       
   234         delete iEncoder;
       
   235         iEncoder = NULL;
       
   236 
       
   237         delete iBitmap;
       
   238         iBitmap = NULL;
       
   239         
       
   240         iState = EIdle;
       
   241         // encoding is ready, inform listener
       
   242         Completed(KErrNone);
       
   243         break;
       
   244     }
       
   245     default:
       
   246     {
       
   247         // unknown state
       
   248         __ASSERT_DEBUG(EFalse, User::Invariant());
       
   249     }
       
   250     }
       
   251 }
       
   252 
       
   253 TInt CMMASnapshot::RunError(TInt aError)
       
   254 {
       
   255     // Reset state
       
   256     iState = EIdle;
       
   257     // Pass error code to observer
       
   258     Completed(aError);
       
   259 
       
   260     return KErrNone;
       
   261 }
       
   262 
       
   263 void CMMASnapshot::DoCancel()
       
   264 {
       
   265     // snapshot taking cannot be cancelled
       
   266     if (iScaler)
       
   267     {
       
   268         iScaler->Cancel();
       
   269     }
       
   270     if (iEncoder)
       
   271     {
       
   272         iEncoder->Cancel();
       
   273     }
       
   274     iState = EIdle;
       
   275 }
       
   276 
       
   277 //  END OF FILE