os/graphics/fbs/fontandbitmapserver/sfbs/BITBMP.CPP
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/graphics/fbs/fontandbitmapserver/sfbs/BITBMP.CPP	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,3369 @@
     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 <fbs.h>
    1.21 +#include <bitmap.h>
    1.22 +#include <graphicsaccelerator.h>
    1.23 +#include <graphics/lookuptable.h>
    1.24 +#include <graphics/blendingalgorithms.h>
    1.25 +#include <graphics/bitmapuid.h>
    1.26 +#include "UTILS.H"
    1.27 +#include <s32mem.h>
    1.28 +#include "ShiftedFileStore.h"
    1.29 +#include "CompileAssert.h"
    1.30 +#include "CompressionBookmark.h"
    1.31 +#include "BitmapCompr.h"
    1.32 +#include "palette.h"
    1.33 +#include "fbsrasterizer.h"
    1.34 +#include "bitmap.inl"
    1.35 +#include "BitwiseBitmap.inl"
    1.36 +#include "bitmapconst.h"
    1.37 +
    1.38 +#ifdef __ARMCC__
    1.39 +#pragma arm
    1.40 +#pragma O3
    1.41 +#pragma Otime
    1.42 +#endif
    1.43 +
    1.44 +const TInt KMaxPixelSize = KMaxTInt / 4; // Maximum pixel size to avoid some overflow problems
    1.45 +const TUint KMaxByteSize = TUint(KMaxTInt / 2); // Maximum byte size to avoid other overflow problems
    1.46 +enum
    1.47 +    {
    1.48 +    EFirstTime = 65536
    1.49 +    };
    1.50 +GLREF_C void Panic(TFbsPanic aPanic);
    1.51 +
    1.52 +#define COLOR_VALUE(ScanLinePtr, XPos) (*((ScanLinePtr) + ((XPos) >> 5)) & ( 1 << ((XPos) & 0x1F)))
    1.53 +
    1.54 +EXPORT_C void CBitwiseBitmap::operator delete(TAny *aThis)
    1.55 +	{
    1.56 +	if (((CBitwiseBitmap*)aThis)->iHeap)
    1.57 +		((CBitwiseBitmap*)aThis)->iHeap->Free(aThis);
    1.58 +	}
    1.59 +
    1.60 +EXPORT_C CBitwiseBitmap::CBitwiseBitmap(RHeap* aHeap,CChunkPile* aPile):
    1.61 +	iUid(KCBitwiseBitmapUid),
    1.62 +	iSettings(ENone),
    1.63 +	iHeap(aHeap),
    1.64 +	iPile(aPile),
    1.65 +	iByteWidth(0),
    1.66 +	iDataOffset(0),
    1.67 +	iIsCompressedInRAM(EFalse)
    1.68 +	{
    1.69 +	//CBitwiseBitmap size can't be changed! If the bitmap is ROM based,
    1.70 +	//then CBitwiseBitmap object is not created, but CBitwiseBitmap pointer is
    1.71 +	//used to access ROM based bitmap data.
    1.72 +	//The following line is a part of CFbsBitmap::DoLoad(...) source code:
    1.73 +	//iRomPointer=(CBitwiseBitmap*)(((TUint8*)rompointer)+offset);
    1.74 +	//We have to preserve data compatibility with already existing ROM bitmaps.
    1.75 +	enum
    1.76 +		{
    1.77 +		KCBitwiseBitmapSize = 72
    1.78 +		};
    1.79 +	COMPILE_TIME_ASSERT(sizeof(CBitwiseBitmap) == KCBitwiseBitmapSize);
    1.80 +#ifdef SYMBIAN_DEBUG_FBS_LOCKHEAP
    1.81 +	Extra()->iLockCount = 0;
    1.82 +	Extra()->iThreadId = TThreadId(KNullThreadId);
    1.83 +#endif
    1.84 +	Extra()->iTouchCount = 0;
    1.85 +	Extra()->iSerialNumber = 0;
    1.86 +	}
    1.87 +
    1.88 +EXPORT_C CBitwiseBitmap::~CBitwiseBitmap()
    1.89 +	{
    1.90 +	Reset();
    1.91 +	}
    1.92 +
    1.93 +EXPORT_C void CBitwiseBitmap::Reset()
    1.94 +	{
    1.95 +	if (iDataOffset)
    1.96 +		{
    1.97 +		if(iUid.iUid==KCBitwiseBitmapHardwareUid.iUid)
    1.98 +			{
    1.99 +			RHardwareBitmap hwb(iDataOffset);	// iDataOffset = handle for hardware bitmap
   1.100 +			hwb.Destroy();
   1.101 +			}
   1.102 +		else
   1.103 +			if (iPile) iPile->Free(DataAddress());
   1.104 +		}
   1.105 +	iUid.iUid = KCBitwiseBitmapUid.iUid;
   1.106 +	iDataOffset=0;
   1.107 +	iSettings.SetDisplayModes(ENone);
   1.108 +	iByteWidth=0;
   1.109 +	iHeader=SEpocBitmapHeader();
   1.110 +	iIsCompressedInRAM=EFalse;
   1.111 +	}
   1.112 +
   1.113 +EXPORT_C TUid CBitwiseBitmap::Uid() const
   1.114 +	{
   1.115 +	return(iUid);
   1.116 +	}
   1.117 +
   1.118 +EXPORT_C TInt CBitwiseBitmap::Construct(const TSize& aSize,TDisplayMode aDispMode,TUid aCreatorUid)
   1.119 +	{
   1.120 +	if (iHeap == NULL || iPile == NULL)
   1.121 +		return KErrNoMemory;
   1.122 +
   1.123 +	if (aSize.iWidth > KMaxPixelSize || aSize.iHeight > KMaxPixelSize)
   1.124 +		return KErrTooBig;
   1.125 +
   1.126 +	TUint8* data = NULL;
   1.127 +	Reset();
   1.128 +
   1.129 +	iSettings.SetDisplayModes(aDispMode);
   1.130 +	iByteWidth = ByteWidth(aSize.iWidth,aDispMode);
   1.131 +
   1.132 +	TInt64 hugeDataSize = TInt64(aSize.iHeight) * TInt64(iByteWidth);
   1.133 +	if (I64HIGH(hugeDataSize) != 0 || I64LOW(hugeDataSize) > KMaxByteSize)
   1.134 +		return KErrTooBig;
   1.135 +
   1.136 +	TInt dataSize = I64LOW(hugeDataSize);
   1.137 +	iHeader.iBitmapSize = sizeof(SEpocBitmapHeader) + dataSize;
   1.138 +	iHeader.iStructSize = sizeof(SEpocBitmapHeader);
   1.139 +	iHeader.iSizeInPixels = aSize;
   1.140 +	iHeader.iSizeInTwips = TSize(0,0);
   1.141 +	iHeader.iBitsPerPixel = Bpp(aDispMode);
   1.142 +	iHeader.iColor = IsColor(aDispMode);
   1.143 +	iHeader.iPaletteEntries = 0;
   1.144 +	iHeader.iCompression = ENoBitmapCompression;
   1.145 +
   1.146 +	if (aSize.iHeight && aSize.iWidth)
   1.147 +		{
   1.148 +		if(aCreatorUid!=KUidCFbsBitmapCreation)
   1.149 +			{
   1.150 +			RHardwareBitmap hwb;
   1.151 +			TAcceleratedBitmapInfo info;
   1.152 +			TInt ret = hwb.Create(aDispMode,aSize,aCreatorUid);
   1.153 +			if(ret==KErrNone)
   1.154 +				ret = hwb.GetInfo(info);
   1.155 +			if(ret!=KErrNone)
   1.156 +				{
   1.157 +				Reset();
   1.158 +				return ret;
   1.159 +				}
   1.160 +
   1.161 +			iSettings.SetVolatileBitmap();
   1.162 +			data = info.iAddress;
   1.163 +			dataSize = info.iLinePitch*info.iSize.iHeight;
   1.164 +			__ASSERT_DEBUG(info.iLinePitch >= iByteWidth, ::Panic(EFbsHardwareBitmapError));
   1.165 +			iByteWidth = info.iLinePitch;
   1.166 +
   1.167 +			iDataOffset = hwb.iHandle;	// iDataOffset = handle for hardware bitmap
   1.168 +			iUid.iUid = KCBitwiseBitmapHardwareUid.iUid;
   1.169 +#ifdef SYMBIAN_DISABLE_HARDWARE_BITMAP_WHITEFILL
   1.170 +			return KErrNone;
   1.171 +#endif
   1.172 +			}
   1.173 +		else
   1.174 +			{
   1.175 +			data = iPile->Alloc(dataSize);
   1.176 +			iDataOffset = data - iPile->ChunkBase();
   1.177 +			}
   1.178 +		if (!data)
   1.179 +			{
   1.180 +			iDataOffset=0;
   1.181 +			Reset();
   1.182 +			return(KErrNoMemory);	// no memory exit point
   1.183 +			}
   1.184 +		}
   1.185 +
   1.186 +	if (dataSize < KMaxLargeBitmapAlloc || aDispMode == EColor4K || iUid.iUid == KCBitwiseBitmapHardwareUid.iUid)
   1.187 +		{
   1.188 +		WhiteFill(data,dataSize,aDispMode);
   1.189 +		}
   1.190 +
   1.191 +	return KErrNone;	// success exit point
   1.192 +	}
   1.193 +
   1.194 +
   1.195 +EXPORT_C TInt CBitwiseBitmap::ConstructExtended(const TSize& aSize, TDisplayMode aDispMode, TUid aType, TInt aDataSize)
   1.196 +	{
   1.197 +	if (iHeap == NULL || iPile == NULL)
   1.198 +		return KErrNoMemory;
   1.199 +	if (aSize.iWidth > KMaxPixelSize || aSize.iHeight > KMaxPixelSize)
   1.200 +		return KErrTooBig;
   1.201 +	if (aType.iUid == KCBitwiseBitmapUid.iUid || aType.iUid == KCBitwiseBitmapHardwareUid.iUid)
   1.202 +		return KErrArgument; // make sure the extended bitmap type is not one of the standard types
   1.203 +	if (aDataSize > KMaxByteSize)
   1.204 +		return KErrTooBig;
   1.205 +	Reset();
   1.206 +	iUid = aType;
   1.207 +	iSettings.SetDisplayModes(aDispMode);
   1.208 +	iByteWidth = ByteWidth(aSize.iWidth, aDispMode);
   1.209 +	iHeader.iBitmapSize = sizeof(SEpocBitmapHeader) + aDataSize;
   1.210 +	iHeader.iStructSize = sizeof(SEpocBitmapHeader);
   1.211 +	iHeader.iSizeInPixels = aSize;
   1.212 +	iHeader.iSizeInTwips = TSize(0,0);
   1.213 +	iHeader.iBitsPerPixel = Bpp(aDispMode);
   1.214 +	iHeader.iColor = IsColor(aDispMode);
   1.215 +	iHeader.iPaletteEntries = 0;
   1.216 +	iHeader.iCompression = EProprietaryCompression;
   1.217 +	TUint8* data = iPile->Alloc(aDataSize);
   1.218 +	if (!data)
   1.219 +		{
   1.220 +		Reset();
   1.221 +		return KErrNoMemory;
   1.222 +		}
   1.223 +	iDataOffset = data - iPile->ChunkBase();
   1.224 +	return KErrNone;
   1.225 +	}
   1.226 +
   1.227 +
   1.228 +EXPORT_C void CBitwiseBitmap::ConstructL(RFs& aFs,const TDesC& aFilename,TInt32 aId,TUint aFileOffset)
   1.229 +	{
   1.230 +	//If aFileOffset != 0 then aFilename is a file with an embedded MBM file section at the end.
   1.231 +	//The implementation uses the fact that mbm files are implemented as
   1.232 +	//filestores and stream ID is actually the offset from the beginning of the filestore.
   1.233 +	//If stream ID changes its meaning in the future -
   1.234 +	//the method implementation has to be reviewed and changed too.
   1.235 +	User::LeaveIfNull(iHeap);
   1.236 +	User::LeaveIfNull(iPile);
   1.237 +	TUint fileMode = EFileRead;
   1.238 +	if(aFileOffset != 0) //This is a file with an embedded MBM file section at the end.
   1.239 +		fileMode |= EFileShareReadersOnly;
   1.240 +	CShiftedFileStore* filestore = CShiftedFileStore::OpenLC(aFs,aFilename,fileMode,aFileOffset);
   1.241 +	TStreamId streamid = filestore->Root();
   1.242 +	//TStreamId is the offset from the beggining of the file.
   1.243 +	//Obviously, if the bitmap file section is at the middle of the physical file,
   1.244 +	//we should add aFileOffset value to TStreamId value and use it.
   1.245 +	TStreamId streamid2(streamid.Value() + aFileOffset);
   1.246 +	RStoreReadStream readstream;
   1.247 +	readstream.OpenLC(*filestore,streamid2);
   1.248 +	TInt numbitmaps = readstream.ReadInt32L();
   1.249 +	if (aId < 0 || aId >= numbitmaps)
   1.250 +		User::Leave(KErrEof);
   1.251 +
   1.252 +	TStreamId bmpstreamid;
   1.253 +	bmpstreamid.InternalizeL(readstream);
   1.254 +	TStreamId bmpstreamid2(bmpstreamid.Value() + aFileOffset);
   1.255 +	for (TInt count = 0; count < aId; count++)
   1.256 +		{
   1.257 +		bmpstreamid2.InternalizeL(readstream);
   1.258 +		bmpstreamid2 = TStreamId(bmpstreamid2.Value() + aFileOffset);
   1.259 +		}
   1.260 +
   1.261 +	CleanupStack::PopAndDestroy();
   1.262 +	RStoreReadStream bmpstream;
   1.263 +	bmpstream.OpenLC(*filestore,bmpstreamid2);
   1.264 +	InternalizeL(bmpstream);
   1.265 +	CleanupStack::PopAndDestroy(2);
   1.266 +	}
   1.267 +
   1.268 +EXPORT_C void CBitwiseBitmap::ConstructL(RFile& aFile,TInt32 aId,TUint aFileOffset)
   1.269 +	{
   1.270 +	//If aFileOffset != 0 then aFilename is a file with an embedded MBM file section at the end.
   1.271 +	//The implementation uses the fact that mbm files are implemented as
   1.272 +	//filestores and stream ID is actually the offset from the beginning of the filestore.
   1.273 +	//If stream ID changes its meaning in the future -
   1.274 +	//the method implementation has to be reviewed and changed too.
   1.275 +	User::LeaveIfNull(iHeap);
   1.276 +	User::LeaveIfNull(iPile);
   1.277 +
   1.278 +	CShiftedFileStore* filestore = CShiftedFileStore::FromL(aFile,aFileOffset);
   1.279 +	CleanupStack::PushL(filestore);
   1.280 +	TStreamId streamid = filestore->Root();
   1.281 +	//TStreamId is the offset from the beggining of the file.
   1.282 +	//Obviously, if the bitmap file section is at the middle of the physical file,
   1.283 +	//we should add aFileOffset value to TStreamId value and use it.
   1.284 +	TStreamId streamid2(streamid.Value() + aFileOffset);
   1.285 +	RStoreReadStream readstream;
   1.286 +	readstream.OpenLC(*filestore,streamid2);
   1.287 +	TInt numbitmaps = readstream.ReadInt32L();
   1.288 +	if (aId < 0 || aId >= numbitmaps)
   1.289 +		User::Leave(KErrEof);
   1.290 +	//Retrieving the streamid of the bitmap of that id from the file
   1.291 +	TStreamId bmpstreamid;
   1.292 +	bmpstreamid.InternalizeL(readstream);
   1.293 +	TStreamId bmpstreamid2(bmpstreamid.Value() + aFileOffset);
   1.294 +	for (TInt count = 0; count < aId; count++)
   1.295 +		{
   1.296 +		bmpstreamid2.InternalizeL(readstream);
   1.297 +		bmpstreamid2 = TStreamId(bmpstreamid2.Value() + aFileOffset);
   1.298 +		}
   1.299 +	//Use the streamid found to initialize the bitmap raw data in the memory
   1.300 +	CleanupStack::PopAndDestroy(&readstream);
   1.301 +	RStoreReadStream bmpstream;
   1.302 +	bmpstream.OpenLC(*filestore,bmpstreamid2);
   1.303 +	InternalizeL(bmpstream);
   1.304 +	CleanupStack::PopAndDestroy(2,filestore);
   1.305 +	}
   1.306 +
   1.307 +
   1.308 +EXPORT_C void CBitwiseBitmap::ConstructL(CShiftedFileStore* aFileStore,TStreamId aStreamId)
   1.309 +	{
   1.310 +	User::LeaveIfNull(iHeap);
   1.311 +	User::LeaveIfNull(iPile);
   1.312 +
   1.313 +	RStoreReadStream bmpstream;
   1.314 +	bmpstream.OpenLC(*aFileStore,aStreamId);
   1.315 +	InternalizeL(bmpstream);
   1.316 +	CleanupStack::PopAndDestroy();
   1.317 +	}
   1.318 +
   1.319 +EXPORT_C TInt CBitwiseBitmap::CopyData(const CBitwiseBitmap& aSourceBitmap)
   1.320 +	{
   1.321 +	__ASSERT_DEBUG(iHeap && iPile, ::Panic(EFbsPanicBitmapDataCopy));
   1.322 +	__ASSERT_DEBUG(!iIsCompressedInRAM, ::Panic(EFbsPanicBitmapDataCopy));
   1.323 +	__ASSERT_DEBUG(iUid.iUid == KCBitwiseBitmapUid.iUid, ::Panic(EFbsPanicBitmapDataCopy));
   1.324 +	if (aSourceBitmap.iUid.iUid != KCBitwiseBitmapUid.iUid)
   1.325 +		return KErrNotSupported;
   1.326 +	const TDisplayMode displayMode = aSourceBitmap.iSettings.CurrentDisplayMode();
   1.327 +	__ASSERT_DEBUG(iSettings.CurrentDisplayMode() == displayMode, ::Panic(EFbsPanicBitmapDataCopy));
   1.328 +	if (aSourceBitmap.iHeader.iSizeInPixels.iWidth > 0)
   1.329 +		iHeader.iSizeInTwips.iWidth = (aSourceBitmap.iHeader.iSizeInTwips.iWidth * iHeader.iSizeInPixels.iWidth)
   1.330 +			/ aSourceBitmap.iHeader.iSizeInPixels.iWidth;
   1.331 +	if (aSourceBitmap.iHeader.iSizeInPixels.iHeight > 0)
   1.332 +		iHeader.iSizeInTwips.iHeight = (aSourceBitmap.iHeader.iSizeInTwips.iHeight * iHeader.iSizeInPixels.iHeight)
   1.333 +			/ aSourceBitmap.iHeader.iSizeInPixels.iHeight;
   1.334 +	TUint32* destBase = DataAddress();
   1.335 +	TUint32* srcBase = aSourceBitmap.DataAddress();
   1.336 +	if (!destBase || !srcBase)
   1.337 +		return KErrNone;
   1.338 +	TInt minPixelHeight = Min(iHeader.iSizeInPixels.iHeight, aSourceBitmap.iHeader.iSizeInPixels.iHeight);
   1.339 +	if (aSourceBitmap.iIsCompressedInRAM)
   1.340 +		{
   1.341 +		TUint8* dest = (TUint8*)destBase;
   1.342 +		TInt minPixelWidth = Min(iHeader.iSizeInPixels.iWidth, aSourceBitmap.iHeader.iSizeInPixels.iWidth);
   1.343 +		TPtr8 pDest(dest, iByteWidth, iByteWidth);
   1.344 +		TPoint pt(0, 0);
   1.345 +		TPoint ditherOffset(0, 0);
   1.346 +		TLineScanningPosition scanPos(srcBase);
   1.347 +		scanPos.iScanLineBuffer = HBufC8::New(aSourceBitmap.iByteWidth + 4);
   1.348 +		if (!scanPos.iScanLineBuffer)
   1.349 +			return KErrNoMemory;
   1.350 +		for (TInt row = 0; row < minPixelHeight; ++row)
   1.351 +			{
   1.352 +			pDest.Set(dest, iByteWidth, iByteWidth);
   1.353 +			pt.iY = row;
   1.354 +			aSourceBitmap.GetScanLine(pDest, pt, minPixelWidth, EFalse, ditherOffset, displayMode, srcBase, scanPos);
   1.355 +			dest += iByteWidth;
   1.356 +			}
   1.357 +		delete scanPos.iScanLineBuffer;
   1.358 +		}
   1.359 +	else
   1.360 +		{
   1.361 +		TUint8* dest = (TUint8*)destBase;
   1.362 +		TUint8* src = (TUint8*)srcBase;
   1.363 +		TInt minByteWidth = Min(iByteWidth, aSourceBitmap.iByteWidth);
   1.364 +		for(TInt row = 0; row < minPixelHeight; ++row)
   1.365 +			{
   1.366 +			Mem::Copy(dest, src, minByteWidth);
   1.367 +			dest += iByteWidth;
   1.368 +			src += aSourceBitmap.iByteWidth;
   1.369 +			}
   1.370 +		}
   1.371 +	if (iHeader.iSizeInPixels.iWidth > aSourceBitmap.iHeader.iSizeInPixels.iWidth)
   1.372 +		{
   1.373 +		TInt extraBits = (aSourceBitmap.iHeader.iSizeInPixels.iWidth * aSourceBitmap.iHeader.iBitsPerPixel) & 31;
   1.374 +		if (extraBits > 0)
   1.375 +			{
   1.376 +			TUint32 mask = KMaxTUint32;
   1.377 +			mask <<= extraBits;
   1.378 +			TInt destWordWidth = iByteWidth >> 2;
   1.379 +			TInt srcWordWidth = aSourceBitmap.iByteWidth >> 2;
   1.380 +			TUint32* maskAddress = destBase + srcWordWidth - 1;
   1.381 +			for (TInt row = 0; row < minPixelHeight; ++row)
   1.382 +				{
   1.383 +				*maskAddress |= mask;
   1.384 +				maskAddress += destWordWidth;
   1.385 +				}
   1.386 +			}
   1.387 +		}
   1.388 +	return KErrNone;
   1.389 +	}
   1.390 +
   1.391 +EXPORT_C void CBitwiseBitmap::ExternalizeL(RWriteStream& aStream,const CFbsBitmap& aHandleBitmap) const
   1.392 +	{
   1.393 +	ExternalizeRectangleL(aStream,iHeader.iSizeInPixels,aHandleBitmap);
   1.394 +	}
   1.395 +
   1.396 +EXPORT_C void CBitwiseBitmap::ExternalizeRectangleL(RWriteStream& aStream,const TRect& aRect,const CFbsBitmap& aHandleBitmap) const
   1.397 +	{
   1.398 +	if (aRect.IsEmpty())
   1.399 +		User::Leave(KErrArgument);
   1.400 +	// the bitmap must have been already prepared for data access
   1.401 +	if (aHandleBitmap.iUseCount == 0)
   1.402 +		User::Leave(KErrArgument);
   1.403 +
   1.404 +	// If the bitmap is palette-compressed in RAM externalisation is currently not supported
   1.405 +	// Externalisation of extended bitmaps is currently not supported either
   1.406 +	if (iHeader.iCompression == EGenericPaletteCompression || iHeader.iCompression == EProprietaryCompression)
   1.407 +		User::Leave(KErrNotSupported);
   1.408 +	const TRect bitmapRect(iHeader.iSizeInPixels);
   1.409 +	TRect sourceRect(aRect);
   1.410 +	if (!sourceRect.Intersects(bitmapRect))
   1.411 +		User::Leave(KErrTooBig);
   1.412 +	sourceRect.Intersection(bitmapRect);
   1.413 +
   1.414 +	TDisplayMode displayMode = iSettings.CurrentDisplayMode();
   1.415 +	const TInt scanLineByteLength = CBitwiseBitmap::ByteWidth(sourceRect.Width(),displayMode);
   1.416 +	const TInt rectByteSize = sourceRect.Height() * scanLineByteLength;
   1.417 +	TUint8* buffer = (TUint8*)User::AllocLC(scanLineByteLength);
   1.418 +
   1.419 +	TPtr8 scanline(buffer,scanLineByteLength,scanLineByteLength);
   1.420 +	scanline.Fill(0xff);
   1.421 +	const TPoint zeroPoint;
   1.422 +	TInt compressedSize = 0;
   1.423 +	TInt row;
   1.424 +
   1.425 +	for (row = sourceRect.iTl.iY; row < sourceRect.iBr.iY; row++)
   1.426 +		{
   1.427 +		GetScanLine(scanline,TPoint(sourceRect.iTl.iX,row),sourceRect.Width(),EFalse,zeroPoint,displayMode,aHandleBitmap.DataAddress());
   1.428 +		compressedSize += SizeOfDataCompressed(buffer,scanLineByteLength);
   1.429 +		}
   1.430 +
   1.431 +	TBool compress = EFalse;
   1.432 +	if(compressedSize > 0)
   1.433 +		{
   1.434 +		compress = (displayMode == EColor4K) || (compressedSize < (rectByteSize >> 1) + (rectByteSize >> 2));
   1.435 +		}
   1.436 +
   1.437 +	aStream.WriteInt32L(sizeof(SEpocBitmapHeader) + ((compress) ? compressedSize : rectByteSize));
   1.438 +	aStream.WriteInt32L(iHeader.iStructSize);
   1.439 +	aStream.WriteInt32L(sourceRect.Width());
   1.440 +	aStream.WriteInt32L(sourceRect.Height());
   1.441 +	aStream.WriteInt32L(HorizontalPixelsToTwips(sourceRect.Width()));
   1.442 +	aStream.WriteInt32L(VerticalPixelsToTwips(sourceRect.Height()));
   1.443 +	aStream.WriteInt32L(iHeader.iBitsPerPixel);
   1.444 +	aStream.WriteUint32L(iHeader.iColor);
   1.445 +	aStream.WriteInt32L(0);
   1.446 +	aStream.WriteUint32L(compress ? CompressionType(iHeader.iBitsPerPixel, iHeader.iColor) : ENoBitmapCompression);
   1.447 +
   1.448 +	for (row = sourceRect.iTl.iY; row < sourceRect.iBr.iY; row++)
   1.449 +		{
   1.450 +		GetScanLine(scanline,TPoint(sourceRect.iTl.iX,row),sourceRect.Width(),EFalse,zeroPoint,displayMode,aHandleBitmap.DataAddress());
   1.451 +
   1.452 +		if (!compress)
   1.453 +			aStream.WriteL(buffer,scanLineByteLength);
   1.454 +		else
   1.455 +			DoExternalizeDataCompressedL(aStream,buffer,scanLineByteLength);
   1.456 +		}
   1.457 +
   1.458 +	CleanupStack::PopAndDestroy(); // buffer
   1.459 +	}
   1.460 +
   1.461 +EXPORT_C void CBitwiseBitmap::InternalizeHeaderL(RReadStream& aStream,SEpocBitmapHeader& aHeader)
   1.462 +	{
   1.463 +	aHeader.iBitmapSize=aStream.ReadInt32L();
   1.464 +	aHeader.iStructSize=aStream.ReadInt32L();
   1.465 +	if (aHeader.iStructSize!=sizeof(SEpocBitmapHeader)) User::Leave(KErrCorrupt);
   1.466 +	aHeader.iSizeInPixels.iWidth=aStream.ReadInt32L();
   1.467 +	aHeader.iSizeInPixels.iHeight=aStream.ReadInt32L();
   1.468 +	aHeader.iSizeInTwips.iWidth=aStream.ReadInt32L();
   1.469 +	aHeader.iSizeInTwips.iHeight=aStream.ReadInt32L();
   1.470 +	aHeader.iBitsPerPixel=aStream.ReadInt32L();
   1.471 +	aHeader.iColor=(TInt)aStream.ReadUint32L();
   1.472 +	aHeader.iPaletteEntries=aStream.ReadInt32L();
   1.473 +	if (aHeader.iPaletteEntries != 0)
   1.474 +		{
   1.475 +		//Palettes are not supported.
   1.476 +		User::Leave(KErrNotSupported);
   1.477 +		}
   1.478 +	aHeader.iCompression=(TBitmapfileCompression)aStream.ReadUint32L();
   1.479 +	CheckHeaderIsValidL(aHeader);
   1.480 +	}
   1.481 +
   1.482 +void CBitwiseBitmap::CheckHeaderIsValidL(const SEpocBitmapHeader& aHeader)	
   1.483 +	{
   1.484 +	//These fields are signed in the structure?
   1.485 +	TInt bitmapSize = aHeader.iBitmapSize;
   1.486 +	TInt imageHeightPix = aHeader.iSizeInPixels.iHeight;
   1.487 +	TInt imageWidthPix = aHeader.iSizeInPixels.iWidth;
   1.488 +	TInt bitsPerPixel = aHeader.iBitsPerPixel;
   1.489 +	TInt compression = aHeader.iCompression;
   1.490 +	TInt colour = aHeader.iColor;
   1.491 +	TBool corruptFlag = EFalse;
   1.492 +	
   1.493 +	//Need to copy the values from the structure
   1.494 +	TDisplayMode equivalentMode = CBitwiseBitmap::DisplayMode(aHeader.iBitsPerPixel,aHeader.iColor);
   1.495 +	if (equivalentMode == ENone)
   1.496 +		{
   1.497 +		User::Leave(KErrNotSupported);	
   1.498 +		}
   1.499 +	if(aHeader.iColor < 0)
   1.500 +		 {
   1.501 +		 corruptFlag = ETrue;
   1.502 +		 } 
   1.503 +	 	//easieast way to check if compression type is appropriate is to ask the compressor 
   1.504 +	if (compression && compression!= CBitwiseBitmap::CompressionType(bitsPerPixel,colour))
   1.505 +		{
   1.506 +		corruptFlag = ETrue;
   1.507 +		}
   1.508 +	//danger when using CBitwiseBitmap is they could panic for bad input...
   1.509 +	if (imageHeightPix <= 0 || imageWidthPix <= 0 || bitsPerPixel <= 0)
   1.510 +		{
   1.511 +		corruptFlag = ETrue;
   1.512 +		}
   1.513 +	const TInt KMeg = 1024 * 1024;
   1.514 +	//Test that scanline bytes calculation won't overflow.
   1.515 +	TInt bytesPerPack;	// pixel size in memory
   1.516 +	TInt bytesPerCompression;	// compressed unit data size
   1.517 +    User::LeaveIfError(CompressedFormatInfo(equivalentMode, bytesPerPack, bytesPerCompression));
   1.518 +	if (imageWidthPix > 2047 * KMeg / bytesPerPack)
   1.519 +		{
   1.520 +		corruptFlag = ETrue;
   1.521 +		}
   1.522 +	TInt uncompressedWidthBytes = CBitwiseBitmap::ByteWidth(imageWidthPix,equivalentMode);	//we know this won't overflow, now.
   1.523 +	//use top set bit indexes of 32 bit integer values to estimate when W*H multiply will overflow
   1.524 +	TInt exponentWidth = 0;
   1.525 +	TInt exponentHeight = 0;
   1.526 +	if (uncompressedWidthBytes & 0xffff0000)
   1.527 +		{
   1.528 +		exponentWidth += 16;
   1.529 +		}
   1.530 +	if (imageHeightPix & 0xffff0000)
   1.531 +		{
   1.532 +		exponentHeight += 16;
   1.533 +		}
   1.534 +	if (exponentWidth || exponentHeight)
   1.535 +		{
   1.536 +		if (uncompressedWidthBytes & 0xFF00FF00)
   1.537 +			{
   1.538 +			exponentWidth += 8;
   1.539 +			}
   1.540 +		if (imageHeightPix & 0xFF00FF00)
   1.541 +			{
   1.542 +			exponentHeight += 8;
   1.543 +			}
   1.544 +		if (uncompressedWidthBytes & 0xf0f0f0f0)
   1.545 +			{
   1.546 +			exponentWidth += 4;
   1.547 +			}
   1.548 +		if (imageHeightPix & 0xf0f0f0f0)
   1.549 +			{
   1.550 +			exponentHeight += 4;
   1.551 +			}
   1.552 +		if (uncompressedWidthBytes & 0xCCCCCCCC)
   1.553 +			{
   1.554 +			exponentWidth += 2;
   1.555 +			}
   1.556 +		if (imageHeightPix & 0xCCCCCCCC)
   1.557 +			{
   1.558 +				exponentHeight += 2;
   1.559 +		}
   1.560 +		if (uncompressedWidthBytes & 0xaaaaaaaa)
   1.561 +			{
   1.562 +			exponentWidth += 1;
   1.563 +			}
   1.564 +		if (imageHeightPix & 0xaaaaaaaa)
   1.565 +			{
   1.566 +			exponentHeight += 1;
   1.567 +			}
   1.568 +		TInt exponentTotal = exponentWidth + exponentHeight;
   1.569 +		if (exponentTotal >= 31)
   1.570 +			{
   1.571 +			//The result would defuinitely exceed a signed int
   1.572 +			corruptFlag = ETrue;
   1.573 +			}
   1.574 +		else if (exponentTotal == 30)
   1.575 +			{
   1.576 +			//as a bit test, both "next most significat bits" must be set to cause a carry-over,
   1.577 +			//but that isn't so trivial to test.
   1.578 +			if ((uncompressedWidthBytes >> 1) * imageHeightPix > 1024 * KMeg)
   1.579 +				{
   1.580 +				corruptFlag = ETrue;
   1.581 +				}
   1.582 +			}
   1.583 +		}
   1.584 +	if (compression)
   1.585 +		{
   1.586 +		/* estimate compressed file size limits
   1.587 +		byte compression uses lead code 0..127 = repeat next byte n+1 times. -1..-128 = copy next -n bytes
   1.588 +		16, 24, 32 use byte lead codes as above followed by words, triplets, or dwords
   1.589 +		1,2,4,8,16 all encode any dword alignment buffer padding data as full data values.
   1.590 +		32 doesn't have padding issue. 24 does not encode padding bytes.
   1.591 +		12 bit compression uses 0..15 spare nibble to encode short runs. 0=unique. Can never make file bigger.*/
   1.592 +		if (bitsPerPixel == 12)
   1.593 +			{
   1.594 +			//min file size is 1/16 of rect size
   1.595 +			if (bitmapSize < sizeof(SEpocBitmapHeader) + ((uncompressedWidthBytes * imageHeightPix) / 16))
   1.596 +				{
   1.597 +				corruptFlag = ETrue;	
   1.598 +				}
   1.599 +			if (bitmapSize > sizeof(SEpocBitmapHeader) + uncompressedWidthBytes * imageHeightPix)
   1.600 +				{
   1.601 +				corruptFlag = ETrue;
   1.602 +				}
   1.603 +			}
   1.604 +		else
   1.605 +			{
   1.606 +				TInt packedValsPerFile = (uncompressedWidthBytes / bytesPerPack) * imageHeightPix;
   1.607 +				//for some of the compressors 0 means a run of 2, so max 127 means a run of 129
   1.608 +				TInt estMinCompressedBlocksPerFile = (packedValsPerFile - 1) / 129 + 1;	
   1.609 +				/* Absolute minimum is blocks of 128 repeats possibly spanning multiple scanlines
   1.610 +				This can't be compressed by the current per-scanline compressor,
   1.611 +				but is acceptable to the decompressor. */
   1.612 +				if (bitmapSize < sizeof(SEpocBitmapHeader) + estMinCompressedBlocksPerFile * (bytesPerCompression + 1))
   1.613 +					{
   1.614 +					corruptFlag = ETrue;
   1.615 +					}
   1.616 +			/* Absolute maximum is to store every pixel as a seperate run of 1 byte.
   1.617 +			The current compressor would never do this... but the file is legal! */
   1.618 +				if (bitmapSize > sizeof(SEpocBitmapHeader) + packedValsPerFile * (bytesPerCompression + 1))
   1.619 +					{
   1.620 +					corruptFlag = ETrue;
   1.621 +					}
   1.622 +			}
   1.623 +		}
   1.624 +	else
   1.625 +		{
   1.626 +		if (bitmapSize != sizeof(SEpocBitmapHeader) + uncompressedWidthBytes * imageHeightPix)
   1.627 +			{
   1.628 +			corruptFlag = ETrue;
   1.629 +			}
   1.630 +		}
   1.631 +	if(corruptFlag)
   1.632 +		{
   1.633 +		User::Leave(KErrCorrupt);
   1.634 +		}
   1.635 +			
   1.636 +	}
   1.637 +
   1.638 +/**
   1.639 +Internalizes the bit map contents from a stream.
   1.640 +@param aStream The read stream containing the bit map.
   1.641 +*/
   1.642 +EXPORT_C void CBitwiseBitmap::InternalizeL(RReadStream& aStream)
   1.643 +	{
   1.644 +	if (iHeap==NULL || iPile==NULL)
   1.645 +		User::Leave(KErrNoMemory);
   1.646 +	Reset();
   1.647 +	InternalizeHeaderL(aStream,iHeader);
   1.648 +
   1.649 +	TDisplayMode displayMode = DisplayMode(iHeader.iBitsPerPixel,iHeader.iColor);
   1.650 +	if(displayMode == ENone)
   1.651 +		{
   1.652 +		Reset();
   1.653 +		User::Leave(KErrCorrupt);
   1.654 +		}
   1.655 +
   1.656 +	iSettings.SetDisplayModes(displayMode);
   1.657 +
   1.658 +	iByteWidth = ByteWidth(iHeader.iSizeInPixels.iWidth,iSettings.CurrentDisplayMode());
   1.659 +
   1.660 +	TUint8* data=NULL;
   1.661 +	TInt bytesize = iByteWidth * iHeader.iSizeInPixels.iHeight;
   1.662 +	if (bytesize > 0)
   1.663 +		{
   1.664 +		data = iPile->Alloc(bytesize);
   1.665 +		iDataOffset = data - iPile->ChunkBase();
   1.666 +		if (!data)
   1.667 +			{
   1.668 +			iDataOffset=0;
   1.669 +			Reset();
   1.670 +			User::LeaveNoMemory();
   1.671 +			}
   1.672 +		}
   1.673 +	TRAPD(err,DoInternalizeL(aStream,iHeader.iBitmapSize-iHeader.iStructSize,DataAddress()));
   1.674 +	if (err!=KErrNone)
   1.675 +		{
   1.676 +		Reset();
   1.677 +		User::Leave(err);
   1.678 +		}
   1.679 +	}
   1.680 +
   1.681 +
   1.682 +void CBitwiseBitmap::DoInternalizeL(RReadStream& aStream,TInt aSrceSize,TUint32* aBase)
   1.683 +	{
   1.684 +	if (iHeader.iCompression==ENoBitmapCompression)
   1.685 +		aStream.ReadL((TUint8*)aBase,aSrceSize);
   1.686 +	else if (iHeader.iCompression < ERLECompressionLast)
   1.687 +		{
   1.688 +		TBitmapfileCompression compression = iHeader.iCompression;
   1.689 +		iHeader.iCompression = ENoBitmapCompression;
   1.690 +		iHeader.iBitmapSize = iByteWidth*iHeader.iSizeInPixels.iHeight+sizeof(SEpocBitmapHeader);
   1.691 +		DoInternalizeCompressedDataL(aStream,aSrceSize,aBase,compression);
   1.692 +		}
   1.693 + 	else
   1.694 +		CheckHeaderIsValidL(iHeader);
   1.695 +	}
   1.696 +
   1.697 +EXPORT_C TDisplayMode CBitwiseBitmap::DisplayMode() const
   1.698 +	{
   1.699 +	return iSettings.CurrentDisplayMode();
   1.700 +	}
   1.701 +
   1.702 +EXPORT_C TInt CBitwiseBitmap::HorizontalPixelsToTwips(TInt aPixels) const
   1.703 +	{
   1.704 +	if (iHeader.iSizeInPixels.iWidth==0)
   1.705 +		return(0);
   1.706 +	TInt twips;
   1.707 +	twips = (aPixels*iHeader.iSizeInTwips.iWidth+(iHeader.iSizeInPixels.iWidth/2))/iHeader.iSizeInPixels.iWidth;
   1.708 +	return(twips);
   1.709 +	}
   1.710 +
   1.711 +EXPORT_C TInt CBitwiseBitmap::VerticalPixelsToTwips(TInt aPixels) const
   1.712 +	{
   1.713 +	if (iHeader.iSizeInPixels.iHeight==0)
   1.714 +		return(0);
   1.715 +	TInt twips;
   1.716 +	twips = (aPixels*iHeader.iSizeInTwips.iHeight+(iHeader.iSizeInPixels.iHeight/2))/iHeader.iSizeInPixels.iHeight;
   1.717 +	return (twips);
   1.718 +	}
   1.719 +
   1.720 +EXPORT_C TSize CBitwiseBitmap::SizeInPixels() const
   1.721 +	{
   1.722 +	return(iHeader.iSizeInPixels);
   1.723 +	}
   1.724 +
   1.725 +EXPORT_C TSize CBitwiseBitmap::SizeInTwips() const
   1.726 +	{
   1.727 +	return(iHeader.iSizeInTwips);
   1.728 +	}
   1.729 +
   1.730 +EXPORT_C TInt CBitwiseBitmap::HorizontalTwipsToPixels(TInt aTwips) const
   1.731 +	{
   1.732 +	if (iHeader.iSizeInTwips.iWidth==0)
   1.733 +		return(0);
   1.734 +	TInt pixels;
   1.735 +	pixels = (aTwips*iHeader.iSizeInPixels.iWidth+(iHeader.iSizeInTwips.iWidth/2))/iHeader.iSizeInTwips.iWidth;
   1.736 +	return(pixels);
   1.737 +	}
   1.738 +
   1.739 +EXPORT_C TInt CBitwiseBitmap::VerticalTwipsToPixels(TInt aTwips) const
   1.740 +	{
   1.741 +	if (iHeader.iSizeInTwips.iHeight==0)
   1.742 +		return(0);
   1.743 +	TInt pixels;
   1.744 +	pixels = (aTwips*iHeader.iSizeInPixels.iHeight+(iHeader.iSizeInTwips.iHeight/2))/iHeader.iSizeInTwips.iHeight;
   1.745 +	return(pixels);
   1.746 +	}
   1.747 +
   1.748 +/**
   1.749 +The method retrieves the red, green, blue (RGB) color value of the pixel with
   1.750 +specified coordinates.
   1.751 +Note: The method works for uncompressed bitmaps and extended bitmaps only.
   1.752 +@internalComponent
   1.753 +@released
   1.754 +@pre aBase != NULL;
   1.755 +@param aColor It will be initialized with the pixel color value on success, otherwise
   1.756 +aColor value will be left unchanged.
   1.757 +@param aPixel Pixel coordinates.
   1.758 +@param aBase It points to the beginning of the bitmap data.
   1.759 +*/
   1.760 +EXPORT_C void CBitwiseBitmap::GetPixel(TRgb& aColor,const TPoint& aPixel,TUint32* aBase, CFbsRasterizer* aRasterizer) const
   1.761 +	{
   1.762 +	// This operation is not currently supported for compressed bitmaps.
   1.763 +	if (iHeader.iCompression != ENoBitmapCompression && iHeader.iCompression != EProprietaryCompression)
   1.764 +		{
   1.765 +		__ASSERT_DEBUG(EFalse, ::Panic(EFbsBitmapInvalidCompression));
   1.766 +		return;
   1.767 +		}
   1.768 +	if (!iDataOffset)
   1.769 +		return;
   1.770 +
   1.771 +	TInt x=aPixel.iX,y=aPixel.iY;
   1.772 +	if (x < -iHeader.iSizeInPixels.iWidth)
   1.773 +		x %= iHeader.iSizeInPixels.iWidth;
   1.774 +	if (y < -iHeader.iSizeInPixels.iHeight)
   1.775 +		y %= iHeader.iSizeInPixels.iHeight;
   1.776 +	if (x < 0)
   1.777 +		x += iHeader.iSizeInPixels.iWidth;
   1.778 +	if (y < 0)
   1.779 +		y += iHeader.iSizeInPixels.iHeight;
   1.780 +
   1.781 +	if (iHeader.iCompression == EProprietaryCompression)
   1.782 +		{
   1.783 +		if (aRasterizer)
   1.784 +			{
   1.785 +			TUint32* slptr = const_cast<TUint32*>(aRasterizer->ScanLine(Extra()->iSerialNumber, TPoint(x,y), 1));
   1.786 +			if (slptr)
   1.787 +				{
   1.788 +				aColor = GetRgbPixelEx(x, slptr);
   1.789 +				}
   1.790 +			else
   1.791 +				{
   1.792 +				// wrong rasterizer for this extended bitmap - return white pixel
   1.793 +				aColor = KRgbWhite;
   1.794 +				}
   1.795 +			}
   1.796 +		else
   1.797 +			{
   1.798 +			// no rasterizer - return white pixel
   1.799 +			aColor = KRgbWhite;
   1.800 +			}
   1.801 +		}
   1.802 +	else
   1.803 +		{
   1.804 +		aColor = GetRgbPixelEx(x,ScanLineAddress(aBase,y));
   1.805 +		}
   1.806 +	}
   1.807 +
   1.808 +EXPORT_C TInt CBitwiseBitmap::GetScanLinePtr(TUint32*& aSlptr, TInt& aLength,TPoint& aPixel,TUint32* aBase, TLineScanningPosition& aLineScanningPosition) const
   1.809 +	{
   1.810 +	if (!iDataOffset)
   1.811 +		return KErrNone;
   1.812 +	if (aPixel.iX >= iHeader.iSizeInPixels.iWidth || aPixel.iX < -iHeader.iSizeInPixels.iWidth)
   1.813 +		aPixel.iX %= iHeader.iSizeInPixels.iWidth;
   1.814 +	if (aPixel.iY >= iHeader.iSizeInPixels.iHeight || aPixel.iY < -iHeader.iSizeInPixels.iHeight)
   1.815 +		aPixel.iY %= iHeader.iSizeInPixels.iHeight;
   1.816 +	if (aPixel.iX < 0)
   1.817 +		aPixel.iX += iHeader.iSizeInPixels.iWidth;
   1.818 +	if (aPixel.iY < 0)
   1.819 +		aPixel.iY += iHeader.iSizeInPixels.iHeight;
   1.820 +	if (aPixel.iX + aLength > iHeader.iSizeInPixels.iWidth)
   1.821 +		aLength = iHeader.iSizeInPixels.iWidth - aPixel.iX;
   1.822 +	if (iHeader.iCompression != ENoBitmapCompression)
   1.823 +		{
   1.824 +		return DoGetScanLinePtr(aSlptr, aPixel,aLength,aBase,aLineScanningPosition);
   1.825 +		}
   1.826 +	else
   1.827 +		{
   1.828 +		aSlptr = ScanLineAddress(aBase,aPixel.iY);
   1.829 +		}
   1.830 +	return KErrNone;
   1.831 +	}
   1.832 +
   1.833 +EXPORT_C TInt CBitwiseBitmap::GetScanLinePtr(TUint32*& aSlptr, TPoint& aPixel,TInt aLength,TUint32* aBase, TLineScanningPosition& aLineScanningPosition) const
   1.834 +	{
   1.835 +	return GetScanLinePtr(aSlptr, aLength,aPixel,aBase, aLineScanningPosition);
   1.836 +	}
   1.837 +
   1.838 +TUint8 CBitwiseBitmap::GetGrayPixelEx(TInt aX,TUint32* aScanlinePtr) const
   1.839 +	{
   1.840 +	// returns pixel as EGray256 value (0 - 255)
   1.841 +
   1.842 +	if (iHeader.iColor)
   1.843 +		return TUint8(GetRgbPixelEx(aX,aScanlinePtr)._Gray256());
   1.844 +	else
   1.845 +		{
   1.846 +		if (!aScanlinePtr)
   1.847 +			return 0;
   1.848 +
   1.849 +		if (aX >= iHeader.iSizeInPixels.iWidth)
   1.850 +			aX %= iHeader.iSizeInPixels.iWidth;
   1.851 +
   1.852 +		switch (iHeader.iBitsPerPixel)
   1.853 +			{
   1.854 +			case 2:
   1.855 +				{
   1.856 +				TUint32 col = *(aScanlinePtr+(aX>>4));
   1.857 +				col>>=((aX&0xf)<<1);
   1.858 +				col&=3;
   1.859 +				col |= (col << 6) | (col<<4) | (col<<2);
   1.860 +				return TUint8(col);
   1.861 +				}
   1.862 +			case 4:
   1.863 +				{
   1.864 +				TUint32 col = *(aScanlinePtr+(aX>>3));
   1.865 +				col >>= ((aX&7)<<2);
   1.866 +				col &= 0xf;
   1.867 +				return TUint8(col |= (col << 4));
   1.868 +				}
   1.869 +			case 1:
   1.870 +				{
   1.871 +				TUint32 col = *(aScanlinePtr+(aX>>5));
   1.872 +				if (col&(1<<(aX&0x1f))) return 255 ;
   1.873 +				return 0;
   1.874 +				}
   1.875 +			case 8:
   1.876 +				return *(((TUint8*)aScanlinePtr) + aX);
   1.877 +			default:
   1.878 +				return 0;
   1.879 +			 }
   1.880 +
   1.881 +		}
   1.882 +	}
   1.883 +
   1.884 +TRgb CBitwiseBitmap::GetRgbPixelEx(TInt aX,TUint32* aScanlinePtr) const
   1.885 +	{
   1.886 +	// returns pixel as TRgb
   1.887 +
   1.888 +	if (iHeader.iColor)
   1.889 +		{
   1.890 +		if (!aScanlinePtr)
   1.891 +			return KRgbBlack;
   1.892 +
   1.893 +		if (aX>=iHeader.iSizeInPixels.iWidth)
   1.894 +			aX%=iHeader.iSizeInPixels.iWidth;
   1.895 +
   1.896 +		switch (iHeader.iBitsPerPixel)
   1.897 +			{
   1.898 +			case 32:
   1.899 +				if (iSettings.CurrentDisplayMode() == EColor16MAP)
   1.900 +					return TRgb::_Color16MAP(*(aScanlinePtr + aX));
   1.901 +				else if (iSettings.CurrentDisplayMode() == EColor16MA)
   1.902 +					return TRgb::_Color16MA(*(aScanlinePtr + aX));
   1.903 +				//scanLineBytePointer format: BGR0 - 0RGB as INT32.
   1.904 +				else
   1.905 +					return TRgb::_Color16MU(*(aScanlinePtr + aX));
   1.906 +			case 24:
   1.907 +				{
   1.908 +				TUint8* scanLineBytePointer = (TUint8*)aScanlinePtr + aX * 3;
   1.909 +				TInt color16M = *scanLineBytePointer++;
   1.910 +				color16M |= (*scanLineBytePointer++) << 8;
   1.911 +				color16M |= (*scanLineBytePointer++) << 16;
   1.912 +				return TRgb::_Color16M(color16M);
   1.913 +				}
   1.914 +			case 16:
   1.915 +				return TRgb::_Color64K(*(((TUint16*)aScanlinePtr) + aX));
   1.916 +			case 12:
   1.917 +				return TRgb::_Color4K(*(((TUint16*)aScanlinePtr) + aX));
   1.918 +			case 8:
   1.919 +				return TRgb::Color256(*((TUint8*)aScanlinePtr + aX));
   1.920 +			case 4:
   1.921 +				{
   1.922 +				TUint8 colorIndex = *((TUint8*)aScanlinePtr + (aX >> 1));
   1.923 +				if (aX & 1)
   1.924 +					colorIndex >>= 4;
   1.925 +				return TRgb::Color16(colorIndex & 0xf);
   1.926 +				}
   1.927 +			default:
   1.928 +				return KRgbBlack;
   1.929 +			}
   1.930 +		}
   1.931 +	else
   1.932 +		return TRgb::_Gray256(GetGrayPixelEx(aX,aScanlinePtr));
   1.933 +	}
   1.934 +
   1.935 +void CBitwiseBitmap::GetRgbPixelExMany(TInt aX,TUint32* aScanlinePtr, TUint32* aDest,TInt aLength) const
   1.936 +    {
   1.937 +    __ASSERT_DEBUG(aScanlinePtr && aDest, User::Invariant());
   1.938 +    __ASSERT_DEBUG(aX      >= 0, User::Invariant());
   1.939 +    __ASSERT_DEBUG(aLength >= 0, User::Invariant());
   1.940 +    __ASSERT_DEBUG(aX+aLength <= iHeader.iSizeInPixels.iWidth, User::Invariant());
   1.941 +
   1.942 +    union {
   1.943 +        TUint8*  scanPtr8;
   1.944 +        TUint16* scanPtr16;
   1.945 +        TUint32* scanPtr32;
   1.946 +    };
   1.947 +    TUint32 color;
   1.948 +    TUint32 rawData;
   1.949 +    const TUint32 opaqueAlpha = 0xff000000;
   1.950 +    TUint32 color16MA = 0;  // // cached map to color
   1.951 +
   1.952 +    if (aLength < 1)
   1.953 +        {
   1.954 +        return;
   1.955 +        }
   1.956 +
   1.957 +    if (iHeader.iColor)
   1.958 +        {
   1.959 +        switch (iHeader.iBitsPerPixel)
   1.960 +            {
   1.961 +            case 4:
   1.962 +                {
   1.963 +                scanPtr8 = reinterpret_cast<TUint8*>(aScanlinePtr);
   1.964 +                TUint32 color16 = EFirstTime; // map source color: 16 color mode
   1.965 +                do
   1.966 +                    {
   1.967 +                    rawData = (aX & 1) ? (scanPtr8[aX >> 1] >> 4) : (scanPtr8[aX >> 1] &0x0F);
   1.968 +                    if ((rawData != color16))
   1.969 +                        { // first pixel or colour change
   1.970 +                        color16MA = TRgb::Color16(rawData)._Color16MA();
   1.971 +                        color16 = rawData;
   1.972 +                        }
   1.973 +                    *aDest++ = color16MA;
   1.974 +                    aX++;
   1.975 +                    }
   1.976 +                    while (--aLength);
   1.977 +                return;
   1.978 +                }
   1.979 +            case 8:
   1.980 +                {
   1.981 +                scanPtr8 = reinterpret_cast<TUint8*>(aScanlinePtr) + aX;
   1.982 +                const TUint32* lookup = DynamicPalette::DefaultColor256Util()->iColorTable;
   1.983 +                TUint32 color256 = EFirstTime; // map source color: 256 color
   1.984 +                do
   1.985 +                    {
   1.986 +                    rawData = *scanPtr8++;
   1.987 +                    if ((rawData != color256))
   1.988 +                        { // colour change or first pixel
   1.989 +                        color16MA = lookup[rawData];
   1.990 +                        color256 = rawData;
   1.991 +                        // translate between bgr & rgb
   1.992 +                        color16MA = ((color16MA & 0x0000ff) << 16) | (color16MA & 0x00ff00) | ((color16MA & 0xff0000) >> 16) | opaqueAlpha;
   1.993 +                        }
   1.994 +                    *aDest++ = color16MA;
   1.995 +                    }
   1.996 +                    while (--aLength);
   1.997 +                return;
   1.998 +                }
   1.999 +            case 12:
  1.1000 +                {
  1.1001 +                scanPtr16 = reinterpret_cast<TUint16*>(aScanlinePtr) + aX;
  1.1002 +                TUint32 color4K = EFirstTime; // map source color: 4K color black => 16M color black
  1.1003 +                do
  1.1004 +                    {
  1.1005 +                    rawData = *scanPtr16++;
  1.1006 +                    if ((rawData != color4K))
  1.1007 +                        { // colour change
  1.1008 +                        color16MA = TRgb::_Color4K(rawData)._Color16MA();
  1.1009 +                        color4K = rawData;
  1.1010 +                        }
  1.1011 +                    *aDest++ = color16MA;
  1.1012 +                    }
  1.1013 +                    while (--aLength);
  1.1014 +                return;
  1.1015 +                }
  1.1016 +            case 16:
  1.1017 +                {
  1.1018 +                scanPtr16 = reinterpret_cast<TUint16*>(aScanlinePtr) + aX;
  1.1019 +                const TUint16* lowAdd = Convert16to32bppLow();
  1.1020 +                const TUint32* highAdd = Convert16to32bppHigh();
  1.1021 +                TUint32 color64K = EFirstTime; // map source color: 64K color black => 16M color black
  1.1022 +                do
  1.1023 +                    {
  1.1024 +                    rawData = *scanPtr16++;
  1.1025 +                    if ((rawData != color64K))
  1.1026 +                        { // colour change
  1.1027 +                        color16MA = highAdd[rawData >> 8] | lowAdd[rawData & 0x00FF];
  1.1028 +                        color64K = rawData;
  1.1029 +                        }
  1.1030 +                    *aDest++ = color16MA;
  1.1031 +                    }
  1.1032 +                    while (--aLength);
  1.1033 +                return;
  1.1034 +                }
  1.1035 +            case 24:
  1.1036 +                {
  1.1037 +                scanPtr8 = reinterpret_cast<TUint8*>(aScanlinePtr) + aX*3;
  1.1038 +                do
  1.1039 +                    {
  1.1040 +                    if ((aLength < 4) || (3 & (TUint32)(scanPtr8)))
  1.1041 +                        {
  1.1042 +                        aLength--;
  1.1043 +                        color  =  *scanPtr8++;
  1.1044 +                        color |= (*scanPtr8++) << 8;
  1.1045 +                        color |= (*scanPtr8++) << 16;
  1.1046 +                        *aDest++ = color | opaqueAlpha;
  1.1047 +                        }
  1.1048 +                    else
  1.1049 +                        { // source is now TUint32 aligned - so read source as blocks of 3 TUint32's & write as 4
  1.1050 +                        TUint32 word1, word2, word3;
  1.1051 +                        TInt iter = (aLength / 4) - 1;
  1.1052 +                        aLength = aLength & 0x0003;
  1.1053 +                        do
  1.1054 +                            {
  1.1055 +                            word1 = *scanPtr32++; 
  1.1056 +                            *aDest++ = word1 | opaqueAlpha;
  1.1057 +                            word2 = *scanPtr32++; 
  1.1058 +                            color = (word1 >> 24) | ((word2 & 0xFFFF) << 8); 
  1.1059 +                            *aDest++ = color | opaqueAlpha;
  1.1060 +                            word3 = *scanPtr32++; 
  1.1061 +                            color = (word2 >> 16) | ((word3 & 0x00FF) << 16); 
  1.1062 +                            *aDest++ = color | opaqueAlpha;
  1.1063 +                            *aDest++ = (word3 >> 8) | opaqueAlpha;
  1.1064 +                            }
  1.1065 +                            while (iter--);
  1.1066 +                        }
  1.1067 +                    }
  1.1068 +                    while (aLength);
  1.1069 +                return;
  1.1070 +                }
  1.1071 +            case 32:
  1.1072 +                {
  1.1073 +                scanPtr32 = aScanlinePtr + aX;
  1.1074 +                if(iSettings.CurrentDisplayMode() == EColor16MAP)
  1.1075 +                    { // unrolled loop uses "Duff's Device"
  1.1076 +                    const TUint16* normTable = PtrTo16BitNormalisationTable();
  1.1077 +                    --aLength;
  1.1078 +                    TInt iter = aLength / 8;
  1.1079 +                    switch(aLength & 7) 
  1.1080 +                        {
  1.1081 +                        case 7:
  1.1082 +                            do {
  1.1083 +                                *aDest++ = PMA2NonPMAPixel(*scanPtr32++, normTable);
  1.1084 +                        case 6:
  1.1085 +                                *aDest++ = PMA2NonPMAPixel(*scanPtr32++, normTable);
  1.1086 +                        case 5:
  1.1087 +                                *aDest++ = PMA2NonPMAPixel(*scanPtr32++, normTable);
  1.1088 +                        case 4:
  1.1089 +                                *aDest++ = PMA2NonPMAPixel(*scanPtr32++, normTable);
  1.1090 +                        case 3:
  1.1091 +                                *aDest++ = PMA2NonPMAPixel(*scanPtr32++, normTable);
  1.1092 +                        case 2:
  1.1093 +                                *aDest++ = PMA2NonPMAPixel(*scanPtr32++, normTable);
  1.1094 +                        case 1:
  1.1095 +                                *aDest++ = PMA2NonPMAPixel(*scanPtr32++, normTable);
  1.1096 +                        case 0:
  1.1097 +                                *aDest++ = PMA2NonPMAPixel(*scanPtr32++, normTable);
  1.1098 +                            } while (iter-- > 0);
  1.1099 +                        }
  1.1100 +                    }
  1.1101 +                else if (iSettings.CurrentDisplayMode() == EColor16MU)
  1.1102 +                    { // unrolled loop uses "Duff's Device"
  1.1103 +                    --aLength;
  1.1104 +                    TInt iter = aLength / 8;
  1.1105 +                    switch(aLength & 7) 
  1.1106 +                        {
  1.1107 +                        case 7:
  1.1108 +                            do {
  1.1109 +                                *aDest++ = (*scanPtr32++) | opaqueAlpha;
  1.1110 +                        case 6:
  1.1111 +                                *aDest++ = (*scanPtr32++) | opaqueAlpha;
  1.1112 +                        case 5:
  1.1113 +                                *aDest++ = (*scanPtr32++) | opaqueAlpha;
  1.1114 +                        case 4:
  1.1115 +                                *aDest++ = (*scanPtr32++) | opaqueAlpha;
  1.1116 +                        case 3:
  1.1117 +                                *aDest++ = (*scanPtr32++) | opaqueAlpha;
  1.1118 +                        case 2:
  1.1119 +                                *aDest++ = (*scanPtr32++) | opaqueAlpha;
  1.1120 +                        case 1:
  1.1121 +                                *aDest++ = (*scanPtr32++) | opaqueAlpha;
  1.1122 +                        case 0:
  1.1123 +                                *aDest++ = (*scanPtr32++) | opaqueAlpha;
  1.1124 +                            } while (iter-- > 0);
  1.1125 +                        }
  1.1126 +                    }
  1.1127 +                else // EColor16MA
  1.1128 +                    {
  1.1129 +                    Mem::Move(aDest, scanPtr32, aLength * 4);
  1.1130 +                    }
  1.1131 +                return;
  1.1132 +                }
  1.1133 +            default:
  1.1134 +                return;
  1.1135 +            }
  1.1136 +        }
  1.1137 +    else
  1.1138 +        {
  1.1139 +        switch (iHeader.iBitsPerPixel)
  1.1140 +            {
  1.1141 +            case 1:
  1.1142 +                {
  1.1143 +                do
  1.1144 +                    {
  1.1145 +                    *aDest++ = TUint32(COLOR_VALUE(aScanlinePtr, aX) ? 0xFFFFFFFF : 0xFF000000);
  1.1146 +                    aX++;
  1.1147 +                    }
  1.1148 +                while (--aLength);
  1.1149 +                break;
  1.1150 +                }
  1.1151 +            case 8:
  1.1152 +                {
  1.1153 +                scanPtr8 = reinterpret_cast<TUint8*>(aScanlinePtr) + aX;
  1.1154 +                do
  1.1155 +                    {
  1.1156 +                    rawData = *scanPtr8++;
  1.1157 +                    color16MA = rawData | (rawData << 8) | (rawData << 16) | opaqueAlpha; 
  1.1158 +                    *aDest++ = color16MA;
  1.1159 +                    }
  1.1160 +                    while (--aLength);              
  1.1161 +                break;
  1.1162 +                }
  1.1163 +            default:
  1.1164 +                {
  1.1165 +                do
  1.1166 +                    {
  1.1167 +                    rawData = GetGrayPixelEx(aX++,aScanlinePtr);
  1.1168 +                    color16MA = rawData | (rawData << 8) | (rawData << 16) | opaqueAlpha; 
  1.1169 +                    *aDest++ = color16MA;
  1.1170 +                    }
  1.1171 +                    while (--aLength);              
  1.1172 +                }
  1.1173 +            }
  1.1174 +        }
  1.1175 +    }
  1.1176 +/**
  1.1177 +The method retrieves the RGB color values from the scanline, and converts them
  1.1178 +into the destination screen-mode pixel format. This method handles the special
  1.1179 +case when the destination mode is EColor16MAP (32bit with alpha values premultiplied
  1.1180 +with the color channels. Calls GetRgbPixelExMany for values not 32 bit, as there is no
  1.1181 +alpha information in these color modes. For color mode EColor16MU, no conversion is 
  1.1182 +performed (as alpha is assumed to be 1).
  1.1183 +@internalComponent
  1.1184 +@released
  1.1185 +@param aX	The x co-ordinate the scanline data needs to be retrieved from.
  1.1186 +@param aScanlinePtr	The scanline pointer, i.e. the source data.
  1.1187 +@param aDest	The pointer to the destination buffer. This is where the output is stored.
  1.1188 +@param aLength	The number of bytes to be copied. This value must be a multiple of 4.
  1.1189 +*/
  1.1190 +void CBitwiseBitmap::GetRgbPixelExMany16MAP(TInt aX,TUint32* aScanlinePtr,TUint32* aDest,TInt aLength) const
  1.1191 +    {
  1.1192 +    __ASSERT_DEBUG(aScanlinePtr && aDest, User::Invariant());
  1.1193 +    __ASSERT_DEBUG(aX      >= 0, User::Invariant());
  1.1194 +    __ASSERT_DEBUG(aLength >= 0, User::Invariant());
  1.1195 +    __ASSERT_DEBUG(aX+aLength <= iHeader.iSizeInPixels.iWidth, User::Invariant());
  1.1196 +
  1.1197 +    TUint32* scanPtr32;
  1.1198 +
  1.1199 +    if ((iHeader.iColor) && (iHeader.iBitsPerPixel == 32))
  1.1200 +        {
  1.1201 +        scanPtr32 = aScanlinePtr + aX;
  1.1202 +        if (DisplayMode() == EColor16MAP)
  1.1203 +            {
  1.1204 +            Mem::Move(aDest, scanPtr32, aLength<<2);
  1.1205 +            }
  1.1206 +        else if(DisplayMode()==EColor16MA)
  1.1207 +            {
  1.1208 +            TUint32* ptrLimit = aDest + aLength;
  1.1209 +            const TInt32 zero = 0;
  1.1210 +            while (aDest < ptrLimit)
  1.1211 +                {
  1.1212 +                TUint32 value = *scanPtr32++;
  1.1213 +                TUint32 tA = value >> 24;
  1.1214 +                if (tA == 0)
  1.1215 +                    {
  1.1216 +                    *aDest++ = zero;
  1.1217 +                    }
  1.1218 +                else if (tA != 255) 
  1.1219 +                    {
  1.1220 +                    *aDest++ = NonPMA2PMAPixel(value);
  1.1221 +                    }
  1.1222 +                else
  1.1223 +                    {
  1.1224 +                    *aDest++ = value;
  1.1225 +                    }
  1.1226 +                }
  1.1227 +            }
  1.1228 +        else // DisplayMode() == EColor16MU
  1.1229 +            {
  1.1230 +            if (aLength--)
  1.1231 +                { // unrolled loop uses "Duff's Device"
  1.1232 +                const TUint32 alpha = 0xFF000000;   //set all the alpha to 0xff
  1.1233 +                TInt iter = aLength / 8;
  1.1234 +                switch(aLength & 7) 
  1.1235 +                    {
  1.1236 +                    case 7:
  1.1237 +                        do {
  1.1238 +                            *aDest++ = (*scanPtr32++) | alpha;
  1.1239 +                    case 6:
  1.1240 +                            *aDest++ = (*scanPtr32++) | alpha;
  1.1241 +                    case 5:
  1.1242 +                            *aDest++ = (*scanPtr32++) | alpha;
  1.1243 +                    case 4:
  1.1244 +                            *aDest++ = (*scanPtr32++) | alpha;
  1.1245 +                    case 3:
  1.1246 +                            *aDest++ = (*scanPtr32++) | alpha;
  1.1247 +                    case 2:
  1.1248 +                            *aDest++ = (*scanPtr32++) | alpha;
  1.1249 +                    case 1:
  1.1250 +                            *aDest++ = (*scanPtr32++) | alpha;
  1.1251 +                    case 0:
  1.1252 +                            *aDest++ = (*scanPtr32++) | alpha;
  1.1253 +                        } while (iter-- > 0);
  1.1254 +                    }
  1.1255 +                }
  1.1256 +            }
  1.1257 +        }
  1.1258 +    else
  1.1259 +        {
  1.1260 +        GetRgbPixelExMany(aX, aScanlinePtr, aDest, aLength);
  1.1261 +        }
  1.1262 +    }
  1.1263 +
  1.1264 +void CBitwiseBitmap::GetRgbPixelExMany16M(TInt aX,TUint32* aScanlinePtr, TUint8* aDest,TInt aLength) const
  1.1265 +    {
  1.1266 +    union {
  1.1267 +        TUint8*  scanPtr8;
  1.1268 +        TUint16* scanPtr16;
  1.1269 +        TUint32* scanPtr32;
  1.1270 +    };
  1.1271 +    union {
  1.1272 +        TUint8*  destPtr8;
  1.1273 +        TUint32* destPtr32;
  1.1274 +    };
  1.1275 +    destPtr8 = aDest;
  1.1276 +    TUint32 rawData;
  1.1277 +
  1.1278 +    if (!aScanlinePtr)
  1.1279 +        {
  1.1280 +        const TUint32 zero = 0; // conveniently KRgbBlack is 0 in EColor16M mode
  1.1281 +        while (aLength)
  1.1282 +            {
  1.1283 +            if ((aLength < 4) || (3 & (TUint32)(destPtr8)))
  1.1284 +                {
  1.1285 +                aLength--;
  1.1286 +                *destPtr8++ = zero;
  1.1287 +                *destPtr8++ = zero;
  1.1288 +                *destPtr8++ = zero;
  1.1289 +                }
  1.1290 +            else
  1.1291 +                { // dest is now TUint32 aligned - write 4 pixels into 3 TUint32s
  1.1292 +                aLength -= 4;
  1.1293 +                *destPtr32++ = zero;
  1.1294 +                *destPtr32++ = zero;
  1.1295 +                *destPtr32++ = zero;
  1.1296 +                }
  1.1297 +            }
  1.1298 +        return;
  1.1299 +        }
  1.1300 +
  1.1301 +    if (aX>=iHeader.iSizeInPixels.iWidth)
  1.1302 +        {
  1.1303 +        aX%=iHeader.iSizeInPixels.iWidth;
  1.1304 +        }
  1.1305 +
  1.1306 +    while (aLength)
  1.1307 +        {
  1.1308 +        // cached map to color
  1.1309 +        TUint32 color16M = 0;
  1.1310 +        TInt copyLength = iHeader.iSizeInPixels.iWidth - aX;
  1.1311 +        TUint32* scanPtr32 = aScanlinePtr + aX;
  1.1312 +        if (copyLength > aLength)
  1.1313 +            {
  1.1314 +            copyLength = aLength;
  1.1315 +            }
  1.1316 +        aLength -= copyLength;
  1.1317 +
  1.1318 +        if (iHeader.iColor)
  1.1319 +            {
  1.1320 +            switch(iHeader.iBitsPerPixel)
  1.1321 +                {
  1.1322 +                case 4:
  1.1323 +                    {
  1.1324 +                    scanPtr8 = reinterpret_cast<TUint8*>(aScanlinePtr);
  1.1325 +                    TUint32 color16 = EFirstTime; // map source color: 16 color mode
  1.1326 +                    do
  1.1327 +                        {
  1.1328 +                        rawData = (aX & 1) ? (scanPtr8[aX >> 1] >> 4) : (scanPtr8[aX >> 1] &0x0F);
  1.1329 +                        if ((rawData != color16))
  1.1330 +                            { // first pixel or colour change
  1.1331 +                            color16M = TRgb::Color16(rawData)._Color16M();
  1.1332 +                            color16 = rawData;
  1.1333 +                            }
  1.1334 +                        *destPtr8++ = TUint8(color16M);
  1.1335 +                        *destPtr8++ = TUint8(color16M >> 8);
  1.1336 +                        *destPtr8++ = TUint8(color16M >> 16);
  1.1337 +                        aX++;
  1.1338 +                        }
  1.1339 +                        while (--copyLength);
  1.1340 +                    break;
  1.1341 +                    }
  1.1342 +                case 8:
  1.1343 +                    {
  1.1344 +                    scanPtr8 = reinterpret_cast<TUint8*>(aScanlinePtr) + aX;
  1.1345 +                    const TUint32* lookup = DynamicPalette::DefaultColor256Util()->iColorTable;
  1.1346 +                    TUint32 color256 = EFirstTime; // map source color: 256 color
  1.1347 +                    do
  1.1348 +                        {
  1.1349 +                        rawData = *scanPtr8++;
  1.1350 +                        if ((rawData != color256))
  1.1351 +                            { // first pixel or colour change; so perform new mapping
  1.1352 +                            color16M = lookup[rawData];
  1.1353 +                            color256 = rawData;
  1.1354 +                            }
  1.1355 +                        // Note; byte order performs required bgr -> rgb conversion 
  1.1356 +                        *destPtr8++ = TUint8(color16M >> 16);
  1.1357 +                        *destPtr8++ = TUint8(color16M >> 8);
  1.1358 +                        *destPtr8++ = TUint8(color16M);
  1.1359 +                        }
  1.1360 +                        while (--copyLength);
  1.1361 +                    break;
  1.1362 +                    }
  1.1363 +                case 12:
  1.1364 +                    {
  1.1365 +                    scanPtr16 = reinterpret_cast<TUint16*>(aScanlinePtr) + aX;
  1.1366 +                    TUint32 color4K = EFirstTime; // map source color: 4K color black => 16M color black
  1.1367 +                    do
  1.1368 +                        {
  1.1369 +                        rawData = *scanPtr16++;
  1.1370 +                        if ((rawData != color4K))
  1.1371 +                            { // first pixel or colour change
  1.1372 +                            color16M = TRgb::_Color4K(rawData)._Color16M();
  1.1373 +                            color4K = rawData;
  1.1374 +                            }
  1.1375 +                        *destPtr8++ = TUint8(color16M);
  1.1376 +                        *destPtr8++ = TUint8(color16M >> 8);
  1.1377 +                        *destPtr8++ = TUint8(color16M >> 16);
  1.1378 +                        }
  1.1379 +                        while (--copyLength);
  1.1380 +                    break;
  1.1381 +                    }
  1.1382 +                case 16:
  1.1383 +                    {
  1.1384 +                    scanPtr16 = reinterpret_cast<TUint16*>(aScanlinePtr) + aX;
  1.1385 +                    const TUint16* lowAdd = Convert16to32bppLow();
  1.1386 +                    const TUint32* highAdd = Convert16to32bppHigh();
  1.1387 +                    TUint16 halfWord;
  1.1388 +                    TUint32 color64K = EFirstTime; // 64K color black => 16M color black
  1.1389 +                    color16M = 0;
  1.1390 +                    do
  1.1391 +                        {
  1.1392 +                        halfWord = *scanPtr16++;
  1.1393 +                        if ((halfWord != color64K))
  1.1394 +                            { // colour change
  1.1395 +                            color16M = highAdd[halfWord >> 8] | lowAdd[halfWord & 0x00FF];
  1.1396 +                            color64K = halfWord;
  1.1397 +                            }
  1.1398 +                        *destPtr8++ = TUint8(color16M);
  1.1399 +                        *destPtr8++ = TUint8(color16M >> 8);
  1.1400 +                        *destPtr8++ = TUint8(color16M >> 16);
  1.1401 +                        }
  1.1402 +                        while (--copyLength);
  1.1403 +                    break;
  1.1404 +                    }
  1.1405 +                case 24:
  1.1406 +                    {
  1.1407 +                    Mem::Copy(destPtr8, reinterpret_cast<TUint8*>(aScanlinePtr) + aX, copyLength * 3); // may not be aligned 
  1.1408 +                    destPtr8 += copyLength * 3;
  1.1409 +                    break;
  1.1410 +                    }
  1.1411 +                case 32:
  1.1412 +                    {
  1.1413 +                    scanPtr32 = aScanlinePtr + aX;
  1.1414 +                    if (iSettings.CurrentDisplayMode() == EColor16MAP)
  1.1415 +                        {
  1.1416 +                        const TUint16* normTable = PtrTo16BitNormalisationTable();
  1.1417 +                        do
  1.1418 +                            {
  1.1419 +                            // convert from EColor16MAP to EColor16MA first.
  1.1420 +                            color16M = PMA2NonPMAPixel(*scanPtr32++, normTable);
  1.1421 +                            *destPtr8++ = TUint8(color16M);
  1.1422 +                            *destPtr8++ = TUint8(color16M >> 8);
  1.1423 +                            *destPtr8++ = TUint8(color16M >> 16);
  1.1424 +                            }
  1.1425 +                            while (--copyLength);
  1.1426 +                        }
  1.1427 +                    else
  1.1428 +                        { // EColor16MA or EColor16MU, keep the RGB & throw away the top byte
  1.1429 +                        // scanLineBytePointer format: ARGB - 0RGB as INT32 or BGR0 - 0RGB as INT32.
  1.1430 +                        do
  1.1431 +                            {
  1.1432 +                            if ((copyLength < 4) || (3 & (TUint32)destPtr8))
  1.1433 +                                {
  1.1434 +                                color16M = *scanPtr32++;
  1.1435 +                                --copyLength;
  1.1436 +                                *destPtr8++ = TUint8(color16M);
  1.1437 +                                *destPtr8++ = TUint8(color16M >> 8);
  1.1438 +                                *destPtr8++ = TUint8(color16M >> 16);
  1.1439 +                                }
  1.1440 +                            else // dest is TUint32 aligned: copy 4 pixels into 3 TUint32's
  1.1441 +                                {
  1.1442 +                                color16M = (*scanPtr32++) & 0x00FFFFFF;
  1.1443 +                                rawData = (*scanPtr32++) & 0x00FFFFFF;
  1.1444 +                                copyLength -= 4;
  1.1445 +                                *destPtr32++ = color16M | (rawData << 24);
  1.1446 +                                color16M = (*scanPtr32++) & 0x00FFFFFF;
  1.1447 +                                *destPtr32++ = (color16M << 16) | (rawData >> 8);
  1.1448 +                                rawData = (*scanPtr32++) & 0x00FFFFFF;
  1.1449 +                                *destPtr32++ = (color16M >> 16) | (rawData << 8);
  1.1450 +                                }
  1.1451 +                            }
  1.1452 +                            while (copyLength);
  1.1453 +                        }
  1.1454 +                    break;
  1.1455 +                    }
  1.1456 +                default:
  1.1457 +                    break;
  1.1458 +                }
  1.1459 +            }
  1.1460 +        else
  1.1461 +            { // !iHeader.iColor
  1.1462 +            if (iHeader.iBitsPerPixel == 8)
  1.1463 +                {
  1.1464 +                scanPtr8 = reinterpret_cast<TUint8*>(aScanlinePtr) + aX;
  1.1465 +                do
  1.1466 +                    {
  1.1467 +                    rawData = *scanPtr8++;
  1.1468 +                    *destPtr8++ = (TUint8)rawData;
  1.1469 +                    *destPtr8++ = (TUint8)rawData;
  1.1470 +                    *destPtr8++ = (TUint8)rawData;
  1.1471 +                    }
  1.1472 +                    while (--copyLength);
  1.1473 +                }
  1.1474 +            else
  1.1475 +                {
  1.1476 +                do
  1.1477 +                    {
  1.1478 +                    rawData = GetGrayPixelEx(aX++,aScanlinePtr);
  1.1479 +                    *destPtr8++ = (TUint8)rawData;
  1.1480 +                    *destPtr8++ = (TUint8)rawData;
  1.1481 +                    *destPtr8++ = (TUint8)rawData;
  1.1482 +                    }
  1.1483 +                    while (--copyLength);
  1.1484 +                }
  1.1485 +            }
  1.1486 +        aX = 0; // second copy, if any, comes from start of line
  1.1487 +        }
  1.1488 +
  1.1489 +    return;
  1.1490 +    }
  1.1491 +
  1.1492 +void CBitwiseBitmap::GenerateLineFromCompressedEightBitData(TUint8* aDestBuffer, const TPoint& aPixel,TInt aLength, TUint32* aBase,TLineScanningPosition& aLineScanningPosition) const
  1.1493 +	{
  1.1494 +	const TInt bitmapWidth=iByteWidth;
  1.1495 +	const TInt pixelsPerByte=8/iHeader.iBitsPerPixel;
  1.1496 +	const TInt startPos=aPixel.iY*bitmapWidth+aPixel.iX/pixelsPerByte;
  1.1497 +	const TInt endPos=aPixel.iY*bitmapWidth+(aPixel.iX+aLength+pixelsPerByte-1)/pixelsPerByte;
  1.1498 +	const TInt byteLength=endPos-startPos;
  1.1499 +	TInt writes=byteLength;
  1.1500 +	TUint8* destPtr = ((TUint8*)aDestBuffer);
  1.1501 +	destPtr+=(aPixel.iX/pixelsPerByte);
  1.1502 +	if (aLineScanningPosition.iCursorPos>startPos)
  1.1503 +		{
  1.1504 +		aLineScanningPosition.iSrcDataPtr=(TUint8*)aBase;
  1.1505 +		aLineScanningPosition.iCursorPos=0;
  1.1506 +		}
  1.1507 +	TUint8* srcePtr = (TUint8*)aLineScanningPosition.iSrcDataPtr;
  1.1508 +	TInt8 count=*srcePtr;
  1.1509 +	TInt16 addition;
  1.1510 +	if (count<0)
  1.1511 +		addition=(TInt16) (-count);
  1.1512 +	else
  1.1513 +		addition=(TInt16) (count+1);
  1.1514 +	while (aLineScanningPosition.iCursorPos+addition<startPos)
  1.1515 +		{
  1.1516 +		aLineScanningPosition.iCursorPos+=addition;
  1.1517 +		if (count<0)
  1.1518 +			{
  1.1519 +			srcePtr+=(-count+1);
  1.1520 +			}
  1.1521 +		else
  1.1522 +			{
  1.1523 +			srcePtr+=2;  // Just skip over value
  1.1524 +			}
  1.1525 +		count = *srcePtr;
  1.1526 +		if (count<0)
  1.1527 +			addition=(TInt16) (-count);
  1.1528 +		else
  1.1529 +			addition=(TInt16) (count+1);
  1.1530 +		}
  1.1531 +	// Then scan the line
  1.1532 +	count=0;
  1.1533 +	while (aLineScanningPosition.iCursorPos+count<startPos+byteLength)
  1.1534 +		{
  1.1535 +		TBool negativeCount=EFalse;
  1.1536 +		count=*srcePtr;
  1.1537 +		if (count<0)
  1.1538 +			{
  1.1539 +			negativeCount=ETrue;
  1.1540 +			count=(TInt8) ((-count)-1);
  1.1541 +			}
  1.1542 +		TUint8 value = *(srcePtr+1);
  1.1543 +		TInt distanceToTheLineEnd=startPos+byteLength-aLineScanningPosition.iCursorPos;
  1.1544 +		if (count<distanceToTheLineEnd)
  1.1545 +			{
  1.1546 +			if (!negativeCount)
  1.1547 +				{
  1.1548 +				srcePtr+=2;
  1.1549 +				}
  1.1550 +			else
  1.1551 +				{
  1.1552 +				srcePtr+=1;
  1.1553 +				}
  1.1554 +			TInt countPlusOne = (TInt)count + 1;
  1.1555 +			TInt start = Max(0,startPos-aLineScanningPosition.iCursorPos);
  1.1556 +			if (countPlusOne > start)
  1.1557 +				{
  1.1558 +				TInt length = Min(countPlusOne-start,writes);
  1.1559 +				writes -= countPlusOne-start;
  1.1560 +				if (length > 0)
  1.1561 +					{
  1.1562 +					/*Mem::Fill and Mem::Copy used in order to increase the performance*/
  1.1563 +					if (!negativeCount)
  1.1564 +						{
  1.1565 +						Mem::Fill(destPtr,length,value);
  1.1566 +						}
  1.1567 +					else
  1.1568 +						{
  1.1569 +						Mem::Copy(destPtr,srcePtr+start,length);
  1.1570 +						}
  1.1571 +					destPtr += length;
  1.1572 +					}
  1.1573 +				}
  1.1574 +			if (negativeCount)
  1.1575 +				{
  1.1576 +				srcePtr += countPlusOne;
  1.1577 +				}
  1.1578 +			aLineScanningPosition.iCursorPos += countPlusOne;
  1.1579 +			count=0;
  1.1580 +			}
  1.1581 +			else
  1.1582 +			{
  1.1583 +			TInt correction=1;
  1.1584 +			if (aLineScanningPosition.iCursorPos<startPos)
  1.1585 +				{
  1.1586 +				correction=startPos-aLineScanningPosition.iCursorPos+1;
  1.1587 +				}
  1.1588 +			TInt length = Min(byteLength,writes);
  1.1589 +			writes -= length;
  1.1590 +			/*Mem::Fill and Mem::Copy used in order to increase the performance*/
  1.1591 +			if (!negativeCount)
  1.1592 +				{
  1.1593 +				Mem::Fill(destPtr,length,value);
  1.1594 +				}
  1.1595 +			else
  1.1596 +				{
  1.1597 +				Mem::Copy(destPtr,srcePtr+correction,length);
  1.1598 +				}
  1.1599 +			destPtr += length;
  1.1600 +			}
  1.1601 +		}
  1.1602 +	aLineScanningPosition.iSrcDataPtr=(TUint8*) srcePtr;
  1.1603 +	}
  1.1604 +
  1.1605 +void CBitwiseBitmap::GenerateLineFromCompressedTwelveBitData(TUint8* aDestBuffer, const TPoint& aPixel,TInt aLength, TUint32* aBase, TLineScanningPosition& aLineScanningPosition) const
  1.1606 +	{
  1.1607 +	const TInt bitmapWidth=iByteWidth>>1;
  1.1608 +	const TInt startPos=aPixel.iY*bitmapWidth+aPixel.iX;
  1.1609 +	TInt writes=aLength*2;
  1.1610 +	TUint16* destPtr = ((TUint16*)aDestBuffer);//+aPixel.iX;
  1.1611 +	destPtr+=aPixel.iX;
  1.1612 +
  1.1613 +	if(iPile)
  1.1614 +		{
  1.1615 +		::AdjustLineScanningPosition(aLineScanningPosition, aBase, bitmapWidth, startPos, iHeader.iBitmapSize - sizeof(SEpocBitmapHeader));
  1.1616 +		}
  1.1617 +	TUint16* srcePtr = (TUint16*)aLineScanningPosition.iSrcDataPtr;
  1.1618 +
  1.1619 +	// Fast find the correct position to start
  1.1620 +	TInt count=0;
  1.1621 +	if(::Abs(aLineScanningPosition.iCursorPos - startPos) > startPos)
  1.1622 +		{
  1.1623 +		srcePtr = (TUint16*)aBase;
  1.1624 +		aLineScanningPosition.iCursorPos = 0;
  1.1625 +		}
  1.1626 +	while (aLineScanningPosition.iCursorPos>startPos)
  1.1627 +		{
  1.1628 +		srcePtr--;
  1.1629 +		TUint16 value = *srcePtr;
  1.1630 +		count = value >> 12;
  1.1631 +		aLineScanningPosition.iCursorPos-=(count+1);
  1.1632 +		}
  1.1633 +	while (aLineScanningPosition.iCursorPos<startPos)
  1.1634 +		{
  1.1635 +		TUint16 value = *srcePtr++;
  1.1636 +		count = value >> 12;
  1.1637 +		aLineScanningPosition.iCursorPos+=count+1;
  1.1638 +		}
  1.1639 +	if (aLineScanningPosition.iCursorPos>startPos)
  1.1640 +		{
  1.1641 +		aLineScanningPosition.iCursorPos-=(count+1);
  1.1642 +		srcePtr--;
  1.1643 +		}
  1.1644 +
  1.1645 +	// Then scan the line
  1.1646 +	count=0;
  1.1647 +	while (aLineScanningPosition.iCursorPos+count<startPos+aLength)
  1.1648 +		{
  1.1649 +		TUint16 value = *srcePtr;
  1.1650 +		count = value >> 12;
  1.1651 +		value &= 0x0fff;
  1.1652 +		TInt distanceToTheLineEnd=startPos+aLength-aLineScanningPosition.iCursorPos;
  1.1653 +		if (count<distanceToTheLineEnd)
  1.1654 +			{
  1.1655 +			srcePtr++;
  1.1656 +			for (TInt ii=0 ; ii<=count ; ii++)
  1.1657 +				{
  1.1658 +				if (aLineScanningPosition.iCursorPos>=startPos)
  1.1659 +					{
  1.1660 +					if (writes>0)
  1.1661 +						*destPtr++ = value;
  1.1662 +					writes-=2;
  1.1663 +					}
  1.1664 +				aLineScanningPosition.iCursorPos++;
  1.1665 +
  1.1666 +				}
  1.1667 +			count=0;
  1.1668 +			}
  1.1669 +		else
  1.1670 +			{
  1.1671 +			for (TInt ii=0 ; ii<distanceToTheLineEnd ; ii++)
  1.1672 +				{
  1.1673 +				writes-=2;
  1.1674 +				*destPtr++ = value;
  1.1675 +				if (writes==0)
  1.1676 +					break;
  1.1677 +				}
  1.1678 +			}
  1.1679 +		}
  1.1680 +	aLineScanningPosition.iSrcDataPtr=(TUint8*) srcePtr;
  1.1681 +	}
  1.1682 +
  1.1683 +/**
  1.1684 +The method generates a line from compressed 16 bpp bitmap data.
  1.1685 +@internalComponent
  1.1686 +@see TScanLineDecompressor
  1.1687 +*/
  1.1688 +void CBitwiseBitmap::GenerateLineFromCompressedSixteenBitData(TUint8* aDestBuffer,
  1.1689 +															  const TPoint& aPixel,
  1.1690 +															  TInt aLength,
  1.1691 +															  TUint32* aBase,
  1.1692 +															  TLineScanningPosition& aLineScanningPosition) const
  1.1693 +	{
  1.1694 +	TInt comprDataBytes = iHeader.iBitmapSize - sizeof(SEpocBitmapHeader);
  1.1695 +	TScanLineDecompressor<E2bpp, E2bpp> decompr(aBase, comprDataBytes, iPile!=NULL);
  1.1696 +	decompr(aDestBuffer, aPixel, aLineScanningPosition, iByteWidth, iByteWidth, aLength);
  1.1697 +	}
  1.1698 +
  1.1699 +/**
  1.1700 +The method generates a line from compressed 24 bpp bitmap data.
  1.1701 +@internalComponent
  1.1702 +@see TScanLineDecompressor
  1.1703 +*/
  1.1704 +void CBitwiseBitmap::GenerateLineFromCompressed24BitData(
  1.1705 +									 TUint8* aDestBuffer,
  1.1706 +									 const TPoint& aPixel,
  1.1707 +									 TInt aLength,
  1.1708 +									 TUint32* aBase,
  1.1709 +									 TLineScanningPosition& aLineScanningPosition) const
  1.1710 +	{
  1.1711 +	TInt comprDataBytes = iHeader.iBitmapSize - sizeof(SEpocBitmapHeader);
  1.1712 +	TScanLineDecompressor<E3bpp, E3bpp> decompr(aBase, comprDataBytes, iPile!=NULL);
  1.1713 +	decompr(aDestBuffer, aPixel, aLineScanningPosition, iByteWidth, iByteWidth, aLength);
  1.1714 +	}
  1.1715 +
  1.1716 +/**
  1.1717 +The method generates a line from compressed 24 bpp to 32 bpp bitmap data .
  1.1718 +@internalComponent
  1.1719 +@see TScanLineDecompressor
  1.1720 +*/
  1.1721 +void CBitwiseBitmap::GenerateLineFromCompressed32UBitData(
  1.1722 +									 TUint8* aDestBuffer,
  1.1723 +									 const TPoint& aPixel,
  1.1724 +									 TInt aLength,
  1.1725 +									 TUint32* aBase,
  1.1726 +									 TLineScanningPosition& aLineScanningPosition) const
  1.1727 +	{
  1.1728 +	TInt comprDataBytes = iHeader.iBitmapSize - sizeof(SEpocBitmapHeader);
  1.1729 +	TScanLineDecompressor<E3bpp, E4bpp> decompr(aBase, comprDataBytes, iPile!=NULL);
  1.1730 +
  1.1731 +	TUint32 theByteWidthSrc = iHeader.iSizeInPixels.iWidth * 3;
  1.1732 +	decompr(aDestBuffer, aPixel, aLineScanningPosition, theByteWidthSrc, iByteWidth, aLength);
  1.1733 +	}
  1.1734 +
  1.1735 +/**
  1.1736 +The method generates a line from compressed 32 bpp to 32 bpp bitmap data .
  1.1737 +@internalComponent
  1.1738 +@see TScanLineDecompressor
  1.1739 +*/
  1.1740 +void CBitwiseBitmap::GenerateLineFromCompressed32ABitData(
  1.1741 + 									 TUint8* aDestBuffer,
  1.1742 + 									 const TPoint& aPixel,
  1.1743 + 									 TInt aLength,
  1.1744 + 									 TUint32* aBase,
  1.1745 + 									 TLineScanningPosition& aLineScanningPosition) const
  1.1746 +	{
  1.1747 +	TInt comprDataBytes = iHeader.iBitmapSize - sizeof(SEpocBitmapHeader);
  1.1748 +	TScanLineDecompressor<E4bpp, E4bpp> decompr(aBase, comprDataBytes, iPile!=NULL);
  1.1749 +
  1.1750 +	TUint32 theByteWidthSrc = iHeader.iSizeInPixels.iWidth * 4;
  1.1751 +	decompr(aDestBuffer, aPixel, aLineScanningPosition, theByteWidthSrc, iByteWidth, aLength);
  1.1752 +	}
  1.1753 +
  1.1754 +TInt CBitwiseBitmap::DoGetScanLinePtr(TUint32*& aSlptr, TPoint& aPixel,TInt aLength,TUint32* aBase, TLineScanningPosition& aLineScanningPosition) const
  1.1755 +	{
  1.1756 +	TUint8* buf=NULL;
  1.1757 +	HBufC8* hBuf=aLineScanningPosition.iScanLineBuffer;
  1.1758 +	if (!hBuf)
  1.1759 +		{
  1.1760 +		RFbsSession* session=RFbsSession::GetSession();
  1.1761 +		if (session)
  1.1762 +			{
  1.1763 +			hBuf=session->GetScanLineBuffer();
  1.1764 +			}
  1.1765 +		else
  1.1766 +			{
  1.1767 +			aSlptr=NULL;
  1.1768 +			return KErrSessionClosed;
  1.1769 +			}
  1.1770 +		aLineScanningPosition.iScanLineBuffer=hBuf;
  1.1771 +		}
  1.1772 +	__ASSERT_ALWAYS(hBuf && hBuf->Des().MaxLength() >= iByteWidth, User::Invariant());
  1.1773 +	buf = const_cast<TUint8*>(hBuf->Ptr());
  1.1774 +	switch(iHeader.iCompression)
  1.1775 +		{
  1.1776 +		case ETwelveBitRLECompression:
  1.1777 +			GenerateLineFromCompressedTwelveBitData(buf, aPixel,aLength, aBase, aLineScanningPosition);
  1.1778 +			break;
  1.1779 +		case EByteRLECompression:
  1.1780 +			GenerateLineFromCompressedEightBitData(buf, aPixel,aLength, aBase, aLineScanningPosition);
  1.1781 +			break;
  1.1782 +		case ESixteenBitRLECompression:
  1.1783 +			GenerateLineFromCompressedSixteenBitData(buf, aPixel,aLength, aBase, aLineScanningPosition);
  1.1784 +			break;
  1.1785 +		case ETwentyFourBitRLECompression:
  1.1786 +			GenerateLineFromCompressed24BitData(buf, aPixel, aLength, aBase, aLineScanningPosition);
  1.1787 +			break;
  1.1788 +		case EThirtyTwoUBitRLECompression:
  1.1789 +			GenerateLineFromCompressed32UBitData(buf, aPixel, aLength, aBase, aLineScanningPosition);
  1.1790 +			break;
  1.1791 +		case EThirtyTwoABitRLECompression:
  1.1792 +			GenerateLineFromCompressed32ABitData(buf, aPixel, aLength, aBase, aLineScanningPosition);
  1.1793 +			break;
  1.1794 +		case EGenericPaletteCompression:
  1.1795 +			GenerateLineFromPaletteCompressedData(buf, aPixel, aLength, aBase, aLineScanningPosition);
  1.1796 +			break;
  1.1797 +		case EProprietaryCompression:
  1.1798 +			if (aLineScanningPosition.iRasterizer)
  1.1799 +				{
  1.1800 +				aSlptr = const_cast<TUint32*>(aLineScanningPosition.iRasterizer->ScanLine(Extra()->iSerialNumber, aPixel, aLength));
  1.1801 +				if (aSlptr)
  1.1802 +					{
  1.1803 +					return KErrNone;
  1.1804 +					}
  1.1805 +				}
  1.1806 +			WhiteFill(buf, iByteWidth, iSettings.CurrentDisplayMode());
  1.1807 +			break;
  1.1808 +		default:
  1.1809 +			{
  1.1810 +			__ASSERT_DEBUG(EFalse, ::Panic(EFbsBitmapInvalidCompression));
  1.1811 +			return KErrNotSupported;
  1.1812 +			}
  1.1813 +		}
  1.1814 +	aSlptr = (TUint32*) buf;
  1.1815 +	return KErrNone;
  1.1816 +	}
  1.1817 +
  1.1818 +EXPORT_C void CBitwiseBitmap::GetScanLine(TDes8& aBuf,const TPoint& aPixel,TInt aLength,TBool aDither,const TPoint& aDitherOffset,TDisplayMode aDispMode,TUint32* aBase,TLineScanningPosition& aLineScanningPosition) const
  1.1819 +	{
  1.1820 +	if (!iDataOffset)
  1.1821 +		return;
  1.1822 +
  1.1823 +	TPoint pixel(aPixel);
  1.1824 +	TUint32* slptr=NULL;
  1.1825 +	GetScanLinePtr(slptr, aLength, pixel,aBase, aLineScanningPosition);
  1.1826 +	GetScanLine(slptr,aBuf,pixel,aLength,aDither,aDitherOffset,aDispMode);
  1.1827 +	}
  1.1828 +
  1.1829 +EXPORT_C void CBitwiseBitmap::GetScanLine(TUint32*& aScanLinePtr, TDes8& aDestBuf,const TPoint& aPixel,TInt aLength,TBool aDither,
  1.1830 +	const TPoint& aDitherOffset,TDisplayMode aDestinationDispMode) const
  1.1831 +	{
  1.1832 +	if (!iDataOffset)
  1.1833 +		return;
  1.1834 +	TDisplayMode currentDisplayMode = iSettings.CurrentDisplayMode();
  1.1835 +	if (!aScanLinePtr) // if scanline pointer is null,
  1.1836 +		{
  1.1837 +		WhiteFill((TUint8*)aDestBuf.Ptr(),aDestBuf.MaxLength(),currentDisplayMode);
  1.1838 +		return;
  1.1839 +		}
  1.1840 +	TUint8* ptr = (TUint8*)aDestBuf.Ptr();
  1.1841 +	// if dest pointer is not aligned
  1.1842 +	if (!(TUint32(ptr)&3) && aDestinationDispMode == currentDisplayMode)
  1.1843 +		{
  1.1844 +		if (iHeader.iBitsPerPixel < 8)
  1.1845 +			GetScanLineExBits(aDestBuf,aPixel.iX,aLength,aScanLinePtr);
  1.1846 +		else
  1.1847 +			GetScanLineExBytes(aDestBuf,aPixel.iX,aLength,aScanLinePtr);
  1.1848 +		return;
  1.1849 +		}
  1.1850 +	//read the scanline in destination display format.
  1.1851 +	switch (aDestinationDispMode)
  1.1852 +		{
  1.1853 +	case EGray2:
  1.1854 +		GetScanLineGray2(aDestBuf,aPixel,aLength,aDither,aDitherOffset,aScanLinePtr);
  1.1855 +		break;
  1.1856 +	case EGray4:
  1.1857 +		GetScanLineGray4(aDestBuf,aPixel,aLength,aDither,aDitherOffset,aScanLinePtr);
  1.1858 +		break;
  1.1859 +	case EGray16:
  1.1860 +		GetScanLineGray16(aDestBuf,aPixel,aLength,aScanLinePtr);
  1.1861 +		break;
  1.1862 +	case EGray256:
  1.1863 +		GetScanLineGray256(aDestBuf,aPixel,aLength,aScanLinePtr);
  1.1864 +		break;
  1.1865 +	case EColor16:
  1.1866 +		GetScanLineColor16(aDestBuf,aPixel,aLength,aScanLinePtr);
  1.1867 +		break;
  1.1868 +	case EColor256:
  1.1869 +		GetScanLineColor256(aDestBuf,aPixel,aLength,aScanLinePtr);
  1.1870 +		break;
  1.1871 +	case EColor4K:
  1.1872 +		GetScanLineColor4K(aDestBuf,aPixel,aLength,aScanLinePtr);
  1.1873 +		break;
  1.1874 +	case EColor64K:
  1.1875 +		GetScanLineColor64K(aDestBuf,aPixel,aLength,aScanLinePtr);
  1.1876 +		break;
  1.1877 +	case EColor16M:
  1.1878 +		GetScanLineColor16M(aDestBuf,aPixel,aLength,aScanLinePtr);
  1.1879 +		break;
  1.1880 +	case ERgb:
  1.1881 +		GetScanLineColorRgb(aDestBuf,aPixel,aLength,aScanLinePtr);
  1.1882 +		break;
  1.1883 +	case EColor16MU:
  1.1884 +		GetScanLineColor16MU(aDestBuf,aPixel,aLength,aScanLinePtr);
  1.1885 +		break;
  1.1886 +	case EColor16MA:
  1.1887 +		GetScanLineColor16MA(aDestBuf,aPixel,aLength,aScanLinePtr);
  1.1888 +		break;
  1.1889 +	case EColor16MAP:
  1.1890 +		GetScanLineColor16MAP(aDestBuf,aPixel,aLength,aScanLinePtr);
  1.1891 +		break;
  1.1892 +	default:
  1.1893 +		aDestBuf.SetLength(0);
  1.1894 +		break;
  1.1895 +		};
  1.1896 +	}
  1.1897 +
  1.1898 +EXPORT_C void CBitwiseBitmap::GetScanLine(TDes8& aBuf,const TPoint& aPixel,TInt aLength,TBool aDither,const TPoint& aDitherOffset,TDisplayMode aDispMode,TUint32* aBase) const
  1.1899 +	{
  1.1900 +	TLineScanningPosition pos(aBase);
  1.1901 +	TUint8* base = REINTERPRET_CAST(TUint8*,aBase);
  1.1902 +	const TCompressionBookMark* bookMarkPtr = NULL;
  1.1903 +
  1.1904 +	GetLineScanPos(pos, bookMarkPtr, base);
  1.1905 +	GetScanLine(aBuf,aPixel,aLength,aDither,aDitherOffset,aDispMode,aBase,pos);
  1.1906 +	UpdateBookMark(pos, const_cast<TCompressionBookMark*>(bookMarkPtr), base);
  1.1907 +	}
  1.1908 +
  1.1909 +/**
  1.1910 +Gets the bitmapís vertical scanline starting at the specified x co-ordinate and using
  1.1911 +the specified dither offset.
  1.1912 +Note: The method works for uncompressed bitmaps only.
  1.1913 +@param aBuf The buffer in which the vertical scanline will be returned.
  1.1914 +@param aX The x co-ordinate of the vertical scanline to get.
  1.1915 +@param aDitherOffset The dither offset of the bitmap.
  1.1916 +@param aDispMode Format to be used to write the data to the buffer.
  1.1917 +@param aBase The bitmap's data start address.
  1.1918 +*/
  1.1919 +EXPORT_C void CBitwiseBitmap::GetVerticalScanLine(TDes8& aBuf,TInt aX,TBool aDither,
  1.1920 +												  const TPoint& aDitherOffset,
  1.1921 +												  TDisplayMode aDispMode,
  1.1922 +												  TUint32* aBase,
  1.1923 +												  CFbsRasterizer* aRasterizer) const
  1.1924 +	{
  1.1925 +	if (iHeader.iCompression != ENoBitmapCompression && iHeader.iCompression != EProprietaryCompression)
  1.1926 +		{
  1.1927 +		__ASSERT_DEBUG(EFalse, ::Panic(EFbsBitmapInvalidCompression));
  1.1928 +		return; //not supported for compressed bitmaps
  1.1929 +		}
  1.1930 +
  1.1931 +	if (!iDataOffset)
  1.1932 +		{
  1.1933 +		return;
  1.1934 +		}
  1.1935 +	AdjustXCoord(aX);
  1.1936 +	TInt height=iHeader.iSizeInPixels.iHeight;
  1.1937 +	TUint32* slptr=aBase;
  1.1938 +	TUint8* ptr = (TUint8*)aBuf.Ptr();
  1.1939 +	*ptr=0;
  1.1940 +	const TInt wordwidth=iByteWidth>>2;
  1.1941 +	TInt y = 0;
  1.1942 +
  1.1943 +	if (iHeader.iCompression == EProprietaryCompression)
  1.1944 +		{
  1.1945 +		if (aRasterizer)
  1.1946 +			{
  1.1947 +			slptr = const_cast<TUint32*>(aRasterizer->ScanLine(Extra()->iSerialNumber, TPoint(aX,0), 1));
  1.1948 +			if (!slptr)
  1.1949 +				{
  1.1950 +				// wrong rasterizer for this extended bitmap - fill vertical scan line with white pixels
  1.1951 +				TInt bufLength = ByteWidth(height, aDispMode);
  1.1952 +				aBuf.SetLength(bufLength);
  1.1953 +				WhiteFill(ptr, bufLength, aDispMode);
  1.1954 +				return;
  1.1955 +				}
  1.1956 +			}
  1.1957 +		else
  1.1958 +			{
  1.1959 +			// no rasterizer - fill vertical scan line with white pixels
  1.1960 +			TInt bufLength = ByteWidth(height, aDispMode);
  1.1961 +			aBuf.SetLength(bufLength);
  1.1962 +			WhiteFill(ptr, bufLength, aDispMode);
  1.1963 +			return;
  1.1964 +			}
  1.1965 +		}
  1.1966 +
  1.1967 +	switch(aDispMode)
  1.1968 +		{
  1.1969 +		case EGray2:
  1.1970 +			{
  1.1971 +			TBool oddx=(aDitherOffset.iX&1);
  1.1972 +			TBool oddy=(aDitherOffset.iY&1);
  1.1973 +			height=Min(height,(TInt)((aBuf.MaxLength())<<3));
  1.1974 +			aBuf.SetLength((height+7)>>3);
  1.1975 +			TUint8 mask=1;
  1.1976 +			for(TInt count=0;count<height;count++)
  1.1977 +				{
  1.1978 +				if (!mask)
  1.1979 +					{
  1.1980 +					mask=1;
  1.1981 +					ptr++;
  1.1982 +					*ptr = 0;
  1.1983 +					}
  1.1984 +				if (HashTo1bpp(GetGrayPixelEx(aX,slptr),oddx,oddy))
  1.1985 +					*ptr|=mask;
  1.1986 +				oddx^=1;
  1.1987 +				mask<<=1;
  1.1988 +				slptr = aRasterizer ? const_cast<TUint32*>(aRasterizer->ScanLine(Extra()->iSerialNumber, TPoint(aX,++y), 1)) : slptr + wordwidth;
  1.1989 +				}
  1.1990 +			 break;
  1.1991 +			}
  1.1992 +		case EGray4:
  1.1993 +			{
  1.1994 +			height=Min(height,(TInt)((aBuf.MaxLength())<<2));
  1.1995 +			aBuf.SetLength((height+3)>>2);
  1.1996 +			TInt shift=0;
  1.1997 +			TUint8 col=0;
  1.1998 +			if (iHeader.iBitsPerPixel==4 && aDither)
  1.1999 +				{
  1.2000 +				const TInt hasharray[4]={0,3,2,1};
  1.2001 +				TInt index=(aDitherOffset.iX&1)+((aDitherOffset.iY&1)<<1);
  1.2002 +				for(TInt count=0;count<height;count++,shift+=2)
  1.2003 +					{
  1.2004 +					if (shift==8)
  1.2005 +						{
  1.2006 +						shift=0;
  1.2007 +						ptr++;
  1.2008 +						*ptr=0;
  1.2009 +						}
  1.2010 +					col = TUint8(GetGrayPixelEx(aX,slptr) >> 4);
  1.2011 +					TInt value = col / 5;
  1.2012 +					col%=5;
  1.2013 +					if (col>2) col--;
  1.2014 +					if (hasharray[index]<TInt(col))
  1.2015 +						value++;
  1.2016 +					value<<=shift;
  1.2017 +					*ptr|=value;
  1.2018 +					index^=1;
  1.2019 +					slptr = aRasterizer ? const_cast<TUint32*>(aRasterizer->ScanLine(Extra()->iSerialNumber, TPoint(aX,++y), 1)) : slptr + wordwidth;
  1.2020 +					}
  1.2021 +				}
  1.2022 +			else
  1.2023 +				{
  1.2024 +				for(TInt count=0;count<height;count++,shift+=2)
  1.2025 +					{
  1.2026 +					if (shift==8)
  1.2027 +						{
  1.2028 +						shift=0;
  1.2029 +						ptr++;
  1.2030 +						*ptr=0;
  1.2031 +						}
  1.2032 +					col = TUint8(GetGrayPixelEx(aX,slptr) >> 6);
  1.2033 +					col<<=shift;
  1.2034 +					*ptr|=col;
  1.2035 +					slptr = aRasterizer ? const_cast<TUint32*>(aRasterizer->ScanLine(Extra()->iSerialNumber, TPoint(aX,++y), 1)) : slptr + wordwidth;
  1.2036 +					}
  1.2037 +				}
  1.2038 +			 break;
  1.2039 +			}
  1.2040 +		case EGray16:
  1.2041 +			{
  1.2042 +			height = Min(height,aBuf.MaxLength()<<1);
  1.2043 +			aBuf.SetLength((height+1)>>1);
  1.2044 +			TUint8* ptrLimit = ptr + aBuf.Length() - 1;
  1.2045 +			while (ptr < ptrLimit)
  1.2046 +				{
  1.2047 +				*ptr = TUint8(GetGrayPixelEx(aX,slptr) >> 4);
  1.2048 +				slptr = aRasterizer ? const_cast<TUint32*>(aRasterizer->ScanLine(Extra()->iSerialNumber, TPoint(aX,++y), 1)) : slptr + wordwidth;
  1.2049 +				*ptr++ |= GetGrayPixelEx(aX,slptr) & 0xf0;
  1.2050 +				slptr = aRasterizer ? const_cast<TUint32*>(aRasterizer->ScanLine(Extra()->iSerialNumber, TPoint(aX,++y), 1)) : slptr + wordwidth;
  1.2051 +				}
  1.2052 +			// Fill last byte.
  1.2053 +			// If height is odd, upper 4 bits are zeroed.
  1.2054 +			*ptr = TUint8(GetGrayPixelEx(aX,slptr) >> 4);
  1.2055 +			if (!(height & 1))
  1.2056 +				{
  1.2057 +				// Only fill upper 4 bits of last byte if height is even. 
  1.2058 +				slptr = aRasterizer ? const_cast<TUint32*>(aRasterizer->ScanLine(Extra()->iSerialNumber, TPoint(aX,++y), 1)) : slptr + wordwidth;
  1.2059 +				*ptr++ = GetGrayPixelEx(aX,slptr) & 0xf0;
  1.2060 +				}
  1.2061 +			break;
  1.2062 +			}
  1.2063 +		case EColor16:
  1.2064 +			{
  1.2065 +			height=Min(height,aBuf.MaxLength()<<1);
  1.2066 +			aBuf.SetLength((height+1)>>1);
  1.2067 +			TUint8* ptrLimit = ptr + aBuf.Length() - 1;
  1.2068 +
  1.2069 +			while (ptr < ptrLimit)
  1.2070 +				{
  1.2071 +				*ptr = TUint8(GetRgbPixelEx(aX,slptr).Color16());
  1.2072 +				slptr = aRasterizer ? const_cast<TUint32*>(aRasterizer->ScanLine(Extra()->iSerialNumber, TPoint(aX,++y), 1)) : slptr + wordwidth;
  1.2073 +				*ptr++ |= GetRgbPixelEx(aX,slptr).Color16() << 4;
  1.2074 +				slptr = aRasterizer ? const_cast<TUint32*>(aRasterizer->ScanLine(Extra()->iSerialNumber, TPoint(aX,++y), 1)) : slptr + wordwidth;
  1.2075 +				}
  1.2076 +			// Fill last byte.
  1.2077 +			// If height is odd, upper 4 bits are zeroed.
  1.2078 +			*ptr = TUint8(GetRgbPixelEx(aX,slptr).Color16());
  1.2079 +			if (!(height & 1))
  1.2080 +				{
  1.2081 +				// Only fill upper 4 bits of last byte if height is even. 
  1.2082 +				slptr = aRasterizer ? const_cast<TUint32*>(aRasterizer->ScanLine(Extra()->iSerialNumber, TPoint(aX,++y), 1)) : slptr + wordwidth;
  1.2083 +				*ptr++ = GetRgbPixelEx(aX,slptr).Color16() << 4;
  1.2084 +				}
  1.2085 +			break;
  1.2086 +			}
  1.2087 +		case EGray256:
  1.2088 +			{
  1.2089 +			height = Min(height,aBuf.MaxLength());
  1.2090 +			aBuf.SetLength(height);
  1.2091 +			TUint8* ptrLimit = ptr + height;
  1.2092 +			while (ptr < ptrLimit)
  1.2093 +				{
  1.2094 +				*ptr++ = GetGrayPixelEx(aX,slptr);
  1.2095 +				slptr = aRasterizer ? const_cast<TUint32*>(aRasterizer->ScanLine(Extra()->iSerialNumber, TPoint(aX,++y), 1)) : slptr + wordwidth;
  1.2096 +				}
  1.2097 +			break;
  1.2098 +			}
  1.2099 +		case EColor256:
  1.2100 +			{
  1.2101 +			height = Min(height,aBuf.MaxLength());
  1.2102 +			aBuf.SetLength(height);
  1.2103 +			TUint8* ptrLimit = ptr + height;
  1.2104 +
  1.2105 +			while (ptr < ptrLimit)
  1.2106 +				{
  1.2107 +				*ptr++ = TUint8(GetRgbPixelEx(aX,slptr).Color256());
  1.2108 +				slptr = aRasterizer ? const_cast<TUint32*>(aRasterizer->ScanLine(Extra()->iSerialNumber, TPoint(aX,++y), 1)) : slptr + wordwidth;
  1.2109 +				}
  1.2110 +			break;
  1.2111 +			}
  1.2112 +		case EColor4K:
  1.2113 +			{
  1.2114 +			height = Min(height,aBuf.MaxLength() >> 1);
  1.2115 +			aBuf.SetLength(height << 1);
  1.2116 +			TUint16* dwordPtr = (TUint16*)ptr;
  1.2117 +			TUint16* ptrLimit = dwordPtr + height;
  1.2118 +
  1.2119 +			while (dwordPtr < ptrLimit)
  1.2120 +				{
  1.2121 +				*dwordPtr++ = TUint16(GetRgbPixelEx(aX,slptr)._Color4K());
  1.2122 +				slptr = aRasterizer ? const_cast<TUint32*>(aRasterizer->ScanLine(Extra()->iSerialNumber, TPoint(aX,++y), 1)) : slptr + wordwidth;
  1.2123 +				}
  1.2124 +			break;
  1.2125 +			}
  1.2126 +		case EColor64K:
  1.2127 +			{
  1.2128 +			height = Min(height,aBuf.MaxLength() >> 1);
  1.2129 +			aBuf.SetLength(height << 1);
  1.2130 +			TUint16* dwordPtr = (TUint16*)ptr;
  1.2131 +			TUint16* ptrLimit = dwordPtr + height;
  1.2132 +
  1.2133 +			while (dwordPtr < ptrLimit)
  1.2134 +				{
  1.2135 +				*dwordPtr++ = TUint16(GetRgbPixelEx(aX,slptr)._Color64K());
  1.2136 +				slptr = aRasterizer ? const_cast<TUint32*>(aRasterizer->ScanLine(Extra()->iSerialNumber, TPoint(aX,++y), 1)) : slptr + wordwidth;
  1.2137 +				}
  1.2138 +			break;
  1.2139 +			}
  1.2140 +		case EColor16M:
  1.2141 +			{
  1.2142 +			height = Min(height,aBuf.MaxLength() / 3);
  1.2143 +			aBuf.SetLength(height * 3);
  1.2144 +			const TUint8* ptrLimit = ptr + (height * 3);
  1.2145 +
  1.2146 +			while (ptr < ptrLimit)
  1.2147 +				{
  1.2148 +				const TInt color16M = GetRgbPixelEx(aX,slptr)._Color16M();
  1.2149 +				*ptr++ = TUint8(color16M);
  1.2150 +				*ptr++ = TUint8(color16M >> 8);
  1.2151 +				*ptr++ = TUint8(color16M >> 16);
  1.2152 +				slptr = aRasterizer ? const_cast<TUint32*>(aRasterizer->ScanLine(Extra()->iSerialNumber, TPoint(aX,++y), 1)) : slptr + wordwidth;
  1.2153 +				}
  1.2154 +			break;
  1.2155 +			}
  1.2156 +		case ERgb:
  1.2157 +		case EColor16MU:
  1.2158 +		case EColor16MA:
  1.2159 +		case EColor16MAP:
  1.2160 +			{
  1.2161 +			height = Min(height,aBuf.MaxLength() >> 2);
  1.2162 +			aBuf.SetLength(height << 2);
  1.2163 +			TUint32* pixelPtr = (TUint32*)ptr;
  1.2164 +			TUint32* pixelPtrLimit = pixelPtr + height;
  1.2165 +
  1.2166 +			if (aDispMode == EColor16MAP && iSettings.CurrentDisplayMode() == EColor16MA)
  1.2167 +				while (pixelPtr < pixelPtrLimit)
  1.2168 +				{
  1.2169 +				*pixelPtr++ = NonPMA2PMAPixel(*(slptr + aX));
  1.2170 +				slptr = aRasterizer ? const_cast<TUint32*>(aRasterizer->ScanLine(Extra()->iSerialNumber, TPoint(aX,++y), 1)) : slptr + wordwidth;
  1.2171 +				}
  1.2172 +			else if (aDispMode == EColor16MAP && iSettings.CurrentDisplayMode() == EColor16MAP)
  1.2173 +				while (pixelPtr < pixelPtrLimit)
  1.2174 +				{
  1.2175 +				*pixelPtr++ = *(slptr + aX);
  1.2176 +				slptr = aRasterizer ? const_cast<TUint32*>(aRasterizer->ScanLine(Extra()->iSerialNumber, TPoint(aX,++y), 1)) : slptr + wordwidth;
  1.2177 +				}
  1.2178 +			else if (aDispMode == EColor16MU)
  1.2179 +				while (pixelPtr < pixelPtrLimit)
  1.2180 +					{
  1.2181 +					*pixelPtr++ = GetRgbPixelEx(aX, slptr).Internal() | 0xFF000000;
  1.2182 +					slptr = aRasterizer ? const_cast<TUint32*>(aRasterizer->ScanLine(Extra()->iSerialNumber, TPoint(aX,++y), 1)) : slptr + wordwidth;
  1.2183 +					}
  1.2184 +			else
  1.2185 +				while (pixelPtr < pixelPtrLimit)
  1.2186 +					{
  1.2187 +					*pixelPtr++ = GetRgbPixelEx(aX, slptr).Internal();
  1.2188 +					slptr = aRasterizer ? const_cast<TUint32*>(aRasterizer->ScanLine(Extra()->iSerialNumber, TPoint(aX,++y), 1)) : slptr + wordwidth;
  1.2189 +					}
  1.2190 +			break;
  1.2191 +			}
  1.2192 +		default:
  1.2193 +			aBuf.SetLength(0);
  1.2194 +		}
  1.2195 +	}
  1.2196 +
  1.2197 +EXPORT_C void CBitwiseBitmap::StretchScanLine(TDes8& aBuf,const TPoint& aPixel,TInt aClipStrchX,TInt aClipStrchLen,TInt aStretchLength,TInt aOrgX,TInt aOrgLen,const TPoint& aDitherOffset,TDisplayMode aDispMode,TUint32* aBase) const
  1.2198 +	{
  1.2199 +	TLineScanningPosition pos(aBase);
  1.2200 +	StretchScanLine(aBuf,aPixel,aClipStrchX,aClipStrchLen,aStretchLength,aOrgX,aOrgLen,aDitherOffset,aDispMode,aBase,pos);
  1.2201 +	}
  1.2202 +
  1.2203 +EXPORT_C void CBitwiseBitmap::StretchScanLine(TDes8& aBuf,const TPoint& aPixel,TInt aClipStrchX,TInt aClipStrchLen,TInt aStretchLength,TInt aOrgX,TInt aOrgLen,const TPoint& aDitherOffset,TDisplayMode aDispMode,TUint32* aBase, TLineScanningPosition& aLineScanningPosition) const
  1.2204 +	{
  1.2205 +	if (!iDataOffset)
  1.2206 +		return;
  1.2207 +	TInt x=aPixel.iX,y=aPixel.iY;
  1.2208 +	if (x>=iHeader.iSizeInPixels.iWidth || x<-iHeader.iSizeInPixels.iWidth)
  1.2209 +		x%=iHeader.iSizeInPixels.iWidth;
  1.2210 +	if (y>=iHeader.iSizeInPixels.iHeight || y<-iHeader.iSizeInPixels.iHeight)
  1.2211 +		y%=iHeader.iSizeInPixels.iHeight;
  1.2212 +	if (x<0) x+=iHeader.iSizeInPixels.iWidth;
  1.2213 +	if (y<0) y+=iHeader.iSizeInPixels.iHeight;
  1.2214 +	if (aStretchLength<aOrgLen)
  1.2215 +		DoCompressScanLine(aBuf,x,y,aClipStrchX,aClipStrchLen,aStretchLength,aOrgX,aOrgLen,aDitherOffset,aDispMode,aBase,aLineScanningPosition);
  1.2216 +	else
  1.2217 +		DoStretchScanLine(aBuf,x,y,aClipStrchX,aClipStrchLen,aStretchLength,aOrgX,aOrgLen,aDitherOffset,aDispMode,aBase,aLineScanningPosition);
  1.2218 +	}
  1.2219 +
  1.2220 +EXPORT_C TUint32* CBitwiseBitmap::ScanLineAddress(TUint32* aBase,TUint aY) const
  1.2221 +	{
  1.2222 +	if (aY == 0 || iDataOffset == 0)
  1.2223 +		return aBase;
  1.2224 +
  1.2225 +	if (aY >= TUint(iHeader.iSizeInPixels.iHeight))
  1.2226 +		aY %= iHeader.iSizeInPixels.iHeight;
  1.2227 +
  1.2228 +	return aBase + (aY * (DataStride() >> 2));
  1.2229 +	}
  1.2230 +
  1.2231 +TUint32* CBitwiseBitmap::DataAddress() const
  1.2232 +	{
  1.2233 +	if (iDataOffset==0) return(NULL);
  1.2234 +
  1.2235 +	if(iUid.iUid==KCBitwiseBitmapHardwareUid.iUid)   // RHardwareBitmap
  1.2236 +		{
  1.2237 +		RHardwareBitmap hwb(iDataOffset);	// iDataOffset = handle for hardware bitmap
  1.2238 +		TAcceleratedBitmapInfo info;
  1.2239 +		const TInt ret = hwb.GetInfo(info);
  1.2240 +		return ret!=KErrNone ? NULL : (reinterpret_cast<TUint32*>(info.iAddress));
  1.2241 +		}
  1.2242 +
  1.2243 +	if (iHeap == NULL)
  1.2244 +		return(reinterpret_cast<TUint32*>((TUint8*)this+iDataOffset));
  1.2245 +	return(reinterpret_cast<TUint32*>(iPile->ChunkBase()+iDataOffset));
  1.2246 +	}
  1.2247 +
  1.2248 +EXPORT_C TInt CBitwiseBitmap::DataStride() const
  1.2249 +	{
  1.2250 +	return iByteWidth;
  1.2251 +	}
  1.2252 +
  1.2253 +TUint32 CBitwiseBitmap::HashTo2bpp(TUint32 aGray256,TInt aDitherIndex) const
  1.2254 +	{
  1.2255 +	static const TUint hasharray[4]={0,3,2,1};
  1.2256 +	TInt gray16 = aGray256 >> 4;
  1.2257 +	TInt gray4 = gray16 + 1;
  1.2258 +	gray4 += gray4 << 1;
  1.2259 +	gray4 >>= 4;
  1.2260 +	gray16 %= 5;
  1.2261 +	if (gray16 > 2)
  1.2262 +		gray16--;
  1.2263 +	if (hasharray[aDitherIndex] < TUint(gray16))
  1.2264 +		gray4++;
  1.2265 +	return gray4;
  1.2266 +	}
  1.2267 +
  1.2268 +TUint32 CBitwiseBitmap::HashTo1bpp(TUint32 aGray256,TBool aOddX,TBool aOddY) const
  1.2269 +	{
  1.2270 +	TUint32 aGray4 = aGray256 >> 6;
  1.2271 +	switch(aGray4)
  1.2272 +		{
  1.2273 +		case 3:
  1.2274 +			return 1;
  1.2275 +		case 2:
  1.2276 +			{
  1.2277 +			if (aOddX && aOddY)
  1.2278 +				return 0;
  1.2279 +			else
  1.2280 +				return 1;
  1.2281 +			}
  1.2282 +		case 1:
  1.2283 +			{
  1.2284 +			if ((aOddX && aOddY) || (!aOddX && !aOddY))
  1.2285 +				return 1;
  1.2286 +			}
  1.2287 +			//coverity [fallthrough]
  1.2288 +		default:
  1.2289 +			return 0;
  1.2290 +		}
  1.2291 +	}
  1.2292 +
  1.2293 +/**
  1.2294 +Tests whether or not the bitmap is monochrome.
  1.2295 +Monochrome bitmaps have a display-mode of 1 bit-per-pixel.
  1.2296 +Note: The method works for uncompressed bitmaps only.
  1.2297 +@param aBase Bitmap's data base address
  1.2298 +@return True if the bitmap is monochrome; false otherwise.
  1.2299 +*/
  1.2300 +EXPORT_C TBool CBitwiseBitmap::IsMonochrome(TUint32* aBase) const
  1.2301 +	{
  1.2302 +	if (IsCompressed())
  1.2303 +		{
  1.2304 +		__ASSERT_DEBUG(EFalse, ::Panic(EFbsBitmapInvalidCompression));
  1.2305 +		return EFalse;  // Not currently supported for compressed bitmaps
  1.2306 +		}
  1.2307 +
  1.2308 +	if (!iDataOffset)
  1.2309 +		{
  1.2310 +		return(EFalse);
  1.2311 +		}
  1.2312 +	TInt bitwidth=iHeader.iBitsPerPixel*iHeader.iSizeInPixels.iWidth;
  1.2313 +	if(iHeader.iBitsPerPixel == 12)
  1.2314 +		{//EColor4K mode - 1 pixel occupies 16 bits, most significant 4 bits are not used.
  1.2315 +		bitwidth=16*iHeader.iSizeInPixels.iWidth;
  1.2316 +		}
  1.2317 +	TInt wordwidth=bitwidth>>5;
  1.2318 +	TInt endshift=32-(bitwidth&0x1f);
  1.2319 +	TInt endmask=0;
  1.2320 +	if (endshift<32) endmask=0xffffffff>>endshift;
  1.2321 +	TUint32* bitptr=aBase;
  1.2322 +	//In a loop from first to last scanline:
  1.2323 +	//Check each pixel - is it monochrome or not (pixel color must be BLACK or WHITE).
  1.2324 +	//Get next scanline.
  1.2325 +	TUint32* endbitptr=bitptr+wordwidth;
  1.2326 +	for(TInt row=0;row<iHeader.iSizeInPixels.iHeight;row++)
  1.2327 +		{
  1.2328 +		if(iHeader.iBitsPerPixel == 24)
  1.2329 +			{//1 word contains 1 pixel and 8 bits from the next pixel.
  1.2330 +			for(TInt x=0;x<iHeader.iSizeInPixels.iWidth;x++)
  1.2331 +				{
  1.2332 +				TUint8* scanLine = reinterpret_cast <TUint8*> (bitptr) + x * 3;
  1.2333 +				TUint color16M = *scanLine++;
  1.2334 +				color16M |= (*scanLine++) << 8;
  1.2335 +				color16M |= (*scanLine++) << 16;
  1.2336 +				if (IsWordMonochrome(color16M)==EFalse)
  1.2337 +					return(EFalse);
  1.2338 +				}
  1.2339 +			}
  1.2340 +		else
  1.2341 +			{
  1.2342 +			TUint32* tmpbitptr=bitptr;
  1.2343 +			while(tmpbitptr<endbitptr)
  1.2344 +				if (IsWordMonochrome(*tmpbitptr++)==EFalse)
  1.2345 +					return(EFalse);
  1.2346 +			if (endmask)
  1.2347 +				if (IsWordMonochrome(*endbitptr&endmask)==EFalse)
  1.2348 +					return(EFalse);
  1.2349 +			}
  1.2350 +		bitptr+=wordwidth;
  1.2351 +		endbitptr+=wordwidth;
  1.2352 +		}
  1.2353 +	return(ETrue);
  1.2354 +	}
  1.2355 +
  1.2356 +TBool CBitwiseBitmap::IsWordMonochrome(TUint32 aWord) const
  1.2357 +	{
  1.2358 +	TDisplayMode displayMode = iSettings.CurrentDisplayMode();
  1.2359 +	switch(displayMode)
  1.2360 +		{
  1.2361 +		case EGray2:
  1.2362 +			return ETrue;
  1.2363 +		case EGray4:
  1.2364 +			{
  1.2365 +			TUint32 lowerbits=aWord&0x55555555;
  1.2366 +			TUint32 upperbits=(aWord>>1)&0x55555555;
  1.2367 +			if (lowerbits^upperbits)
  1.2368 +				return EFalse;
  1.2369 +			return ETrue;
  1.2370 +			}
  1.2371 +		case EGray16:
  1.2372 +		case EColor16:
  1.2373 +			{
  1.2374 +			if (aWord==0xffffffff || aWord==0)
  1.2375 +				return ETrue;
  1.2376 +			for(TInt count=0;count<8;count++)
  1.2377 +				{
  1.2378 +				TUint32 nibble=aWord&0xf;
  1.2379 +				if ((nibble>0) && (nibble<0xf))
  1.2380 +					return EFalse;
  1.2381 +				aWord>>=4;
  1.2382 +				}
  1.2383 +			return ETrue;
  1.2384 +			}
  1.2385 +		case EGray256:
  1.2386 +		case EColor256:
  1.2387 +			{
  1.2388 +			TUint8* bytePtr = (TUint8*)&aWord;
  1.2389 +			TUint8* bytePtrLimit = bytePtr + 4;
  1.2390 +			while (bytePtr < bytePtrLimit)
  1.2391 +				{
  1.2392 +				if (*bytePtr && (*bytePtr != 0xff))
  1.2393 +					return EFalse;
  1.2394 +				bytePtr++;
  1.2395 +				}
  1.2396 +			return ETrue;
  1.2397 +			}
  1.2398 +		case EColor4K:
  1.2399 +			{
  1.2400 +			aWord &= 0x0fff0fff;
  1.2401 +			TUint16 color4K = (TUint16)aWord;
  1.2402 +			if (color4K && (color4K != 0xfff))
  1.2403 +				return EFalse;
  1.2404 +			color4K = (TUint16)(aWord >> 16);
  1.2405 +			if (color4K && (color4K != 0xfff))
  1.2406 +				return EFalse;
  1.2407 +			return ETrue;
  1.2408 +			}
  1.2409 +		case EColor64K:
  1.2410 +			{
  1.2411 +			TUint16 color64K = (TUint16)aWord;
  1.2412 +			if (color64K && (color64K != 0xffff))
  1.2413 +				return EFalse;
  1.2414 +			color64K = (TUint16)(aWord >> 16);
  1.2415 +			if (color64K && (color64K != 0xffff))
  1.2416 +				return EFalse;
  1.2417 +			return ETrue;
  1.2418 +			}
  1.2419 +		case EColor16M:
  1.2420 +		case EColor16MU:
  1.2421 +		case EColor16MA:
  1.2422 +		case EColor16MAP:
  1.2423 +			{
  1.2424 +			aWord &= 0xffffff;
  1.2425 +			if (aWord && (aWord != 0x00ffffff))
  1.2426 +				return EFalse;
  1.2427 +			return ETrue;
  1.2428 +			}
  1.2429 +		default:
  1.2430 +			return EFalse;
  1.2431 +		}
  1.2432 +	}
  1.2433 +
  1.2434 +EXPORT_C TBool CBitwiseBitmap::IsLargeBitmap() const
  1.2435 +	{
  1.2436 +	if(iUid.iUid==KCBitwiseBitmapHardwareUid.iUid)
  1.2437 +		return EFalse; // RHardwareBitmap
  1.2438 +
  1.2439 +	if (iHeap==NULL) return(EFalse); // rom bitmap
  1.2440 +
  1.2441 +	// Consider all RAM bitmaps large, so that legacy applications always
  1.2442 +	// call LockHeap()/UnlockHeap() around DataAddress(), which allows
  1.2443 +	// better handling of hardware acceleration caches, if present.
  1.2444 +	// Note that, since the large bitmap threshold has always been in the
  1.2445 +	// documentation, it is not guaranteed that legacy applications call
  1.2446 +	// this function to determine whether a bitmap is large or not.
  1.2447 +	return ETrue;
  1.2448 +	}
  1.2449 +
  1.2450 +EXPORT_C TInt CBitwiseBitmap::HardwareBitmapHandle() const
  1.2451 +	{
  1.2452 +	if(iUid.iUid!=KCBitwiseBitmapHardwareUid.iUid)
  1.2453 +		return 0;
  1.2454 +	return iDataOffset; // Really the handle
  1.2455 +	}
  1.2456 +
  1.2457 +/**
  1.2458 +Set a flag to indicate that this bitmap has to be compressed in the FBServer background thread
  1.2459 +@return KErrNone if possible to compress, KErrAlreadyExists if already compressed
  1.2460 +*/
  1.2461 +EXPORT_C TInt CBitwiseBitmap::CheckBackgroundCompressData()
  1.2462 +	{
  1.2463 +	switch (iHeader.iBitsPerPixel)
  1.2464 +		{
  1.2465 +	case 1:
  1.2466 +	case 2:
  1.2467 +	case 4:
  1.2468 +	case 8:
  1.2469 +	case 12:
  1.2470 +	case 16:
  1.2471 +	case 24:
  1.2472 +	case 32:
  1.2473 +		break;
  1.2474 +	default:
  1.2475 +		return KErrNotSupported;
  1.2476 +		}
  1.2477 +	// Return if the bitmap is already compressed.
  1.2478 +	if (iHeader.iCompression != ENoBitmapCompression)
  1.2479 +		return KErrAlreadyExists;
  1.2480 +
  1.2481 +	// See if it's possible to allocate memory.
  1.2482 +	if (iHeap == NULL || iPile == NULL)
  1.2483 +		return KErrNoMemory;
  1.2484 +
  1.2485 +	return KErrNone;
  1.2486 +	}
  1.2487 +
  1.2488 +
  1.2489 +
  1.2490 +/**
  1.2491 +Compress a bitmap if possible.
  1.2492 +If the bitmap is already compressed, or if compression yields no decrease in size, do nothing,
  1.2493 +but return success (KErrNone).
  1.2494 +@return KErrNone if successful, otherwise a system wide error code.
  1.2495 +*/
  1.2496 +EXPORT_C TInt CBitwiseBitmap::CompressData()
  1.2497 +	{
  1.2498 +	switch (iHeader.iBitsPerPixel)
  1.2499 +		{
  1.2500 +	case 1:
  1.2501 +	case 2:
  1.2502 +	case 4:
  1.2503 +	case 8:
  1.2504 +	case 12:
  1.2505 +	case 16:
  1.2506 +	case 24:
  1.2507 +	case 32:
  1.2508 +		break;
  1.2509 +	default:
  1.2510 +		return KErrNone;
  1.2511 +		}
  1.2512 +	// Return if the bitmap is already compressed.
  1.2513 +	if (iHeader.iCompression != ENoBitmapCompression)
  1.2514 +		return KErrNone;
  1.2515 +
  1.2516 +	// Find out if compression is possible and return if not.
  1.2517 +	TUint8* data = (TUint8*)DataAddress();
  1.2518 +	TInt data_bytes = iHeader.iBitmapSize - iHeader.iStructSize;
  1.2519 +
  1.2520 +	//Update the padding bitmap data to speedup the RLE Compression
  1.2521 +	UpdatePaddingData((TUint32*)data);
  1.2522 +
  1.2523 +	TInt compressed_data_bytes = (SizeOfDataCompressed((TUint8*)data,data_bytes) + 3) / 4 * 4;
  1.2524 +
  1.2525 +	if (!data || !data_bytes)
  1.2526 +		return KErrNone;
  1.2527 +
  1.2528 +	//  	 if (compressed_data_bytes >= data_bytes)
  1.2529 +	//  It now attempts to check whether compression is worth while.  (speed vs space saving)
  1.2530 +	__ASSERT_DEBUG(KCompressionThreshold >=0 && KCompressionThreshold <= 256, ::Panic(EFbsInvalidCompressionThreshold));
  1.2531 +	if (compressed_data_bytes >= (data_bytes * KCompressionThreshold) >> 8)
  1.2532 +		return KErrNone;
  1.2533 +
  1.2534 +	// See if it's possible to allocate memory.
  1.2535 +	if (iHeap == NULL || iPile == NULL)
  1.2536 +		return KErrNoMemory;
  1.2537 +
  1.2538 +	// Create a buffer to receive the compressed data.
  1.2539 +	TUint8* compressed_data = NULL;
  1.2540 +	TInt allocSize = compressed_data_bytes;
  1.2541 +	TBool bookMark = EFalse;
  1.2542 +	if (allocSize > KCompressionBookMarkThreshold)
  1.2543 +		{
  1.2544 +		allocSize += sizeof(TCompressionBookMark) + 4;
  1.2545 +		bookMark = ETrue;
  1.2546 +		}
  1.2547 +	compressed_data = iPile->Alloc(allocSize);
  1.2548 +	if (!compressed_data)
  1.2549 +		return KErrNoMemory;
  1.2550 +	if (bookMark)
  1.2551 +		{
  1.2552 +		TCompressionBookMark emptyBookmark;
  1.2553 +		*((TCompressionBookMark*)(compressed_data + compressed_data_bytes + 4)) = emptyBookmark;
  1.2554 +		}
  1.2555 +	iDataOffset = compressed_data - iPile->ChunkBase();
  1.2556 +	iHeader.iBitmapSize = sizeof(SEpocBitmapHeader) + compressed_data_bytes;
  1.2557 +	iHeader.iCompression = CompressionType(iHeader.iBitsPerPixel, iHeader.iColor);
  1.2558 +
  1.2559 +	// Compress the data into a stream over the new buffer.
  1.2560 +	TPtr8 output_ptr(compressed_data,compressed_data_bytes);
  1.2561 +	RDesWriteStream output_stream(output_ptr);
  1.2562 +	// This function cannot leave - but trap it anyway till I am fully satisfied about that.
  1.2563 +	TRAP_IGNORE(DoExternalizeDataCompressedL(output_stream,data,data_bytes));
  1.2564 +	output_stream.Close();
  1.2565 +
  1.2566 +	iIsCompressedInRAM = ETrue;
  1.2567 +	// Free the old data.
  1.2568 +	iPile->Free(data);
  1.2569 +
  1.2570 +	return KErrNone;
  1.2571 +	}
  1.2572 +
  1.2573 +/**
  1.2574 +Compress a bitmap if possible.
  1.2575 +If the bitmap is already compressed, or if compression yields no decrease in size, do nothing,
  1.2576 +but return success (KErrNone).
  1.2577 +@publishedAll
  1.2578 +@param aScheme The type of bitmap file compression.
  1.2579 +@return KErrNone if successful, otherwise a system wide error code.
  1.2580 +*/
  1.2581 +EXPORT_C TInt CBitwiseBitmap::CompressData(TBitmapfileCompressionScheme aScheme)
  1.2582 +	{
  1.2583 +	TInt err=KErrNone;
  1.2584 +	if (aScheme==ERLECompression)
  1.2585 +		err=CompressData();
  1.2586 +	else if (aScheme==EPaletteCompression)
  1.2587 +		err=PaletteCompress();
  1.2588 +	else if (aScheme==EPaletteCompressionWithRLEFallback)
  1.2589 +		{
  1.2590 +		err=PaletteCompress();
  1.2591 +		if (err==KErrNotSupported)
  1.2592 +			err=CompressData();
  1.2593 +		}
  1.2594 +
  1.2595 +	return err;
  1.2596 +	}
  1.2597 +
  1.2598 +EXPORT_C TBool CBitwiseBitmap::IsCompressedInRAM() const
  1.2599 +	{
  1.2600 +	return iIsCompressedInRAM;
  1.2601 +	}
  1.2602 +
  1.2603 +/**
  1.2604 +Check for a bitmap if it is compressed in some manner.
  1.2605 +@return ETrue if successful Or EFalse if unsuccessful
  1.2606 +@internalComponent
  1.2607 +*/
  1.2608 +EXPORT_C TBool CBitwiseBitmap::IsCompressed() const
  1.2609 +	{
  1.2610 +	return ( iHeader.iCompression != ENoBitmapCompression );
  1.2611 +	}
  1.2612 +
  1.2613 +EXPORT_C void CBitwiseBitmap::SetCompressionBookmark(TLineScanningPosition& aLineScanningPosition, TUint32* aBase, const CFbsBitmap* /*aFbsBitmap*/)
  1.2614 +	{
  1.2615 +	if (iPile == NULL) return; //Rom bitmap
  1.2616 +	if (!iIsCompressedInRAM || (iHeader.iCompression == EGenericPaletteCompression))
  1.2617 +		{
  1.2618 +		return;
  1.2619 +		}
  1.2620 +
  1.2621 +	TInt compressed_data_bytes=iHeader.iBitmapSize-sizeof(SEpocBitmapHeader);
  1.2622 +	if (compressed_data_bytes>KCompressionBookMarkThreshold)
  1.2623 +		{
  1.2624 +		TUint8* compressed_data=(TUint8*) aBase;
  1.2625 +		TInt alignedSize=(compressed_data_bytes+3)/4*4;
  1.2626 +		compressed_data+=alignedSize+4;
  1.2627 +		TCompressionBookMark* bookMark=(TCompressionBookMark*) (compressed_data);
  1.2628 +		if (!bookMark->IsCheckSumOk())
  1.2629 +			return;
  1.2630 +		bookMark->iCursorPos=aLineScanningPosition.iCursorPos;
  1.2631 +		bookMark->iSrcDataOffset=aLineScanningPosition.iSrcDataPtr-((TUint8*)aBase);
  1.2632 +		bookMark->CalculateCheckSum();
  1.2633 +		}
  1.2634 +	}
  1.2635 +/**
  1.2636 +Optimises the bitmap data for Run Length Encoding by changing unused
  1.2637 +pixel data.
  1.2638 +This function calculates number of padding pixels per scanline and
  1.2639 +replaces their color with the color of the scanline's final pixel.
  1.2640 +*/
  1.2641 +void CBitwiseBitmap::UpdatePaddingData(TUint32* aData)
  1.2642 +	{
  1.2643 +	TInt stride=DataStride();
  1.2644 +	//Do the process only for 8bpp and 16bpp.
  1.2645 +	switch (iHeader.iBitsPerPixel)
  1.2646 +		{
  1.2647 +	case 8:
  1.2648 +		{
  1.2649 +		const TInt nPadding = stride - iHeader.iSizeInPixels.iWidth;
  1.2650 +		if(nPadding!=1 && nPadding!=2 && nPadding!=3)
  1.2651 +			return;
  1.2652 +		TUint8* srcePtr = reinterpret_cast<TUint8*>(aData);
  1.2653 +		//Find the last byte value in each scanline and assign in padding bytes
  1.2654 +		TUint8* lastPixelPtr = srcePtr + iHeader.iSizeInPixels.iWidth - 1;
  1.2655 +		for(TInt row=0; row<iHeader.iSizeInPixels.iHeight; row++)
  1.2656 +			{
  1.2657 +			TUint8 pixel = *lastPixelPtr;
  1.2658 +			TUint8* padPtr = lastPixelPtr + 1;
  1.2659 +			switch(nPadding)
  1.2660 +				{
  1.2661 +			case 3: *padPtr++ = pixel;
  1.2662 +			case 2: *padPtr++ = pixel;
  1.2663 +			case 1: *padPtr++ = pixel;
  1.2664 +				}
  1.2665 +			lastPixelPtr += stride;
  1.2666 +			}
  1.2667 +		break;
  1.2668 +		}
  1.2669 +	case 16:
  1.2670 +		{
  1.2671 +		TUint16* srcePtr = reinterpret_cast<TUint16*>(aData);
  1.2672 +		stride>>=1;
  1.2673 +		const TInt nPadding = stride - iHeader.iSizeInPixels.iWidth;
  1.2674 +		if(nPadding!=1)
  1.2675 +			return;
  1.2676 +		//Find the last byte value in each scanline and assign in padding bytes
  1.2677 +		TUint16* lastPixelPtr = srcePtr + iHeader.iSizeInPixels.iWidth - 1;
  1.2678 +		for(TInt row=0; row<iHeader.iSizeInPixels.iHeight; row++)
  1.2679 +			{
  1.2680 +			TUint16 pixel = *lastPixelPtr;
  1.2681 +			TUint16* padPtr = lastPixelPtr + 1;
  1.2682 +			*padPtr++ = pixel;
  1.2683 +			lastPixelPtr += stride;
  1.2684 +			}
  1.2685 +		break;
  1.2686 +		}
  1.2687 +	default:
  1.2688 +		return;
  1.2689 +		}
  1.2690 +	}
  1.2691 +
  1.2692 +void CBitwiseBitmap::WhiteFill(TUint8* aData,TInt aDataSize,TDisplayMode aDispMode)
  1.2693 +	{
  1.2694 +	if(aData)
  1.2695 +		{
  1.2696 +		if (aDispMode != EColor4K)
  1.2697 +			Mem::Fill(aData,aDataSize,0xff);
  1.2698 +		else
  1.2699 +			{
  1.2700 +			TUint16* pixelPtr = (TUint16*)aData;
  1.2701 +			TUint16* pixelPtrLimit = pixelPtr + (aDataSize / 2);
  1.2702 +			while (pixelPtr < pixelPtrLimit)
  1.2703 +				*pixelPtr++ = 0x0fff;
  1.2704 +			}		
  1.2705 +		}
  1.2706 +	}
  1.2707 +
  1.2708 +TInt CBitwiseBitmap::ByteWidth(TInt aPixelWidth,TDisplayMode aDispMode)
  1.2709 +	{
  1.2710 +	TInt wordWidth = 0;
  1.2711 +
  1.2712 +	switch (aDispMode)
  1.2713 +		{
  1.2714 +	case EGray2:
  1.2715 +		wordWidth = (aPixelWidth + 31) / 32;
  1.2716 +		break;
  1.2717 +	case EGray4:
  1.2718 +		wordWidth = (aPixelWidth + 15) / 16;
  1.2719 +		break;
  1.2720 +	case EGray16:
  1.2721 +	case EColor16:
  1.2722 +		wordWidth = (aPixelWidth + 7) / 8;
  1.2723 +		break;
  1.2724 +	case EGray256:
  1.2725 +	case EColor256:
  1.2726 +		wordWidth = (aPixelWidth + 3) / 4;
  1.2727 +		break;
  1.2728 +	case EColor4K:
  1.2729 +	case EColor64K:
  1.2730 +		wordWidth = (aPixelWidth + 1) / 2;
  1.2731 +		break;
  1.2732 +	case EColor16M:
  1.2733 +		wordWidth = (((aPixelWidth * 3) + 11) / 12) * 3;
  1.2734 +		break;
  1.2735 +	case EColor16MU:
  1.2736 +	case ERgb:
  1.2737 +	case EColor16MA:
  1.2738 +	case EColor16MAP:
  1.2739 +		wordWidth = aPixelWidth;
  1.2740 +		break;
  1.2741 +	default:
  1.2742 +		::Panic(EFbsBitmapInvalidMode);
  1.2743 +		}
  1.2744 +
  1.2745 +	return wordWidth * 4;
  1.2746 +	}
  1.2747 +
  1.2748 +TInt CBitwiseBitmap::Bpp(TDisplayMode aDispMode)
  1.2749 +	{
  1.2750 +	switch (aDispMode)
  1.2751 +		{
  1.2752 +	case EGray2:
  1.2753 +		return 1;
  1.2754 +	case EGray4:
  1.2755 +		return 2;
  1.2756 +	case EGray16:
  1.2757 +	case EColor16:
  1.2758 +		return 4;
  1.2759 +	case EGray256:
  1.2760 +	case EColor256:
  1.2761 +		return 8;
  1.2762 +	case EColor4K:
  1.2763 +		return 12;
  1.2764 +	case EColor64K:
  1.2765 +		return 16;
  1.2766 +	case EColor16M:
  1.2767 +		return 24;
  1.2768 +	case EColor16MU:
  1.2769 +	case EColor16MA:
  1.2770 +	case EColor16MAP:
  1.2771 +		return 32;
  1.2772 +	default:
  1.2773 +		::Panic(EFbsBitmapInvalidMode);
  1.2774 +		}
  1.2775 +
  1.2776 +	return 0;
  1.2777 +	}
  1.2778 +
  1.2779 +TInt CBitwiseBitmap::CompressedFormatInfo(TDisplayMode aDispMode, TInt& aBytesPerPack, TInt& aBytesPerCompressed)
  1.2780 +	{
  1.2781 +	switch (aDispMode)
  1.2782 +		{
  1.2783 +		case EGray2:
  1.2784 +		case EGray4:
  1.2785 +		case EGray16:
  1.2786 +		case EColor16:
  1.2787 +		case EGray256:
  1.2788 +		case EColor256:
  1.2789 +			aBytesPerPack = 1;
  1.2790 +			aBytesPerCompressed = 1;
  1.2791 +			break;
  1.2792 +		case EColor4K:
  1.2793 +		case EColor64K:
  1.2794 +			aBytesPerPack = 2;
  1.2795 +			aBytesPerCompressed = 2;
  1.2796 +			break;
  1.2797 +		case EColor16M:
  1.2798 +			aBytesPerPack = 3;
  1.2799 +			aBytesPerCompressed = 3;
  1.2800 +			break;
  1.2801 +		case EColor16MU:
  1.2802 +			aBytesPerPack = 4;
  1.2803 +			aBytesPerCompressed = 3;		 		  //when compressed, 16MU is stored as 16M 
  1.2804 +			break;
  1.2805 +		case EColor16MA:
  1.2806 +		case EColor16MAP:
  1.2807 +			aBytesPerPack = 4;
  1.2808 +			aBytesPerCompressed = 4;
  1.2809 +			break;
  1.2810 +		default:
  1.2811 +			__ASSERT_DEBUG(0, ::Panic(EFbsBitmapInvalidMode));
  1.2812 +			return KErrNotSupported;
  1.2813 +		}
  1.2814 +
  1.2815 +	return KErrNone;
  1.2816 +	}
  1.2817 +TInt CBitwiseBitmap::IsColor(TDisplayMode aDispMode)
  1.2818 +	{
  1.2819 +	switch (aDispMode)
  1.2820 +		{
  1.2821 +	case EGray2:
  1.2822 +	case EGray4:
  1.2823 +	case EGray16:
  1.2824 +	case EGray256:
  1.2825 +		return SEpocBitmapHeader::ENoColor;
  1.2826 +	case EColor16:
  1.2827 +	case EColor256:
  1.2828 +	case EColor4K:
  1.2829 +	case EColor64K:
  1.2830 +	case EColor16M:
  1.2831 +	case EColor16MU:
  1.2832 +		return SEpocBitmapHeader::EColor;
  1.2833 +	case EColor16MA:
  1.2834 +		return SEpocBitmapHeader::EColorAlpha;
  1.2835 +	case EColor16MAP:
  1.2836 +		return SEpocBitmapHeader::EColorAlphaPM;
  1.2837 +	default:
  1.2838 +		::Panic(EFbsBitmapInvalidMode);
  1.2839 +		}
  1.2840 +
  1.2841 +	return SEpocBitmapHeader::EColorUndefined;
  1.2842 +	}
  1.2843 +
  1.2844 +TDisplayMode CBitwiseBitmap::DisplayMode(TInt aBpp, TInt aColor)
  1.2845 +	{
  1.2846 +	if (aColor)
  1.2847 +		{
  1.2848 +		switch (aBpp)
  1.2849 +			{
  1.2850 +		case 4:
  1.2851 +			return EColor16;
  1.2852 +		case 8:
  1.2853 +			return EColor256;
  1.2854 +		case 12:
  1.2855 +			return EColor4K;
  1.2856 +		case 16:
  1.2857 +			return EColor64K;
  1.2858 +		case 24:
  1.2859 +			return EColor16M;
  1.2860 +		case 32:
  1.2861 +			if(aColor == SEpocBitmapHeader::EColor)
  1.2862 +				return EColor16MU;
  1.2863 +			else if(aColor == SEpocBitmapHeader::EColorAlphaPM)
  1.2864 +				return EColor16MAP;
  1.2865 +			else if(aColor == SEpocBitmapHeader::EColorAlpha)
  1.2866 +				return EColor16MA;
  1.2867 +			else
  1.2868 +				return ENone;
  1.2869 +		default:
  1.2870 +			return ENone;
  1.2871 +			}
  1.2872 +		}
  1.2873 +	else
  1.2874 +		{
  1.2875 +		switch (aBpp)
  1.2876 +			{
  1.2877 +		case 1:
  1.2878 +			return EGray2;
  1.2879 +		case 2:
  1.2880 +			return EGray4;
  1.2881 +		case 4:
  1.2882 +			return EGray16;
  1.2883 +		case 8:
  1.2884 +			return EGray256;
  1.2885 +		default:
  1.2886 +			return ENone;
  1.2887 +			}
  1.2888 +		}
  1.2889 +	}
  1.2890 +
  1.2891 +TBitmapfileCompression CBitwiseBitmap::CompressionType(TInt aBpp, TInt aColor)
  1.2892 +	{
  1.2893 +	switch (aBpp)
  1.2894 +		{
  1.2895 +	case 1:
  1.2896 +	case 2:
  1.2897 +	case 4:
  1.2898 +	case 8:
  1.2899 +		return EByteRLECompression;
  1.2900 +	case 12:
  1.2901 +		return ETwelveBitRLECompression;
  1.2902 +	case 16:
  1.2903 +		return ESixteenBitRLECompression;
  1.2904 +	case 24:
  1.2905 +		return ETwentyFourBitRLECompression;
  1.2906 +	case 32:
  1.2907 +		__ASSERT_DEBUG((aColor==SEpocBitmapHeader::EColor) ||
  1.2908 +				(aColor==SEpocBitmapHeader::EColorAlpha) ||
  1.2909 +				aColor==SEpocBitmapHeader::EColorAlphaPM,
  1.2910 +				::Panic(EFbsBitmapInvalidCompression));
  1.2911 +		if(aColor == SEpocBitmapHeader::EColor)
  1.2912 +			{
  1.2913 +			return EThirtyTwoUBitRLECompression;
  1.2914 +			}
  1.2915 +		else
  1.2916 +			{
  1.2917 +			return EThirtyTwoABitRLECompression;
  1.2918 +			}
  1.2919 +	default:
  1.2920 +		return ENoBitmapCompression;
  1.2921 +		}
  1.2922 +	}
  1.2923 +
  1.2924 +/**
  1.2925 +@internalComponent
  1.2926 +@return The display mode used to create the bitmap.
  1.2927 +*/
  1.2928 +TDisplayMode CBitwiseBitmap::InitialDisplayMode() const
  1.2929 +	{
  1.2930 +	return iSettings.InitialDisplayMode();
  1.2931 +	}
  1.2932 +
  1.2933 +/**
  1.2934 +The method changes current display mode of the bitmap.
  1.2935 +Requested display mode can't be greater (bpp value) than the initial display mode.
  1.2936 +CBitwiseBitmap instances are shared between the client and server side and
  1.2937 +SetDisplayMode() can be called only from the client side, because its functionality depends
  1.2938 +on the RFbsSession instance.
  1.2939 +The method can't leave because of out of memory condition or something else - no
  1.2940 +additional memory is allocated or "L" methods called.
  1.2941 +The bitmap content is preserved when converting it to the requested display mode,
  1.2942 +but there may be some loss of a quality.
  1.2943 +@internalComponent
  1.2944 +@param aDisplayMode Requested display mode.
  1.2945 +@param aDataAddress Bitmap data address.
  1.2946 +@return KErrArgument If the requested mode is invalid, or greater (bpp value) than the
  1.2947 +initial display mode.
  1.2948 +@return KErrNotSupported  If the bitmap is compressed, or it is a ROM bitmap,
  1.2949 +an extended bitmap or a hardware bitmap.
  1.2950 +@return KErrNone If the method call is successfull.
  1.2951 +*/
  1.2952 +TInt CBitwiseBitmap::SetDisplayMode(TDisplayMode aDisplayMode, TUint32* aDataAddress)
  1.2953 +	{
  1.2954 +	TDisplayMode curDisplayMode = iSettings.CurrentDisplayMode();
  1.2955 +	//If requested mode is the same as current mode - do nothing.
  1.2956 +	if(curDisplayMode == aDisplayMode)
  1.2957 +		{
  1.2958 +		return KErrNone;
  1.2959 +		}
  1.2960 +	//Argument and bitmap state check.
  1.2961 +	TInt err = DisplayModeArgCheck(aDisplayMode, aDataAddress);
  1.2962 +	if(err != KErrNone)
  1.2963 +		{
  1.2964 +		return err;
  1.2965 +		}
  1.2966 +	//data pointers and scan line width calculation.
  1.2967 +	TInt scanLineWidthNew = CBitwiseBitmap::ByteWidth(iHeader.iSizeInPixels.iWidth, aDisplayMode);
  1.2968 +	TInt scanLineWidthInitial = CBitwiseBitmap::ByteWidth(iHeader.iSizeInPixels.iWidth, iSettings.InitialDisplayMode());
  1.2969 +	TInt bmpSizeInitial = scanLineWidthInitial * iHeader.iSizeInPixels.iHeight;
  1.2970 +	TUint8* baseAddr = reinterpret_cast <TUint8*> (aDataAddress);
  1.2971 +	TUint8* dataAddrNew = baseAddr;
  1.2972 +	TInt yStart = 0;
  1.2973 +	TInt yInc = 1;
  1.2974 +	TInt yEnd = iHeader.iSizeInPixels.iHeight;
  1.2975 +	//If requested display mode has more bits per pixel than current display mode - we have
  1.2976 +	//to start copying operation from the end of the bitmap, otherwise we will overwrite the
  1.2977 +	//bitmap data.
  1.2978 +	if(aDisplayMode > curDisplayMode)
  1.2979 +		{
  1.2980 +		dataAddrNew += bmpSizeInitial - scanLineWidthNew;
  1.2981 +		scanLineWidthNew = -scanLineWidthNew;
  1.2982 +		yStart = yEnd - 1;
  1.2983 +		yInc = -1;
  1.2984 +		yEnd = -1;
  1.2985 +		}
  1.2986 +	//Change the display mode
  1.2987 +	ChangeDisplayMode(aDisplayMode, scanLineWidthNew, dataAddrNew, aDataAddress, yStart, yInc, yEnd);
  1.2988 +	//Move the data to the aDataAddress.
  1.2989 +	if(aDisplayMode > curDisplayMode)
  1.2990 +		{
  1.2991 +		TInt bmpSizeNew = -scanLineWidthNew * iHeader.iSizeInPixels.iHeight;
  1.2992 +		Mem::Move(baseAddr, baseAddr + bmpSizeInitial - bmpSizeNew, bmpSizeNew);
  1.2993 +		}
  1.2994 +	//Update the bitmap properties
  1.2995 +	UpdateBitmapProperties(aDisplayMode);
  1.2996 +	return KErrNone;
  1.2997 +	}
  1.2998 +
  1.2999 +/**
  1.3000 +The method is caled from CBitwiseBitmap::SetDisplayMode() and
  1.3001 +checks the aDisplayMode argument and bitmap internal state.
  1.3002 +Requested display mode can't be greater (bpp value) than the initial display mode.
  1.3003 +Note: The method must be called only from CBitwiseBitmap::SetDisplayMode method.
  1.3004 +@internalComponent
  1.3005 +@param aDisplayMode Requested display mode.
  1.3006 +@param aDataAddress Bitmap data address.
  1.3007 +@return KErrArgument If the requested mode is invalid, or greater (bpp value)
  1.3008 +than the initial mode.
  1.3009 +@return KErrNotSupported If the bitmap is compressed, or it is a ROM bitmap,
  1.3010 +an extended bitmap or a hardware bitmap.
  1.3011 +@return KErrNone If the method call is successfull.
  1.3012 +@see CBitwiseBitmap::SetDisplayMode.
  1.3013 +*/
  1.3014 +TInt CBitwiseBitmap::DisplayModeArgCheck(TDisplayMode aDisplayMode, TUint32* aDataAddress) const
  1.3015 +	{
  1.3016 +	if(!aDataAddress || iHeader.iSizeInPixels.iWidth == 0 || iHeader.iSizeInPixels.iHeight == 0)
  1.3017 +		{
  1.3018 +		return KErrGeneral;
  1.3019 +		}
  1.3020 +	TBool romAddr = EFalse;
  1.3021 +	User::IsRomAddress(romAddr, aDataAddress);
  1.3022 +	if(romAddr ||									//ROM bitmap
  1.3023 +	   (iUid.iUid != KCBitwiseBitmapUid.iUid) ||	//RHardwareBitmap or extended bitmap
  1.3024 +	   IsCompressed()								//Compressed
  1.3025 +	  )
  1.3026 +		{
  1.3027 +		return KErrNotSupported;
  1.3028 +		}
  1.3029 +	if(aDisplayMode == ENone || aDisplayMode == ERgb)
  1.3030 +		{
  1.3031 +		return KErrArgument;
  1.3032 +		}
  1.3033 +	if (iSettings.InitialDisplayMode()==EColor16 && aDisplayMode==EGray256)
  1.3034 +		{
  1.3035 +		return KErrArgument;
  1.3036 +		}
  1.3037 +	// The order of the display mode in the TDisplayMode
  1.3038 +	// ENone,EGray2,EGray4,EGray16,EGray256,EColor16,EColor256,EColor64K,EColor16M,ERgb,EColor4K,EColor16MU
  1.3039 +
  1.3040 +	//special case where initial mode is EColor4K & to be set to EColor64K & EColor16M which has lower enum
  1.3041 +	if (iSettings.InitialDisplayMode()==EColor4K )
  1.3042 +		{
  1.3043 +		if (aDisplayMode==EColor64K || aDisplayMode==EColor16M)
  1.3044 +			return KErrArgument;
  1.3045 +		}
  1.3046 +
  1.3047 +	if(aDisplayMode == EColor16MAP)
  1.3048 +		{
  1.3049 +		TDisplayMode mode = iSettings.InitialDisplayMode();
  1.3050 +		if((mode == EColor16MA)||(mode == EColor16MU)||(mode == EColor16MAP))
  1.3051 +			{
  1.3052 +			return KErrNone;
  1.3053 +			}
  1.3054 +		else{
  1.3055 +			return KErrArgument;
  1.3056 +			}
  1.3057 +		}
  1.3058 +	if(iSettings.InitialDisplayMode() == EColor16MAP)
  1.3059 +		{
  1.3060 +		return KErrNone;
  1.3061 +		}
  1.3062 +	if(aDisplayMode > iSettings.InitialDisplayMode())
  1.3063 +		{
  1.3064 +		if (iSettings.InitialDisplayMode()>=EColor64K && aDisplayMode == EColor4K)
  1.3065 +			{
  1.3066 +			return KErrNone;
  1.3067 +			}
  1.3068 +	        if (iSettings.InitialDisplayMode()==EColor16MU &&
  1.3069 +	        	(Bpp(aDisplayMode) == 32))
  1.3070 +			{
  1.3071 +			return KErrNone;
  1.3072 +			}
  1.3073 +		return KErrArgument;
  1.3074 +		}
  1.3075 +
  1.3076 +	return KErrNone;
  1.3077 +	}
  1.3078 +
  1.3079 +/**
  1.3080 +The method changes current display mode of the bitmap converting bitmap scan lines
  1.3081 +color and writting the resulting scan line to the same memory occupied by the bitmap.
  1.3082 +No additional memory is allocated.
  1.3083 +Note: The method must be called only from CBitwiseBitmap::SetDisplayMode method.
  1.3084 +@internalComponent
  1.3085 +@param aNewDisplayMode Requested display mode.
  1.3086 +@param aScanLineWidthNew Scan line width - with the new display mode. It could be negative
  1.3087 +if the new display mode is with less bits per pixel than the existing display mode.
  1.3088 +@param aDataAddrNew New bitmap data - Points to the place where the copying has to start to.
  1.3089 +@param aDataAddress Bitmap data address.
  1.3090 +@param aYStart First scan line number.
  1.3091 +@param aYInc Scan line increment value.
  1.3092 +@param aYEnd Last scan line number.
  1.3093 +@see CBitwiseBitmap::SetDisplayMode.
  1.3094 +*/
  1.3095 +void CBitwiseBitmap::ChangeDisplayMode( TDisplayMode aNewDisplayMode,
  1.3096 +										TInt aScanLineWidthNew,
  1.3097 +										TUint8* aDataAddrNew,
  1.3098 +										TUint32* aDataAddress,
  1.3099 +										TInt aYStart,
  1.3100 +										TInt aYInc,
  1.3101 +										TInt aYEnd)
  1.3102 +	{
  1.3103 +	const TInt KScanLineBufSizeInPixels = 256;//temporary scan line buffer size - in pixels
  1.3104 +	const TInt KRgbBytes = 4;
  1.3105 +	const TInt KScanLineBufSizeInBytes = KScanLineBufSizeInPixels * KRgbBytes;
  1.3106 +	TUint8 scanLineData[KScanLineBufSizeInBytes];
  1.3107 +	static const TInt KScanLinePixels[] =  //The rounded number of pixels - the closest number that
  1.3108 +		{							//could fill scanLineData buffer - depending on current display mode
  1.3109 +		0,												//ENone - INVALID mode
  1.3110 +		KScanLineBufSizeInBytes * 8,					//EGray2
  1.3111 +		KScanLineBufSizeInBytes * 4,					//EGray4
  1.3112 +		KScanLineBufSizeInBytes * 2,					//EGray16
  1.3113 +		KScanLineBufSizeInBytes * 1,					//EGray256
  1.3114 +		KScanLineBufSizeInBytes * 2,					//EColor16
  1.3115 +		KScanLineBufSizeInBytes * 1,					//EColor256
  1.3116 +		KScanLineBufSizeInPixels * KRgbBytes / 2,		//EColor64K
  1.3117 +		KScanLineBufSizeInPixels,						//EColor16M
  1.3118 +		0,												//ERgb - INVALID mode
  1.3119 +		KScanLineBufSizeInPixels * KRgbBytes / 2,		//EColor4K - the same as EColor64K
  1.3120 +		KScanLineBufSizeInPixels * KRgbBytes / 4,		//EColor16MU
  1.3121 +		KScanLineBufSizeInPixels * KRgbBytes / 4,		//EColor16MA
  1.3122 +		KScanLineBufSizeInPixels * KRgbBytes / 4		//EColor16MAP
  1.3123 +		};
  1.3124 +	__ASSERT_DEBUG(aNewDisplayMode < TInt(sizeof(KScanLinePixels) / sizeof(KScanLinePixels[0])), ::Panic(EFbsBitmapInvalidMode3));
  1.3125 +	//
  1.3126 +	TPtr8 ptr(scanLineData, sizeof(scanLineData), sizeof(scanLineData));
  1.3127 +	TDes8& scanLineDes = ptr;
  1.3128 +	TLineScanningPosition lineScanningPosition(aDataAddress);
  1.3129 +	TPoint startPixel(0, 0);
  1.3130 +	TPoint ditherOffset(0, 0);
  1.3131 +	//For each line:
  1.3132 +	//1. Get a scan line in requested display mode
  1.3133 +	//2. Copy the scan line to the destination buffer, pointed by dataAddrNew
  1.3134 +	TUint8* dataAddrNew = aDataAddrNew;
  1.3135 +	for(TInt i=aYStart;i!=aYEnd;i+=aYInc)
  1.3136 +		{
  1.3137 +		startPixel.iX = 0;
  1.3138 +		startPixel.iY = i;
  1.3139 +		TUint8* scanLineDataAddr = dataAddrNew;
  1.3140 +		TInt scanLinePixelsLeft = iHeader.iSizeInPixels.iWidth;
  1.3141 +		while(scanLinePixelsLeft > 0)
  1.3142 +			{
  1.3143 +			TInt pixelsCnt = KScanLinePixels[aNewDisplayMode];	//how many pixels we can get at a time - the maximum
  1.3144 +			TInt bytesCnt = KScanLineBufSizeInBytes;			//how many bytes the pixels are - the maximum
  1.3145 +			if(pixelsCnt > scanLinePixelsLeft)	//in that case the "while" loop will be executed for the last time
  1.3146 +				{
  1.3147 +				pixelsCnt = scanLinePixelsLeft;
  1.3148 +				bytesCnt = CBitwiseBitmap::ByteWidth(pixelsCnt, aNewDisplayMode);
  1.3149 +				}
  1.3150 +			__ASSERT_DEBUG(pixelsCnt > 0, ::Panic(EFbsBitmapInvalidMode2));//I want to be sure - if someone adds an additional display mode - ChangeDisplayMode() source has been updated too!
  1.3151 +			GetScanLine(scanLineDes, startPixel, pixelsCnt, EFalse,	  //Get the scan line data
  1.3152 +						ditherOffset, aNewDisplayMode, aDataAddress, lineScanningPosition);
  1.3153 +			Mem::Copy(scanLineDataAddr, scanLineData, bytesCnt);//copy the data to its new address
  1.3154 +			scanLineDataAddr += bytesCnt;//increment the address
  1.3155 +			scanLinePixelsLeft -= pixelsCnt;//decrement the count of pixels left
  1.3156 +			startPixel.iX += pixelsCnt;//increment the X coordinate
  1.3157 +			}//end of - while(scanLineLengthLeft > 0)
  1.3158 +		dataAddrNew += aScanLineWidthNew;
  1.3159 +		}//end of - for(TInt i=aYStart;i!=aYEnd;i+=aYInc)
  1.3160 +	}
  1.3161 +
  1.3162 +/**
  1.3163 +The method updates CBitwiseBitmap data members regarding to the new display mode.
  1.3164 +Note: The method must be called only from CBitwiseBitmap::SetDisplayMode method.
  1.3165 +@internalComponent
  1.3166 +@param aNewDisplayMode The new display mode.
  1.3167 +@see CBitwiseBitmap::SetDisplayMode.
  1.3168 +*/
  1.3169 +void CBitwiseBitmap::UpdateBitmapProperties(TDisplayMode aNewDisplayMode)
  1.3170 +	{
  1.3171 +	iSettings.SetCurrentDisplayMode(aNewDisplayMode);
  1.3172 +	iByteWidth = CBitwiseBitmap::ByteWidth(iHeader.iSizeInPixels.iWidth, aNewDisplayMode);
  1.3173 +	iHeader.iBitsPerPixel = CBitwiseBitmap::Bpp(aNewDisplayMode);
  1.3174 +	iHeader.iColor = CBitwiseBitmap::IsColor(aNewDisplayMode);
  1.3175 +	}
  1.3176 +
  1.3177 +/**
  1.3178 +The method swaps the bitmap width and height.
  1.3179 +For example: if the bitmap size is (40, 20), the new bitmap size will be (20, 40).
  1.3180 +Bitmap content is not preserved.
  1.3181 +@internalComponent
  1.3182 +@param aDataAddress Bitmap data address.
  1.3183 +@return KErrNone The call was successfull.
  1.3184 +@return KErrAccessDenied ROM bitmap size can't be swapped.
  1.3185 +@return KErrNotSupported Hardware or extended bitmap size can't be swapped.
  1.3186 +*/
  1.3187 +TInt CBitwiseBitmap::SwapWidthAndHeight(TUint32* aDataAddress)
  1.3188 +	{
  1.3189 +	if (iUid.iUid != KCBitwiseBitmapUid.iUid)	// RHardwareBitmap or extended bitmap
  1.3190 +		{
  1.3191 +		return KErrNotSupported;
  1.3192 +		}
  1.3193 +
  1.3194 +	TBool romAddr = EFalse;
  1.3195 +	User::IsRomAddress(romAddr, aDataAddress);
  1.3196 +	if (romAddr)								//ROM bitmap
  1.3197 +		{
  1.3198 +		return KErrAccessDenied;
  1.3199 +		}
  1.3200 +
  1.3201 +    //Check the new bitmap size - it should not exeed the size of the allocated memory
  1.3202 +    TInt newWidthInBytes = CBitwiseBitmap::ByteWidth(iHeader.iSizeInPixels.iHeight, iSettings.CurrentDisplayMode());
  1.3203 +	TInt64 hugeDataSize = TInt64(iHeader.iSizeInPixels.iWidth) * TInt64(newWidthInBytes);
  1.3204 +    __ASSERT_ALWAYS(I64HIGH(hugeDataSize) == 0 &&
  1.3205 +                    I64LOW(hugeDataSize) <= TUint(iHeader.iBitmapSize - iHeader.iStructSize),
  1.3206 +                    ::Panic(EFbsBitmapSwappingImpossible));
  1.3207 +
  1.3208 +	//Initialize the data members with the new values
  1.3209 +	iByteWidth = CBitwiseBitmap::ByteWidth(iHeader.iSizeInPixels.iHeight, iSettings.CurrentDisplayMode());
  1.3210 +	TInt temp = iHeader.iSizeInPixels.iWidth;
  1.3211 +	iHeader.iSizeInPixels.iWidth = iHeader.iSizeInPixels.iHeight;
  1.3212 +	iHeader.iSizeInPixels.iHeight = temp;
  1.3213 +	temp = iHeader.iSizeInTwips.iWidth;
  1.3214 +	iHeader.iSizeInTwips.iWidth = iHeader.iSizeInTwips.iHeight;
  1.3215 +	iHeader.iSizeInTwips.iHeight = temp;
  1.3216 +	return KErrNone;
  1.3217 +	}
  1.3218 +
  1.3219 +/**
  1.3220 +Compile time check is performed on the class size - the class size must be
  1.3221 +the same as the size of TDisplayMode type. If the class size is not the same
  1.3222 +as  TDisplayMode type size - BC will be broken.
  1.3223 +Note: CBitwiseBitmap::iSettings data member must be aligned on 16 bits boundary
  1.3224 +because CBitwiseBitmap instances can be a part of the ROM image.
  1.3225 +@internalComponent
  1.3226 +@param aDisplayMode The display mode.
  1.3227 +*/
  1.3228 +CBitwiseBitmap::TSettings::TSettings(TDisplayMode aDisplayMode):
  1.3229 +	iData(0)
  1.3230 +	{
  1.3231 +	//"CBitwiseBitmap::iSettings" data mamber - bit format:
  1.3232 +	// MSB                                              LSB
  1.3233 +	// 16         			:           8             :            8
  1.3234 +	// Flags                  Initial display mode      Current display mode
  1.3235 +	//
  1.3236 +	//CBitwiseBitmap - TSettings member has been put in place of previous iDispMode
  1.3237 +	//class member. So, TSettings data member has to occupy the same space as
  1.3238 +	//not existing anymore iDispMode member.
  1.3239 +	COMPILE_TIME_ASSERT(sizeof(TSettings) == sizeof(TDisplayMode));
  1.3240 +	//We can't have TDisplayMode enum value greater than 255 because we encode it
  1.3241 +	//int 8 bits of iData data member.
  1.3242 +	COMPILE_TIME_ASSERT(EColorLast < 256);
  1.3243 +	SetDisplayModes(aDisplayMode);
  1.3244 +	}
  1.3245 +
  1.3246 +/**
  1.3247 +The method initializes both - current display mode and initial display mode parts of iData
  1.3248 +with aDisplayMode parameter.
  1.3249 +@internalComponent
  1.3250 +@param aDisplayMode The display mode used for current display mode and initial display mode
  1.3251 +parts of iData initialization
  1.3252 +*/
  1.3253 +void CBitwiseBitmap::TSettings::SetDisplayModes(TDisplayMode aDisplayMode)
  1.3254 +	{
  1.3255 +	iData &= 0xFFFF0000;
  1.3256 +	iData |= TUint16(aDisplayMode << 8);
  1.3257 +	iData |= TUint8(aDisplayMode);
  1.3258 +	}
  1.3259 +
  1.3260 +/**
  1.3261 +The method initializes current display mode part of iData with aDisplayMode parameter.
  1.3262 +@internalComponent
  1.3263 +@param aDisplayMode The display mode used for current display mode part of iData initialization.
  1.3264 +*/
  1.3265 +void CBitwiseBitmap::TSettings::SetCurrentDisplayMode(TDisplayMode aDisplayMode)
  1.3266 +	{
  1.3267 +	iData &= 0xFFFFFF00;
  1.3268 +	iData |= TUint8(aDisplayMode);
  1.3269 +	}
  1.3270 +
  1.3271 +/**
  1.3272 +The method returns current display mode.
  1.3273 +@internalComponent
  1.3274 +Note: Current display mode can never be greater (bpp value) than initial display mode.
  1.3275 +@return Current display mode.
  1.3276 +*/
  1.3277 +TDisplayMode CBitwiseBitmap::TSettings::CurrentDisplayMode() const
  1.3278 +	{
  1.3279 +	return TDisplayMode(iData & 0x000000FF);
  1.3280 +	}
  1.3281 +
  1.3282 +/**
  1.3283 +The method returns initial display mode.
  1.3284 +@internalComponent
  1.3285 +@return The initial display mode.
  1.3286 +*/
  1.3287 +TDisplayMode CBitwiseBitmap::TSettings::InitialDisplayMode() const
  1.3288 +	{
  1.3289 +	return TDisplayMode((iData & 0x0000FF00) >> 8);
  1.3290 +	}
  1.3291 +
  1.3292 +/**
  1.3293 +The method adjusts specified X coordinate if it is negative or outside the bitmap.
  1.3294 +@internalComponent
  1.3295 +@param  aX - a reference to x coordinate - the value might be changed after the method call.
  1.3296 +*/
  1.3297 +void CBitwiseBitmap::AdjustXCoord(TInt& aX) const
  1.3298 +	{
  1.3299 +	if (aX>=iHeader.iSizeInPixels.iWidth || aX<-iHeader.iSizeInPixels.iWidth)
  1.3300 +		aX%=iHeader.iSizeInPixels.iWidth;
  1.3301 +	if (aX<0)
  1.3302 +		aX+=iHeader.iSizeInPixels.iWidth;
  1.3303 +	}
  1.3304 +
  1.3305 +/**
  1.3306 +If the bitmap is compressed in RAM, the method will find its compresssion bookmark,
  1.3307 +which is located at the end of the bitmap data and will reinitialize aLineScanPos
  1.3308 +parameter.
  1.3309 +@internalComponent
  1.3310 +@param aLineScanPos Line scaning position. It is used by scan line decompression methods.
  1.3311 +@param aComprBookMark If the bitmap is compressed in RAM, aComprBookMark will be initialized
  1.3312 +to point to its compression bookmark data. The compression bookmark data will be used for
  1.3313 +aLineScanPos initialization.
  1.3314 +@param  aBase It points to the beginning of the bitmap data.
  1.3315 +*/
  1.3316 +void CBitwiseBitmap::GetLineScanPos(TLineScanningPosition& aLineScanPos,
  1.3317 +									const TCompressionBookMark*& aComprBookMark,
  1.3318 +									const TUint8* aBase) const
  1.3319 +	{
  1.3320 +	if (iIsCompressedInRAM && (iHeader.iCompression != EGenericPaletteCompression) && (iHeap != NULL))
  1.3321 +		{
  1.3322 +		TInt compressed_data_bytes = iHeader.iBitmapSize - sizeof(SEpocBitmapHeader);
  1.3323 +		if(compressed_data_bytes > KCompressionBookMarkThreshold)
  1.3324 +			{
  1.3325 +			if(aBase)
  1.3326 +				{
  1.3327 +				TInt alignedSize = (compressed_data_bytes + 3) / 4 * 4;
  1.3328 +				const TUint8* data = aBase + alignedSize + 4;
  1.3329 +				aComprBookMark = reinterpret_cast <const TCompressionBookMark*> (data);
  1.3330 +				if (aComprBookMark->IsCheckSumOk())
  1.3331 +					{
  1.3332 +					aLineScanPos.iSrcDataPtr = const_cast <TUint8*> (aBase) + aComprBookMark->iSrcDataOffset;
  1.3333 +					aLineScanPos.iCursorPos = aComprBookMark->iCursorPos;
  1.3334 +					}
  1.3335 +				}
  1.3336 +			}
  1.3337 +		}
  1.3338 +	}
  1.3339 +
  1.3340 +/**
  1.3341 +If the bitmap is compressed in RAM, the method will update its compresssion bookmark data,
  1.3342 +which is located at the end of the bitmap data.
  1.3343 +@internalComponent
  1.3344 +@param aLineScanPos Line scaning position.
  1.3345 +@param aComprBookMark If the bitmap is compressed in RAM, aComprBookMark points to its
  1.3346 +compression bookmark data.
  1.3347 +@param  aBase It points to the beginning of the bitmap data.
  1.3348 +*/
  1.3349 +void CBitwiseBitmap::UpdateBookMark(const TLineScanningPosition& aLineScanPos,
  1.3350 +									TCompressionBookMark* aComprBookMark,
  1.3351 +									const TUint8* aBase) const
  1.3352 +	{
  1.3353 +	if (aComprBookMark)
  1.3354 +		{
  1.3355 +		if (aComprBookMark->IsCheckSumOk())
  1.3356 +			{
  1.3357 +			aComprBookMark->iSrcDataOffset=aLineScanPos.iSrcDataPtr-aBase;
  1.3358 +			aComprBookMark->iCursorPos=aLineScanPos.iCursorPos;
  1.3359 +			aComprBookMark->CalculateCheckSum();
  1.3360 +			}
  1.3361 +		}
  1.3362 +	}
  1.3363 +
  1.3364 +/**
  1.3365 +The header is exposed by CFbsBitmap so this doesn't break encapsulation.
  1.3366 +Specifically added to allow CBitmapObject to see compression information.
  1.3367 +@return Address of iHeader.
  1.3368 +*/
  1.3369 +EXPORT_C SEpocBitmapHeader CBitwiseBitmap::Header() const
  1.3370 +	{
  1.3371 +	return iHeader ;
  1.3372 +	}