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