photosgallery/collectionframework/thumbnailcreator/src/glxtnzoomedimagetask.cpp
changeset 0 4e91876724a2
equal deleted inserted replaced
-1:000000000000 0:4e91876724a2
       
     1 /*
       
     2 * Copyright (c) 2008-2009 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:    Task to generate a cropped thumbnail for a JPEG image.
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 
       
    21 /**
       
    22  * @internal reviewed 31/07/2007 by Simon Brooks
       
    23  */
       
    24 
       
    25 #include "glxtnzoomedimagetask.h"
       
    26 
       
    27 #include <fbs.h>
       
    28 #include <glxassert.h>
       
    29 #include <glxlog.h>
       
    30 #include <IclExtJpegApi.h>  // For CExtJpegDecoder
       
    31 #include <glxtracer.h>
       
    32 
       
    33 #include "glxtnfileinfo.h"
       
    34 #include "glxtnfileutility.h"
       
    35 #include "glxtnthumbnailrequest.h"
       
    36 #include "mglxtnthumbnailcreatorclient.h"
       
    37 
       
    38 // -----------------------------------------------------------------------------
       
    39 // NewL
       
    40 // -----------------------------------------------------------------------------
       
    41 //
       
    42 CGlxtnZoomedImageTask* CGlxtnZoomedImageTask::NewL(
       
    43         const TGlxThumbnailRequest& aRequestInfo,
       
    44         CGlxtnFileUtility& aFileUtility, MGlxtnThumbnailCreatorClient& aClient)
       
    45     {
       
    46     TRACER("CGlxtnZoomedImageTask* CGlxtnZoomedImageTask::NewL( const TGlxThumbnailRequest& aRequestInfo, CGlxtnFileUtility& aFileUtility, MGlxtnThumbnailCreatorClient& aClient)");
       
    47     CGlxtnZoomedImageTask* task = new (ELeave) CGlxtnZoomedImageTask(
       
    48                                         aRequestInfo, aFileUtility, aClient);
       
    49     CleanupStack::PushL(task);
       
    50     task->ConstructL(aRequestInfo.iBitmapHandle);
       
    51     CleanupStack::Pop( task );
       
    52     return task;
       
    53     }
       
    54 
       
    55 // -----------------------------------------------------------------------------
       
    56 // Destructor
       
    57 // -----------------------------------------------------------------------------
       
    58 //
       
    59 CGlxtnZoomedImageTask::~CGlxtnZoomedImageTask()
       
    60     {
       
    61     TRACER("CGlxtnZoomedImageTask::~CGlxtnZoomedImageTask()");
       
    62     delete iDecoder;
       
    63     delete iInfo;
       
    64     delete iThumbnail;
       
    65     }
       
    66 
       
    67 // -----------------------------------------------------------------------------
       
    68 // Constructor
       
    69 // -----------------------------------------------------------------------------
       
    70 //
       
    71 CGlxtnZoomedImageTask::CGlxtnZoomedImageTask(
       
    72                                     const TGlxThumbnailRequest& aRequestInfo,
       
    73                                     CGlxtnFileUtility& aFileUtility,
       
    74                                     MGlxtnThumbnailCreatorClient& aClient) :
       
    75     CGlxtnClientTask(KGlxtnTaskIdZoomedThumbnail, aRequestInfo.iId, aClient),
       
    76     iFileUtility(aFileUtility), iRequestedSize(aRequestInfo.iSizeClass),
       
    77     iCroppingRect(aRequestInfo.iCroppingRect), iFilter(aRequestInfo.iFilter),
       
    78     iDrmAllowed(aRequestInfo.iDrmAllowed)
       
    79     {
       
    80     TRACER("CGlxtnZoomedImageTask::CGlxtnZoomedImageTask( const TGlxThumbnailRequest& aRequestInfo, CGlxtnFileUtility& aFileUtility, MGlxtnThumbnailCreatorClient& aClient)");
       
    81     }
       
    82 
       
    83 // -----------------------------------------------------------------------------
       
    84 // ConstructL
       
    85 // -----------------------------------------------------------------------------
       
    86 //
       
    87 void CGlxtnZoomedImageTask::ConstructL(TInt aBitmapHandle)
       
    88     {
       
    89     TRACER("void CGlxtnZoomedImageTask::ConstructL(TInt aBitmapHandle)");
       
    90     // Duplicate the client's bitmap
       
    91     iThumbnail = new (ELeave) CFbsBitmap();
       
    92     User::LeaveIfError(iThumbnail->Duplicate(aBitmapHandle));
       
    93     }
       
    94 
       
    95 // -----------------------------------------------------------------------------
       
    96 // DoStartL
       
    97 // -----------------------------------------------------------------------------
       
    98 //
       
    99 TBool CGlxtnZoomedImageTask::DoStartL(TRequestStatus& aStatus)
       
   100     {
       
   101     TRACER("TBool CGlxtnZoomedImageTask::DoStartL(TRequestStatus& aStatus)");
       
   102     iInfo = new (ELeave) CGlxtnFileInfo;
       
   103     Client().FetchFileInfoL(iInfo, ItemId(), &aStatus);
       
   104     iState = EStateFetchingUri;
       
   105 
       
   106     return ETrue;
       
   107     }
       
   108 
       
   109 // -----------------------------------------------------------------------------
       
   110 // DoCancel
       
   111 // -----------------------------------------------------------------------------
       
   112 //
       
   113 void CGlxtnZoomedImageTask::DoCancel()
       
   114     {
       
   115     TRACER("void CGlxtnZoomedImageTask::DoCancel()");
       
   116     if ( EStateFetchingUri == iState )
       
   117         {
       
   118         Client().CancelFetchUri(ItemId());
       
   119         }
       
   120     if ( iDecoder )
       
   121         {
       
   122         iDecoder->Cancel();
       
   123         }
       
   124 
       
   125     iFileUtility.ClearBadFileMarker();
       
   126     }
       
   127 
       
   128 // -----------------------------------------------------------------------------
       
   129 // DoRunL
       
   130 // -----------------------------------------------------------------------------
       
   131 //
       
   132 TBool CGlxtnZoomedImageTask::DoRunL(TRequestStatus& aStatus)
       
   133     {
       
   134     TRACER("TBool CGlxtnZoomedImageTask::DoRunL(TRequestStatus& aStatus)");
       
   135     TInt error = aStatus.Int();
       
   136 	TBool active = EFalse;
       
   137 
       
   138     if ( KErrNone == error )
       
   139         {
       
   140         switch ( iState )
       
   141             {
       
   142             case EStateFetchingUri:
       
   143                 {
       
   144                 User::LeaveIfNull(iInfo);
       
   145                 iFileUtility.CheckBadFileListL( iInfo->FilePath() );
       
   146 
       
   147                 TBool video;
       
   148                 iInfo->IdentifyFileL(video, iProtected);
       
   149                 if ( iProtected && !iDrmAllowed )
       
   150                     {
       
   151                     User::Leave(KErrAccessDenied);
       
   152                     }
       
   153 
       
   154                 DecodePartialImageL(aStatus);
       
   155                 active = ETrue;
       
   156                 iState = EStateDecoding;
       
   157                 }
       
   158                 break;
       
   159 
       
   160             case EStateDecoding:
       
   161                 // Nothing to do
       
   162                 break;
       
   163 
       
   164             default:
       
   165                 GLX_ASSERT_ALWAYS( EFalse, Panic( EGlxPanicIllegalState ),
       
   166                                 "CGlxtnZoomedImageTask: Illegal state" );
       
   167                 break;
       
   168             }
       
   169         }
       
   170 
       
   171     if ( !active )
       
   172         {
       
   173         iFileUtility.ClearBadFileMarker();
       
   174         Client().ThumbnailFetchComplete(ItemId(),
       
   175                                         EGlxThumbnailQualityHigh, error);
       
   176         }
       
   177        
       
   178     return active; 
       
   179     }
       
   180 
       
   181 // -----------------------------------------------------------------------------
       
   182 // DoRunError
       
   183 // -----------------------------------------------------------------------------
       
   184 //
       
   185 TBool CGlxtnZoomedImageTask::DoRunError(TInt aError)
       
   186     {
       
   187     TRACER("TBool CGlxtnZoomedImageTask::DoRunError(TInt aError)");
       
   188     iFileUtility.ClearBadFileMarker();
       
   189     Client().ThumbnailFetchComplete(ItemId(), EGlxThumbnailQualityHigh, aError);
       
   190 
       
   191     return EFalse;
       
   192     }
       
   193 
       
   194 // -----------------------------------------------------------------------------
       
   195 // DecodePartialImageL
       
   196 // -----------------------------------------------------------------------------
       
   197 //
       
   198 void CGlxtnZoomedImageTask::DecodePartialImageL(TRequestStatus& aStatus)
       
   199     {
       
   200     TRACER("void CGlxtnZoomedImageTask::DecodePartialImageL(TRequestStatus& aStatus)");
       
   201     TRAPD(err, CreateDecoderL(CExtJpegDecoder::EHwImplementation));
       
   202     if ( KErrNone != err )
       
   203         {
       
   204         CreateDecoderL(CExtJpegDecoder::ESwImplementation);
       
   205         }
       
   206 
       
   207     TSize sourceSize(iDecoder->FrameInfo().iOverallSizeInPixels);
       
   208     if ( 0 == sourceSize.iWidth || 0 == sourceSize.iHeight )
       
   209         {
       
   210         User::Leave(KErrCorrupt);
       
   211         }
       
   212 
       
   213     iCroppingRect.Intersection(TRect(sourceSize));
       
   214     if ( iCroppingRect.IsEmpty() )
       
   215         {
       
   216         User::Leave(KErrArgument);
       
   217         }
       
   218 
       
   219     // Find max scaling factor which won't make image smaller than target size
       
   220     TInt shift = 3;
       
   221     while ( shift
       
   222             && (sourceSize.iWidth >> shift) < iRequestedSize.iWidth
       
   223             && (sourceSize.iHeight >> shift) < iRequestedSize.iHeight )
       
   224         {
       
   225         shift--;
       
   226         }
       
   227     TInt fudge = (1 << shift) - 1;  // Used to "round up" the scaled values
       
   228     TSize loadSize((iCroppingRect.Width() + fudge) >> shift,
       
   229                     (iCroppingRect.Height() + fudge) >> shift);
       
   230 
       
   231     // Resize empty bitmap to required size
       
   232     User::LeaveIfError(iThumbnail->Resize(loadSize));
       
   233 
       
   234     iDecoder->SetCroppingL(iCroppingRect);
       
   235     iDecoder->Convert(&aStatus, *iThumbnail);
       
   236     }
       
   237 
       
   238 // -----------------------------------------------------------------------------
       
   239 // CreateDecoderL
       
   240 // -----------------------------------------------------------------------------
       
   241 //
       
   242 void CGlxtnZoomedImageTask::CreateDecoderL(TInt aDecoderType)
       
   243     {
       
   244     TRACER("void CGlxtnZoomedImageTask::CreateDecoderL(TInt aDecoderType)");
       
   245     delete iDecoder;
       
   246     iDecoder = NULL;
       
   247     iDecoder = CExtJpegDecoder::FileNewL(
       
   248                     static_cast<CExtJpegDecoder::TDecoderType>(aDecoderType),
       
   249                     iFileUtility.FsSession(), iInfo->FilePath());
       
   250 
       
   251     if ( !(CExtJpegDecoder::ECapCropping & iDecoder->CapabilitiesL()) )
       
   252         {
       
   253         User::Leave(KErrNotSupported);
       
   254         }
       
   255     }
       
   256 
       
   257 //  End of File