imagingandcamerafws/imaginginttest/Codecs/PPM1/PPM1Convert.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 <101F45F1_extra.rsg>
       
    21 #include "PPM1Codec.h"
       
    22 #include "PPM1Panic.h"
       
    23 #include "PPMDecs.h"
       
    24 #include "PPM1Uids.hrh"
       
    25 
       
    26 _LIT(KPPM1PanicCategory, "PPM1ConvertPlugin");
       
    27 
       
    28 
       
    29 // Global panic function
       
    30 GLDEF_C void Panic(TInt aError)
       
    31 	{
       
    32 	User::Panic(KPPM1PanicCategory, aError);
       
    33 	}
       
    34 
       
    35 
       
    36 // decoder.
       
    37 CPpmDecoder* CPpmDecoder::NewL()
       
    38 	{
       
    39 	return new (ELeave) CPpmDecoder;
       
    40 	}
       
    41 
       
    42 CPpmDecoder::CPpmDecoder()
       
    43 	{}
       
    44 
       
    45 CPpmDecoder::~CPpmDecoder()
       
    46 	{
       
    47 	delete iExtensionManager;
       
    48 	Cleanup();
       
    49 	}
       
    50 
       
    51 void CPpmDecoder::ImageType(TInt aFrameNumber, TUid& aImageType, TUid& aImageSubType) const
       
    52 	{
       
    53 	__ASSERT_ALWAYS(aFrameNumber == 0, Panic(EFrameNumberOutOfRange));
       
    54 	aImageType = KImageTypePpmUid;
       
    55 	aImageSubType = KNullUid;
       
    56 	}
       
    57 
       
    58 // Scan header.
       
    59 // Validate that format is correct.
       
    60 // Create codec.
       
    61 // Fill in image info. (All frames)
       
    62 void CPpmDecoder::ScanDataL()
       
    63 	{
       
    64 	ReadFormatL();
       
    65 	
       
    66 	ASSERT(ImageReadCodec() == NULL);
       
    67 	CPpmReadCodec* imageReadCodec = CPpmReadCodec::NewL(*this); 
       
    68 	SetImageReadCodec(imageReadCodec);
       
    69 	
       
    70 	if (!iExtensionManager)
       
    71 		{
       
    72 		// Manager settings persist over all frames.
       
    73 		iExtensionManager = CPluginExtensionManager::NewL(imageReadCodec);
       
    74 		}
       
    75 	else
       
    76 		{
       
    77 		// Maintain manager settings, but reset the codec that it points to.
       
    78 		iExtensionManager->ResetCodecExtension(imageReadCodec);
       
    79 		}
       
    80 	
       
    81 	imageReadCodec->SetExtensionManager(iExtensionManager);
       
    82 
       
    83 	ReadFrameHeadersL();
       
    84 	}
       
    85 
       
    86 const TInt KPpmFileHeaderSize = 512; // use a high figure to get around possible comments - assumes high enough
       
    87 
       
    88 void CPpmDecoder::ReadFormatL()
       
    89 	{
       
    90 	TPtrC8 bufferDes;
       
    91 
       
    92 	ReadDataL(0, bufferDes, KPpmFileHeaderSize);
       
    93 
       
    94 	TLex8 lex (bufferDes);
       
    95 	TRAPD(error, InternalizeHeaderL(lex));
       
    96 
       
    97 	if (error!=KErrNone)
       
    98 		{
       
    99 		if (error==KErrGeneral) // treat as if underflow - means expected integers were not present
       
   100 			{
       
   101 			error = KErrUnderflow;
       
   102 			}			
       
   103 		User::Leave(error);
       
   104 		}
       
   105 	}
       
   106 
       
   107 void CPpmDecoder::InternalizeHeaderL(TLex8& aLex)
       
   108 	{
       
   109 	SetInComment(EFalse); // since we read from the top, always assume is clear
       
   110 
       
   111 	// first check we have either P3 or P6 at top of file
       
   112 	TChar char1 = aLex.Get();
       
   113 	if (char1!='P')
       
   114 		{
       
   115 		User::Leave(KErrNotSupported);
       
   116 		}		
       
   117 	TChar char2 = aLex.Get();
       
   118 	if (char2=='3')
       
   119 		{
       
   120 		SetCompressed(EFalse);
       
   121 		}		
       
   122 	else if (char2=='6')
       
   123 		{
       
   124 		SetCompressed(ETrue);
       
   125 		}		
       
   126 	else
       
   127 		{
       
   128 		User::Leave(KErrNotSupported);
       
   129 		}		
       
   130 
       
   131 	SkipCommentAndWhiteSpaceL(aLex);
       
   132 
       
   133 	TInt width;
       
   134 	User::LeaveIfError(aLex.Val(width));
       
   135 
       
   136 	SkipCommentAndWhiteSpaceL(aLex);
       
   137 
       
   138 	TInt height;
       
   139 	User::LeaveIfError(aLex.Val(height));
       
   140 
       
   141 	SkipCommentAndWhiteSpaceL(aLex);
       
   142 
       
   143 	TInt maxValue;
       
   144 	User::LeaveIfError(aLex.Val(maxValue));
       
   145 	if (maxValue>255)
       
   146 		{
       
   147 		User::Leave(KErrNotSupported);
       
   148 		}		
       
   149 
       
   150 	TInt bitsPerPixel, power;
       
   151 	for (bitsPerPixel=1, power=2; maxValue>power-1; power<<=1, bitsPerPixel+=1)
       
   152 		/*loop*/;
       
   153 
       
   154 	if (Compressed()) 
       
   155 		{
       
   156 		aLex.Inc(); // skip over just the end of line
       
   157 		}		
       
   158 
       
   159 	// we are now pointing at the data
       
   160 
       
   161 	SetStartPosition(aLex.Offset());
       
   162 	SetDataLength(KMaxTInt); // we don't know image size, so set big
       
   163 
       
   164 	TSize size = TSize(width, height);
       
   165 	iMaxValue = maxValue;
       
   166 
       
   167 	TFrameInfo imageInfo = ImageInfo();
       
   168 	imageInfo.iFrameCoordsInPixels.SetRect(TPoint(0, 0), size);
       
   169 	imageInfo.iOverallSizeInPixels = size;
       
   170 	imageInfo.iFrameSizeInTwips = TSize(0, 0);
       
   171 	imageInfo.iBitsPerPixel = bitsPerPixel;
       
   172 	imageInfo.iDelay = 0;
       
   173 	imageInfo.iFlags = TFrameInfo::EColor|TFrameInfo::ECanDither;
       
   174 	TDisplayMode mode;
       
   175 	if (bitsPerPixel<=4) // we always have rgb values
       
   176 		{
       
   177 		mode=EColor4K;
       
   178 		}		
       
   179 	else
       
   180 		{
       
   181 		mode=EColor16M;
       
   182 		}		
       
   183 	imageInfo.iFrameDisplayMode = mode;
       
   184 	SetImageInfo(imageInfo);
       
   185 
       
   186 	iDataShift = 8 - bitsPerPixel; // correct everything to 0..255
       
   187 	ASSERT(iDataShift>=0);
       
   188 	}
       
   189 
       
   190 // looks for white space or comment, and will skip if found
       
   191 void CPpmDecoder::SkipCommentAndWhiteSpaceL(TLex8& aLex)
       
   192 	{
       
   193 	if (InComment())
       
   194 		{
       
   195 		DoSkipCommentL(aLex);
       
   196 		}		
       
   197 	for (;;)
       
   198 		{
       
   199 		ASSERT(!InComment());
       
   200 		TChar peek = aLex.Peek();
       
   201 		if (peek==0) // overflowed end of descriptor
       
   202 			{
       
   203 			User::Leave(KErrUnderflow);
       
   204 			}			
       
   205 		else if (peek.IsSpace())
       
   206 			{
       
   207 			aLex.SkipSpace();
       
   208 			}			
       
   209 		else if (peek=='#') // comment - skip to end of line
       
   210 			{
       
   211 			DoSkipCommentL(aLex);
       
   212 			}			
       
   213 		else
       
   214 			{
       
   215 			break;
       
   216 			}			
       
   217 		}
       
   218 	}
       
   219 
       
   220 // we are in comment, so skip to end of line. If do not get there, register the fact
       
   221 void CPpmDecoder::DoSkipCommentL(TLex8& aLex)
       
   222 	{
       
   223 	TChar ch;
       
   224 	do
       
   225 		{
       
   226 		ch = aLex.Get();
       
   227 		}
       
   228 	while (ch != '\n');
       
   229 	if (ch==0) // reached end of descriptor
       
   230 		{
       
   231 		SetInComment(ETrue);
       
   232 		User::Leave(KErrUnderflow);
       
   233 		}
       
   234 	else
       
   235 		{
       
   236 		SetInComment(EFalse);
       
   237 		}			
       
   238 	}
       
   239 
       
   240 
       
   241 CFrameInfoStrings* CPpmDecoder::FrameInfoStringsL(RFs& aFs, TInt aFrameNumber)
       
   242 	{
       
   243 	if (aFrameNumber!=0)
       
   244 		{
       
   245 		User::Leave(KErrArgument);
       
   246 		}		
       
   247 
       
   248 	const TUid ppmCodecDllUid = {KPpm1DecoderDllUidValue};
       
   249 
       
   250 	RResourceFile resourceFile;
       
   251 	OpenExtraResourceFileLC(aFs,ppmCodecDllUid,resourceFile);
       
   252 
       
   253 	HBufC8* resourceInfo = resourceFile.AllocReadLC(THEDECODERINFO);
       
   254 	TResourceReader resourceReader;
       
   255 	resourceReader.SetBuffer(resourceInfo);
       
   256 
       
   257 	TBuf<128> info;
       
   258 	TBuf<128> templte;
       
   259 
       
   260 	CFrameInfoStrings* frameInfoStrings = CFrameInfoStrings::NewLC();
       
   261 
       
   262 	info = resourceReader.ReadTPtrC();
       
   263 	frameInfoStrings->SetDecoderL(info);
       
   264 
       
   265 	info = resourceReader.ReadTPtrC();
       
   266 	frameInfoStrings->SetFormatL(info);
       
   267 
       
   268 	templte = resourceReader.ReadTPtrC();
       
   269 	const TFrameInfo& frameInfo = FrameInfo(aFrameNumber);
       
   270 	const TSize& size = frameInfo.iOverallSizeInPixels;
       
   271 	info.Format(templte, size.iWidth, size.iHeight);
       
   272 	frameInfoStrings->SetDimensionsL(info);
       
   273 
       
   274 	templte = resourceReader.ReadTPtrC();
       
   275 	info.Format(templte, iMaxValue);
       
   276 	frameInfoStrings->SetDepthL(info);
       
   277 
       
   278 	CDesCArrayFlat* resourceArray = resourceReader.ReadDesCArrayL();
       
   279 	CleanupStack::PushL(resourceArray);
       
   280 	TUint formatIndex = Compressed() ? 1 : 0;
       
   281 	info = (*resourceArray)[formatIndex];
       
   282 	CleanupStack::PopAndDestroy(resourceArray);
       
   283 	frameInfoStrings->SetDetailsL(info);
       
   284 	
       
   285 	CleanupStack::Pop(frameInfoStrings); 
       
   286 	CleanupStack::PopAndDestroy(2); // resourceInfo + resourceFile
       
   287 	return frameInfoStrings;
       
   288 	}
       
   289 
       
   290 void CPpmDecoder::GetExtensionL(TUid aExtUid, MImageConvExtension*& aExtPtr)
       
   291 	{
       
   292 	ASSERT(iExtensionManager);
       
   293 	if(!Compressed())
       
   294 		{
       
   295 		User::Leave(KErrNotSupported);
       
   296 		}
       
   297 	iExtensionManager->GetExtensionL(aExtUid, aExtPtr);
       
   298 	}
       
   299 
       
   300 void CPpmDecoder::SetClippingRectL(const TRect* aClipRect)
       
   301 	{
       
   302 	if(!Compressed())
       
   303 		{
       
   304 		User::Leave(KErrNotSupported);
       
   305 		}
       
   306 	
       
   307 	RPointerArray<TFrameInfo> frameInfo;
       
   308 	CleanupClosePushL(frameInfo);
       
   309 		
       
   310 	// PPM only has a single frame
       
   311 	frameInfo.AppendL(&FrameInfo(0));
       
   312 	
       
   313 	// The clipping operation is reset by passing a NULL rect pointer.
       
   314 	ASSERT(iExtensionManager);
       
   315 	iExtensionManager->SetClippingRectL(aClipRect, frameInfo);
       
   316 	CleanupStack::PopAndDestroy(); // frameInfo
       
   317 	}
       
   318 
       
   319 TInt CPpmDecoder::GetDestinationSize(TSize& aSize, TInt aFrameNumber)
       
   320 	{
       
   321 	ASSERT(iExtensionManager);
       
   322 	const TFrameInfo frameInfo = FrameInfo(aFrameNumber);
       
   323 	TSize originalSize = frameInfo.iOverallSizeInPixels;
       
   324 	TInt err = iExtensionManager->GetDestinationSize(originalSize);
       
   325 	if(err == KErrNone)
       
   326 		{
       
   327 		aSize = originalSize;
       
   328 		}
       
   329 	return err;
       
   330 	}
       
   331 
       
   332 // PNG encoder
       
   333 CPpmEncoder* CPpmEncoder::NewL()
       
   334 	{
       
   335 	return new(ELeave) CPpmEncoder;
       
   336 	}
       
   337 
       
   338 CPpmEncoder::CPpmEncoder()
       
   339 	{
       
   340 	}
       
   341 
       
   342 CPpmEncoder::~CPpmEncoder()
       
   343 	{
       
   344 	CImageEncoderPlugin::Cleanup();
       
   345 	}
       
   346 
       
   347 void CPpmEncoder::PrepareEncoderL(const CFrameImageData* /*aFrameImageData*/)
       
   348 	{
       
   349 	CPpm1WriteCodec* codec = CPpm1WriteCodec::NewL();
       
   350 	SetImageWriteCodec(codec);		// takes ownership of imageReadCodec
       
   351 	}
       
   352 
       
   353 void CPpmEncoder::UpdateHeaderL()
       
   354 	{
       
   355 	}
       
   356 
       
   357 #ifndef EKA2
       
   358 
       
   359 // DLL entry point
       
   360 GLDEF_C TInt E32Dll(TDllReason /*aReason*/)
       
   361 	{
       
   362 	return KErrNone;
       
   363 	}
       
   364 
       
   365 #endif // EKA2
       
   366