os/graphics/fbs/fontandbitmapserver/sfbs/BITCOMP.CPP
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/graphics/fbs/fontandbitmapserver/sfbs/BITCOMP.CPP	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,2233 @@
     1.4 +// Copyright (c) 1995-2009 Nokia Corporation and/or its subsidiary(-ies).
     1.5 +// All rights reserved.
     1.6 +// This component and the accompanying materials are made available
     1.7 +// under the terms of "Eclipse Public License v1.0"
     1.8 +// which accompanies this distribution, and is available
     1.9 +// at the URL "http://www.eclipse.org/legal/epl-v10.html".
    1.10 +//
    1.11 +// Initial Contributors:
    1.12 +// Nokia Corporation - initial contribution.
    1.13 +//
    1.14 +// Contributors:
    1.15 +//
    1.16 +// Description:
    1.17 +//
    1.18 +
    1.19 +#include <f32file.h>
    1.20 +#include <s32file.h>
    1.21 +#include <fbs.h>
    1.22 +#include <bitmap.h>
    1.23 +#include "UTILS.H"
    1.24 +#include <e32hashtab.h>
    1.25 +
    1.26 +GLREF_C void Panic(TFbsPanic aPanic);
    1.27 +
    1.28 +void CBitwiseBitmap::DoInternalizeCompressedDataL(RReadStream& aStream,TInt aSrceSizeInBytes,TUint32* aBase,TBitmapfileCompression aCompression)
    1.29 +	{
    1.30 +	TInt destSizeInBytes = iHeader.iSizeInPixels.iHeight * iByteWidth;
    1.31 +	TUint8* destPtr = (TUint8*)aBase;
    1.32 +
    1.33 +	// try to allocate memory for the compressed bitmap
    1.34 +	TUint8* decompressionBuffer = (TUint8*)User::Alloc(aSrceSizeInBytes);
    1.35 +
    1.36 +	if (decompressionBuffer)
    1.37 +		{
    1.38 +		// read the compressed bitmap, then decompress
    1.39 +		CleanupStack::PushL(decompressionBuffer);
    1.40 +		aStream.ReadL(decompressionBuffer,aSrceSizeInBytes);
    1.41 +		switch (aCompression)
    1.42 +			{
    1.43 +		case EByteRLECompression:
    1.44 +			DoDecompressByteData(destPtr,destSizeInBytes,decompressionBuffer,aSrceSizeInBytes);
    1.45 +			break;
    1.46 +		case ETwelveBitRLECompression:
    1.47 +			DoDecompressTwelveBitData(destPtr,destSizeInBytes,decompressionBuffer,aSrceSizeInBytes);
    1.48 +			break;
    1.49 +		case ESixteenBitRLECompression:
    1.50 +			DoDecompressSixteenBitData(destPtr,destSizeInBytes,decompressionBuffer,aSrceSizeInBytes);
    1.51 +			break;
    1.52 +		case ETwentyFourBitRLECompression:
    1.53 +			DoDecompressTwentyFourBitData(destPtr,destSizeInBytes,decompressionBuffer,aSrceSizeInBytes);
    1.54 +			break;
    1.55 +		case EThirtyTwoUBitRLECompression:
    1.56 +			DoDecompressThirtyTwoUBitData(destPtr,destSizeInBytes,decompressionBuffer,aSrceSizeInBytes);
    1.57 +			break;
    1.58 +		case EThirtyTwoABitRLECompression:
    1.59 +			DoDecompressThirtyTwoABitData(destPtr,destSizeInBytes,decompressionBuffer,aSrceSizeInBytes);
    1.60 +			break;
    1.61 +		default:
    1.62 +			break;
    1.63 +			}
    1.64 +		CleanupStack::PopAndDestroy(); // decompressionBuffer
    1.65 +		return;
    1.66 +		}
    1.67 +
    1.68 +	// not enough heap to pre-load the source bitmap
    1.69 +	switch (aCompression)
    1.70 +		{
    1.71 +	case EByteRLECompression:
    1.72 +		DoDecompressByteDataAltL(aStream,aSrceSizeInBytes,aBase);
    1.73 +		break;
    1.74 +	case ETwelveBitRLECompression:
    1.75 +		DoDecompressTwelveBitDataAltL(aStream,aSrceSizeInBytes,aBase);
    1.76 +		break;
    1.77 +	case ESixteenBitRLECompression:
    1.78 +		DoDecompressSixteenBitDataAltL(aStream,aSrceSizeInBytes,aBase);
    1.79 +		break;
    1.80 +	case ETwentyFourBitRLECompression:
    1.81 +		DoDecompressTwentyFourBitDataAltL(aStream,aSrceSizeInBytes,aBase);
    1.82 +		break;
    1.83 +	case EThirtyTwoUBitRLECompression:
    1.84 +		DoDecompressThirtyTwoUBitDataAltL(aStream,aSrceSizeInBytes,aBase);
    1.85 +		break;
    1.86 +	case EThirtyTwoABitRLECompression:
    1.87 +		DoDecompressThirtyTwoABitDataAltL(aStream,aSrceSizeInBytes,aBase);
    1.88 +		break;
    1.89 +	default:
    1.90 +		break;
    1.91 +		}
    1.92 +	}
    1.93 +
    1.94 +void CBitwiseBitmap::DoDecompressByteData(TUint8* aDestBuffer,TInt aDestSize,TUint8* aSrceBuffer,TInt aSrceSize)
    1.95 +	{
    1.96 +	TUint8* srcePtr = aSrceBuffer;
    1.97 +	TUint8* destPtr = aDestBuffer;
    1.98 +	TUint8* srcePtrLimit = aSrceBuffer + aSrceSize;
    1.99 +	TUint8* destPtrLimit = aDestBuffer + aDestSize;
   1.100 +
   1.101 +	while (srcePtr < srcePtrLimit && destPtr < destPtrLimit)
   1.102 +		{
   1.103 +		TInt8 count = *srcePtr++;
   1.104 +
   1.105 +		if (count >= 0)
   1.106 +			{
   1.107 +			const TInt numBytes = Min(count + 1, destPtrLimit - destPtr);
   1.108 +			Mem::Fill(destPtr,numBytes,*srcePtr++);
   1.109 +			destPtr += numBytes;
   1.110 +			}
   1.111 +		else
   1.112 +			{
   1.113 +			const TInt numBytes = Min(-count, destPtrLimit - destPtr);
   1.114 +			Mem::Copy(destPtr,srcePtr,numBytes);
   1.115 +			srcePtr += numBytes;
   1.116 +			destPtr += numBytes;
   1.117 +			}
   1.118 +		}
   1.119 +	__ASSERT_DEBUG(srcePtr == srcePtrLimit && destPtr == destPtrLimit,Panic(EFbsBitmapDecompressionError));
   1.120 +	}
   1.121 +
   1.122 +void CBitwiseBitmap::DoDecompressByteDataAltL(RReadStream& aStream,TInt aSrceSizeInBytes,TUint32* aBase)
   1.123 +	{
   1.124 +	TInt destSizeInBytes = iHeader.iSizeInPixels.iHeight * iByteWidth;
   1.125 +	TUint8* destPtr = (TUint8*)aBase;
   1.126 +	TUint8* destPtrLimit = destPtr + destSizeInBytes;
   1.127 +
   1.128 +	while(aSrceSizeInBytes > 0 && destPtr < destPtrLimit)
   1.129 +		{
   1.130 +		TInt8 count = aStream.ReadInt8L();
   1.131 +		aSrceSizeInBytes--;
   1.132 +
   1.133 +		if (count >= 0)
   1.134 +			{
   1.135 +			const TInt numBytes = Min(count + 1, destPtrLimit - destPtr);
   1.136 +			TUint8 value = aStream.ReadUint8L();
   1.137 +			aSrceSizeInBytes--;
   1.138 +			Mem::Fill(destPtr,numBytes,value);
   1.139 +			destPtr += numBytes;
   1.140 +			}
   1.141 +		else
   1.142 +			{
   1.143 +			const TInt numBytes = Min(-count, destPtrLimit - destPtr);
   1.144 +			aStream.ReadL(destPtr,numBytes);
   1.145 +			aSrceSizeInBytes -= numBytes;
   1.146 +			destPtr += numBytes;
   1.147 +			}
   1.148 +		}
   1.149 +	__ASSERT_DEBUG(aSrceSizeInBytes == 0 && destPtr == destPtrLimit,Panic(EFbsBitmapDecompressionError));
   1.150 +	}
   1.151 +
   1.152 +/** Fills the 32bit pixels into the destination pointer
   1.153 +This method uses the concept of Duff's Device 
   1.154 +@param aDestPtr32 pointer to 32bit destination buffer.
   1.155 +@param aCount Number of 32bit pixels to be copied.
   1.156 +@param aValue32 32bit pixel data. */
   1.157 +void CBitwiseBitmap::BitmapFill32(TUint32* aDestPtr32, TInt aCount, TUint32 aValue32)
   1.158 +	{
   1.159 +	__ASSERT_DEBUG(aCount > 0, Panic(EFbsBitmapDecompressionError));
   1.160 +	if (aCount > 0)
   1.161 +		{ // for performance loop is unrolled, using "Duff's Device" pattern
   1.162 +		TInt blocksOf16 = aCount / 16;	// number of blocks of 16 full words to write
   1.163 +		// the first iteration writes 1 to 15 words
   1.164 +
   1.165 +		switch (aCount & 0x0f)
   1.166 +			{ // note that case statements intentionally cascade
   1.167 +		case 0:
   1.168 +write16dblPixels:	// second and subsequent iterations always write 16 words
   1.169 +			--blocksOf16;
   1.170 +			*aDestPtr32++ = aValue32;
   1.171 +			//coverity [fallthrough]
   1.172 +		case 15:
   1.173 +			*aDestPtr32++ = aValue32;
   1.174 +			//coverity [fallthrough]
   1.175 +		case 14:
   1.176 +			*aDestPtr32++ = aValue32;
   1.177 +			//coverity [fallthrough]
   1.178 +		case 13:
   1.179 +			*aDestPtr32++ = aValue32;
   1.180 +			//coverity [fallthrough]
   1.181 +		case 12:
   1.182 +			*aDestPtr32++ = aValue32;
   1.183 +			//coverity [fallthrough]
   1.184 +		case 11:
   1.185 +			*aDestPtr32++ = aValue32;
   1.186 +			//coverity [fallthrough]
   1.187 +		case 10:
   1.188 +			*aDestPtr32++ = aValue32;
   1.189 +			//coverity [fallthrough]
   1.190 +		case 9:
   1.191 +			*aDestPtr32++ = aValue32;
   1.192 +			//coverity [fallthrough]
   1.193 +		case 8:
   1.194 +			*aDestPtr32++ = aValue32;
   1.195 +			//coverity [fallthrough]
   1.196 +		case 7:
   1.197 +			*aDestPtr32++ = aValue32;
   1.198 +			//coverity [fallthrough]
   1.199 +		case 6:
   1.200 +			*aDestPtr32++ = aValue32;
   1.201 +			//coverity [fallthrough]
   1.202 +		case 5:
   1.203 +			*aDestPtr32++ = aValue32;
   1.204 +			//coverity [fallthrough]
   1.205 +		case 4:
   1.206 +			*aDestPtr32++ = aValue32;
   1.207 +			//coverity [fallthrough]
   1.208 +		case 3:
   1.209 +			*aDestPtr32++ = aValue32;
   1.210 +			//coverity [fallthrough]
   1.211 +		case 2:
   1.212 +			*aDestPtr32++ = aValue32;
   1.213 +			//coverity [fallthrough]
   1.214 +		case 1:
   1.215 +			*aDestPtr32++ = aValue32;
   1.216 +			}
   1.217 +
   1.218 +		if (blocksOf16 > 0)
   1.219 +			{
   1.220 +			goto write16dblPixels;
   1.221 +			}
   1.222 +		}
   1.223 +	}
   1.224 +
   1.225 +/** Fills the 16bit pixels into the destination pointer
   1.226 +This method uses the concept of Duff's Device 
   1.227 +@param aDestPtr16 pointer to 16bit destination buffer.
   1.228 +@param aCount Number of 16bit pixels to be copied.
   1.229 +@param aValue16 16bit pixel data. */
   1.230 +inline void CBitwiseBitmap::BitmapFill16(TUint16* aDestPtr16, TInt aCount, TUint16 aValue16)
   1.231 +	{
   1.232 +	// Call the 32-bit fill method if there at least 8 pixels to fill
   1.233 +	if (aCount >= 8)
   1.234 +		{
   1.235 +		// first pixel is on half word address?
   1.236 +		if (reinterpret_cast<TUint32>(aDestPtr16) & 2) 
   1.237 +			{
   1.238 +			*aDestPtr16++ = aValue16; 
   1.239 +			--aCount; 
   1.240 +			} 
   1.241 +
   1.242 +		// destPtr16 is now full Word aligned
   1.243 +		const TInt countDoublePixels = aCount / 2;
   1.244 +		TUint32* destPtr32 = (TUint32*) aDestPtr16;
   1.245 +		BitmapFill32(destPtr32, countDoublePixels, (aValue16 << 16) | aValue16);
   1.246 +
   1.247 +		aCount -= countDoublePixels * 2;
   1.248 +		if (aCount == 1)
   1.249 +			{ // last pixel is on a half-word
   1.250 +			aDestPtr16 += countDoublePixels * 2;
   1.251 +			*aDestPtr16++ = aValue16; 
   1.252 +			}
   1.253 +		}
   1.254 +	else
   1.255 +		{ // less than 8 pixels to fill
   1.256 +		switch (aCount)
   1.257 +			{ // note that case statements intentionally cascade
   1.258 +		case 7:
   1.259 +			*aDestPtr16++ = aValue16; 
   1.260 +		case 6:
   1.261 +			*aDestPtr16++ = aValue16; 
   1.262 +		case 5:
   1.263 +			*aDestPtr16++ = aValue16; 
   1.264 +		case 4:
   1.265 +			*aDestPtr16++ = aValue16; 
   1.266 +		case 3:
   1.267 +			*aDestPtr16++ = aValue16; 
   1.268 +		case 2:
   1.269 +			*aDestPtr16++ = aValue16; 
   1.270 +		case 1:
   1.271 +			*aDestPtr16++ = aValue16; 
   1.272 +		case 0: // nothing to do
   1.273 +			;
   1.274 +			}
   1.275 +		}
   1.276 +	}
   1.277 +
   1.278 +void CBitwiseBitmap::DoDecompressTwelveBitData(TUint8* aDestBuffer,TInt aDestSize,TUint8* aSrceBuffer,TInt aSrceSize)
   1.279 +	{
   1.280 +	TUint16* srcePtr = (TUint16*)aSrceBuffer;
   1.281 +	TUint16* destPtr = (TUint16*)aDestBuffer;
   1.282 +	TUint16* srcePtrLimit = srcePtr + (aSrceSize / 2);
   1.283 +	TUint16* destPtrLimit = destPtr + (aDestSize / 2);
   1.284 +
   1.285 +	while (srcePtr < srcePtrLimit && destPtr < destPtrLimit)
   1.286 +		{
   1.287 +		TUint16 value = *srcePtr++;
   1.288 +		TInt count = Min(value >> 12, destPtrLimit - destPtr);
   1.289 +		value &= 0x0fff;
   1.290 +
   1.291 +		while (count >= 0)
   1.292 +			{
   1.293 +			*destPtr++ = value;
   1.294 +			count--;
   1.295 +			}
   1.296 +		}
   1.297 +
   1.298 +	__ASSERT_DEBUG(srcePtr == srcePtrLimit && destPtr == destPtrLimit,Panic(EFbsBitmapDecompressionError));
   1.299 +	}
   1.300 +
   1.301 +void CBitwiseBitmap::DoDecompressTwelveBitDataAltL(RReadStream& aStream,TInt aSrceSizeInBytes,TUint32* aBase)
   1.302 +	{
   1.303 +	TInt destSizeInBytes = iHeader.iSizeInPixels.iHeight * iByteWidth;
   1.304 +	TUint16* destPtr = (TUint16*)aBase;
   1.305 +	TUint16* destPtrLimit = destPtr + (destSizeInBytes / 2);
   1.306 +
   1.307 +	while(aSrceSizeInBytes > 0 && destPtr < destPtrLimit)
   1.308 +		{
   1.309 +		TUint16 value = aStream.ReadUint16L();
   1.310 +		TInt count = Min(value >> 12, destPtrLimit - destPtr);
   1.311 +		value &= 0x0fff;
   1.312 +		aSrceSizeInBytes -= 2;
   1.313 +
   1.314 +		while (count >= 0)
   1.315 +			{
   1.316 +			*destPtr++ = value;
   1.317 +			count--;
   1.318 +			}
   1.319 +		}
   1.320 +	__ASSERT_DEBUG(aSrceSizeInBytes == 0 && destPtr == destPtrLimit,Panic(EFbsBitmapDecompressionError));
   1.321 +	}
   1.322 +
   1.323 +/** The function decodes 24-bit compressed buffer to the 16-bit stream with unused top bytes by using RLE compression algorithm*/
   1.324 +void CBitwiseBitmap::DoDecompressSixteenBitData(TUint8* aDestBuffer,TInt aDestSize,TUint8* aSrceBuffer,TInt aSrceSize)
   1.325 +	{
   1.326 +	__ASSERT_DEBUG((reinterpret_cast<TUint32>(aDestBuffer) & 0x2)==0,Panic(EFbsBitmapDecompressionError)); // make sure the start address is half-word aligned
   1.327 +	__ASSERT_DEBUG((aDestSize & 0x2)==0,Panic(EFbsBitmapDecompressionError)); // make sure the start address is half-word aligned
   1.328 +
   1.329 +	TUint8* srcePtr8 = aSrceBuffer; // pointer to compressed source Byte(8-bit) stream 
   1.330 +	TUint16* destPtr16 = reinterpret_cast<TUint16*>(aDestBuffer); //pointer to uncompressed destination HalfWord(16-bit) pixel stream
   1.331 +	TUint8* srcePtrLimit8 = aSrceBuffer + aSrceSize; //maximum number of compressed source bytes
   1.332 +	TUint16* destPtrLimit16 = reinterpret_cast<TUint16*>(aDestBuffer + aDestSize); //maximum number of uncompressed destination pixel stream
   1.333 +	const TInt KPixelSize = 2; //number of bytes of the source stream that is to be considered as one Pixel
   1.334 +	
   1.335 +	while (srcePtr8 < srcePtrLimit8 && destPtr16 < destPtrLimit16)
   1.336 +		{
   1.337 +		TInt8 count = *srcePtr8++; //number of pixels to be retrieved from the source stream
   1.338 +
   1.339 +		if (count >= 0) //repeating number of pixels
   1.340 +			{
   1.341 +			const TInt numPixels = 1 + Min(count, (destPtrLimit16 - destPtr16));
   1.342 +			const TUint8 lowByte = *srcePtr8++;
   1.343 +			const TUint8 highByte = *srcePtr8++;
   1.344 +			const TUint16 pixel = highByte<<8 | lowByte; //Pixel buffer which needs to be stored in destPtr16
   1.345 +
   1.346 +			BitmapFill16(destPtr16, numPixels, pixel);
   1.347 +			destPtr16 += numPixels;
   1.348 +			}
   1.349 +		else
   1.350 +			{
   1.351 +			const TInt numPixels = Min(-count, destPtrLimit16 - destPtr16); //number of pixels to be copied
   1.352 +			const TInt numBytes = numPixels * KPixelSize; //number of bytes needs to be retrieved from the srcePtr
   1.353 +			Mem::Copy(destPtr16, srcePtr8, numBytes); // copy bytes
   1.354 +			srcePtr8 += numBytes; //incrementing the srcePtr by number of bytes
   1.355 +			destPtr16 += numPixels; //incrementing the destPtr16 by number of pixels
   1.356 +			}
   1.357 +		}
   1.358 +	__ASSERT_DEBUG(srcePtr8 == srcePtrLimit8 && destPtr16 == destPtrLimit16, Panic(EFbsBitmapDecompressionError));
   1.359 +	}
   1.360 +
   1.361 +/** The alternative decoding function which decompresses 24-bit buffer to the 16-bit stream with unused top bytes 
   1.362 + 	by using RLE compression algorithm. The function is used under low memory conditions. */
   1.363 +void CBitwiseBitmap::DoDecompressSixteenBitDataAltL(RReadStream& aStream,TInt aSrceSizeInBytes,TUint32* aBase)
   1.364 +	{
   1.365 +	__ASSERT_DEBUG((reinterpret_cast<TUint32>(aBase) & 0x2)==0,Panic(EFbsBitmapDecompressionError)); // make sure the start address is half-word aligned
   1.366 +
   1.367 +	TInt destSizeInPixels = (iHeader.iSizeInPixels.iHeight * iByteWidth); //size of destination Byte pixel buffers
   1.368 +	destSizeInPixels >>= 1; //dividing by two, to get size of destination interms of Halfword pixel buffers
   1.369 +	TUint16* destPtr16 = reinterpret_cast<TUint16*>(aBase); //pointer to uncompressed destination HalfWord(16-bit) pixel stream
   1.370 +	TUint16* destPtrLimit16 = destPtr16 + destSizeInPixels; //maximum number of compressed source bytes
   1.371 +	const TInt KPixelSize = 2; //number of bytes of the source stream that is to be considered as one Pixel
   1.372 +
   1.373 +	while(aSrceSizeInBytes > 0 && destPtr16 < destPtrLimit16)
   1.374 +		{
   1.375 +		TInt8 count = aStream.ReadInt8L(); //number of pixels to be retrieved from the source stream
   1.376 +		aSrceSizeInBytes--; //One byte from source stream read, hence reduce size by one Byte
   1.377 +
   1.378 +		if (count >= 0)
   1.379 +			{
   1.380 +			const TInt numPixels = 1 + Min(count, (destPtrLimit16 - destPtr16));
   1.381 +			const TUint16 pixel = aStream.ReadUint16L(); //Pixel buffer which needs to be stored in destPtr16
   1.382 +			aSrceSizeInBytes -= KPixelSize; //Two bytes (Halfword pixel) read, reduce size by two Bytes
   1.383 +			
   1.384 +			BitmapFill16(destPtr16, numPixels, pixel);
   1.385 +			destPtr16 += numPixels;
   1.386 +			}
   1.387 +		else
   1.388 +			{
   1.389 +			const TInt numPixels = Min(-count, destPtrLimit16 - destPtr16); //number of pixels to be stored into destPtr16
   1.390 +			const TInt numBytes = numPixels * KPixelSize; //number of bytes needs to be retrieved from the srcePtr
   1.391 +			aStream.ReadL(destPtr16, numPixels); //read TUint16's
   1.392 +			aSrceSizeInBytes -= numBytes; //two-byte (halfword) pixel buffers read, reduce by number of bytes
   1.393 +			destPtr16 += numPixels; //incrementing the destPtr16 by number of pixels
   1.394 +			}
   1.395 +		}
   1.396 +	__ASSERT_DEBUG(aSrceSizeInBytes == 0 && destPtr16 == destPtrLimit16, Panic(EFbsBitmapDecompressionError));
   1.397 +	}
   1.398 +
   1.399 +void CBitwiseBitmap::DoDecompressTwentyFourBitData(TUint8* aDestBuffer,TInt aDestSize,TUint8* aSrceBuffer,TInt aSrceSize)
   1.400 +	{
   1.401 +	TUint8* srcePtr = aSrceBuffer;
   1.402 +	TUint8* destPtr = aDestBuffer;
   1.403 +	TUint8* srcePtrLimit = aSrceBuffer + aSrceSize;
   1.404 +	TUint8* destPtrLimit = aDestBuffer + aDestSize;
   1.405 +
   1.406 +	while (srcePtr < srcePtrLimit && destPtr < destPtrLimit)
   1.407 +		{
   1.408 +		TInt8 count = *srcePtr++;
   1.409 +
   1.410 +		if (count >= 0)
   1.411 +			{
   1.412 +			count = Min(count, (destPtrLimit - destPtr) / 3);
   1.413 +			TUint8 component1 = *srcePtr++;
   1.414 +			TUint8 component2 = *srcePtr++;
   1.415 +			TUint8 component3 = *srcePtr++;
   1.416 +
   1.417 +			while (count >= 0)
   1.418 +				{
   1.419 +				*destPtr++ = component1;
   1.420 +				*destPtr++ = component2;
   1.421 +				*destPtr++ = component3;
   1.422 +				count--;
   1.423 +				}
   1.424 +			}
   1.425 +		else
   1.426 +			{
   1.427 +			const TInt numBytes = Min(count * -3, destPtrLimit - destPtr);
   1.428 +			Mem::Copy(destPtr,srcePtr,numBytes);
   1.429 +			srcePtr += numBytes;
   1.430 +			destPtr += numBytes;
   1.431 +			}
   1.432 +		}
   1.433 +	__ASSERT_DEBUG(srcePtr == srcePtrLimit && destPtr == destPtrLimit,Panic(EFbsBitmapDecompressionError));
   1.434 +	}
   1.435 +
   1.436 +void CBitwiseBitmap::DoDecompressTwentyFourBitDataAltL(RReadStream& aStream,TInt aSrceSizeInBytes,TUint32* aBase)
   1.437 +	{
   1.438 +	TInt destSizeInBytes = iHeader.iSizeInPixels.iHeight * iByteWidth;
   1.439 +	TUint8* destPtr = (TUint8*)aBase;
   1.440 +	TUint8* destPtrLimit = destPtr + destSizeInBytes;
   1.441 +
   1.442 +	while(aSrceSizeInBytes > 0 && destPtr < destPtrLimit)
   1.443 +		{
   1.444 +		TInt8 count = aStream.ReadInt8L();
   1.445 +		aSrceSizeInBytes--;
   1.446 +
   1.447 +		if (count >= 0)
   1.448 +			{
   1.449 +			count = Min(count, (destPtrLimit - destPtr) / 3);
   1.450 +			TUint8 component1 = aStream.ReadUint8L();
   1.451 +			TUint8 component2 = aStream.ReadUint8L();
   1.452 +			TUint8 component3 = aStream.ReadUint8L();
   1.453 +			aSrceSizeInBytes -= 3;
   1.454 +
   1.455 +			while (count >= 0)
   1.456 +				{
   1.457 +				*destPtr++ = component1;
   1.458 +				*destPtr++ = component2;
   1.459 +				*destPtr++ = component3;
   1.460 +				count--;
   1.461 +				}
   1.462 +			}
   1.463 +		else
   1.464 +			{
   1.465 +			const TInt numBytes = Min(count * -3, destPtrLimit - destPtr);
   1.466 +			aStream.ReadL(destPtr, numBytes);
   1.467 +			aSrceSizeInBytes -= numBytes;
   1.468 +			destPtr += numBytes;
   1.469 +			}
   1.470 +		}
   1.471 +	__ASSERT_DEBUG(aSrceSizeInBytes == 0 && destPtr == destPtrLimit,Panic(EFbsBitmapDecompressionError));
   1.472 +	}
   1.473 +
   1.474 +/** The function decodes 24-bit compressed buffer to the 32-bit stream with unused top bytes by using RLE compression algorithm*/
   1.475 +void CBitwiseBitmap::DoDecompressThirtyTwoUBitData(TUint8* aDestBuffer,TInt aDestSize,TUint8* aSrceBuffer,TInt aSrceSize)
   1.476 +	{
   1.477 +	
   1.478 +	__ASSERT_DEBUG((reinterpret_cast<TUint32>(aDestBuffer) & 0x3)==0,Panic(EFbsBitmapDecompressionError)); // make sure the start address is word aligned
   1.479 +	__ASSERT_DEBUG((aDestSize & 0x3)==0,Panic(EFbsBitmapDecompressionError)); // make sure the size is whole no of pixels
   1.480 +	
   1.481 +	TUint8* srcePtr8 = aSrceBuffer;
   1.482 +	TUint8* srcePtrLimit8 = aSrceBuffer + aSrceSize;
   1.483 +
   1.484 +	TUint32* destPtr32 = reinterpret_cast<TUint32*>(aDestBuffer);
   1.485 +	TUint32* destPtrLimit32 = reinterpret_cast<TUint32*>(aDestBuffer + aDestSize);
   1.486 +
   1.487 +	while (srcePtr8 < srcePtrLimit8 && destPtr32 < destPtrLimit32)
   1.488 +		{
   1.489 +		
   1.490 +		TInt8 count = *srcePtr8++; // important to read into a byte variable
   1.491 +
   1.492 +		if (count >= 0) //repeating pixel buffer
   1.493 +			{
   1.494 +			count  = Min(count, (destPtrLimit32 - destPtr32));
   1.495 +			const TUint8 component1 = *srcePtr8++;
   1.496 +			const TUint8 component2 = *srcePtr8++;
   1.497 +			const TUint8 component3 = *srcePtr8++;
   1.498 +
   1.499 +			const TUint32 pixel = 0xff000000 | (component3<<16) | (component2<<8) | component1;
   1.500 +
   1.501 +			// for performance loop is unrolled, using "Duff's Device" pattern
   1.502 +			TInt blocksOf16 = (count >> 4);	// number of blocks of 16 full words to write
   1.503 +			// the first iteration writes 1 to 15 words
   1.504 +			switch (count & 0x0f)
   1.505 +				{ // note that case statements intentionally cascade
   1.506 +			case 15:
   1.507 +				do {	// second and subsequent iterations always write 16 words
   1.508 +				*destPtr32++ = pixel;
   1.509 +			case 14:
   1.510 +				*destPtr32++ = pixel;
   1.511 +			case 13:
   1.512 +				*destPtr32++ = pixel;
   1.513 +			case 12:
   1.514 +				*destPtr32++ = pixel;
   1.515 +			case 11:
   1.516 +				*destPtr32++ = pixel;
   1.517 +			case 10:
   1.518 +				*destPtr32++ = pixel;
   1.519 +			case 9:
   1.520 +				*destPtr32++ = pixel;
   1.521 +			case 8:
   1.522 +				*destPtr32++ = pixel;
   1.523 +			case 7:
   1.524 +				*destPtr32++ = pixel;
   1.525 +			case 6:
   1.526 +				*destPtr32++ = pixel;
   1.527 +			case 5:
   1.528 +				*destPtr32++ = pixel;
   1.529 +			case 4:
   1.530 +				*destPtr32++ = pixel;
   1.531 +			case 3:
   1.532 +				*destPtr32++ = pixel;
   1.533 +			case 2:
   1.534 +				*destPtr32++ = pixel;
   1.535 +			case 1:
   1.536 +				*destPtr32++ = pixel;
   1.537 +			case 0:
   1.538 +				*destPtr32++ = pixel;
   1.539 + 
   1.540 +				} while(0 <= --blocksOf16);
   1.541 +				}
   1.542 +			}
   1.543 +		else // negative value corresponds non repeating pixel buffer
   1.544 +			{
   1.545 +			const TInt numPixel = Min(-count, destPtrLimit32 - destPtr32) ;
   1.546 +			for(TInt ii = 0; ii < numPixel; ii++)
   1.547 +				{
   1.548 +				TUint8 component1 = *srcePtr8++;
   1.549 +				TUint8 component2 = *srcePtr8++;
   1.550 +				TUint8 component3 = *srcePtr8++;
   1.551 +
   1.552 +				*destPtr32++ =  0xff000000 | (component3<<16) | (component2<<8) | component1;
   1.553 +				}
   1.554 +			}
   1.555 +		}
   1.556 +
   1.557 +	__ASSERT_DEBUG(srcePtr8 == srcePtrLimit8 && destPtr32 == destPtrLimit32,Panic(EFbsBitmapDecompressionError));
   1.558 +	}
   1.559 +
   1.560 +/** The alternative decoding function which decompresses 24-bit buffer to the 32-bit stream with unused top bytes 
   1.561 +	by using RLE compression algorithm. The function is using in case of memory shortage. */
   1.562 +void CBitwiseBitmap::DoDecompressThirtyTwoUBitDataAltL(RReadStream& aStream,TInt aSrceSizeInBytes,TUint32* aBase)
   1.563 +	{
   1.564 +	const TInt destSizeInDwords = iHeader.iSizeInPixels.iHeight * (iByteWidth>>2);
   1.565 +	TUint32* destPtr32 = aBase;
   1.566 +	TUint32* destPtrLimit32 = destPtr32 + destSizeInDwords;
   1.567 +	
   1.568 +	const TInt KMaxRLENonRepeatingPixelBufferSize = 128 * 3;
   1.569 +	TUint8 dataBuffer[KMaxRLENonRepeatingPixelBufferSize];
   1.570 +
   1.571 +	while(aSrceSizeInBytes > 0 && destPtr32 < destPtrLimit32)
   1.572 +		{
   1.573 +		TInt8 count = aStream.ReadInt8L();
   1.574 +		aSrceSizeInBytes--;
   1.575 +
   1.576 +		if (count >= 0) //repeating pixel buffer
   1.577 +			{
   1.578 +			count = Min(count, (destPtrLimit32 - destPtr32));
   1.579 +			const TUint8 component1 = aStream.ReadUint8L();
   1.580 +			const TUint8 component2 = aStream.ReadUint8L();
   1.581 +			const TUint8 component3 = aStream.ReadUint8L();
   1.582 +			aSrceSizeInBytes -= 3;
   1.583 +
   1.584 +			const TUint32 pixel = 0xff000000 | (component3<<16) | (component2<<8) | component1; 
   1.585 +			
   1.586 +			TInt blocksOf16 = (count >> 4);	// number of blocks of 16 full words to write
   1.587 +			// the first iteration writes 1 to 15 words
   1.588 +			switch (count & 0x0f)
   1.589 +				{ // note that case statements intentionally cascade
   1.590 +			case 15:
   1.591 +				do {	// second and subsequent iterations always write 16 words
   1.592 +				*destPtr32++ = pixel;
   1.593 +			case 14:
   1.594 +				*destPtr32++ = pixel;
   1.595 +			case 13:
   1.596 +				*destPtr32++ = pixel;
   1.597 +			case 12:
   1.598 +				*destPtr32++ = pixel;
   1.599 +			case 11:
   1.600 +				*destPtr32++ = pixel;
   1.601 +			case 10:
   1.602 +				*destPtr32++ = pixel;
   1.603 +			case 9:
   1.604 +				*destPtr32++ = pixel;
   1.605 +			case 8:
   1.606 +				*destPtr32++ = pixel;
   1.607 +			case 7:
   1.608 +				*destPtr32++ = pixel;
   1.609 +			case 6:
   1.610 +				*destPtr32++ = pixel;
   1.611 +			case 5:
   1.612 +				*destPtr32++ = pixel;
   1.613 +			case 4:
   1.614 +				*destPtr32++ = pixel;
   1.615 +			case 3:
   1.616 +				*destPtr32++ = pixel;
   1.617 +			case 2:
   1.618 +				*destPtr32++ = pixel;
   1.619 +			case 1:
   1.620 +				*destPtr32++ = pixel;
   1.621 +			case 0:
   1.622 + 				*destPtr32++ = pixel;
   1.623 +
   1.624 +				} while(0 <= --blocksOf16);
   1.625 +				}
   1.626 +			}
   1.627 +		else // negative value corresponds non repeating pixel buffer
   1.628 +			{
   1.629 +			const TInt numDestDwords = Min(-count, destPtrLimit32 - destPtr32);
   1.630 +			const TInt numSrcBytes = numDestDwords * 3;
   1.631 +			aStream.ReadL(&dataBuffer[0], numSrcBytes);
   1.632 +			aSrceSizeInBytes -= numSrcBytes;
   1.633 +			TUint8* srcPtr = dataBuffer;
   1.634 +			const TInt& numPixel = numDestDwords;
   1.635 +			for(TInt ii = 0; ii < numPixel; ii++)
   1.636 +				{
   1.637 +				const TUint8 component1 = *srcPtr++;
   1.638 +				const TUint8 component2 = *srcPtr++;
   1.639 +				const TUint8 component3 = *srcPtr++;
   1.640 +
   1.641 +				*destPtr32++ = 0xff000000 | (component3<<16) | (component2<<8) | component1; 
   1.642 +				}
   1.643 +			}
   1.644 +		}
   1.645 +
   1.646 +	__ASSERT_DEBUG(aSrceSizeInBytes == 0 && destPtr32 == destPtrLimit32,Panic(EFbsBitmapDecompressionError));
   1.647 +	}
   1.648 +
   1.649 +/** The function decodes 32-bit compressed buffer (where top bytes are used as alpha channel) to the 32-bit stream by using RLE compression algorithm*/
   1.650 +void CBitwiseBitmap::DoDecompressThirtyTwoABitData(TUint8* aDestBuffer,TInt aDestSize,TUint8* aSrceBuffer,TInt aSrceSize)
   1.651 +	{
   1.652 +	TUint8* srcePtr = aSrceBuffer;
   1.653 +	TUint8* srcePtrLimit = aSrceBuffer + aSrceSize;
   1.654 +
   1.655 +	TUint32* destPtr32 = reinterpret_cast<TUint32*>(aDestBuffer);
   1.656 +	TUint32* destPtrLimit32 = reinterpret_cast<TUint32*>(aDestBuffer + aDestSize);
   1.657 +
   1.658 +	while (srcePtr < srcePtrLimit && destPtr32 < destPtrLimit32)
   1.659 +		{
   1.660 +		TInt8 count = *srcePtr++;
   1.661 +
   1.662 +		if (count >= 0) //repeating pixel buffer
   1.663 +			{
   1.664 +			count = Min(count, (destPtrLimit32 - destPtr32));
   1.665 +			TUint8 component1 = *srcePtr++;
   1.666 +			TUint8 component2 = *srcePtr++;
   1.667 +			TUint8 component3 = *srcePtr++;
   1.668 +			TUint8 component4 = *srcePtr++;
   1.669 +			const TUint32 pixel = (component4<<24) | (component3<<16) | (component2<<8) | component1;
   1.670 +
   1.671 +			// for performance loop is unrolled, using "Duff's Device" pattern
   1.672 +			TInt blocksOf16 = (count >> 4);	// number of blocks of 16 full words to write
   1.673 +			// the first iteration writes 1 to 15 words
   1.674 +			switch (count & 0x0f)
   1.675 +				{ // note that case statements intentionally cascade
   1.676 +			case 15:
   1.677 +				do {	// second and subsequent iterations always write 16 words
   1.678 +				*destPtr32++ = pixel;
   1.679 +			case 14:
   1.680 +				*destPtr32++ = pixel;
   1.681 +			case 13:
   1.682 +				*destPtr32++ = pixel;
   1.683 +			case 12:
   1.684 +				*destPtr32++ = pixel;
   1.685 +			case 11:
   1.686 +				*destPtr32++ = pixel;
   1.687 +			case 10:
   1.688 +				*destPtr32++ = pixel;
   1.689 +			case 9:
   1.690 +				*destPtr32++ = pixel;
   1.691 +			case 8:
   1.692 +				*destPtr32++ = pixel;
   1.693 +			case 7:
   1.694 +				*destPtr32++ = pixel;
   1.695 +			case 6:
   1.696 +				*destPtr32++ = pixel;
   1.697 +			case 5:
   1.698 +				*destPtr32++ = pixel;
   1.699 +			case 4:
   1.700 +				*destPtr32++ = pixel;
   1.701 +			case 3:
   1.702 +				*destPtr32++ = pixel;
   1.703 +			case 2:
   1.704 +				*destPtr32++ = pixel;
   1.705 +			case 1:
   1.706 +				*destPtr32++ = pixel;
   1.707 +			case 0:
   1.708 + 				*destPtr32++ = pixel;
   1.709 +
   1.710 +				} while(0 <= --blocksOf16);
   1.711 +				}
   1.712 +			}
   1.713 +		else // negative value corresponds non repeating pixel buffer
   1.714 +			{
   1.715 +			const TInt numPixel = Min(-count, destPtrLimit32 - destPtr32);
   1.716 +
   1.717 +			Mem::Copy(destPtr32, srcePtr, numPixel*4); 
   1.718 +			destPtr32 += numPixel;
   1.719 +			srcePtr += numPixel*4;
   1.720 +
   1.721 +			}
   1.722 +		}
   1.723 +
   1.724 +	__ASSERT_DEBUG(srcePtr == srcePtrLimit && destPtr32 == destPtrLimit32,Panic(EFbsBitmapDecompressionError));
   1.725 +	}
   1.726 +
   1.727 +/** The alternative decoding function which decompresses 32-bit buffer (where top bytes are used as alpha channel)
   1.728 +	to the 32-bit stream by using RLE compression algorithm. The function is using in case of memory shortage. */
   1.729 +void CBitwiseBitmap::DoDecompressThirtyTwoABitDataAltL(RReadStream& aStream,TInt aSrceSizeInBytes,TUint32* aBase)
   1.730 +	{
   1.731 +	const TInt destSizeInDwords = iHeader.iSizeInPixels.iHeight * (iByteWidth>>2);
   1.732 +	TUint32* destPtr32 = aBase;
   1.733 +	TUint32* destPtrLimit32 = destPtr32 + destSizeInDwords;
   1.734 +
   1.735 +	while(aSrceSizeInBytes > 0 && destPtr32 < destPtrLimit32)
   1.736 +		{
   1.737 +		TInt8 count = aStream.ReadInt8L();
   1.738 +		aSrceSizeInBytes--;
   1.739 +
   1.740 +		if (count >= 0) //repeating pixel buffer
   1.741 +			{
   1.742 +			count = Min(count, (destPtrLimit32 - destPtr32));
   1.743 +			const TUint32 pixel = aStream.ReadUint32L();
   1.744 +			aSrceSizeInBytes -= 4;
   1.745 +
   1.746 +			// for performance loop is unrolled, using "Duff's Device" pattern
   1.747 +			TInt blocksOf16 = (count >> 4);	// number of blocks of 16 full words to write
   1.748 +			// the first iteration writes 1 to 15 words
   1.749 +			switch (count & 0x0f)
   1.750 +				{ // note that case statements intentionally cascade
   1.751 +			case 15:
   1.752 +				do {	// second and subsequent iterations always write 16 words
   1.753 +				*destPtr32++ = pixel;
   1.754 +			case 14:
   1.755 +				*destPtr32++ = pixel;
   1.756 +			case 13:
   1.757 +				*destPtr32++ = pixel;
   1.758 +			case 12:
   1.759 +				*destPtr32++ = pixel;
   1.760 +			case 11:
   1.761 +				*destPtr32++ = pixel;
   1.762 +			case 10:
   1.763 +				*destPtr32++ = pixel;
   1.764 +			case 9:
   1.765 +				*destPtr32++ = pixel;
   1.766 +			case 8:
   1.767 +				*destPtr32++ = pixel;
   1.768 +			case 7:
   1.769 +				*destPtr32++ = pixel;
   1.770 +			case 6:
   1.771 +				*destPtr32++ = pixel;
   1.772 +			case 5:
   1.773 +				*destPtr32++ = pixel;
   1.774 +			case 4:
   1.775 +				*destPtr32++ = pixel;
   1.776 +			case 3:
   1.777 +				*destPtr32++ = pixel;
   1.778 +			case 2:
   1.779 +				*destPtr32++ = pixel;
   1.780 +			case 1:
   1.781 +				*destPtr32++ = pixel;
   1.782 +			case 0:
   1.783 + 				*destPtr32++ = pixel;
   1.784 +
   1.785 +				} while(0 <= --blocksOf16);
   1.786 +				}
   1.787 +			}
   1.788 +		else // negative value corresponds non repeating pixel buffer
   1.789 +			{
   1.790 +			const TInt numPixels = Min(-count, destPtrLimit32 - destPtr32);
   1.791 +			aStream.ReadL((TUint16*)destPtr32, numPixels * 2); // read TUint16's
   1.792 +			aSrceSizeInBytes -= numPixels * 4;
   1.793 +			destPtr32 += numPixels;
   1.794 +			}
   1.795 +		}
   1.796 +
   1.797 +	__ASSERT_DEBUG(aSrceSizeInBytes == 0 && destPtr32 == destPtrLimit32,Panic(EFbsBitmapDecompressionError));
   1.798 +	}
   1.799 +
   1.800 +void CBitwiseBitmap::DoExternalizeDataCompressedL(RWriteStream& aStream,TUint8* aData,TInt aSizeInBytes) const
   1.801 +	{
   1.802 +	switch (iHeader.iBitsPerPixel)
   1.803 +		{
   1.804 +	case 1:
   1.805 +	case 2:
   1.806 +	case 4:
   1.807 +	case 8:
   1.808 +		DoExternalizeByteDataCompressedL(aStream,aData,aSizeInBytes);
   1.809 +		break;
   1.810 +	case 12:
   1.811 +		DoExternalizeTwelveBitDataCompressedL(aStream,aData,aSizeInBytes);
   1.812 +		break;
   1.813 +	case 16:
   1.814 +		DoExternalizeSixteenBitDataCompressedL(aStream,aData,aSizeInBytes);
   1.815 +		break;
   1.816 +	case 24:
   1.817 +		DoExternalizeTwentyFourBitDataCompressedL(aStream,aData,aSizeInBytes);
   1.818 +		break;
   1.819 +	case 32:
   1.820 +		__ASSERT_DEBUG(iHeader.iColor == SEpocBitmapHeader::EColor||
   1.821 +						iHeader.iColor == SEpocBitmapHeader::EColorAlpha||
   1.822 +						iHeader.iColor == SEpocBitmapHeader::EColorAlphaPM,
   1.823 +						Panic(EFbsBitmapInvalidCompression));
   1.824 +		if(iHeader.iColor == SEpocBitmapHeader::EColor)
   1.825 +			{
   1.826 +			DoExternalizeThirtyTwoUBitDataCompressedL(aStream,aData,aSizeInBytes);
   1.827 +			}
   1.828 +		else
   1.829 +			{
   1.830 +			DoExternalizeThirtyTwoABitDataCompressedL(aStream,aData,aSizeInBytes);
   1.831 +			}
   1.832 +		break;
   1.833 +	default:
   1.834 +		break;
   1.835 +		}
   1.836 +	}
   1.837 +
   1.838 +void CBitwiseBitmap::DoExternalizeByteDataCompressedL(RWriteStream& aStream,TUint8* aData,TInt aSizeInBytes) const
   1.839 +	{
   1.840 +	TUint8* dataLimit=aData+aSizeInBytes-2;
   1.841 +	TUint8 runPair[2];
   1.842 +	while(aData<dataLimit)
   1.843 +		{
   1.844 +		TUint8 value=*aData;
   1.845 +		TUint8* runStartPtr = aData;
   1.846 +
   1.847 +		if (*(aData+1)==value && *(aData+2)==value)
   1.848 +			{
   1.849 +			aData+=3;
   1.850 +			while(aData<dataLimit && *aData==value)
   1.851 +				aData++;
   1.852 +			TInt runLength=aData-runStartPtr;
   1.853 +			runPair[0]=127;
   1.854 +			runPair[1]=value;
   1.855 +			while(runLength>128)
   1.856 +				{
   1.857 +				aStream.WriteL(&runPair[0],2);
   1.858 +				runLength-=128;
   1.859 +				}
   1.860 +			runPair[0]=TUint8(runLength-1);
   1.861 +			aStream.WriteL(&runPair[0],2);
   1.862 +			}
   1.863 +		else
   1.864 +			{
   1.865 +			while(aData<dataLimit && (*(aData+1)!=value || *(aData+2)!=value))
   1.866 +				{
   1.867 +				aData++;
   1.868 +				value=*aData;
   1.869 +				}
   1.870 +			TInt runLength = aData - runStartPtr;
   1.871 +			while (runLength > 128)
   1.872 +				{
   1.873 +				aStream.WriteInt8L(-128);
   1.874 +				aStream.WriteL(runStartPtr,128);
   1.875 +				runLength-=128;
   1.876 +				runStartPtr+=128;
   1.877 +				}
   1.878 +			aStream.WriteInt8L(-runLength);
   1.879 +			aStream.WriteL(runStartPtr,runLength);
   1.880 +			}
   1.881 +		}
   1.882 +	dataLimit+=2;
   1.883 +	if (aData<dataLimit)
   1.884 +		{
   1.885 +		TInt runLength=dataLimit-aData;
   1.886 +		aStream.WriteInt8L(-runLength);
   1.887 +		aStream.WriteL(aData,runLength);
   1.888 +		}
   1.889 +	}
   1.890 +
   1.891 +void CBitwiseBitmap::DoExternalizeTwelveBitDataCompressedL(RWriteStream& aStream,TUint8* aData,TInt aSizeInBytes) const
   1.892 +	{
   1.893 +	TUint16* srcePtr = (TUint16*)aData;
   1.894 +	TUint16* srcePtrLimit = srcePtr + (aSizeInBytes / 2);
   1.895 +
   1.896 +	while (srcePtr < srcePtrLimit)
   1.897 +		{
   1.898 +		TUint16* runStartPtr = srcePtr;
   1.899 +		TUint16 value = TUint16(*srcePtr & 0x0fff);
   1.900 +		do
   1.901 +			{
   1.902 +			srcePtr++;
   1.903 +			}
   1.904 +		while (srcePtr < srcePtrLimit && *srcePtr == value);
   1.905 +
   1.906 +		TInt pixelLength = srcePtr - runStartPtr;
   1.907 +		TUint16 maxLengthData = TUint16(value | 0xf000);
   1.908 +
   1.909 +		while (pixelLength > 16)
   1.910 +			{
   1.911 +			aStream.WriteUint16L(maxLengthData);
   1.912 +			pixelLength -= 16;
   1.913 +			}
   1.914 +
   1.915 +		if (pixelLength > 0)
   1.916 +			aStream.WriteUint16L(value | TUint16((pixelLength - 1) << 12));
   1.917 +		}
   1.918 +	}
   1.919 +
   1.920 +void CBitwiseBitmap::DoExternalizeSixteenBitDataCompressedL(RWriteStream& aStream,TUint8* aData,TInt aSizeInBytes) const
   1.921 +	{
   1.922 +	TUint16* srcePtr = (TUint16*)aData;
   1.923 +	TUint16* srceLimitPtr = srcePtr + (aSizeInBytes / 2);
   1.924 +	TUint16* srceLimitPtrMinusOne = srceLimitPtr - 1;
   1.925 +
   1.926 +	while (srcePtr < srceLimitPtrMinusOne)
   1.927 +		{
   1.928 +		TUint16 value = *srcePtr;
   1.929 +		TUint16* runStartPtr = srcePtr++;
   1.930 +
   1.931 +		if(*srcePtr == value)
   1.932 +			{
   1.933 +			do
   1.934 +				{
   1.935 +				srcePtr++;
   1.936 +				}
   1.937 +			while(srcePtr < srceLimitPtr && *srcePtr == value);
   1.938 +
   1.939 +			TInt pixelLength = srcePtr-runStartPtr;
   1.940 +			while (pixelLength > 128)
   1.941 +				{
   1.942 +				aStream.WriteInt8L(127);
   1.943 +				aStream.WriteUint16L(value);
   1.944 +				pixelLength -= 128;
   1.945 +				}
   1.946 +
   1.947 +			aStream.WriteUint8L(pixelLength - 1);
   1.948 +			aStream.WriteUint16L(value);
   1.949 +			}
   1.950 +		else
   1.951 +			{
   1.952 +			value = *srcePtr;
   1.953 +			while (srcePtr < srceLimitPtrMinusOne && *(srcePtr + 1) != value)
   1.954 +				{
   1.955 +				srcePtr++;
   1.956 +				value = *srcePtr;
   1.957 +				}
   1.958 +
   1.959 +			TInt pixelLength = srcePtr - runStartPtr;
   1.960 +			while (pixelLength > 128)
   1.961 +				{
   1.962 +				aStream.WriteInt8L(-128);
   1.963 +				aStream.WriteL(runStartPtr,128);
   1.964 +				runStartPtr += 128;
   1.965 +				pixelLength -= 128;
   1.966 +				}
   1.967 +
   1.968 +			aStream.WriteInt8L(-pixelLength);
   1.969 +			aStream.WriteL(runStartPtr,pixelLength);
   1.970 +			}
   1.971 +		}
   1.972 +
   1.973 +	TInt remainingPixels = srceLimitPtr - srcePtr;
   1.974 +	if (remainingPixels > 0)
   1.975 +		{
   1.976 +		aStream.WriteInt8L(-remainingPixels);
   1.977 +		aStream.WriteL(srcePtr,remainingPixels);
   1.978 +		}
   1.979 +	}
   1.980 +
   1.981 +void CBitwiseBitmap::DoExternalizeTwentyFourBitDataCompressedL(RWriteStream& aStream,TUint8* aData,TInt aSizeInBytes) const
   1.982 +	{
   1.983 +	TUint8* srceLimitPtr = aData + aSizeInBytes;
   1.984 +	TUint8* srceLimitPtrMinusThree = srceLimitPtr - 3; // three bytes == one pixel
   1.985 +
   1.986 +	while (aData < srceLimitPtrMinusThree)
   1.987 +		{
   1.988 +		TUint8* runStartPtr = aData;
   1.989 +		TUint8 component1 = *aData++;
   1.990 +		TUint8 component2 = *aData++;
   1.991 +		TUint8 component3 = *aData++;
   1.992 +
   1.993 +		if (TrueColorPointerCompare(aData,component1,component2,component3))
   1.994 +			{
   1.995 +			do
   1.996 +				{
   1.997 +				aData += 3;
   1.998 +				}
   1.999 +			while (aData < srceLimitPtr && TrueColorPointerCompare(aData,component1,component2,component3));
  1.1000 +
  1.1001 +			TInt pixelLength = (aData - runStartPtr) / 3;
  1.1002 +			while (pixelLength > 128)
  1.1003 +				{
  1.1004 +				aStream.WriteInt8L(127);
  1.1005 +				aStream.WriteUint8L(component1);
  1.1006 +				aStream.WriteUint8L(component2);
  1.1007 +				aStream.WriteUint8L(component3);
  1.1008 +				pixelLength -= 128;
  1.1009 +				}
  1.1010 +
  1.1011 +			aStream.WriteInt8L(pixelLength - 1);
  1.1012 +			aStream.WriteUint8L(component1);
  1.1013 +			aStream.WriteUint8L(component2);
  1.1014 +			aStream.WriteUint8L(component3);
  1.1015 +			}
  1.1016 +		else
  1.1017 +			{
  1.1018 +			TBool more  = ETrue;
  1.1019 +			TBool eqRun = EFalse;
  1.1020 +			do
  1.1021 +				{
  1.1022 +				component1 = *aData++;
  1.1023 +				component2 = *aData++;
  1.1024 +				component3 = *aData++;
  1.1025 +				more = (aData < srceLimitPtr);
  1.1026 +				eqRun = more && TrueColorPointerCompare(aData,component1,component2,component3);
  1.1027 +				}
  1.1028 +			while (more && !eqRun);
  1.1029 +			if (eqRun)
  1.1030 +				aData -= 3;
  1.1031 +			TInt pixelLength = (aData - runStartPtr) / 3;
  1.1032 +			while (pixelLength > 128)
  1.1033 +				{
  1.1034 +				aStream.WriteInt8L(-128);
  1.1035 +				aStream.WriteL(runStartPtr,384);
  1.1036 +				runStartPtr += 384;
  1.1037 +				pixelLength -= 128;
  1.1038 +				}
  1.1039 +
  1.1040 +			aStream.WriteInt8L(-pixelLength);
  1.1041 +			aStream.WriteL(runStartPtr,pixelLength * 3);
  1.1042 +			}
  1.1043 +		}
  1.1044 +
  1.1045 +	TInt remainingPixels = srceLimitPtr - aData;
  1.1046 +	if (remainingPixels > 0)
  1.1047 +		{
  1.1048 +		TInt pixelLength = remainingPixels / 3;
  1.1049 +		aStream.WriteInt8L(-pixelLength);
  1.1050 +		aStream.WriteL(aData,remainingPixels);
  1.1051 +		}
  1.1052 +	}
  1.1053 +	
  1.1054 +/** The function externalizes 32-bit buffer with unused top bytes to the 24-bit compressed stream by using RLE compression algorithm*/
  1.1055 +void CBitwiseBitmap::DoExternalizeThirtyTwoUBitDataCompressedL(RWriteStream& aStream,TUint8* aData8,TInt aSizeInBytes) const
  1.1056 +	{
  1.1057 +
  1.1058 +	__ASSERT_DEBUG((reinterpret_cast<TUint32>(aData8) & 0x3)==0,Panic(EFbsBitmapDecompressionError)); // make sure the start address is word aligned
  1.1059 +	__ASSERT_DEBUG((aSizeInBytes & 0x3)==0,Panic(EFbsBitmapDecompressionError)); // make sure the size is whole no of pixels
  1.1060 +
  1.1061 +	TUint32* ptr = reinterpret_cast<TUint32*>(aData8);
  1.1062 +	TUint32* srceLimitPtr = reinterpret_cast<TUint32*>(aData8 + aSizeInBytes);
  1.1063 +	TUint32* srceLimitPtr2ndLast = srceLimitPtr - 1; 
  1.1064 +
  1.1065 +	while (ptr < srceLimitPtr2ndLast)
  1.1066 +		{
  1.1067 +		TUint32* runStartPtr = ptr;
  1.1068 +		TUint32 pixel = *ptr++ & 0x00ffffff;
  1.1069 +
  1.1070 +		if ( (((*ptr)&0x00ffffff)==pixel) )
  1.1071 +			{
  1.1072 +			do
  1.1073 +				{
  1.1074 +				ptr++;
  1.1075 +				}
  1.1076 +			while (ptr< srceLimitPtr && (((*ptr)&0x00ffffff)==pixel) );
  1.1077 +
  1.1078 +			TInt pixelLength = (ptr - runStartPtr);
  1.1079 +			while (pixelLength > 128)
  1.1080 +				{
  1.1081 +				aStream.WriteInt8L(127);
  1.1082 +				aStream.WriteUint8L(pixel&0x000000ff);
  1.1083 +				aStream.WriteUint8L((pixel>>8)&0x000000ff);
  1.1084 +				aStream.WriteUint8L((pixel>>16)&0x000000ff);
  1.1085 +				pixelLength -= 128;
  1.1086 +				}
  1.1087 +
  1.1088 +			aStream.WriteInt8L(pixelLength - 1);
  1.1089 +			aStream.WriteUint8L(pixel&0x000000ff);
  1.1090 +			aStream.WriteUint8L((pixel>>8)&0x000000ff);
  1.1091 +			aStream.WriteUint8L((pixel>>16)&0x000000ff);
  1.1092 +			}
  1.1093 +		else
  1.1094 +			{
  1.1095 +			TBool more  = ETrue;
  1.1096 +			TBool eqRun = EFalse;
  1.1097 +			do
  1.1098 +				{
  1.1099 +				pixel = *ptr++ & 0x00ffffff;
  1.1100 +				more = (ptr < srceLimitPtr);
  1.1101 +				eqRun = more && (((*ptr)&0x00ffffff)==pixel);
  1.1102 +				} while (more && !eqRun);
  1.1103 +			if (eqRun)
  1.1104 +				ptr--;
  1.1105 +			TInt pixelLength = (ptr - runStartPtr);
  1.1106 +			while (pixelLength > 128)
  1.1107 +				{
  1.1108 +				aStream.WriteInt8L(-128);
  1.1109 +				for(TInt ii = 0; ii < 128; ii++)
  1.1110 +					{
  1.1111 +					aStream.WriteL(reinterpret_cast<TUint8*>(runStartPtr), 3);
  1.1112 +					runStartPtr++;
  1.1113 +					}
  1.1114 +				pixelLength -= 128;
  1.1115 +				}
  1.1116 +
  1.1117 +			aStream.WriteInt8L(-pixelLength);
  1.1118 +			for(TInt kk = 0; kk < pixelLength; kk++)
  1.1119 +				{
  1.1120 +				aStream.WriteL(reinterpret_cast<TUint8*>(runStartPtr), 3);
  1.1121 +				runStartPtr++;
  1.1122 +				}
  1.1123 +			}
  1.1124 +		}
  1.1125 +
  1.1126 +	const TInt remainingPixels = srceLimitPtr - ptr;
  1.1127 +	if (remainingPixels > 0)
  1.1128 +		{
  1.1129 +		__ASSERT_DEBUG(remainingPixels == 1, Panic(EFbsBitmapDecompressionError));
  1.1130 +
  1.1131 +		aStream.WriteInt8L(-remainingPixels);
  1.1132 +		for(TInt ii = 0; ii < remainingPixels; ii++)
  1.1133 +			{
  1.1134 +			aStream.WriteL(reinterpret_cast<TUint8*>(ptr), 3);
  1.1135 +			ptr++;
  1.1136 +			}
  1.1137 +		}
  1.1138 +	}
  1.1139 +
  1.1140 +/** The function externalizes 32-bit buffer with alpha channel in top byte to the 32-bit compressed stream by using RLE compression algorithm*/
  1.1141 +void CBitwiseBitmap::DoExternalizeThirtyTwoABitDataCompressedL(RWriteStream& aStream,TUint8* aData8,TInt aSizeInBytes) const
  1.1142 +	{
  1.1143 +
  1.1144 +	__ASSERT_DEBUG((reinterpret_cast<TUint32>(aData8) & 0x3)==0,Panic(EFbsBitmapDecompressionError)); // make sure the start address is word aligned
  1.1145 +	__ASSERT_DEBUG((aSizeInBytes & 0x3)==0,Panic(EFbsBitmapDecompressionError)); // make sure the size is whole no of pixels
  1.1146 +
  1.1147 +	TUint32* ptr = reinterpret_cast<TUint32*>(aData8);
  1.1148 +	TUint32* srceLimitPtr = reinterpret_cast<TUint32*>(aData8 + aSizeInBytes);
  1.1149 +	TUint32* srceLimitPtr2ndLast = srceLimitPtr - 1; 
  1.1150 +
  1.1151 +	while (ptr < srceLimitPtr2ndLast)
  1.1152 +		{
  1.1153 +		TUint32* runStartPtr = ptr;
  1.1154 +		TUint32 pixel = *ptr++;
  1.1155 +
  1.1156 +		if (*ptr==pixel)
  1.1157 +			{
  1.1158 +			do
  1.1159 +				{
  1.1160 +				ptr++;
  1.1161 +				}
  1.1162 +			while (ptr < srceLimitPtr && *ptr==pixel);
  1.1163 +
  1.1164 +			TInt pixelLength = (ptr - runStartPtr);
  1.1165 +			while (pixelLength > 128)
  1.1166 +				{
  1.1167 +				aStream.WriteInt8L(127);
  1.1168 +				aStream.WriteUint32L(pixel);
  1.1169 +				pixelLength -= 128;
  1.1170 +				}
  1.1171 +
  1.1172 +			aStream.WriteInt8L(pixelLength - 1);
  1.1173 +			aStream.WriteUint32L(pixel);
  1.1174 +			}
  1.1175 +		else
  1.1176 +			{
  1.1177 +			TBool more  = ETrue;
  1.1178 +			TBool eqRun = EFalse;
  1.1179 +			do
  1.1180 +				{
  1.1181 +				pixel = *ptr++;
  1.1182 +				more = (ptr < srceLimitPtr);
  1.1183 +				eqRun = more && *ptr==pixel;
  1.1184 +				} while (more && !eqRun);
  1.1185 +			
  1.1186 +			if (eqRun)
  1.1187 +				ptr--;
  1.1188 +			
  1.1189 +			TInt pixelLength = (ptr - runStartPtr);
  1.1190 +			while (pixelLength > 128)
  1.1191 +				{
  1.1192 +				aStream.WriteInt8L(-128);
  1.1193 +				aStream.WriteL(reinterpret_cast<TUint8*>(runStartPtr), 128*4);
  1.1194 +				runStartPtr += 128;
  1.1195 +				pixelLength -= 128;
  1.1196 +				}
  1.1197 +
  1.1198 +			aStream.WriteInt8L(-pixelLength);
  1.1199 +			aStream.WriteL(reinterpret_cast<TUint8*>(runStartPtr), pixelLength*4);
  1.1200 +			}
  1.1201 +		}
  1.1202 +
  1.1203 +	const TInt remainingPixels = srceLimitPtr - ptr;
  1.1204 +	if (remainingPixels > 0)
  1.1205 +		{
  1.1206 +		__ASSERT_DEBUG(remainingPixels == 1, Panic(EFbsBitmapDecompressionError));
  1.1207 +
  1.1208 +		aStream.WriteInt8L(-remainingPixels);
  1.1209 +		aStream.WriteL(reinterpret_cast<TUint8*>(ptr), remainingPixels*4);
  1.1210 +		}
  1.1211 +	}
  1.1212 +
  1.1213 +TInt CBitwiseBitmap::SizeOfDataCompressed(TUint8* aData,TInt aSizeInBytes) const
  1.1214 +	{
  1.1215 +	if(aSizeInBytes<=0)
  1.1216 +		return 0;
  1.1217 +
  1.1218 +	switch (iHeader.iBitsPerPixel)
  1.1219 +		{
  1.1220 +	case 1:
  1.1221 +	case 2:
  1.1222 +	case 4:
  1.1223 +	case 8:
  1.1224 +		return SizeOfByteDataCompressed(aData,aSizeInBytes);
  1.1225 +	case 12:
  1.1226 +		return SizeOfTwelveBitDataCompressed(aData,aSizeInBytes);
  1.1227 +	case 16:
  1.1228 +		return SizeOfSixteenBitDataCompressed(aData,aSizeInBytes);
  1.1229 +	case 24:
  1.1230 +		return SizeOfTwentyFourBitDataCompressed(aData,aSizeInBytes);
  1.1231 +	case 32:
  1.1232 +		__ASSERT_DEBUG(iHeader.iColor == SEpocBitmapHeader::EColor||
  1.1233 +						iHeader.iColor == SEpocBitmapHeader::EColorAlpha||
  1.1234 +						iHeader.iColor == SEpocBitmapHeader::EColorAlphaPM,
  1.1235 +						Panic(EFbsBitmapInvalidCompression));
  1.1236 +		if(iHeader.iColor == SEpocBitmapHeader::EColor)
  1.1237 +			{
  1.1238 +			return SizeOfThirtyTwoUBitDataCompressed(aData,aSizeInBytes);
  1.1239 +			}
  1.1240 +		else
  1.1241 +			{
  1.1242 +			return SizeOfThirtyTwoABitDataCompressed(aData,aSizeInBytes);
  1.1243 +			}
  1.1244 +	default:
  1.1245 +		break;
  1.1246 +		}
  1.1247 +
  1.1248 +	return 0;
  1.1249 +	}
  1.1250 +
  1.1251 +TInt CBitwiseBitmap::SizeOfByteDataCompressed(TUint8* aData,TInt aSizeInBytes) const
  1.1252 +	{
  1.1253 +	if(aSizeInBytes<=0)
  1.1254 +		return 0;
  1.1255 +
  1.1256 +	TInt compressedSize=0;
  1.1257 +	TUint8* dataLimit=aData+aSizeInBytes-2;
  1.1258 +	
  1.1259 +	while(aData<dataLimit)
  1.1260 +		{
  1.1261 +		TUint8 value=*aData;
  1.1262 +		if (*(aData+1)==value && *(aData+2)==value)
  1.1263 +			{
  1.1264 +			TUint8* runStartPtr=aData;
  1.1265 +			aData+=3;
  1.1266 +			while(aData<dataLimit && *aData==value)
  1.1267 +				aData++;
  1.1268 +			TInt runLength=aData-runStartPtr;
  1.1269 +
  1.1270 +			compressedSize+= 2*(((runLength-1)>>7) + 1) ;
  1.1271 +			}
  1.1272 +		else
  1.1273 +			{
  1.1274 +			TUint8* runStartPtr=aData;
  1.1275 +			while(aData<dataLimit && (*(aData+1)!=value || *(aData+2)!=value))
  1.1276 +				{
  1.1277 +				aData++;
  1.1278 +				value=*aData;
  1.1279 +				}
  1.1280 +			TInt runLength=aData-runStartPtr;
  1.1281 +			
  1.1282 +			compressedSize+= runLength + ((runLength-1)>>7) + 1;
  1.1283 +			}
  1.1284 +		}
  1.1285 +	dataLimit+=2;
  1.1286 +	if (aData<dataLimit)
  1.1287 +		compressedSize+=dataLimit-aData+1;
  1.1288 +	return(compressedSize);
  1.1289 +	}
  1.1290 +
  1.1291 +TInt CBitwiseBitmap::SizeOfTwelveBitDataCompressed(TUint8* aData,TInt aSizeInBytes) const
  1.1292 +	{
  1.1293 +	if(aSizeInBytes<=0)
  1.1294 +		return 0;
  1.1295 +
  1.1296 +	TInt compressedSize = 0;
  1.1297 +	TUint16* srcePtr = (TUint16*)aData;
  1.1298 +	TUint16* srcePtrLimit = srcePtr + (aSizeInBytes / 2);
  1.1299 +
  1.1300 +	while (srcePtr < srcePtrLimit)
  1.1301 +		{
  1.1302 +		TUint16* runStartPtr = srcePtr;
  1.1303 +		TUint16 value = TUint16(*srcePtr & 0x0fff);
  1.1304 +		do
  1.1305 +			{
  1.1306 +			srcePtr++;
  1.1307 +			}
  1.1308 +		while (srcePtr < srcePtrLimit && *srcePtr == value);
  1.1309 +
  1.1310 +		TInt pixelLength = srcePtr - runStartPtr;
  1.1311 +
  1.1312 +		compressedSize += 2*( ((pixelLength-1)>>4) + 1);
  1.1313 +		}
  1.1314 +	return compressedSize;
  1.1315 +	}
  1.1316 +
  1.1317 +TInt CBitwiseBitmap::SizeOfSixteenBitDataCompressed(TUint8* aData,TInt aSizeInBytes) const
  1.1318 +	{
  1.1319 +	if(aSizeInBytes<=0)
  1.1320 +		return 0;
  1.1321 +
  1.1322 +	TInt compressedSize = 0;
  1.1323 +	TUint16* srcePtr = (TUint16*)aData;
  1.1324 +	TUint16* srceLimitPtr = srcePtr + (aSizeInBytes / 2);
  1.1325 +	TUint16* srceLimitPtrMinusOne = srceLimitPtr - 1;
  1.1326 +
  1.1327 +	while (srcePtr < srceLimitPtrMinusOne)
  1.1328 +		{
  1.1329 +		TUint16 value = *srcePtr;
  1.1330 +		TUint16* runStartPtr = srcePtr++;
  1.1331 +
  1.1332 +		if(*srcePtr == value)
  1.1333 +			{
  1.1334 +			do
  1.1335 +				{
  1.1336 +				srcePtr++;
  1.1337 +				}
  1.1338 +			while(srcePtr < srceLimitPtr && *srcePtr == value);
  1.1339 +
  1.1340 +			TInt pixelLength = srcePtr-runStartPtr;
  1.1341 +
  1.1342 +			compressedSize += 3*( ((pixelLength-1)>>7) + 1);
  1.1343 +			}
  1.1344 +		else
  1.1345 +			{
  1.1346 +			value = *srcePtr;
  1.1347 +			while (srcePtr < srceLimitPtrMinusOne && *(srcePtr + 1) != value)
  1.1348 +				{
  1.1349 +				srcePtr++;
  1.1350 +				value = *srcePtr;
  1.1351 +				}
  1.1352 +
  1.1353 +			TInt pixelLength = srcePtr-runStartPtr;
  1.1354 +
  1.1355 +			compressedSize += (pixelLength * 2) + ((pixelLength-1)>>7) + 1;
  1.1356 +			}
  1.1357 +		}
  1.1358 +	if (srcePtr < srceLimitPtr)
  1.1359 +		compressedSize += ((srceLimitPtr - srcePtr) * 2) + 1;
  1.1360 +	return compressedSize;
  1.1361 +	}
  1.1362 +
  1.1363 +TInt CBitwiseBitmap::SizeOfTwentyFourBitDataCompressed(TUint8* aData,TInt aSizeInBytes) const
  1.1364 +	{
  1.1365 +	if(aSizeInBytes<=0)
  1.1366 +		return 0;
  1.1367 +
  1.1368 +	TInt compressedSize = 0;
  1.1369 +	TUint8* srceLimitPtr = aData + aSizeInBytes;
  1.1370 +	TUint8* srceLimitPtrMinusThree = srceLimitPtr - 3; // three bytes == one pixel
  1.1371 +
  1.1372 +	while (aData < srceLimitPtrMinusThree)
  1.1373 +		{
  1.1374 +		TUint8* runStartPtr = aData;
  1.1375 +		TUint8 component1 = *aData++;
  1.1376 +		TUint8 component2 = *aData++;
  1.1377 +		TUint8 component3 = *aData++;
  1.1378 +
  1.1379 +		if (TrueColorPointerCompare(aData,component1,component2,component3))
  1.1380 +			{
  1.1381 +			do
  1.1382 +				{
  1.1383 +				aData += 3;
  1.1384 +				}
  1.1385 +			while (aData < srceLimitPtr && TrueColorPointerCompare(aData,component1,component2,component3));
  1.1386 +
  1.1387 +			TInt pixelLength = (aData - runStartPtr) / 3;
  1.1388 +			
  1.1389 +			compressedSize += 4*( ((pixelLength-1)>>7) + 1); 
  1.1390 +			}
  1.1391 +		else
  1.1392 +			{
  1.1393 +			TBool more  = ETrue;
  1.1394 +			TBool eqRun = EFalse;
  1.1395 +			do
  1.1396 +				{
  1.1397 +				component1 = *aData++;
  1.1398 +				component2 = *aData++;
  1.1399 +				component3 = *aData++;
  1.1400 +				more = (aData < srceLimitPtr);
  1.1401 +				eqRun = more && TrueColorPointerCompare(aData,component1,component2,component3);
  1.1402 +				}
  1.1403 +			while (more && !eqRun);
  1.1404 +			if (eqRun)
  1.1405 +				aData -= 3;
  1.1406 +			TInt pixelLength = (aData - runStartPtr) / 3;
  1.1407 +
  1.1408 +			compressedSize += (pixelLength * 3) + ((pixelLength-1)>>7) + 1;
  1.1409 +			}
  1.1410 +		}
  1.1411 +	
  1.1412 +	if (aData < srceLimitPtr)
  1.1413 +		compressedSize += srceLimitPtr - aData + 1;
  1.1414 +
  1.1415 +	return compressedSize;
  1.1416 +	}
  1.1417 +
  1.1418 +/** The function calculates the size of 24-bit RLE compression stream which could be obtain from given 32-bit buffer, 
  1.1419 +	where the top bytes are unused*/
  1.1420 +TInt CBitwiseBitmap::SizeOfThirtyTwoUBitDataCompressed(TUint8* aData8,TInt aSizeInBytes) const
  1.1421 +	{
  1.1422 +	if(aSizeInBytes<=0)
  1.1423 +		return 0;
  1.1424 +
  1.1425 +	__ASSERT_DEBUG((reinterpret_cast<TUint32>(aData8) & 0x3)==0,Panic(EFbsBitmapDecompressionError)); // make sure the start address is word aligned
  1.1426 +	__ASSERT_DEBUG((aSizeInBytes & 0x3)==0,Panic(EFbsBitmapDecompressionError)); // make sure the size is whole no of pixels
  1.1427 +
  1.1428 +
  1.1429 +	TInt compressedSize = 0;
  1.1430 +	
  1.1431 +	
  1.1432 +	TUint32* ptr = reinterpret_cast<TUint32*>(aData8);
  1.1433 +	TUint32* srceLimitPtr = reinterpret_cast<TUint32*>(aData8 + aSizeInBytes);
  1.1434 +	TUint32* srceLimitPtr2ndLast = srceLimitPtr - 1; 
  1.1435 +	while (ptr < srceLimitPtr2ndLast)
  1.1436 +		{
  1.1437 +		TUint32* runStartPtr = ptr;
  1.1438 +		TUint32 pixel = *ptr++ & 0x00ffffff;
  1.1439 +
  1.1440 +		if ((((*ptr)&0x00ffffff)==pixel))
  1.1441 +			{
  1.1442 +			do
  1.1443 +				{
  1.1444 +				ptr++;
  1.1445 +				}
  1.1446 +			while (ptr < srceLimitPtr && (((*ptr)&0x00ffffff)==pixel));
  1.1447 +
  1.1448 +			TInt pixelLength = (ptr - runStartPtr);
  1.1449 +			compressedSize += 4*( ((pixelLength-1)>>7) + 1); 
  1.1450 +			}	
  1.1451 +		else
  1.1452 +			{
  1.1453 +			TBool more  = ETrue;
  1.1454 +			TBool eqRun = EFalse;
  1.1455 +			do
  1.1456 +				{
  1.1457 +				pixel = *ptr++ & 0x00ffffff;
  1.1458 +				more = (ptr < srceLimitPtr);
  1.1459 +				eqRun = more && (((*ptr)&0x00ffffff)==pixel);
  1.1460 +				} while (more && !eqRun);
  1.1461 +
  1.1462 +			if (eqRun)
  1.1463 +				ptr--;
  1.1464 +
  1.1465 +			TInt pixelLength = (ptr - runStartPtr) ;
  1.1466 +			compressedSize += 3*pixelLength + ((pixelLength-1)>>7) + 1; 
  1.1467 +			}
  1.1468 +		}
  1.1469 +
  1.1470 +	if (ptr < srceLimitPtr)
  1.1471 +		compressedSize += (srceLimitPtr - ptr)*3 + 1;
  1.1472 +
  1.1473 +	return compressedSize;
  1.1474 +	}
  1.1475 +
  1.1476 +
  1.1477 +TBool CBitwiseBitmap::TrueColorPointerCompare(TUint8* aColorPointer,TUint8 aComponent1,TUint8 aComponent2,TUint8 aComponent3) const
  1.1478 +	{
  1.1479 +	return (*aColorPointer == aComponent1 && *(aColorPointer + 1) == aComponent2 && *(aColorPointer + 2) == aComponent3);
  1.1480 +	}
  1.1481 +
  1.1482 +/** This function calculates the size of 32-bit RLE compression stream which is obtained from a given 32-bit buffer, 
  1.1483 +	where the top 8 bits are used to represent the alpha channel*/
  1.1484 +TInt CBitwiseBitmap::SizeOfThirtyTwoABitDataCompressed(TUint8* aData8,TInt aSizeInBytes) const
  1.1485 +	{
  1.1486 +	if(aSizeInBytes<=0)
  1.1487 +		return 0;
  1.1488 +
  1.1489 +	__ASSERT_DEBUG((reinterpret_cast<TUint32>(aData8) & 0x3)==0,Panic(EFbsBitmapDecompressionError)); // make sure the start address is word aligned
  1.1490 +	__ASSERT_DEBUG((aSizeInBytes & 0x3)==0,Panic(EFbsBitmapDecompressionError)); // make sure the size is whole no of pixels
  1.1491 +
  1.1492 +
  1.1493 +	TInt compressedSize = 0;
  1.1494 +	
  1.1495 +	
  1.1496 +	TUint32* ptr = reinterpret_cast<TUint32*>(aData8);
  1.1497 +	TUint32* srceLimitPtr = reinterpret_cast<TUint32*>(aData8 + aSizeInBytes);
  1.1498 +	TUint32* srceLimitPtr2ndLast = srceLimitPtr - 1; 
  1.1499 +	while (ptr < srceLimitPtr2ndLast)
  1.1500 +		{
  1.1501 +		TUint32* runStartPtr = ptr;
  1.1502 +		TUint32 pixel = *ptr++;
  1.1503 +
  1.1504 +		if (*ptr==pixel)
  1.1505 +			{
  1.1506 +			do
  1.1507 +				{
  1.1508 +				ptr++;
  1.1509 +				}
  1.1510 +			while (ptr < srceLimitPtr && *ptr==pixel);
  1.1511 +
  1.1512 +			TInt pixelLength = (ptr - runStartPtr);
  1.1513 +			compressedSize += 5*( ((pixelLength-1)>>7) + 1); 
  1.1514 +			}	
  1.1515 +		else
  1.1516 +			{
  1.1517 +			TBool more  = ETrue;
  1.1518 +			TBool eqRun = EFalse;
  1.1519 +			do
  1.1520 +				{
  1.1521 +				pixel = *ptr++;
  1.1522 +				more = (ptr < srceLimitPtr);
  1.1523 +				eqRun = more && *ptr==pixel;
  1.1524 +				} while (more && !eqRun);
  1.1525 +
  1.1526 +			if (eqRun)
  1.1527 +				ptr--;
  1.1528 +
  1.1529 +			TInt pixelLength = (ptr - runStartPtr) ;
  1.1530 +			compressedSize += 4*pixelLength + ((pixelLength-1)>>7) + 1; 
  1.1531 +			}
  1.1532 +		}
  1.1533 +
  1.1534 +	if (ptr < srceLimitPtr)
  1.1535 +		compressedSize += (srceLimitPtr - ptr)*4 + 1;
  1.1536 +
  1.1537 +	return compressedSize;
  1.1538 +	}
  1.1539 +
  1.1540 +
  1.1541 +/*	Here's a BNF description of the RLE encoding in use for all but the 12-bit compression:
  1.1542 +
  1.1543 +	encoding := run*
  1.1544 +	run := equal-run | unequal-run
  1.1545 +	equal-run := <runlength - 1> pixel-value
  1.1546 +	unequal-run := <-runlength> pixel-value+    (i.e. pixel-value repeated <runlength> times)
  1.1547 +	runlength := uint8 in 1..128
  1.1548 +	pixelvalue := byte-pixel | 16bit-pixel | 24bit-pixel | 32bit-u-pixel | 32bit-a-pixel
  1.1549 +	byte-pixel := octet
  1.1550 +	16bit-pixel := octet octet
  1.1551 +	24bit-pixel := rr gg bb
  1.1552 +	32bit-u-pixel := rr gg bb
  1.1553 +	32bit-a-pixel := rr gg bb aa
  1.1554 +	rr := octet  (red component)
  1.1555 +	gg := octet  (green component)
  1.1556 +	bb := octet  (blue component)
  1.1557 +	aa := octet  (alpha channel)
  1.1558 +
  1.1559 +	This scheme models runs of 2-128 equal pixels or of upto 127 unequal pixels.
  1.1560 +	Obviously a run of 0 is meaningless.  A run of 1 is considered to be an 'unequal' run containing
  1.1561 +	a single pixel.
  1.1562 +
  1.1563 +	For 12-bit compression a different encoding scheme is used. Only equal-runs are written, and the
  1.1564 +	run	length is encoded into the top 4 bits of a 16 bit word.  Here's a BNF for 12-bit compression:
  1.1565 +	
  1.1566 +	12-bit-encoding := run*
  1.1567 +	run := equal-run
  1.1568 +	equal-run := high-octet low-octet
  1.1569 +	high-octet := [<runlength - 1> rr]
  1.1570 +	low-octet := [gg bb]
  1.1571 +	runlength := 1..16
  1.1572 +	rr := quartet  (red component)
  1.1573 +	gg := quartet  (green component)
  1.1574 +	bb := quartet  (blue component)
  1.1575 +
  1.1576 +*/
  1.1577 +
  1.1578 +//Palette compression additions
  1.1579 +
  1.1580 +/**
  1.1581 +Attempts to compress a bitmap by reducing it to a palette + data.
  1.1582 +If the compression fails, for any of the reasons detailed below, RLE compression will be attempted instead.
  1.1583 +Prerequisites:
  1.1584 +- Bitmap must not already be compressed.
  1.1585 +- Bitmap must contain no more than 255 colors - If bitmap contains >255 colors then palette compression is unlikely to be effective.
  1.1586 +- Bitmap must be 16, 24 or 32 (non alpha) bpp.  Other modes could be implemented, but smaller bit depths will yield less compression
  1.1587 +Small bitmaps (under 1000 pixels) will be unlikely to compress well if at all
  1.1588 +Structure of compressed bitmap will be as follows;
  1.1589 +size of palette[4 bytes] | palette[size * 4 bytes per entry] | data[pixels * upto 1 byte per pixel]
  1.1590 +Bitmap data is packed into memory as efficiently as possible, according to the number of bits required.
  1.1591 +Each line of the compressed bitmap will start on a byte boundary, but not necessarily on a word boundary.
  1.1592 +*/
  1.1593 +TInt CBitwiseBitmap::PaletteCompress()
  1.1594 +	{
  1.1595 +	//Compression algorithm
  1.1596 +	//1.Iterate bitmap data, building hash of unique colours found
  1.1597 +	//2.Iterate the hash, and build linear array of the unique colours (this is the palette)
  1.1598 +	//3.Iterate the array, and set the array index of each colour back into the hash
  1.1599 +	//4.Iterate the bitmap data again, for each pixel replace with palette index found from hash
  1.1600 +	
  1.1601 +	//Bitmap must be uncompressed
  1.1602 +	if (iHeader.iCompression != ENoBitmapCompression)
  1.1603 +		return KErrNone;
  1.1604 +	
  1.1605 +	//There must be some data in the bitmap
  1.1606 +	TUint8* base = REINTERPRET_CAST(TUint8*, DataAddress());
  1.1607 +	if (!base)
  1.1608 +		return KErrNone;
  1.1609 +	
  1.1610 +	//Get the bytes per pixel, and make sure it is a supported configuration
  1.1611 +	TUint sourceBytesPerPixel = PaletteBytesPerPixel(iHeader.iBitsPerPixel);
  1.1612 +	if (sourceBytesPerPixel == 0)
  1.1613 +		return KErrNotSupported;
  1.1614 +	
  1.1615 +	//Create a hash table and give it a decent amount of RAM to start with
  1.1616 +	RHashMap<TInt,TInt> colors;
  1.1617 +	if (colors.Reserve(256) != KErrNone)
  1.1618 +		return KErrNoMemory;
  1.1619 +
  1.1620 +	//Loop the data area of the bitmap, one pixel (sourceBytesPerPixel bytes) at a time
  1.1621 +	TInt numPixels = (iHeader.iSizeInPixels.iWidth * iHeader.iSizeInPixels.iHeight);
  1.1622 +	//we need some pixels!
  1.1623 +	if (numPixels == 0)
  1.1624 +		{
  1.1625 +		colors.Close();
  1.1626 +		return KErrNone;
  1.1627 +		}
  1.1628 +	
  1.1629 +	TUint8* top = base + (iHeader.iSizeInPixels.iHeight * iByteWidth) ; //address of lastmost byte
  1.1630 +	TInt32 lineLengthInBytes = iHeader.iSizeInPixels.iWidth * sourceBytesPerPixel;  //The actual useful data on each line
  1.1631 +	TInt32 lineLengthInBytesPadded = iByteWidth ; //the actual (aligned) length of each line
  1.1632 +	TUint8* pixelAddress;
  1.1633 +	TUint8* lineAddress;
  1.1634 +	//Loop each line of the bitmap data.  Note that each line ends on a 32bit align in the source MBM data.
  1.1635 +	TUint8* lineEnd=0;
  1.1636 +
  1.1637 +	//Separate looping for 16 & 24+ bpp.  Adds a chunk of duplicated code but saves checking bpp for  every pixel
  1.1638 +	if (iHeader.iBitsPerPixel < 24)
  1.1639 +		{
  1.1640 +		for (lineAddress=base;lineAddress<top;lineAddress+=iByteWidth)
  1.1641 +			{
  1.1642 +			//Loop each pixel within the line
  1.1643 +			lineEnd = lineAddress + lineLengthInBytes;
  1.1644 +			for (pixelAddress = lineAddress; pixelAddress < lineEnd; pixelAddress += sourceBytesPerPixel)
  1.1645 +				{
  1.1646 +				//Extract colour value
  1.1647 +				TUint color = *pixelAddress;
  1.1648 +				color |= (*(pixelAddress+1)) << 8;
  1.1649 +				//Insert colour value into hash
  1.1650 +				//Quicker to use Find() to eliminate duplicates Insert()
  1.1651 +				if (colors.Find(color) == NULL)
  1.1652 +					{
  1.1653 +					if (colors.Insert(color,0) != KErrNone)
  1.1654 +						{
  1.1655 +						colors.Close();
  1.1656 +						return KErrNoMemory;
  1.1657 +						}
  1.1658 +					}
  1.1659 +				}
  1.1660 +			//Test each line whether it's worth continuing to the end	
  1.1661 +			//We aren't interested if there's more than 256 colours (RLE compression will probably do a better job in this case)
  1.1662 +			if (colors.Count() > 256)
  1.1663 +				{
  1.1664 +				colors.Close();
  1.1665 +				return KErrNotSupported ;
  1.1666 +				}
  1.1667 +			}
  1.1668 +		}
  1.1669 +	else //>=24 bpp
  1.1670 +		{
  1.1671 +		for (lineAddress = base; lineAddress<top; lineAddress += iByteWidth)
  1.1672 +			{
  1.1673 +			//Loop each pixel within the line
  1.1674 +			lineEnd = lineAddress + lineLengthInBytes;
  1.1675 +			for (pixelAddress = lineAddress; pixelAddress < lineEnd; pixelAddress += sourceBytesPerPixel)
  1.1676 +				{
  1.1677 +				//Extract colour value
  1.1678 +				TUint color = *pixelAddress;
  1.1679 +				color |= (*(pixelAddress+1)) << 8;
  1.1680 +				color |= (*(pixelAddress+2)) << 16;
  1.1681 +				//Insert colour value into hash
  1.1682 +				//Quicker to use Find() to eliminate duplicates Insert()
  1.1683 +
  1.1684 +				if (colors.Find(color) == NULL)
  1.1685 +					{
  1.1686 +					if (colors.Insert(color,0) != KErrNone)
  1.1687 +						{
  1.1688 +						colors.Close();
  1.1689 +						return KErrNoMemory;
  1.1690 +						}
  1.1691 +					}
  1.1692 +				}
  1.1693 +			//Test each line whether it's worth continuing to the end
  1.1694 +			//We aren't interested if there's more than 256 colours (RLE compression will probably do a better job in this case)
  1.1695 +			if (colors.Count() > 256)
  1.1696 +				{
  1.1697 +				colors.Close();
  1.1698 +				return KErrNotSupported;
  1.1699 +				}
  1.1700 +			} //for
  1.1701 +		}//end if
  1.1702 +	
  1.1703 +	
  1.1704 +	//Now we have enough data to build the palette by iterating all the entries in the colors hash
  1.1705 +	RArray<TUint> palette(colors.Count());
  1.1706 +	THashMapIter<TInt,TInt> colorIterator(colors);
  1.1707 +	const TInt* color = colorIterator.NextKey();
  1.1708 +	while (color)
  1.1709 +		{
  1.1710 +		if (palette.Append(*color) != KErrNone)
  1.1711 +			{
  1.1712 +			palette.Close();
  1.1713 +			colors.Close();
  1.1714 +			return KErrNoMemory;
  1.1715 +			}
  1.1716 +		//set the index of each entry back into the color hash for lookup later
  1.1717 +		//const cast needed as CurrentValue returns a const pointer; for no good reason?
  1.1718 +		TInt* index = CONST_CAST(TInt*, colorIterator.CurrentValue());
  1.1719 +		*index = palette.Count() - 1;
  1.1720 +		color = colorIterator.NextKey();
  1.1721 +		}
  1.1722 +		
  1.1723 +	//Set up some new memory for the palettised bitmap
  1.1724 +	//size is (4 bytes for palette size) + (4 bytes per palette entry) + (upto 1 byte per pixel)
  1.1725 +	//pixels are packed according to number of colors
  1.1726 +	TUint compressedBitsPerPixel = PaletteBitsPerPixel(colors.Count());
  1.1727 +	TUint compressedPixelsPerByte = 8 / compressedBitsPerPixel;
  1.1728 +	TUint compressedLineLengthInBytesPadded = (iHeader.iSizeInPixels.iWidth + compressedPixelsPerByte - 1)/ compressedPixelsPerByte;
  1.1729 +	TInt compressedDataBytes = 4 + 4 * colors.Count() + compressedLineLengthInBytesPadded * iHeader.iSizeInPixels.iHeight;
  1.1730 +
  1.1731 +	//add two extra ints for storing function pointers (8 bytes )
  1.1732 +	compressedDataBytes += 8 ;
  1.1733 +	
  1.1734 +	TUint8* compressedBase = NULL;
  1.1735 +	compressedBase = iPile->Alloc(compressedDataBytes);
  1.1736 +	if (!compressedBase)
  1.1737 +		{
  1.1738 +		palette.Close();
  1.1739 +		colors.Close();
  1.1740 +		return KErrNoMemory;
  1.1741 +		}
  1.1742 +	iDataOffset = compressedBase - iPile->ChunkBase();
  1.1743 +
  1.1744 +	iHeader.iBitmapSize = sizeof(SEpocBitmapHeader) + compressedDataBytes;
  1.1745 +	iHeader.iCompression = EGenericPaletteCompression;
  1.1746 +	
  1.1747 +	//copy the palette length into the data area...
  1.1748 +	*(REINTERPRET_CAST(TInt*, compressedBase)) = palette.Count(); //let's hope we're 4 byte aligned...
  1.1749 +	compressedBase+=4;
  1.1750 +	//...then the palette itself...
  1.1751 +	for (TUint loop = 0; loop < palette.Count(); loop++)
  1.1752 +		{
  1.1753 +		*(REINTERPRET_CAST(TInt*, compressedBase)) = palette[loop];
  1.1754 +		compressedBase +=4;
  1.1755 +		}
  1.1756 +		
  1.1757 +	//Work out, then store, the decoding functions required for packing density and colour depth.
  1.1758 +	//This saves having to do it for every scanline during decompression.
  1.1759 +
  1.1760 +	TDecodeFunction decodeFunction = NULL;
  1.1761 +	switch (compressedPixelsPerByte)
  1.1762 +		{
  1.1763 +		case 1:
  1.1764 +			decodeFunction=CBitwiseBitmap::PaletteDecode1PixelPerByte;
  1.1765 +			break;			
  1.1766 +		case 2:
  1.1767 +			decodeFunction=CBitwiseBitmap::PaletteDecode2PixelPerByte;
  1.1768 +			break;			
  1.1769 +		case 4:
  1.1770 +			decodeFunction=CBitwiseBitmap::PaletteDecode4PixelPerByte;
  1.1771 +			break;
  1.1772 +		case 8:
  1.1773 +			decodeFunction=CBitwiseBitmap::PaletteDecode8PixelPerByte;
  1.1774 +			break;			
  1.1775 +		default:
  1.1776 +			::Panic(EFbsNotSupportedForCompression);
  1.1777 +		}
  1.1778 +	*(REINTERPRET_CAST(TDecodeFunction*, compressedBase))  = decodeFunction ;
  1.1779 +	compressedBase += 4 ;
  1.1780 +
  1.1781 +	//Select the appropriate assignment function based on the bits-per-pixel of the target
  1.1782 +	TAssignFunction assignFunction = NULL;
  1.1783 +	switch (iHeader.iBitsPerPixel)
  1.1784 +		{
  1.1785 +		case 16:
  1.1786 +			assignFunction=CBitwiseBitmap::PaletteAssign16BitColor;
  1.1787 +			break;
  1.1788 +		case 24:
  1.1789 +			assignFunction=CBitwiseBitmap::PaletteAssign24BitColor;
  1.1790 +			break;
  1.1791 +		case 32:
  1.1792 +			assignFunction=CBitwiseBitmap::PaletteAssign32BitColor;
  1.1793 +			break;
  1.1794 +		default:
  1.1795 +			::Panic(EFbsNotSupportedForCompression);
  1.1796 +		}
  1.1797 +	*(REINTERPRET_CAST(TAssignFunction*, compressedBase)) = assignFunction ;
  1.1798 +	compressedBase += 4 ;
  1.1799 +		
  1.1800 + 	//...and finally the data
  1.1801 +	pixelAddress = base;
  1.1802 +
  1.1803 +	//separate loops for 16 & 24+ bpp again
  1.1804 +
  1.1805 +	if ( iHeader.iBitsPerPixel < 24 )
  1.1806 +		{
  1.1807 +		for (lineAddress = base; lineAddress < top; lineAddress += lineLengthInBytesPadded)
  1.1808 +			{
  1.1809 +			//Loop each pixel within the line
  1.1810 +			pixelAddress = lineAddress;
  1.1811 +			while (pixelAddress < lineAddress + lineLengthInBytes)
  1.1812 +				{
  1.1813 +				TUint8 pack = 0;
  1.1814 +				//loop for each pixel to pack into the byte.  If we run out of pixels in the line, we write out the pack byte and continue on the next line
  1.1815 +				for (TInt ii = 0; ii < compressedPixelsPerByte && pixelAddress < lineAddress + lineLengthInBytes; ii++)
  1.1816 +					{
  1.1817 +					pack <<= compressedBitsPerPixel;
  1.1818 +					//extract the color
  1.1819 +					TUint color = *pixelAddress;
  1.1820 +					color |= (*(pixelAddress+1)) << 8;
  1.1821 +					
  1.1822 +					//lookup the palette index for the color
  1.1823 +					TInt* paletteIndex = colors.Find(color);
  1.1824 +					//pack the palette index into the target byte
  1.1825 +					pack |= *paletteIndex;
  1.1826 +					//next pixel
  1.1827 +					pixelAddress += sourceBytesPerPixel;
  1.1828 +					}
  1.1829 +				//store the packed pixel data into the new compressed data area
  1.1830 +				*compressedBase = pack;
  1.1831 +				compressedBase++;				
  1.1832 +				}
  1.1833 +			}
  1.1834 +		}
  1.1835 +	else //>= 24bpp
  1.1836 +		{
  1.1837 +		for (lineAddress = base; lineAddress < top; lineAddress += lineLengthInBytesPadded)
  1.1838 +			{
  1.1839 +			//Loop each pixel within the line
  1.1840 +			pixelAddress = lineAddress;
  1.1841 +			while (pixelAddress < lineAddress + lineLengthInBytes)
  1.1842 +				{
  1.1843 +				TUint8 pack = 0;
  1.1844 +				//loop for each pixel to pack into the byte.  If we run out of pixels in the line, we write out the pack byte and continue on the next line
  1.1845 +				for (TInt ii = 0; ii < compressedPixelsPerByte && pixelAddress < lineAddress + lineLengthInBytes; ii++)
  1.1846 +					{
  1.1847 +					pack <<= compressedBitsPerPixel;
  1.1848 +					//extract the color
  1.1849 +				TUint color = *pixelAddress;
  1.1850 +					color |= (*(pixelAddress+1)) << 8;
  1.1851 +					color |= (*(pixelAddress+2)) << 16;
  1.1852 +					//if 32 bit, just ignore the 4th byte as it is unused for color data
  1.1853 +
  1.1854 +					//lookup the palette index for the color
  1.1855 +					TInt* paletteIndex = colors.Find(color);
  1.1856 +					//pack the palette index into the target byte
  1.1857 +					pack |= *paletteIndex;
  1.1858 +					//next pixel
  1.1859 +					pixelAddress += sourceBytesPerPixel;
  1.1860 +					}
  1.1861 +				//store the packed pixel data into the new compressed data area
  1.1862 +				*compressedBase = pack;
  1.1863 +				compressedBase++;				
  1.1864 +				}
  1.1865 +			}
  1.1866 +		}//if
  1.1867 +
  1.1868 +
  1.1869 +	//Set the RAM compression flag
  1.1870 +	iIsCompressedInRAM = ETrue;
  1.1871 +
  1.1872 +	//Free the old data.
  1.1873 +	iPile->Free(base);
  1.1874 +	//Clean up	
  1.1875 +	palette.Close();
  1.1876 +	colors.Close();
  1.1877 +	return KErrNone;
  1.1878 +	}
  1.1879 +	
  1.1880 +/**
  1.1881 +Create a scan line from a palette compressed bitmap.
  1.1882 +Starting from aPixel in the bitmap pointed to be aBase, populate aDestBuffer with aLength pixels looked up in the palette.
  1.1883 +Note this function assumes 16, 24 or 32 non alpha bit uncompressed bitmaps, compressed into 8 bit palettes (ie <256 colors)
  1.1884 +Structure of bitmap is (4 bytes for palette size) + (4 bytes per palette entry) + (1 byte per pixel)
  1.1885 +@param aDestBuffer Points to the destination buffer. After the call it will be filled
  1.1886 +with the decompressed data.
  1.1887 +@param aPixel The decompression starts from this pixel
  1.1888 +@param aLength Length of requested decompressed data - in pixels
  1.1889 +@param aBase Points to the beginning of compressed bitmap data
  1.1890 +@param aLineScannngPosition Saved information about the last used position in the compressed data
  1.1891 +*/
  1.1892 +void CBitwiseBitmap::GenerateLineFromPaletteCompressedData(
  1.1893 +									 TUint8* aDestBuffer, 
  1.1894 +									 const TPoint& aPixel,
  1.1895 +									 TInt aLength, 
  1.1896 +									 TUint32* aBase, 
  1.1897 +									 TLineScanningPosition& /*aLineScanningPosition*/) const
  1.1898 +	{
  1.1899 +	//At entry, aBase will point to the start of the compressed data, ie the palette size
  1.1900 +	//Each line in the bitmap will start at a byte boundary in the compressed data.
  1.1901 +	
  1.1902 +	TUint32* srcPalette = aBase + 1; //Address of the palette in the source data
  1.1903 +	TUint srcNumPaletteEntries = *aBase;	//Number of entries in the palette (will be <=255)
  1.1904 +	TUint compressedBitsPerPixel = PaletteBitsPerPixel(srcNumPaletteEntries);
  1.1905 +	__ASSERT_DEBUG(compressedBitsPerPixel <= 8, Panic( EFbsBitmapDecompressionError )) ;
  1.1906 +	const TUint8 lookup[] = {0, 8, 4, 0, 2, 0, 0, 0, 1};
  1.1907 +	//TUint compressedPixelsPerByte = 8 / compressedBitsPerPixel;
  1.1908 +	TUint compressedPixelsPerByte = lookup[compressedBitsPerPixel];
  1.1909 +	__ASSERT_DEBUG(compressedPixelsPerByte>0, ::Panic(EFbsNotSupportedForCompression));	
  1.1910 +
  1.1911 +	TUint8* srcData = REINTERPRET_CAST(TUint8*, srcPalette + srcNumPaletteEntries);	//Address of the pixel data in the source data
  1.1912 +
  1.1913 +	//Extract the function pointers for decoding functions. Set up decode & assign functions.
  1.1914 +	TDecodeFunction decodeFunction = NULL ;
  1.1915 +	decodeFunction = reinterpret_cast<TDecodeFunction>(*((TUint32*)srcData )) ;
  1.1916 +	srcData += 4 ;
  1.1917 +	TAssignFunction assignFunction = NULL;
  1.1918 +	assignFunction = reinterpret_cast<TAssignFunction>(*((TUint32*)srcData )) ;
  1.1919 +	srcData += 4 ;
  1.1920 +
  1.1921 +	//Note: The following lines have been optimised to avoid divisions.
  1.1922 +	//By way of explanation the original lines have been left as comments
  1.1923 +
  1.1924 +	//TUint srcBytesPerLinePadded = (iHeader.iSizeInPixels.iWidth + compressedPixelsPerByte - 1) / compressedPixelsPerByte;	//number of bytes occupied by each line in the compressed bitmap.
  1.1925 +	TUint srcBytesPerLinePadded = ((iHeader.iSizeInPixels.iWidth + compressedPixelsPerByte - 1)  * compressedBitsPerPixel) >> 3;	//number of bytes occupied by each line in the compressed bitmap.
  1.1926 +	//TUint srcStartBytesFromBase = aPixel.iY * srcBytesPerLinePadded + aPixel.iX / compressedPixelsPerByte;	//Starting bytes from the start of the bitmap
  1.1927 +	TUint srcStartBytesFromBase = aPixel.iY * srcBytesPerLinePadded + ((aPixel.iX * compressedBitsPerPixel) >> 3);	//Starting bytes from the start of the bitmap
  1.1928 +	//TUint srcStartPixelInByte = aPixel.iX  % compressedPixelsPerByte;		//starting pixel position in byte (lines start on byte boundary)
  1.1929 +	TUint srcStartPixelInByte = aPixel.iX & ((compressedPixelsPerByte)- 1);		//starting pixel position in byte (lines start on byte boundary)
  1.1930 +	//TUint srcEndBytesFromBase = srcStartBytesFromBase + (aLength + compressedPixelsPerByte - 1) / compressedPixelsPerByte;		//Ending bytes from the start of the bitmap
  1.1931 +	TUint srcEndBytesFromBase = srcStartBytesFromBase + (((aLength + compressedPixelsPerByte - 1) * compressedBitsPerPixel) >> 3);		//Ending bytes from the start of the bitmap
  1.1932 +	//TUint srcEndPixelInByte = (aPixel.iX + aLength) % compressedPixelsPerByte;		//Ending pixel position in byte
  1.1933 +	TUint srcEndPixelInByte = (aPixel.iX + aLength) & ((compressedPixelsPerByte)-1);		//Ending pixel position in byte
  1.1934 +	TUint8* srcStartData = srcData + srcStartBytesFromBase;	//Address of the first byte of packed pixels in the source
  1.1935 +	TUint8* srcEndData = srcData + srcEndBytesFromBase;	//Address of the last+1 byte of packed pixels in the source
  1.1936 +	TUint8* destStartData = aDestBuffer + ((aPixel.iX*iHeader.iBitsPerPixel) >> 3); //Address of the first pixel in the destination
  1.1937 +	
  1.1938 +	//3 stages to the decompression:
  1.1939 +	//1. Decompress any pixels which are a subset of the first byte
  1.1940 +	//2. Loop whole bytes extracting all pixels at once
  1.1941 +	//3. Decompress any pixels which are a subset of the last byte
  1.1942 +
  1.1943 +	TUint8* srcDataPtr = srcStartData;
  1.1944 +	
  1.1945 +	//Stage 1: Decompress any pixels which are a subset of the first byte
  1.1946 +	if (srcStartPixelInByte > 0)
  1.1947 +		PaletteDecodeAndAssignGeneric(srcDataPtr++, srcPalette, destStartData, srcStartPixelInByte, compressedPixelsPerByte-1, compressedPixelsPerByte, compressedBitsPerPixel);
  1.1948 +
  1.1949 +	//If the last byte is only partly packed with pixels from this line, stop one byte short in the main loop
  1.1950 +	if (srcEndPixelInByte > 0)
  1.1951 +		srcEndData--;
  1.1952 +	
  1.1953 +	//Stage 2: Loop all the required pixels and call the appropriate functions		
  1.1954 +	while (srcDataPtr < srcEndData)
  1.1955 +		{
  1.1956 +		__ASSERT_DEBUG(srcDataPtr <= srcEndData, ::Panic(EFbsNotSupportedForCompression));	
  1.1957 +		__ASSERT_DEBUG(destStartData <= aDestBuffer + ((aPixel.iX*iHeader.iBitsPerPixel) >> 3) + aLength * PaletteBytesPerPixel(iHeader.iBitsPerPixel), ::Panic(EFbsNotSupportedForCompression));	
  1.1958 +		(*decodeFunction)(srcDataPtr++, srcPalette, destStartData, assignFunction);
  1.1959 +		}
  1.1960 +	
  1.1961 +	//Stage 3: Decompress any pixels which are a subset of the last byte
  1.1962 +	if (srcEndPixelInByte > 0)
  1.1963 +		PaletteDecodeAndAssignGeneric(srcDataPtr++, srcPalette, destStartData, 0, srcEndPixelInByte-1, compressedPixelsPerByte, compressedBitsPerPixel);
  1.1964 +	}
  1.1965 +
  1.1966 +/** 
  1.1967 +This function deals with all different bit depths & color counts dynamically - smaller but slower
  1.1968 +@param aDataPtr Address in compressed data to read from
  1.1969 +@param aPalettePtr Address of the start of the palette in the compressed data
  1.1970 +@param aDestPtr Address to write uncompressed data to.  Will be incremented on return from function.
  1.1971 +@param aStartPixel Zero based position within the compressed byte of the first pixel to decompress
  1.1972 +@param aEndPixel Zero based position within the compressed byte of the last pixel to decompress
  1.1973 +@param aCompressedPixelsPerByte Number of pixels packed into each byte of the compressed data
  1.1974 +@param aCompressedBitsPerPixel Number of bits used to express each pixel in the compressed data.  Nothing to do with the color depth of the image.  
  1.1975 +*/
  1.1976 +void CBitwiseBitmap::PaletteDecodeAndAssignGeneric(	TUint8* aDataPtr, 
  1.1977 +												TUint32* aPalettePtr, 
  1.1978 +												TUint8*& aDestPtr,
  1.1979 +												TUint aStartPixel, 
  1.1980 +												TUint aEndPixel, 
  1.1981 +												TUint aCompressedPixelsPerByte,
  1.1982 +												TUint aCompressedBitsPerPixel) const
  1.1983 +	{
  1.1984 +	__ASSERT_DEBUG(aStartPixel<aCompressedPixelsPerByte, ::Panic(EFbsNotSupportedForCompression));
  1.1985 +	__ASSERT_DEBUG(aEndPixel<aCompressedPixelsPerByte, ::Panic(EFbsNotSupportedForCompression));
  1.1986 +
  1.1987 +	//create a mask for the appropriate number of bits
  1.1988 +	TUint8 mask = 0xFF;
  1.1989 +	mask <<= 8 - aCompressedBitsPerPixel;
  1.1990 +
  1.1991 +	//Adjust the mask in case we've been asked to start at an intermediate pixel
  1.1992 +	mask >>= aStartPixel * aCompressedBitsPerPixel;
  1.1993 +	
  1.1994 +	TUint8 pack = *aDataPtr; //max of 8 bits for palette entry
  1.1995 +	
  1.1996 +	//Loop the pixel data from the requested start to the requested end
  1.1997 +	for (TInt ii = aStartPixel;  ii <= aEndPixel;  ii++)
  1.1998 +		{	
  1.1999 +		//extract the bits corresponding to the required color index from the pack using the mask
  1.2000 +		TUint8 index = pack&mask;
  1.2001 +		//shift the index to the right to get true value
  1.2002 +		index >>= (aCompressedPixelsPerByte- ii - 1) * aCompressedBitsPerPixel;
  1.2003 +		//get the address of the required color value from the palette
  1.2004 +		TUint32 color = *(aPalettePtr + index);
  1.2005 +		//and copy the actual color value into the scanline buffer
  1.2006 +		*aDestPtr ++= color;
  1.2007 +		*aDestPtr ++= color >> 8;
  1.2008 +		if (iHeader.iBitsPerPixel >= 24)
  1.2009 +			*aDestPtr ++= color >> 16;
  1.2010 +		if (iHeader.iBitsPerPixel == 32)
  1.2011 +			*aDestPtr ++= 0xFF;  //use 0xFF rather than 0x00 as it is more alpha friendly
  1.2012 +		
  1.2013 +		//shift the mask to get the next required bits
  1.2014 +		mask >>= aCompressedBitsPerPixel;
  1.2015 +		}
  1.2016 +	}
  1.2017 +
  1.2018 +/** 
  1.2019 +Specialised function for decoding pixels from a palette compressed bitmap with 1 pixel packed in each byte.
  1.2020 +Implemented for speed, not size
  1.2021 +@param aDataPtr Address in compressed data to read from
  1.2022 +@param aPalettePtr Address of the start of the palette in the compressed data
  1.2023 +@param aDestPtr Address to write uncompressed data to.  Will be incremented on return from function.
  1.2024 +@param aAssignFunction Function pointer to assigment function to use to write actual pixel data to uncompressed scanline
  1.2025 +*/
  1.2026 +void CBitwiseBitmap::PaletteDecode1PixelPerByte(TUint8* aDataPtr, TUint32* aPalettePtr, TUint8*& aDestPtr, TAssignFunction aAssignFunction)
  1.2027 +	{
  1.2028 +	(*aAssignFunction)(aDestPtr, *(aPalettePtr + *aDataPtr));
  1.2029 +	}
  1.2030 +	
  1.2031 +/** 
  1.2032 +Specialised function for decoding pixels from a palette compressed bitmap with 2 pixels packed in each byte.
  1.2033 +Implemented for speed, not size
  1.2034 +@param aDataPtr Address in compressed data to read from
  1.2035 +@param aPalettePtr Address of the start of the palette in the compressed data
  1.2036 +@param aDestPtr Address to write uncompressed data to.  Will be incremented on return from function.
  1.2037 +@param aAssignFunction Function pointer to assigment function to use to write actual pixel data to uncompressed scanline
  1.2038 +*/
  1.2039 +void CBitwiseBitmap::PaletteDecode2PixelPerByte(TUint8* aDataPtr, TUint32* aPalettePtr, TUint8*& aDestPtr, TAssignFunction aAssignFunction)
  1.2040 +	{
  1.2041 +	TUint8 mask = 0xF0; //binary 11110000
  1.2042 +	TUint8 pack = *aDataPtr;
  1.2043 +	//Pixel 1
  1.2044 +	TUint8 index = pack&mask;
  1.2045 +	index >>= 4;
  1.2046 +	TUint32 color = *(aPalettePtr + index);
  1.2047 +	(*aAssignFunction)(aDestPtr, color);
  1.2048 +	mask >>= 4;
  1.2049 + 	//Pixel 2
  1.2050 + 	index = pack&mask;
  1.2051 + 	color = *(aPalettePtr + index);
  1.2052 +	(*aAssignFunction)(aDestPtr, color); 			
  1.2053 +	}
  1.2054 +
  1.2055 +/** 
  1.2056 +Specialised function for decoding pixels from a palette compressed bitmap with 4 pixels packed in each byte.
  1.2057 +Implemented for speed, not size
  1.2058 +@param aDataPtr Address in compressed data to read from
  1.2059 +@param aPalettePtr Address of the start of the palette in the compressed data
  1.2060 +@param aDestPtr Address to write uncompressed data to.  Will be incremented on return from function.
  1.2061 +@param aAssignFunction Function pointer to assigment function to use to write actual pixel data to uncompressed scanline
  1.2062 +*/
  1.2063 +void CBitwiseBitmap::PaletteDecode4PixelPerByte(TUint8* aDataPtr, TUint32* aPalettePtr, TUint8*& aDestPtr, TAssignFunction aAssignFunction)
  1.2064 +	{
  1.2065 +	TUint8 mask = 0xC0; //binary 11000000
  1.2066 +	TUint8 pack = *aDataPtr;
  1.2067 +	//Pixel 1
  1.2068 +	TUint8 index = pack&mask;
  1.2069 +	index >>= 6;
  1.2070 +	TUint32 color = *(aPalettePtr + index);
  1.2071 +	(*aAssignFunction)(aDestPtr, color);
  1.2072 +	mask >>= 2;
  1.2073 + 	//Pixel 2
  1.2074 + 	index = pack&mask;
  1.2075 +	index >>= 4;
  1.2076 + 	color = *(aPalettePtr + index);
  1.2077 +	(*aAssignFunction)(aDestPtr, color); 			
  1.2078 +	mask >>= 2;
  1.2079 + 	//Pixel 3
  1.2080 + 	index = pack&mask;
  1.2081 +	index >>= 2;
  1.2082 + 	color = *(aPalettePtr + index);
  1.2083 +	(*aAssignFunction)(aDestPtr, color); 			
  1.2084 +	mask >>= 2;
  1.2085 + 	//Pixel 4
  1.2086 + 	index = pack&mask;
  1.2087 + 	color = *(aPalettePtr + index);
  1.2088 +	(*aAssignFunction)(aDestPtr, color); 			
  1.2089 +	}
  1.2090 +
  1.2091 +/** 
  1.2092 +Specialised function for decoding pixels from a palette compressed bitmap with 8 pixels packed in each byte.
  1.2093 +Implemented for speed, not size
  1.2094 +@param aDataPtr Address in compressed data to read from
  1.2095 +@param aPalettePtr Address of the start of the palette in the compressed data
  1.2096 +@param aDestPtr Address to write uncompressed data to.  Will be incremented on return from function.
  1.2097 +@param aAssignFunction Function pointer to assigment function to use to write actual pixel data to uncompressed scanline
  1.2098 +*/
  1.2099 +void CBitwiseBitmap::PaletteDecode8PixelPerByte(TUint8* aDataPtr, TUint32* aPalettePtr, TUint8*& aDestPtr, TAssignFunction aAssignFunction)
  1.2100 +	{
  1.2101 +	TUint8 mask = 0x80; //binary 10000000
  1.2102 +	TUint8 pack = *aDataPtr;
  1.2103 +	//Pixel 1
  1.2104 +	TUint8 index = pack&mask;
  1.2105 +	index >>= 7;
  1.2106 +	TUint32 color = *(aPalettePtr + index);
  1.2107 +	(*aAssignFunction)(aDestPtr, color);
  1.2108 +	mask >>= 1;
  1.2109 + 	//Pixel 2
  1.2110 + 	index = pack&mask;
  1.2111 +	index >>= 6;
  1.2112 + 	color = *(aPalettePtr + index);
  1.2113 +	(*aAssignFunction)(aDestPtr, color); 			
  1.2114 +	mask>>=1;
  1.2115 + 	//Pixel 3
  1.2116 + 	index = pack&mask;
  1.2117 +	index >>= 5;
  1.2118 + 	color = *(aPalettePtr + index);
  1.2119 +	(*aAssignFunction)(aDestPtr, color); 			
  1.2120 +	mask >>= 1;
  1.2121 +  	//Pixel 4
  1.2122 + 	index = pack&mask;
  1.2123 +	index >>= 4;
  1.2124 + 	color = *(aPalettePtr + index);
  1.2125 +	(*aAssignFunction)(aDestPtr, color); 			
  1.2126 +	mask >>= 1;
  1.2127 +  	//Pixel 5
  1.2128 + 	index = pack&mask;
  1.2129 +	index >>= 3;
  1.2130 + 	color = *(aPalettePtr + index);
  1.2131 +	(*aAssignFunction)(aDestPtr, color); 			
  1.2132 +	mask >>= 1;
  1.2133 +  	//Pixel 6
  1.2134 + 	index = pack&mask;
  1.2135 +	index >>= 2;
  1.2136 + 	color = *(aPalettePtr + index);
  1.2137 +	(*aAssignFunction)(aDestPtr, color); 			
  1.2138 +	mask >>= 1;
  1.2139 +  	//Pixel 7
  1.2140 + 	index = pack&mask;
  1.2141 +	index >>= 1;
  1.2142 + 	color = *(aPalettePtr + index);
  1.2143 +	(*aAssignFunction)(aDestPtr, color); 			
  1.2144 +	mask >>= 1;
  1.2145 + 	//Pixel 8
  1.2146 + 	index = pack&mask;
  1.2147 + 	color = *(aPalettePtr + index);
  1.2148 +	(*aAssignFunction)(aDestPtr, color); 			
  1.2149 +	}
  1.2150 +
  1.2151 +/** 
  1.2152 +Specialised function for assigning pixels into an uncompressed scanline of 16 bit color depth.
  1.2153 +Implemented for speed, not size
  1.2154 +@param aDestPtr Address to write uncompressed data to.  Will be incremented on return from function.
  1.2155 +@param aColor Color info to write.
  1.2156 +*/
  1.2157 +void CBitwiseBitmap::PaletteAssign16BitColor(TUint8*& aDestPtr, TUint32 aColor)
  1.2158 +	{
  1.2159 +	*aDestPtr ++= aColor;
  1.2160 +	*aDestPtr ++= aColor >> 8;
  1.2161 +	}
  1.2162 +
  1.2163 +/** 
  1.2164 +Specialised function for assigning pixels into an uncompressed scanline of 24 bit color depth.
  1.2165 +Implemented for speed, not size
  1.2166 +@param aDestPtr Address to write uncompressed data to.  Will be incremented on return from function.
  1.2167 +@param aColor Color info to write.
  1.2168 +*/
  1.2169 +void CBitwiseBitmap::PaletteAssign24BitColor(TUint8*& aDestPtr, TUint32 aColor)
  1.2170 +	{
  1.2171 +	*aDestPtr ++= aColor;
  1.2172 +	*aDestPtr ++= aColor >> 8;
  1.2173 +	*aDestPtr ++= aColor >> 16;
  1.2174 +	}
  1.2175 +
  1.2176 +/** 
  1.2177 +Specialised function for assigning pixels into an uncompressed scanline of 32 bit color depth.
  1.2178 +Implemented for speed, not size
  1.2179 +@param aDestPtr Address to write uncompressed data to.  Will be incremented on return from function.
  1.2180 +@param aColor Color info to write.
  1.2181 +*/
  1.2182 +void CBitwiseBitmap::PaletteAssign32BitColor(TUint8*& aDestPtr, TUint32 aColor)
  1.2183 +	{
  1.2184 +	*aDestPtr ++= aColor;
  1.2185 +	*aDestPtr ++= aColor >> 8;
  1.2186 +	*aDestPtr ++= aColor >> 16;
  1.2187 +	*aDestPtr ++= 0xFF;  //use 0xFF rather than 0x00 as it is more alpha friendly
  1.2188 +	}
  1.2189 +
  1.2190 +/**
  1.2191 +Get the bits used per pixel when packing multiple pixels in palette compression.
  1.2192 +The value returned is a power of 2, not always the most efficient pack, for alignment reasons, Eg
  1.2193 + 65537 -> KMaxTInt : 32 bpp
  1.2194 + 257 -> 65536 colors : 16 bpp
  1.2195 + 17 -> 256 colors : 8 bpp
  1.2196 + 5  -> 16  colors : 4 bpp
  1.2197 + 3  -> 4   colors : 2 bpp
  1.2198 + 0  -> 2   colors : 1 bpp
  1.2199 +@param aNumColors The number of colors in the bitmap.  This governs the size of the palette and thus
  1.2200 +the number of bits needed to represent an index into it.
  1.2201 +*/
  1.2202 +TUint CBitwiseBitmap::PaletteBitsPerPixel(TInt aNumColors) const
  1.2203 +	{
  1.2204 +	if (aNumColors <= 2)
  1.2205 +		return 1;
  1.2206 +	else if (aNumColors <= 4)
  1.2207 +		return 2;
  1.2208 +	else if (aNumColors <= 16)
  1.2209 +		return 4;
  1.2210 +	else if (aNumColors <= 256)
  1.2211 +		return 8;
  1.2212 +	else if (aNumColors <= 65536)
  1.2213 +		return 16;
  1.2214 +	else
  1.2215 +		return 32;
  1.2216 +	}
  1.2217 +	
  1.2218 +/**
  1.2219 +Gets the bytes used per pixel according to the bits per pixel of a bitmap.
  1.2220 +Also used to find which bit per pixel values are supported by palette compression, hence this is not
  1.2221 +a case of simple division by 8.
  1.2222 +If return value is zero, the supplied bit per pixel value is not supported by palette compression.
  1.2223 +@param aBitsPerPixel The bits per pixel value to transform into bytes
  1.2224 +*/
  1.2225 +TUint CBitwiseBitmap::PaletteBytesPerPixel(TInt aBitsPerPixel) const
  1.2226 +	{
  1.2227 +	if (aBitsPerPixel == 16)
  1.2228 +		return 2;  //16 bit
  1.2229 +	else if (aBitsPerPixel == 24)
  1.2230 +		return 3;   //24 bit
  1.2231 +	else if (aBitsPerPixel == 32 && iHeader.iColor == SEpocBitmapHeader::EColor)
  1.2232 +		return 4;	//32 bit with no alpha
  1.2233 +	else 
  1.2234 +		return 0;
  1.2235 +	}
  1.2236 +