imaging/imagingplugins/codecs/BMPCodec/BMPConvert.cpp
changeset 0 5752a19fdefe
equal deleted inserted replaced
-1:000000000000 0:5752a19fdefe
       
     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 <gdi.h>
       
    17 #include <barsc.h>
       
    18 #include <barsread.h>
       
    19 #include <bautils.h>
       
    20 #include <imageconversion.h>
       
    21 #include "ImageClientMain.h"
       
    22 #include "ImageUtils.h"
       
    23 #include <101F45AE_extra.rsg>
       
    24 #include "BMPConvert.h"
       
    25 #include "icl/ICL_UIDS.hrh"
       
    26 #ifdef SYMBIAN_ENABLE_SPLIT_HEADERS
       
    27 #include <icl/icl_uids_const.hrh>
       
    28 #include <icl/icl_uids_def.hrh>
       
    29 #include <icl/imagecodecdef.h>
       
    30 #endif
       
    31 
       
    32 _LIT(KBMPPanicCategory, "BMPConvertPlugin");
       
    33 
       
    34 // Global panic function
       
    35 GLDEF_C void Panic(TIclPanic aError)
       
    36 	{
       
    37 	User::Panic(KBMPPanicCategory, aError);
       
    38 	}
       
    39 
       
    40 void WriteTUint32(TUint8*& aPtr, TUint32 aValue)
       
    41 	{
       
    42 	Mem::Copy(aPtr, &aValue, 4);
       
    43 	aPtr += 4;
       
    44 	}
       
    45 
       
    46 CBmpDecoder* CBmpDecoder::NewL()
       
    47 	{
       
    48 	return new(ELeave) CBmpDecoder;
       
    49 	}
       
    50 
       
    51 void CBmpDecoder::ImageType(TInt aFrameNumber, TUid& aImageType, TUid& aImageSubType) const
       
    52 	{
       
    53 	__ASSERT_ALWAYS(aFrameNumber == 0, Panic(EFrameNumberOutOfRange));
       
    54 	aImageType = KImageTypeBMPUid;
       
    55 	aImageSubType = KNullUid;
       
    56 	}
       
    57 
       
    58 CBmpDecoder::CBmpDecoder()
       
    59 	{
       
    60 	}
       
    61 
       
    62 CBmpDecoder::~CBmpDecoder()
       
    63 	{
       
    64 	Cleanup();
       
    65 	}
       
    66 
       
    67 void CBmpDecoder::ScanDataL()
       
    68 	{
       
    69 	ReadFormatL();
       
    70 
       
    71 	ASSERT(ImageReadCodec() == NULL);
       
    72 
       
    73 	TSize size = iBitmapHeader.iSizeInPixels;
       
    74 	TRgb* const palette = &(iPalette[0]);
       
    75 
       
    76 	CBmpReadCodec* imageReadCodec = NULL;
       
    77 
       
    78 
       
    79 	const TFrameInfo& imageInfo = ImageInfo();
       
    80 	switch(imageInfo.iFrameDisplayMode)
       
    81 		{
       
    82 	case EGray2:
       
    83 		imageReadCodec = CBmp1BppReadCodec::NewL(size, palette);
       
    84 		break;
       
    85 	case EColor16:
       
    86 		if (iBitmapHeader.iCompression == TBmpHeader::EFourBppRLE)
       
    87 			imageReadCodec = CBmpRLE4ReadCodec::NewL(size, palette);
       
    88 		else
       
    89 			imageReadCodec = CBmpNoComp4BppReadCodec::NewL(size, palette);
       
    90 		break;
       
    91 	case EColor256:
       
    92 		if (iBitmapHeader.iCompression == TBmpHeader::EEightBppRLE)
       
    93 			imageReadCodec = CBmpRLE8ReadCodec::NewL(size, palette);
       
    94 		else
       
    95 			imageReadCodec = CBmpNoComp8BppReadCodec::NewL(size, palette);
       
    96 		break;
       
    97 		
       
    98 	case EColor64K:
       
    99 		if (iBitmapHeader.iCompression == TBmpHeader::EBitFields)
       
   100 			{
       
   101 			imageReadCodec = CBmpBiRgbReadCodec::NewL(size, 2, palette);
       
   102 			}
       
   103 		else if (iBitmapHeader.iCompression == TBmpHeader::ENone)
       
   104 			{
       
   105 			imageReadCodec = CBmpBiRgbReadCodec::NewL(size, 2, NULL);
       
   106 			}
       
   107 		break;
       
   108 		
       
   109 	case EColor16M:
       
   110 		if(iBitmapHeader.iCompression == TBmpHeader::ENone)
       
   111 			{
       
   112 			imageReadCodec = CBmp24BppReadCodec::NewL(size);
       
   113 			}
       
   114 		break;
       
   115 
       
   116 	case EColor16MU:
       
   117 		if (iBitmapHeader.iCompression == TBmpHeader::EBitFields)
       
   118 			{
       
   119 			imageReadCodec = CBmpBiRgbReadCodec::NewL(size, 4, palette);
       
   120 			}
       
   121 		else if (iBitmapHeader.iCompression == TBmpHeader::ENone)
       
   122 			{
       
   123 			imageReadCodec = CBmp32BppReadCodec::NewL(size);
       
   124 			}
       
   125 		break;
       
   126 	default:
       
   127 		User::Leave(KErrNotSupported);
       
   128 		break;
       
   129 		}
       
   130 	
       
   131 	if (!imageReadCodec)
       
   132 		{
       
   133 		User::Leave(KErrNotSupported);
       
   134 		}
       
   135 
       
   136 	SetImageReadCodec(imageReadCodec);
       
   137 
       
   138 	ReadFrameHeadersL();
       
   139 	}
       
   140 
       
   141 void CBmpDecoder::ReadFormatL()
       
   142 	{
       
   143 	TPtrC8 bufferDes;
       
   144 
       
   145 	ReadDataL(0, bufferDes, (KBmpFileHeaderSize + KBmpInfoHeaderV1Size));
       
   146 
       
   147 	// Validate the header.
       
   148 	if (bufferDes.Length() < (KBmpFileHeaderSize + KBmpInfoHeaderV1Size))
       
   149 		User::Leave(KErrUnderflow);
       
   150 	
       
   151 	const TUint8* ptr = &bufferDes[0];
       
   152 	TUint16 sig = *ptr++;
       
   153 	sig |= (*ptr++ << 8);
       
   154 	if (sig != KBmpFileSignature)
       
   155 		User::Leave(KErrCorrupt);
       
   156 
       
   157 	// Read data length from header
       
   158 	TInt dataLength = PtrReadUtil::ReadUint32(ptr);
       
   159 	// If the data length is negative then return KErrCorrupt. Strictly speaking, 
       
   160 	// the data length is a 4-byte unsigned quantity (i.e. a TUint not a TInt) and 
       
   161 	// we should treat it as such, but this would involve an API change to SetDataLength()
       
   162 	// and changing iDataLength in CImageDecoderPriv.from a TInt to a TUint.
       
   163 	// Anyway it's reasonable to assume that if the length is > 0x7FFFFFFF, then
       
   164 	// the file is corrupt.
       
   165 	if (dataLength < (KBmpFileHeaderSize + KBmpInfoHeaderV1Size))
       
   166 		User::Leave(KErrCorrupt);
       
   167 
       
   168 	ptr += 4; // Increment pointer by 4 bytes
       
   169 
       
   170 	ptr += 4; // Skip bfReserved1 & bfReserved2
       
   171 
       
   172 	// Read image data offset from header
       
   173 	TInt imageDataOffset = PtrReadUtil::ReadUint32(ptr);
       
   174 	ptr += 4; // Increment pointer by 4 bytes
       
   175 
       
   176 	if(imageDataOffset < 0 || imageDataOffset > dataLength)
       
   177 		User::Leave(KErrCorrupt);
       
   178 	
       
   179 	dataLength -= imageDataOffset;
       
   180 	SetDataLength(dataLength);
       
   181 
       
   182 	TUint32 infoHeaderSize = PtrReadUtil::ReadUint32(ptr);
       
   183 	ptr += 4; // Skip biSize
       
   184 	if ((infoHeaderSize < KBmpInfoHeaderV1Size) || (infoHeaderSize > KBmpInfoMaxHeaderV2Size))
       
   185 		{
       
   186 		User::Leave(KErrCorrupt);
       
   187 		}
       
   188 
       
   189 	// Read image dimensions in pixels from header
       
   190 	if (infoHeaderSize == KBmpInfoHeaderV1Size)
       
   191 		{
       
   192 		iBitmapHeader.iSizeInPixels.iWidth = PtrReadUtil::ReadUint16(ptr);
       
   193 		ptr += 2;
       
   194 
       
   195 		iBitmapHeader.iSizeInPixels.iHeight = PtrReadUtil::ReadUint16(ptr);
       
   196 		ptr += 2;
       
   197 		}
       
   198 	else
       
   199 		{
       
   200 		iBitmapHeader.iSizeInPixels.iWidth = PtrReadUtil::ReadUint32(ptr);
       
   201 		ptr += 4; // Increment pointer by 4 bytes
       
   202 
       
   203 		iBitmapHeader.iSizeInPixels.iHeight = PtrReadUtil::ReadUint32(ptr);
       
   204 		ptr += 4; // Increment pointer by 4 bytes
       
   205 		}
       
   206 
       
   207 	// Check for valid image size
       
   208 	if (iBitmapHeader.iSizeInPixels.iWidth <= 0 || iBitmapHeader.iSizeInPixels.iHeight == 0)
       
   209 		User::Leave(KErrCorrupt);
       
   210 	//check for unsupported negative height
       
   211 	if(iBitmapHeader.iSizeInPixels.iHeight < 0)
       
   212 		{
       
   213 		User::Leave(KErrNotSupported);
       
   214 		}
       
   215 
       
   216 	if (infoHeaderSize > KBmpInfoHeaderV1Size)
       
   217 		{
       
   218 		// read the extra data associated with v2.x headers
       
   219 		ReadDataL((KBmpFileHeaderSize + KBmpInfoHeaderV1Size), bufferDes, (infoHeaderSize - KBmpInfoHeaderV1Size));
       
   220 		if (bufferDes.Length() < (infoHeaderSize - KBmpInfoHeaderV1Size))
       
   221 			{
       
   222 			User::Leave(KErrUnderflow);
       
   223 			}
       
   224 		ptr = &bufferDes[0];
       
   225 		}
       
   226 	
       
   227 	ptr += 2; // Skip biPlanes
       
   228 
       
   229 	// Read bits per pixel from header
       
   230 	iBitmapHeader.iBitCount = PtrReadUtil::ReadUint16(ptr);
       
   231 	ptr += 2; // Increment pointer by 2 bytes
       
   232 
       
   233 	// Check for valid bit count
       
   234 	if (iBitmapHeader.iBitCount < 1 || iBitmapHeader.iBitCount > 32)
       
   235 		User::Leave(KErrCorrupt);
       
   236 	TFrameInfo imageInfo;
       
   237 	imageInfo = ImageInfo();
       
   238 	imageInfo.iFrameSizeInTwips.iWidth = 0;
       
   239 	imageInfo.iFrameSizeInTwips.iHeight = 0;
       
   240 	TInt coloursUsed = 0;
       
   241 
       
   242 	if (infoHeaderSize == KBmpInfoHeaderV1Size)
       
   243 		{
       
   244 		// number of colours used equals number of bytes between the bitmap header and the bitmap data
       
   245 		// divided by the size of a single palette element
       
   246 		coloursUsed = (imageDataOffset - KBmpFileHeaderSize - KBmpInfoHeaderV1Size) / KBmpSizeofPaletteEntryV1;
       
   247 		}
       
   248 	else
       
   249 		{
       
   250 		// Read compresion type from header
       
   251 		TInt compression = PtrReadUtil::ReadUint32(ptr);
       
   252 		ptr += 4; // Increment pointer by 4 bytes
       
   253 		iBitmapHeader.iCompression = STATIC_CAST(TBmpHeader::TCompression, compression);
       
   254 
       
   255 		// Check for valid compression type
       
   256 		if (iBitmapHeader.iCompression != TBmpHeader::ENone &&
       
   257 			iBitmapHeader.iCompression != TBmpHeader::EEightBppRLE &&
       
   258 			iBitmapHeader.iCompression != TBmpHeader::EFourBppRLE &&
       
   259 			iBitmapHeader.iCompression != TBmpHeader::EBitFields)
       
   260 			User::Leave(KErrCorrupt);
       
   261 
       
   262 		TBmpCompression* compressionImageData = new(ELeave) TBmpCompression;
       
   263 		compressionImageData->iCompression = STATIC_CAST(TBmpCompression::TCompression, compression);
       
   264 		CleanupStack::PushL(compressionImageData);
       
   265 	
       
   266 		User::LeaveIfError(AppendImageData(compressionImageData));
       
   267 		CleanupStack::Pop();
       
   268 
       
   269 		//do not use image data size since it cannot be trusted
       
   270 		ptr += 4; // Skip biSizeImage
       
   271 
       
   272 		// Read Horizontal and vertical resolution
       
   273 		TInt xPelsPerMeter = PtrReadUtil::ReadInt32(ptr);
       
   274 		ptr += 4; // Increment pointer by 4 bytes
       
   275 	
       
   276 		TInt yPelsPerMeter = PtrReadUtil::ReadInt32(ptr);
       
   277 		ptr += 4; // Increment pointer by 4 bytes
       
   278 
       
   279 		TZoomFactor tempImageDevice;
       
   280 	
       
   281 		if (xPelsPerMeter > 0)
       
   282 			{
       
   283 			TInt64 twips = TInt64(iBitmapHeader.iSizeInPixels.iWidth) / TInt64(xPelsPerMeter);
       
   284 			twips *= 7200000;
       
   285 			twips /= 127;
       
   286 			imageInfo.iFrameSizeInTwips.iWidth = I64LOW(twips);
       
   287 			}
       
   288 	
       
   289 		if (yPelsPerMeter > 0)
       
   290 			{
       
   291 			TInt64 twips = TInt64(iBitmapHeader.iSizeInPixels.iHeight) / TInt64(yPelsPerMeter);
       
   292 			twips *= 7200000;
       
   293 			twips /= 127;
       
   294 			imageInfo.iFrameSizeInTwips.iHeight = I64LOW(twips);
       
   295 			}
       
   296 	
       
   297 		// Read colour count
       
   298 		coloursUsed = PtrReadUtil::ReadUint32(ptr); // read biClrUsed
       
   299 		ptr += 4; // Increment pointer by 4 bytes
       
   300 		}
       
   301 	if (coloursUsed == 0)
       
   302 		{
       
   303 		if (iBitmapHeader.iBitCount == 1)
       
   304 			{
       
   305 			iBitmapHeader.iPaletteEntries = 2;
       
   306 			}
       
   307 		else if (iBitmapHeader.iBitCount == 4)
       
   308 			{
       
   309 			iBitmapHeader.iPaletteEntries = 16;
       
   310 			}
       
   311 		else if (iBitmapHeader.iBitCount == 8)
       
   312 			{
       
   313 			iBitmapHeader.iPaletteEntries = 256;
       
   314 			}
       
   315 		else
       
   316 			{
       
   317 			iBitmapHeader.iPaletteEntries = 0; // 16bpp/24bpp/32bpp
       
   318 			}
       
   319 		}
       
   320 	else
       
   321 		{
       
   322 		if (iBitmapHeader.iBitCount == 24)
       
   323 			{
       
   324 			iBitmapHeader.iPaletteEntries = 0; // biClrUsed ignored for 24bpp
       
   325 			}
       
   326 		else
       
   327 			{
       
   328 			iBitmapHeader.iPaletteEntries = coloursUsed;
       
   329 			}
       
   330 		}
       
   331 		
       
   332 
       
   333 	// Check for valid palette entries.
       
   334 	if (iBitmapHeader.iPaletteEntries < 0 || iBitmapHeader.iPaletteEntries > KBmpMaxPaletteEntries)
       
   335 		{
       
   336 		User::Leave(KErrCorrupt);
       
   337 		}
       
   338 
       
   339 	imageInfo.iFrameCoordsInPixels.SetRect(TPoint(0, 0), iBitmapHeader.iSizeInPixels);
       
   340 	imageInfo.iOverallSizeInPixels = iBitmapHeader.iSizeInPixels;
       
   341 	imageInfo.iBitsPerPixel = iBitmapHeader.iBitCount;
       
   342 	imageInfo.iDelay = 0;
       
   343 	imageInfo.iFlags = TFrameInfo::ECanDither;
       
   344 
       
   345 
       
   346 	if (imageInfo.iBitsPerPixel <= 0)
       
   347 		User::Leave(KErrCorrupt);
       
   348 
       
   349 	if (imageInfo.iBitsPerPixel == 1)
       
   350 		{
       
   351 		imageInfo.iFrameDisplayMode = EGray2;
       
   352 		}
       
   353 	else
       
   354 		{
       
   355 		imageInfo.iFlags |= TFrameInfo::EColor;
       
   356 		if (imageInfo.iBitsPerPixel == 4)
       
   357 			{
       
   358 			imageInfo.iFrameDisplayMode = EColor16;
       
   359 			}
       
   360 		else if (imageInfo.iBitsPerPixel == 8)
       
   361 			{
       
   362 			imageInfo.iFrameDisplayMode = EColor256;
       
   363 			}
       
   364 		else if (imageInfo.iBitsPerPixel == 16)
       
   365 			{
       
   366 			imageInfo.iFrameDisplayMode = EColor64K;		
       
   367 			}
       
   368 		else if (imageInfo.iBitsPerPixel == 24)
       
   369 			{
       
   370 			imageInfo.iFrameDisplayMode = EColor16M;
       
   371 			}
       
   372 		else if (imageInfo.iBitsPerPixel == 32)
       
   373 			{
       
   374 			imageInfo.iFrameDisplayMode = EColor16MU;
       
   375 			}
       
   376 		else
       
   377 			{
       
   378 			User::Leave(KErrCorrupt);
       
   379 			}
       
   380 		}
       
   381 		
       
   382 	// in case of EBitFields image type we have to read the channel masks		
       
   383 	// EBitFields is not supported in V1
       
   384 	if ((infoHeaderSize > KBmpInfoHeaderV1Size) && (iBitmapHeader.iCompression == TBmpHeader::EBitFields))
       
   385 		{
       
   386 		TInt numMaskBytes = imageDataOffset - (KBmpFileHeaderSize + infoHeaderSize);
       
   387 		
       
   388 		if ((numMaskBytes <= 0) && (infoHeaderSize != KBmpInfoDefaultHeaderV2Size))
       
   389 			{
       
   390 			// infoHeaderSize is possibly incorrect - correct to standard size
       
   391 			infoHeaderSize = KBmpInfoDefaultHeaderV2Size;
       
   392 			numMaskBytes = imageDataOffset - (KBmpFileHeaderSize + infoHeaderSize);
       
   393 			}
       
   394 			
       
   395 		if (iBitmapHeader.iPaletteEntries || numMaskBytes <= 0 || 
       
   396 				(numMaskBytes % KBmpSizeofPaletteEntryV2) != 0)
       
   397 			{
       
   398 			User::Leave(KErrCorrupt);
       
   399 			}
       
   400 		iBitmapHeader.iPaletteEntries = numMaskBytes / KBmpSizeofPaletteEntryV2;
       
   401 		}
       
   402 		
       
   403 	if (iBitmapHeader.iPaletteEntries > KBmpMaxPaletteEntries)
       
   404 		{
       
   405 		User::Leave(KErrCorrupt);
       
   406 		}
       
   407 	
       
   408 	if (iBitmapHeader.iPaletteEntries > 0)
       
   409 		{
       
   410 		// Read the palette.
       
   411 		TInt paletteLength;
       
   412 		if (infoHeaderSize == KBmpInfoHeaderV1Size)
       
   413 			{
       
   414 			paletteLength = iBitmapHeader.iPaletteEntries * KBmpSizeofPaletteEntryV1;
       
   415 			}
       
   416 		else
       
   417 			{
       
   418 			paletteLength = iBitmapHeader.iPaletteEntries * KBmpSizeofPaletteEntryV2;
       
   419 			}
       
   420 
       
   421 		ReadDataL((KBmpFileHeaderSize + infoHeaderSize), bufferDes, paletteLength);
       
   422 
       
   423 		if (bufferDes.Length() < paletteLength)
       
   424 			User::Leave(KErrUnderflow);
       
   425 
       
   426 		const TUint8* ptr = &bufferDes[0];
       
   427 		for (TInt count = 0; count < iBitmapHeader.iPaletteEntries; count++)
       
   428 			{
       
   429 			TInt blue = ptr[0];
       
   430 			TInt green = ptr[1];
       
   431 			TInt red = ptr[2];
       
   432 			
       
   433 			// Bitmaps with a palette do not contain an alpha channel
       
   434 			iPalette[count] = TRgb(red, green, blue);
       
   435 			ptr += (infoHeaderSize == KBmpInfoHeaderV1Size ?
       
   436 					KBmpSizeofPaletteEntryV1 :
       
   437 					KBmpSizeofPaletteEntryV2); // Skips RGBQUAD reserved value which is mandated 0x00
       
   438 			}
       
   439 		}
       
   440  
       
   441 	SetImageInfo(imageInfo);
       
   442 	SetStartPosition(imageDataOffset);
       
   443 	}
       
   444 
       
   445 CFrameInfoStrings* CBmpDecoder::FrameInfoStringsL(RFs& aFs, TInt aFrameNumber)
       
   446 	{
       
   447 	const TUid KBmpCodecDllUid = {KBMPCodecDllUidValue};
       
   448 
       
   449 	RResourceFile resourceFile;
       
   450 	OpenExtraResourceFileLC(aFs, KBmpCodecDllUid, resourceFile);
       
   451 
       
   452 	HBufC8* resourceInfo = resourceFile.AllocReadLC(THEDECODERINFO);
       
   453 	TResourceReader resourceReader;
       
   454 	resourceReader.SetBuffer(resourceInfo);
       
   455 
       
   456 	TBuf<KCodecResourceStringMax> info;
       
   457 	TBuf<KCodecResourceStringMax> templte;
       
   458 
       
   459 	const TFrameInfo& frameInfo = FrameInfo(aFrameNumber);
       
   460 	const CFrameImageData& frameData = FrameData(aFrameNumber); 
       
   461 	CFrameInfoStrings* frameInfoStrings = CFrameInfoStrings::NewLC();
       
   462 	const TBmpCompression* compressionImageData = STATIC_CAST(const TBmpCompression*, frameData.GetImageData(0));
       
   463 
       
   464 	info = resourceReader.ReadTPtrC();
       
   465 	frameInfoStrings->SetDecoderL(info);
       
   466 
       
   467 	info = resourceReader.ReadTPtrC();
       
   468 	frameInfoStrings->SetFormatL(info);
       
   469 
       
   470 	TInt width = frameInfo.iOverallSizeInPixels.iWidth;
       
   471 	TInt height = frameInfo.iOverallSizeInPixels.iHeight;
       
   472 	TInt depth = frameInfo.iBitsPerPixel;
       
   473 
       
   474 	templte = resourceReader.ReadTPtrC();
       
   475 	info.Format(templte, width, height);
       
   476 	frameInfoStrings->SetDimensionsL(info);
       
   477 
       
   478 	CDesCArrayFlat* resourceArray = resourceReader.ReadDesCArrayL();
       
   479 	CleanupStack::PushL(resourceArray);
       
   480 	TUint formatIndex = (frameInfo.iFlags & TFrameInfo::EColor) ? 1 : 0;
       
   481 	templte = (*resourceArray)[formatIndex];
       
   482 	CleanupStack::PopAndDestroy(resourceArray);
       
   483 	info.Format(templte, depth);
       
   484 	frameInfoStrings->SetDepthL(info);
       
   485 
       
   486 	resourceArray = resourceReader.ReadDesCArrayL();
       
   487 	CleanupStack::PushL(resourceArray);
       
   488 	formatIndex = (compressionImageData->iCompression) ? 1 : 0;
       
   489 	info = (*resourceArray)[formatIndex];
       
   490 	CleanupStack::PopAndDestroy(resourceArray);
       
   491 	frameInfoStrings->SetDetailsL(info);
       
   492 	
       
   493 	CleanupStack::Pop(frameInfoStrings);
       
   494 	CleanupStack::PopAndDestroy(2); // resourceInfo + resourceFile
       
   495 	return frameInfoStrings;
       
   496 	}
       
   497 
       
   498 // Bmp encoder
       
   499 CBmpEncoder* CBmpEncoder::NewL()
       
   500 	{
       
   501 	return new(ELeave) CBmpEncoder;
       
   502 	}
       
   503 
       
   504 CBmpEncoder::CBmpEncoder()
       
   505 	{
       
   506 	}
       
   507 
       
   508 CBmpEncoder::~CBmpEncoder()
       
   509 	{
       
   510 	Cleanup();
       
   511 	}
       
   512 
       
   513 void CBmpEncoder::Cleanup()
       
   514 	{
       
   515 	// Delete any objects we should get rid of
       
   516 	delete iReadBuffer; iReadBuffer = NULL; // Created in DoConvert()
       
   517 	// Base class included
       
   518 	CImageEncoderPlugin::Cleanup();
       
   519 	}
       
   520 
       
   521 void CBmpEncoder::PrepareEncoderL(const CFrameImageData* aFrameImageData)
       
   522 	{
       
   523 	iBitmapHeader.iBitCount = 0;
       
   524 
       
   525 	TInt count = (aFrameImageData) ? aFrameImageData->ImageDataCount() : 0;
       
   526 
       
   527 	if (count == 0)
       
   528 		iBitmapHeader.iBitCount = 24;	// default value
       
   529 
       
   530 	for (TInt index = 0 ; index<count ; index++)
       
   531 		{	
       
   532 		const TImageDataBlock* encoderData = aFrameImageData->GetImageData(index);
       
   533 		if (encoderData->DataType() == KBMPImageDataUid)
       
   534 			{
       
   535 			if (iBitmapHeader.iBitCount != 0)
       
   536 				User::Leave(KErrCorrupt);
       
   537 
       
   538 			const TBmpImageData* bmpImageData = STATIC_CAST(const TBmpImageData*, encoderData);
       
   539 			iBitmapHeader.iBitCount = bmpImageData->iBitsPerPixel;
       
   540 			}
       
   541 		else
       
   542 			User::Leave(KErrCorrupt);
       
   543 		}
       
   544 
       
   545 	if (iBitmapHeader.iBitCount == 0)
       
   546 		User::Leave(KErrCorrupt);
       
   547 
       
   548 	ASSERT(ImageWriteCodec() == NULL);
       
   549 
       
   550 	CBmpWriteCodec* imageWriteCodec = NULL;
       
   551 	if (iBitmapHeader.iBitCount==24)
       
   552 		{
       
   553 		imageWriteCodec = CBmp24BppWriteCodec::NewL();
       
   554 		iBitmapHeader.iPaletteEntries = 0;
       
   555 		}
       
   556 	else if (iBitmapHeader.iBitCount==8)
       
   557 		{
       
   558 		imageWriteCodec =  CBmp8BppWriteCodec::NewL();
       
   559 		iBitmapHeader.iPaletteEntries = 256;
       
   560 		}
       
   561 	else if (iBitmapHeader.iBitCount==4)
       
   562 		{
       
   563 		imageWriteCodec =  CBmp4BppWriteCodec::NewL();
       
   564 		iBitmapHeader.iPaletteEntries = 16;
       
   565 		}
       
   566 	else if (iBitmapHeader.iBitCount==1)
       
   567 		{
       
   568 		imageWriteCodec =  CBmp1BppWriteCodec::NewL();
       
   569 		iBitmapHeader.iPaletteEntries = 2;
       
   570 		}
       
   571 	else
       
   572 		User::Leave(KErrNotSupported);
       
   573 
       
   574 	SetImageWriteCodec(imageWriteCodec);
       
   575 
       
   576 	StartPosition() = KBmpFileHeaderSize + KBmpInfoDefaultHeaderV2Size + (iBitmapHeader.iPaletteEntries * KBmpSizeofPaletteEntryV2);
       
   577 	iBitmapHeader.iCompression = TBmpHeader::ENone;
       
   578 	}
       
   579 
       
   580 void CBmpEncoder::UpdateHeaderL()
       
   581 	{
       
   582 	TInt headerSize = KBmpFileHeaderSize + KBmpInfoDefaultHeaderV2Size + (iBitmapHeader.iPaletteEntries * KBmpSizeofPaletteEntryV2);
       
   583 	if (iReadBuffer && (iReadBuffer->Length() != headerSize))
       
   584 		{
       
   585 		delete iReadBuffer;
       
   586 		iReadBuffer = NULL;
       
   587 		}
       
   588 	if (!iReadBuffer)
       
   589 		iReadBuffer = HBufC8::NewMaxL(headerSize);
       
   590 	TUint8* headerPtr = &iReadBuffer->Des()[0];
       
   591 
       
   592 	// BITMAPFILEHEADER
       
   593 	*headerPtr++ = TUint8(KBmpFileSignature & 0xff);
       
   594 	*headerPtr++ = TUint8(KBmpFileSignature >> 8);
       
   595 
       
   596 	TInt imageSize = CurrentImageSizeL();
       
   597 	WriteTUint32(headerPtr, imageSize);
       
   598 	WriteTUint32(headerPtr, 0);
       
   599 	WriteTUint32(headerPtr, headerSize);
       
   600 
       
   601 	// BITMAPINFOHEADER
       
   602 	WriteTUint32(headerPtr, KBmpInfoDefaultHeaderV2Size); // biSize
       
   603 	WriteTUint32(headerPtr, FrameInfoOverallSizeInPixels().iWidth); // biWidth
       
   604 	WriteTUint32(headerPtr, FrameInfoOverallSizeInPixels().iHeight); // biHeight
       
   605 	WriteTUint32(headerPtr, 1 | (iBitmapHeader.iBitCount << 16)); // biPlanes | (biBitCount << 16)
       
   606 	WriteTUint32(headerPtr, 0); // biCompression
       
   607 	WriteTUint32(headerPtr, 0); // biSizeImage
       
   608 	WriteTUint32(headerPtr, 0); // biXPelsPerMetre
       
   609 	WriteTUint32(headerPtr, 0); // biYPelsPerMetre
       
   610 	WriteTUint32(headerPtr, 0); // biClrUsed
       
   611 	WriteTUint32(headerPtr, 0); // biClrImportant
       
   612 
       
   613 	// RGBQUAD
       
   614 	if (iBitmapHeader.iBitCount == 1)
       
   615 		{
       
   616 		for (TInt count = 0; count < iBitmapHeader.iPaletteEntries; count++)
       
   617 			{
       
   618 			TRgb color(TRgb::Gray2(count));
       
   619 			headerPtr[0] = TUint8(color.Blue());
       
   620 			headerPtr[1] = TUint8(color.Green());
       
   621 			headerPtr[2] = TUint8(color.Red());
       
   622 			headerPtr[3] = 0;
       
   623 			headerPtr+=4;
       
   624 			}
       
   625 		}
       
   626 	if (iBitmapHeader.iBitCount == 4)
       
   627 		{
       
   628 		for (TInt count = 0; count < iBitmapHeader.iPaletteEntries; count++)
       
   629 			{
       
   630 			TRgb color(TRgb::Color16(count));
       
   631 			headerPtr[0] = TUint8(color.Blue());
       
   632 			headerPtr[1] = TUint8(color.Green());
       
   633 			headerPtr[2] = TUint8(color.Red());
       
   634 			headerPtr[3] = 0;
       
   635 			headerPtr+=4;
       
   636 			}
       
   637 		}
       
   638 	else if (iBitmapHeader.iBitCount == 8)
       
   639 		{
       
   640 		for (TInt count = 0; count < iBitmapHeader.iPaletteEntries; count++)
       
   641 			{
       
   642 			TRgb color(TRgb::Color256(count));
       
   643 			headerPtr[0] = TUint8(color.Blue());
       
   644 			headerPtr[1] = TUint8(color.Green());
       
   645 			headerPtr[2] = TUint8(color.Red());
       
   646 			headerPtr[3] = 0;
       
   647 			headerPtr+=4;
       
   648 			}
       
   649 		}
       
   650 
       
   651 	TPtr8 bufferDes(iReadBuffer->Des());
       
   652 	WriteDataL(0,bufferDes);
       
   653 	}
       
   654