commonutilities/imagedecoderwrapper/src/glximagedecoder.cpp
changeset 50 a0f57508af73
parent 42 5e1df1b52eb3
equal deleted inserted replaced
45:863223ea6961 50:a0f57508af73
    20 
    20 
    21 #include <bitmaptransforms.h>
    21 #include <bitmaptransforms.h>
    22 #include <imageconversion.h>
    22 #include <imageconversion.h>
    23 #include <IclExtJpegApi.h>  // For CExtJpegDecoder
    23 #include <IclExtJpegApi.h>  // For CExtJpegDecoder
    24 #include <e32math.h>
    24 #include <e32math.h>
    25 
    25 #include <apgcli.h>
       
    26 
       
    27 _LIT(KMimeJpeg,"image/jpeg");
       
    28 _LIT(KMimeJpg,"image/jpg");
    26 
    29 
    27 // ---------------------------------------------------------------------------
    30 // ---------------------------------------------------------------------------
    28 // Two-phased constructor.
    31 // Two-phased constructor.
    29 // ---------------------------------------------------------------------------
    32 // ---------------------------------------------------------------------------
    30 //
    33 //
    88 		iImageDecoder->Cancel();
    91 		iImageDecoder->Cancel();
    89         delete iImageDecoder;
    92         delete iImageDecoder;
    90         iImageDecoder = NULL;
    93         iImageDecoder = NULL;
    91         }
    94         }
    92 	TRAPD( err, iImageDecoder = CExtJpegDecoder::FileNewL(
    95 	TRAPD( err, iImageDecoder = CExtJpegDecoder::FileNewL(
    93             CExtJpegDecoder::EHwImplementation, iFs, sourceFileName, CImageDecoder::EOptionAutoRotate ) );
    96             CExtJpegDecoder::EHwImplementation, iFs, sourceFileName, CImageDecoder::EOptionAlwaysThread ) );
    94     if ( KErrNone != err )
    97     if ( KErrNone != err )
    95         {
    98         {
    96         TRAP(err,iImageDecoder = CExtJpegDecoder::FileNewL(
    99         TRAP(err,iImageDecoder = CExtJpegDecoder::FileNewL(
    97                 CExtJpegDecoder::ESwImplementation, iFs, sourceFileName, CImageDecoder::EOptionAutoRotate ) );
   100                 CExtJpegDecoder::ESwImplementation, iFs, sourceFileName, CImageDecoder::EOptionAlwaysThread ) );
    98         if ( KErrNone != err )
   101         if ( KErrNone != err )
    99             {
   102             {
   100             // Not a JPEG - use standard decoder
   103             // Not a JPEG - use standard decoder
   101             iImageDecoder = CImageDecoder::FileNewL( iFs, sourceFileName, CImageDecoder::EOptionAutoRotate );
   104             iImageDecoder = CImageDecoder::FileNewL( iFs, sourceFileName, CImageDecoder::EOptionAlwaysThread );
   102             }
   105             }
   103         }
   106         }
   104 	TSize imageSize = iImageDecoder->FrameInfo().iOverallSizeInPixels;
   107 	TSize imageSize = iImageDecoder->FrameInfo().iOverallSizeInPixels;
   105 	//limit size to 1MP
   108 	//limit size to 1MP
   106 	TSize decodeSize = imageSize;
   109 	TSize decodeSize = imageSize;
   114 			compressionFactor = .1;
   117 			compressionFactor = .1;
   115 		}
   118 		}
   116 
   119 
   117 		decodeSize = TSize(imageSize.iWidth * compressionFactor, imageSize.iHeight * compressionFactor);
   120 		decodeSize = TSize(imageSize.iWidth * compressionFactor, imageSize.iHeight * compressionFactor);
   118 	}
   121 	}
       
   122 	//if an image is converted to Pixmap with any of its dimension > 2048 
       
   123 	//the conversion will fail so limiting dimensions to 2000
       
   124 	//on 2048 there is a buffer corruption so display image is distorted  
       
   125 	if(decodeSize.iWidth > KMaxDimensionLimit ||decodeSize.iHeight > KMaxDimensionLimit)
       
   126     {
       
   127         QSize finalSize(decodeSize.iWidth, decodeSize.iHeight);
       
   128         finalSize.scale(KMaxDimensionLimit, KMaxDimensionLimit, Qt::KeepAspectRatio);
       
   129         decodeSize = TSize(finalSize.width(), finalSize.height());
       
   130     }
   119 	//clear the existing Bitmap
   131 	//clear the existing Bitmap
   120 	if(iBitmap)
   132 	if(iBitmap)
   121 	{
   133 	{
   122 		delete iBitmap;
   134 		delete iBitmap;
   123 		iBitmap = NULL;
   135 		iBitmap = NULL;
   124 	}
   136 	}
   125 	// create the destination bitmap and pass it on to the decoder
   137 	// create the destination bitmap and pass it on to the decoder
   126     if(!iBitmap)
   138     if(!iBitmap)
   127         {
   139         {
   128         iBitmap = new (ELeave) CFbsBitmap();
   140         iBitmap = new (ELeave) CFbsBitmap();
   129         iBitmap->Create( decodeSize,EColor64K);
   141         decodeSize = ReCalculateSizeL(aSourceFileName, decodeSize);
       
   142         iBitmap->Create( decodeSize,EColor16MU);
   130         iImageDecoder->Convert( &iStatus, *iBitmap );
   143         iImageDecoder->Convert( &iStatus, *iBitmap );
   131 		SetActive();
   144 		SetActive();
   132 		}
   145 		}
   133 	return QSizeF(decodeSize.iWidth,decodeSize.iHeight) ;
   146 	return QSizeF(decodeSize.iWidth,decodeSize.iHeight) ;
   134 }
   147 }
       
   148 
       
   149 // -----------------------------------------------------------------------------
       
   150 // DoesMimeTypeNeedsRecalculateL()
       
   151 // -----------------------------------------------------------------------------
       
   152 //
       
   153 TBool CGlxImageDecoder::DoesMimeTypeNeedsRecalculateL(QString aSourceFileName){
       
   154     RApaLsSession session;
       
   155     TDataType mimeType;
       
   156     TUid uid;
       
   157     
       
   158     User::LeaveIfError( session.Connect() );
       
   159     CleanupClosePushL( session );
       
   160     TPtrC16 sourceFileName(reinterpret_cast<const TUint16*>(aSourceFileName.utf16()));
       
   161     User::LeaveIfError( session.AppForDocument( sourceFileName, uid, mimeType ) );
       
   162     CleanupStack::PopAndDestroy(&session);
       
   163     
       
   164     if (mimeType.Des().Compare(KMimeJpeg)==0 ||
       
   165             mimeType.Des().Compare(KMimeJpg)==0){
       
   166         return EFalse;
       
   167         }
       
   168     else{
       
   169         return ETrue; 
       
   170         }
       
   171     }
       
   172 
       
   173 // -----------------------------------------------------------------------------
       
   174 // ReCalculateSize 
       
   175 // -----------------------------------------------------------------------------
       
   176 TSize CGlxImageDecoder::ReCalculateSizeL(QString aSourceFileName, TSize aDestSize){
       
   177     if(DoesMimeTypeNeedsRecalculateL(aSourceFileName)){
       
   178         TSize fullFrameSize = iImageDecoder->FrameInfo().iOverallSizeInPixels;
       
   179         // calculate the reduction factor on what size we need
       
   180         TInt reductionFactor = iImageDecoder->ReductionFactor(fullFrameSize, aDestSize);
       
   181         // get the reduced size onto destination size
       
   182         TSize destSize;
       
   183         User::LeaveIfError(iImageDecoder->ReducedSize(fullFrameSize, reductionFactor, destSize));
       
   184         return destSize;
       
   185         }
       
   186     else{
       
   187         return aDestSize;
       
   188         }
       
   189     }
       
   190 
   135 // ---------------------------------------------------------------------------
   191 // ---------------------------------------------------------------------------
   136 // RunL
   192 // RunL
   137 // ---------------------------------------------------------------------------
   193 // ---------------------------------------------------------------------------
   138 //
   194 //
   139 void CGlxImageDecoder::RunL()
   195 void CGlxImageDecoder::RunL()
   174 //
   230 //
   175 QPixmap CGlxImageDecoder::GetPixmap()
   231 QPixmap CGlxImageDecoder::GetPixmap()
   176 	{
   232 	{
   177 	if(iBitmap)
   233 	if(iBitmap)
   178 		{
   234 		{
   179 		//convert the bitmap to pixmap
   235 		iDecodedPixmap = QPixmap::fromSymbianCFbsBitmap(iBitmap);
   180 		iBitmap->LockHeap();
       
   181 		TUint32 *tempData = iBitmap->DataAddress();
       
   182 		uchar *data = (uchar *)(tempData);	
       
   183 		int bytesPerLine = iBitmap->ScanLineLength(iBitmap->SizeInPixels().iWidth , iBitmap->DisplayMode());
       
   184 		 QImage::Format format;
       
   185     switch(iBitmap->DisplayMode()) {
       
   186     case EGray2:
       
   187         format = QImage::Format_MonoLSB;
       
   188         break;
       
   189     case EColor256:
       
   190     case EGray256:
       
   191         format = QImage::Format_Indexed8;
       
   192         break;
       
   193     case EColor4K:
       
   194         format = QImage::Format_RGB444;
       
   195         break;
       
   196     case EColor64K:
       
   197         format = QImage::Format_RGB16;
       
   198         break;
       
   199     case EColor16M:
       
   200         format = QImage::Format_RGB666;
       
   201         break;
       
   202     case EColor16MU:
       
   203         format = QImage::Format_RGB32;
       
   204         break;
       
   205     case EColor16MA:
       
   206         format = QImage::Format_ARGB32;
       
   207         break;
       
   208 #if !defined(__SERIES60_31__) && !defined(__S60_32__)
       
   209     case EColor16MAP:
       
   210         format = QImage::Format_ARGB32_Premultiplied;
       
   211         break;
       
   212 #endif
       
   213     default:
       
   214         format = QImage::Format_Invalid;
       
   215         break;
       
   216     }
       
   217 		//QImage share the memory occupied by data
       
   218 		QImage image(data, iBitmap->SizeInPixels().iWidth, iBitmap->SizeInPixels().iHeight, bytesPerLine, format);
       
   219 		iDecodedPixmap = QPixmap::fromImage(image);
       
   220 		iBitmap->UnlockHeap();
       
   221 		//clean the bitmap as it is not required anymore
       
   222 		delete iBitmap;
   236 		delete iBitmap;
   223 		iBitmap = NULL;
   237 		iBitmap = NULL;
   224 		}
   238 		}
   225 	return iDecodedPixmap;
   239 	return iDecodedPixmap;
   226 	}
   240 	}