commonutilities/imagedecoderwrapper/src/glximagedecoder.cpp
changeset 23 74c9f037fd5d
child 33 1ee2af37811f
equal deleted inserted replaced
5:f7f0874bfe7d 23:74c9f037fd5d
       
     1 /*
       
     2 * Copyright (c) 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:   ?Description
       
    15 *
       
    16 */
       
    17 
       
    18 #include "glximagedecoder.h"
       
    19 #include "glximagedecoderwrapper.h"
       
    20 
       
    21 #include <bitmaptransforms.h>
       
    22 #include <imageconversion.h>
       
    23 #include <IclExtJpegApi.h>  // For CExtJpegDecoder
       
    24 
       
    25 
       
    26 // ---------------------------------------------------------------------------
       
    27 // Two-phased constructor.
       
    28 // ---------------------------------------------------------------------------
       
    29 //
       
    30 CGlxImageDecoder* CGlxImageDecoder::NewL(GlxImageDecoderWrapper* decoderWrapper)
       
    31 {
       
    32 	CGlxImageDecoder* self = new(ELeave)CGlxImageDecoder();
       
    33     CleanupStack::PushL(self);
       
    34     self->ConstructL(decoderWrapper);
       
    35     CleanupStack::Pop(self);
       
    36     return self;
       
    37 }
       
    38 
       
    39 // ---------------------------------------------------------------------------
       
    40 // Constructor
       
    41 // ---------------------------------------------------------------------------
       
    42 //
       
    43 CGlxImageDecoder::CGlxImageDecoder()
       
    44                          :CActive( EPriorityStandard )
       
    45 {
       
    46 }
       
    47 
       
    48 // ---------------------------------------------------------------------------
       
    49 // Destructor
       
    50 // ---------------------------------------------------------------------------
       
    51 //
       
    52 
       
    53 CGlxImageDecoder::~CGlxImageDecoder()
       
    54 {
       
    55     iFs.Close();
       
    56     Cancel();
       
    57     if (iImageDecoder)
       
    58         {
       
    59         delete iImageDecoder;
       
    60         iImageDecoder = NULL;
       
    61         }
       
    62 	if(iBitmap) {
       
    63 		delete iBitmap;
       
    64 		iBitmap = NULL;
       
    65 	}
       
    66 }
       
    67 
       
    68 // ---------------------------------------------------------------------------
       
    69 // ConstructL
       
    70 // ---------------------------------------------------------------------------
       
    71 void CGlxImageDecoder::ConstructL(GlxImageDecoderWrapper* decoderWrapper)
       
    72 {
       
    73     iDecoderWrapper = decoderWrapper;
       
    74     User::LeaveIfError(iFs.Connect());
       
    75     CActiveScheduler::Add( this );
       
    76 }
       
    77 
       
    78 // ---------------------------------------------------------------------------
       
    79 // DoDecodeImageL
       
    80 // ---------------------------------------------------------------------------	
       
    81 void CGlxImageDecoder::DecodeImageL(QString aSourceFileName)
       
    82 {
       
    83 	//convert the argument to Symbian Format
       
    84 	TPtrC16 sourceFileName(reinterpret_cast<const TUint16*>(aSourceFileName.utf16()));
       
    85 	if (iImageDecoder)
       
    86         {
       
    87 		iImageDecoder->Cancel();
       
    88         delete iImageDecoder;
       
    89         iImageDecoder = NULL;
       
    90         }
       
    91 	/*TRAPD( err, iImageDecoder = CExtJpegDecoder::FileNewL(
       
    92             CExtJpegDecoder::EHwImplementation, iFs, sourceFileName, CImageDecoder::EOptionNone ) );
       
    93     if ( KErrNone != err )
       
    94         {
       
    95         TRAP(err,iImageDecoder = CExtJpegDecoder::FileNewL(
       
    96                 CExtJpegDecoder::ESwImplementation, iFs, sourceFileName, CImageDecoder::EOptionNone ) );
       
    97         if ( KErrNone != err )
       
    98             {
       
    99             // Not a JPEG - use standard decoder
       
   100             iImageDecoder = CImageDecoder::FileNewL( iFs, sourceFileName, CImageDecoder::EOptionNone );
       
   101             }
       
   102         }*/
       
   103 	iImageDecoder = CImageDecoder::FileNewL( iFs, sourceFileName, CImageDecoder::EOptionNone );
       
   104 	TSize imageSize = iImageDecoder->FrameInfo().iOverallSizeInPixels;
       
   105 	//clear the existing Bitmap
       
   106 	if(iBitmap)
       
   107 	{
       
   108 		delete iBitmap;
       
   109 		iBitmap = NULL;
       
   110 	}
       
   111 	// create the destination bitmap and pass it on to the decoder
       
   112     if(!iBitmap)
       
   113         {
       
   114         iBitmap = new (ELeave) CFbsBitmap();
       
   115         iBitmap->Create( imageSize,EColor64K);
       
   116         iImageDecoder->Convert( &iStatus, *iBitmap );
       
   117 		SetActive();
       
   118 		}
       
   119 }
       
   120 // ---------------------------------------------------------------------------
       
   121 // RunL
       
   122 // ---------------------------------------------------------------------------
       
   123 //
       
   124 void CGlxImageDecoder::RunL()
       
   125     {
       
   126     if( iStatus == KErrNone )
       
   127         {   
       
   128         iDecoderWrapper->decodedImageAvailable();
       
   129         delete iImageDecoder;
       
   130         iImageDecoder = NULL;
       
   131 		iFs.Close();
       
   132         }	
       
   133     }
       
   134 
       
   135 // ---------------------------------------------------------------------------
       
   136 // DoCancel
       
   137 // ---------------------------------------------------------------------------
       
   138 //
       
   139 void CGlxImageDecoder::DoCancel()
       
   140     {
       
   141     if(iImageDecoder)
       
   142         {
       
   143         iImageDecoder->Cancel();
       
   144         delete iImageDecoder;
       
   145         iImageDecoder = NULL;
       
   146         }
       
   147 	iFs.Close();
       
   148     if(iBitmap)
       
   149         {
       
   150         delete iBitmap;
       
   151         iBitmap = NULL;
       
   152         }
       
   153 	//todo add cleanup logic for pixmap
       
   154     }
       
   155 
       
   156 // ---------------------------------------------------------------------------
       
   157 // GetPixmap
       
   158 // ---------------------------------------------------------------------------
       
   159 //
       
   160 QPixmap CGlxImageDecoder::GetPixmap()
       
   161 	{
       
   162 	if(iBitmap)
       
   163 		{
       
   164 		//convert the bitmap to pixmap
       
   165 		iBitmap->LockHeap();
       
   166 		TUint32 *tempData = iBitmap->DataAddress();
       
   167 		uchar *data = (uchar *)(tempData);	
       
   168 		int bytesPerLine = iBitmap->ScanLineLength(iBitmap->SizeInPixels().iWidth , iBitmap->DisplayMode());
       
   169 		 QImage::Format format;
       
   170     switch(iBitmap->DisplayMode()) {
       
   171     case EGray2:
       
   172         format = QImage::Format_MonoLSB;
       
   173         break;
       
   174     case EColor256:
       
   175     case EGray256:
       
   176         format = QImage::Format_Indexed8;
       
   177         break;
       
   178     case EColor4K:
       
   179         format = QImage::Format_RGB444;
       
   180         break;
       
   181     case EColor64K:
       
   182         format = QImage::Format_RGB16;
       
   183         break;
       
   184     case EColor16M:
       
   185         format = QImage::Format_RGB666;
       
   186         break;
       
   187     case EColor16MU:
       
   188         format = QImage::Format_RGB32;
       
   189         break;
       
   190     case EColor16MA:
       
   191         format = QImage::Format_ARGB32;
       
   192         break;
       
   193 #if !defined(__SERIES60_31__) && !defined(__S60_32__)
       
   194     case EColor16MAP:
       
   195         format = QImage::Format_ARGB32_Premultiplied;
       
   196         break;
       
   197 #endif
       
   198     default:
       
   199         format = QImage::Format_Invalid;
       
   200         break;
       
   201     }
       
   202 		//QImage share the memory occupied by data
       
   203 		QImage image(data, iBitmap->SizeInPixels().iWidth, iBitmap->SizeInPixels().iHeight, bytesPerLine, format);
       
   204 		iDecodedPixmap = QPixmap::fromImage(image);
       
   205 		iBitmap->UnlockHeap();
       
   206 		//clean the bitmap as it is not required anymore
       
   207 		delete iBitmap;
       
   208 		iBitmap = NULL;
       
   209 		}
       
   210 	return iDecodedPixmap;
       
   211 	}
       
   212 
       
   213 // ---------------------------------------------------------------------------
       
   214 // ResetDecoder
       
   215 // ---------------------------------------------------------------------------
       
   216 //
       
   217 
       
   218 void CGlxImageDecoder::ResetDecoder()
       
   219 {
       
   220 	Cancel();
       
   221 	if(iBitmap) {
       
   222 		delete iBitmap;
       
   223 		iBitmap = NULL;
       
   224 	}
       
   225 	iDecodedPixmap = QPixmap();
       
   226 }
       
   227