mmplugins/imagingplugins/codecs/PNGCodec/PNGConvert.cpp
branchRCL_3
changeset 50 948c7f65f6d4
parent 0 40261b775718
equal deleted inserted replaced
49:735348f59235 50:948c7f65f6d4
       
     1 // Copyright (c) 1999-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     2 // All rights reserved.
       
     3 // This component and the accompanying materials are made available
       
     4 // under the terms of "Eclipse Public License v1.0"
       
     5 // which accompanies this distribution, and is available
       
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 //
       
    15 
       
    16 #include <barsc.h>
       
    17 #include <barsread.h>
       
    18 #include <bautils.h>
       
    19 #include <imageconversion.h>
       
    20 #include "ImageClientMain.h"
       
    21 #include "ImageUtils.h"
       
    22 #include <101F45C7_extra.rsg>
       
    23 #include "icl/ICL_UIDS.hrh"
       
    24 #ifdef SYMBIAN_ENABLE_SPLIT_HEADERS
       
    25 #include <icl/icl_uids_const.hrh>
       
    26 #include <icl/icl_uids_def.hrh>
       
    27 #include <icl/imagecodecdef.h>
       
    28 #endif
       
    29 #include "PNGCodec.h"
       
    30 
       
    31 
       
    32 _LIT(KPNGPanicCategory, "PNGConvertPlugin");
       
    33 
       
    34 // Global panic function
       
    35 GLDEF_C void Panic(TIclPanic aError)
       
    36 	{
       
    37 	User::Panic(KPNGPanicCategory, aError);
       
    38 	}
       
    39 
       
    40 
       
    41 // Png decoder.
       
    42 CPngDecoder* CPngDecoder::NewL()
       
    43 	{
       
    44 	return new(ELeave) CPngDecoder;
       
    45 	}
       
    46 
       
    47 CPngDecoder::CPngDecoder()
       
    48 	{}
       
    49 
       
    50 CPngDecoder::~CPngDecoder()
       
    51 	{
       
    52 	Cleanup();
       
    53 	}
       
    54 
       
    55 void CPngDecoder::ImageType(TInt aFrameNumber, TUid& aImageType, TUid& aImageSubType) const
       
    56 	{
       
    57 	__ASSERT_ALWAYS(aFrameNumber == 0, Panic(EFrameNumberOutOfRange));
       
    58 	aImageType = KImageTypePNGUid;
       
    59 	aImageSubType = KNullUid;
       
    60 	}
       
    61 
       
    62 // Scan header.
       
    63 // Validate that format is correct.
       
    64 // Create codec.
       
    65 // Fill in image info. (All frames)
       
    66 void CPngDecoder::ScanDataL()
       
    67 	{
       
    68 	ReadFormatL();
       
    69 	
       
    70 	ASSERT(ImageReadCodec() == NULL);
       
    71 
       
    72 	CPngReadCodec* imageReadCodec;
       
    73 	
       
    74 	imageReadCodec = CPngReadCodec::NewL(*this);
       
    75 	imageReadCodec->SetMissingiENDChunkFail(DecoderOptions() & CImageDecoder::EOptionPngMissingiENDFail);
       
    76 	SetImageReadCodec(imageReadCodec);		// takes ownership of imageReadCodec
       
    77 
       
    78 	ReadFrameHeadersL();
       
    79 	}
       
    80 
       
    81 void CPngDecoder::ReadFormatL()
       
    82 	{
       
    83 	TPtrC8 bufferDes;
       
    84 
       
    85 	ReadDataL(0, bufferDes, KPngFileSignatureLength);
       
    86 
       
    87 	// Validate the header.
       
    88 	if (bufferDes.Length() < KPngFileSignatureLength)
       
    89 		User::Leave(KErrUnderflow);
       
    90 
       
    91 	const TUint8* ptr = bufferDes.Ptr();
       
    92 	if (Mem::Compare(ptr, KPngFileSignatureLength, KPngSignature, KPngFileSignatureLength)!=0)
       
    93 		User::Leave(KErrCorrupt);
       
    94 
       
    95 	// Set start position.
       
    96 	SetStartPosition(KPngFileSignatureLength);
       
    97 
       
    98 	// Get this from the header?
       
    99 	SetDataLength(KMaxTInt);
       
   100 	}
       
   101 
       
   102 CFrameInfoStrings* CPngDecoder::FrameInfoStringsL(RFs& aFs, TInt aFrameNumber)
       
   103 	{
       
   104 
       
   105 	const TUid KPngCodecDllUid = {KPNGCodecDllUidValue};
       
   106 
       
   107 	RResourceFile resourceFile;
       
   108 	OpenExtraResourceFileLC(aFs,KPngCodecDllUid,resourceFile);
       
   109 
       
   110 	HBufC8* resourceInfo = resourceFile.AllocReadLC(THEDECODERINFO);
       
   111 	TResourceReader resourceReader;
       
   112 	resourceReader.SetBuffer(resourceInfo);
       
   113 
       
   114 	TBuf<KCodecResourceStringMax> info;
       
   115 	TBuf<KCodecResourceStringMax> templte;
       
   116 
       
   117 	const TFrameInfo& frameInfo = FrameInfo(aFrameNumber);
       
   118 	CFrameInfoStrings* frameInfoStrings = CFrameInfoStrings::NewLC();
       
   119 
       
   120 	info = resourceReader.ReadTPtrC();
       
   121 	frameInfoStrings->SetDecoderL(info);
       
   122 
       
   123 	info = resourceReader.ReadTPtrC();
       
   124 	frameInfoStrings->SetFormatL(info);
       
   125 
       
   126 	TInt width = frameInfo.iOverallSizeInPixels.iWidth;
       
   127 	TInt height = frameInfo.iOverallSizeInPixels.iHeight;
       
   128 	TInt depth = frameInfo.iBitsPerPixel;
       
   129 
       
   130 	templte = resourceReader.ReadTPtrC();
       
   131 	info.Format(templte, width, height);
       
   132 	frameInfoStrings->SetDimensionsL(info);
       
   133 
       
   134 	CDesCArrayFlat* resourceArray = resourceReader.ReadDesCArrayL();
       
   135 	CleanupStack::PushL(resourceArray);
       
   136 	TUint formatIndex = (frameInfo.iFlags & TFrameInfo::EColor) ? 1 : 0;
       
   137 	templte = (*resourceArray)[formatIndex];
       
   138 	CleanupStack::PopAndDestroy(resourceArray);
       
   139 	info.Format(templte, depth);
       
   140 	frameInfoStrings->SetDepthL(info);
       
   141 
       
   142 	if(frameInfo.iFlags & TFrameInfo::ETransparencyPossible)
       
   143 		{
       
   144 		resourceArray = resourceReader.ReadDesCArrayL();
       
   145 		CleanupStack::PushL(resourceArray);
       
   146 		formatIndex = (frameInfo.iFlags & TFrameInfo::EAlphaChannel) ? 1 : 0;
       
   147 		info = (*resourceArray)[formatIndex];
       
   148 		frameInfoStrings->SetDetailsL(info);
       
   149 		CleanupStack::PopAndDestroy(resourceArray);
       
   150 		}
       
   151 	
       
   152 	CleanupStack::Pop(frameInfoStrings); 
       
   153 	CleanupStack::PopAndDestroy(2); // resourceInfo + resourceFile
       
   154 	return frameInfoStrings;
       
   155 	}
       
   156 
       
   157 void CPngDecoder::InitConvertL()
       
   158 	{
       
   159 	iState = EStateStart; //  when starting convert, always start from the top
       
   160 	CImageDecoderPlugin::InitConvertL();
       
   161 	}
       
   162 
       
   163 void CPngDecoder::NotifyComplete()
       
   164 	{
       
   165 	iState = EStateStart; // ensure that however we exit one run, start at top of next DoConvert()
       
   166 	}
       
   167 
       
   168 void CPngDecoder::DoConvert()
       
   169 	{
       
   170 	switch (iState)
       
   171 		{
       
   172 		case EStateStart:
       
   173 			{
       
   174 			TRAPD(errCode, PrepareForProcessFrameL());
       
   175 
       
   176 			if (errCode!=KErrNone)
       
   177 				{
       
   178 				RequestComplete(errCode);
       
   179 				return;
       
   180 				}
       
   181 			iState = EStateNormalProcess;
       
   182 			}
       
   183 			// fall through
       
   184 		case EStateNormalProcess:
       
   185 			{
       
   186 			TFrameState codecState = EFrameIncomplete; 
       
   187 
       
   188 			TBufPtr8& sourceData = SourceData();
       
   189 			TInt errCode=KErrNone;
       
   190 			if (sourceData.Length()!=0)
       
   191 				TRAP(errCode, codecState = ImageReadCodec()->ProcessFrameL(sourceData));
       
   192 			
       
   193 			ASSERT(iState==EStateNormalProcess || iState==EStateDataProcess); // only valid states
       
   194 			if (errCode!=KErrNone || iState==EStateNormalProcess)
       
   195 				{
       
   196 				HandleProcessFrameResult(errCode, codecState);
       
   197 				iState = EStateStart;
       
   198 				}
       
   199 			else
       
   200 				{
       
   201 				ASSERT(iState==EStateDataProcess); // kick off a data process
       
   202 				SelfComplete(KErrNone); // ask for next run
       
   203 				}
       
   204 			}
       
   205 			break;
       
   206 		case EStateDataProcess:
       
   207 			{
       
   208 			TBool finished = EFalse;
       
   209 			CPngReadCodec* readCodec = static_cast<CPngReadCodec*>(ImageReadCodec());
       
   210 			TRAPD(error, finished = readCodec->DoProcessDataL());
       
   211 			if (error!=KErrNone)
       
   212 				{
       
   213 				ASSERT(error!=KErrUnderflow); // underflow should happen later
       
   214 				RequestComplete(error); // will reset state
       
   215 				return;
       
   216 				}
       
   217 
       
   218 			if (finished)
       
   219 				{
       
   220 				iState = EStateNormalProcess;
       
   221 				}
       
   222 			SelfComplete(KErrNone); // ask for next run, whatever we do
       
   223 			}
       
   224 			break;
       
   225 		default:
       
   226 			ASSERT(EFalse); // should not occur
       
   227 		}
       
   228 	}
       
   229 
       
   230 // Called from codec when we need to swith to EStateDataProcess
       
   231 void CPngDecoder::GoToProcessDataState()
       
   232 	{
       
   233 	ASSERT(iState==EStateNormalProcess); // should be in this state if we get here
       
   234 	iState = EStateDataProcess;
       
   235 	}
       
   236 
       
   237 // PNG encoder
       
   238 CPngEncoder* CPngEncoder::NewL()
       
   239 	{
       
   240 	return new(ELeave) CPngEncoder;
       
   241 	}
       
   242 
       
   243 CPngEncoder::CPngEncoder()
       
   244 	{
       
   245 	}
       
   246 
       
   247 CPngEncoder::~CPngEncoder()
       
   248 	{
       
   249 	CImageEncoderPlugin::Cleanup();
       
   250 	}
       
   251 
       
   252 void CPngEncoder::PrepareEncoderL(const CFrameImageData* aFrameImageData)
       
   253 	{
       
   254 	// Default encode params
       
   255 	TInt bpp = 24;
       
   256 	TBool color = ETrue;
       
   257 	TBool paletted = EFalse;
       
   258 	TInt compressionLevel = TPngEncodeData::EDefaultCompression;
       
   259 
       
   260 	// Use encode params in aFrameImageData, if present
       
   261 	const TInt count = (aFrameImageData) ? aFrameImageData->FrameDataCount() : 0;
       
   262 	for (TInt i=0; i < count; i++)
       
   263 		{
       
   264 		const TFrameDataBlock* encoderData = aFrameImageData->GetFrameData(i);
       
   265 		if (encoderData->DataType() == KPNGEncodeDataUid)
       
   266 			{
       
   267 			const TPngEncodeData* pngEncodeData = static_cast<const TPngEncodeData*>(encoderData);
       
   268 			bpp = pngEncodeData->iBitsPerPixel;
       
   269 			color = pngEncodeData->iColor;
       
   270 			paletted = pngEncodeData->iPaletted;
       
   271 			compressionLevel = pngEncodeData->iLevel;
       
   272 			}
       
   273 		}
       
   274 
       
   275 	// Create the codec
       
   276 	CPngWriteCodec* codec = CPngWriteCodec::NewL(*this, bpp, color, paletted, compressionLevel);
       
   277 	
       
   278 	SetImageWriteCodec(codec);		// takes ownership of imageReadCodec
       
   279 
       
   280 	}
       
   281 
       
   282 void CPngEncoder::InitConvertL()
       
   283 	{
       
   284 	iState = EStateNormalProcess; //  when starting convert, always start from the top
       
   285 	CImageEncoderPlugin::InitConvertL();
       
   286 	}
       
   287 
       
   288 void CPngEncoder::NotifyComplete()
       
   289 	{
       
   290 	iState = EStateNormalProcess; // ensure that however we exit one run, start at top of next DoConvert()
       
   291 	}
       
   292 
       
   293 void CPngEncoder::DoConvert()
       
   294 	{
       
   295 	switch(iState)
       
   296 		{
       
   297 		case EStateNormalProcess:
       
   298 			{
       
   299 			TFrameState codecState = EFrameIncomplete;
       
   300 
       
   301 			TBufPtr8& destData = DestinationData();
       
   302 			TRAPD(error, codecState = ImageWriteCodec()->ProcessFrameL(destData));
       
   303 
       
   304 			if(error!=KErrNone || iState==EStateNormalProcess)
       
   305 				HandleProcessFrameResult(error, codecState);
       
   306 			else
       
   307 				{
       
   308 				ASSERT(iState==EStateDataProcess); // kick off a data process
       
   309 				SelfComplete(KErrNone); // ask for next run
       
   310 				}
       
   311 			}
       
   312 			break;
       
   313 
       
   314 		case EStateDataProcess:
       
   315 			{
       
   316 			TBool finished = EFalse;
       
   317 			CPngWriteCodec* writeCodec = static_cast<CPngWriteCodec*>(ImageWriteCodec());
       
   318 
       
   319 			TRAPD(error, finished = writeCodec->DeflateEncodedDataL());
       
   320 
       
   321 			if(error!=KErrNone)
       
   322 				{
       
   323 				RequestComplete(error); // will reset state
       
   324 				return;
       
   325 				}
       
   326 
       
   327 			if(finished)
       
   328 				iState = EStateNormalProcess;
       
   329 
       
   330 			SelfComplete(KErrNone); // ask for next run, whatever we do
       
   331 			}
       
   332 			break;
       
   333 
       
   334 		default:
       
   335 			ASSERT(EFalse); // invalid state
       
   336 		}
       
   337 	}
       
   338 
       
   339 void CPngEncoder::UpdateHeaderL()
       
   340 	{
       
   341 	}
       
   342 
       
   343 void CPngEncoder::GoToProcessDataState()
       
   344 	{
       
   345 	iState = EStateDataProcess;
       
   346 	}
       
   347