1 // Copyright (c) 1995-2009 Nokia Corporation and/or its subsidiary(-ies).
2 // All rights reserved.
3 // This component and the accompanying materials are made available
4 // under the terms of the License "Symbian Foundation License v1.0" to Symbian Foundation members and "Symbian Foundation End User License Agreement v1.0" to non-members
5 // which accompanies this distribution, and is available
6 // at the URL "http://www.symbianfoundation.org/legal/licencesv10.html".
8 // Initial Contributors:
9 // Nokia Corporation - initial contribution.
16 #if !defined(__BITMAP_H__)
18 #if !defined(__E32STD_H__)
21 #if !defined(__F32FILE_H__)
24 #if !defined(__GDI_H__)
29 #define SYMBIAN_DEBUG_FBS_LOCKHEAP
35 const TUid KCBitwiseBitmapUid={268435520};
36 const TUid KCBitwiseBitmapHardwareUid={0x10009a3d};
37 const TUid KMultiBitmapFileImageUid={268435522};
38 const TInt KCompressionBookMarkThreshold=0x2000;
39 const TInt KNumBytesPerBitmapHandle = sizeof(TInt); // Bitmap handle is the size of a TInt
40 const TInt KMaxBitmapHandleBufferSize = KNumBytesPerBitmapHandle * 2000; // Maximum size of buffer to store all bitmap handles.
42 //Forward declarations
44 class TCompressionBookMark;
47 Defines the types of file compression.
51 enum TBitmapfileCompression
53 /** Bitmap file is not compressed. */
55 /** File is compressed using run-length encoding compression. */
57 /** File is compressed using twelve bit run-length encoding compression. */
58 ETwelveBitRLECompression,
59 /** File is compressed using sixteen bit run-length encoding compression. */
60 ESixteenBitRLECompression,
61 /** File is compressed using twenty four bit run-length encoding compression. */
62 ETwentyFourBitRLECompression,
63 /** File is compressed using twenty four bit run-length encoding compression from 32-bit bitmap buffer. */
64 EThirtyTwoUBitRLECompression,
65 /** File is compressed using thirty two bit run-length encoding compression from 32-bit bitmap buffer. */
66 EThirtyTwoABitRLECompression,
67 /** File is compressed as a palette plus reduced bit-per-pixel. Only applicable to bitmaps already loaded in RAM. */
68 EGenericPaletteCompression,
69 //Insert new enum items here!
70 ERLECompressionLast = 255
74 Defines the available file compression schemes.
78 enum TBitmapfileCompressionScheme
80 /** File is compressed using run-length encoding compression. */
82 /** File is compressed as a palette plus reduced bit-per-pixel. */
84 /** File is compressed as a palette plus reduced bit-per-pixel, with automatic fallback to RLE if bitmap is not suitable*/
85 EPaletteCompressionWithRLEFallback
91 typedef void (*TAssignFunction)(TUint8*& aDestPtr, TUint32 aColor);
96 typedef void (*TDecodeFunction)(TUint8* aDataPtr, TUint32* aPalettePtr, TUint8*& aDestPtr, TAssignFunction aAssignFunction);
110 Contains information about the bitmap when streaming bitmaps to stores.
114 class SEpocBitmapHeader
117 inline SEpocBitmapHeader() :
118 iBitmapSize(sizeof(SEpocBitmapHeader)),
119 iStructSize(sizeof(SEpocBitmapHeader)),
125 iCompression(ENoBitmapCompression)
129 /** Defines whether bitmap is colour and if it comprises an alpha channel. */
132 /** Bitmap doesn't comprise color */
134 /** Bitmap comprises color */
136 /** Bitmap comprises color with alpha channel */
138 /** Bitmap comprises pre-multiplied colors with alpha channel */
140 /** Unknown format */
144 /** The size of the bitmap data, in bytes. */
146 /** The size of the structure in which the bitmap data is stored. */
148 /** The size of the bitmap, in pixels. */
150 /** The size of the bitmap, in twips. */
152 /** The bitmap's number of bits per pixel */
154 /** Whether or not the bitmap is colour and comprises an alpha value. */
156 /** The number of entries in the bitmap's palette. */
157 TInt iPaletteEntries;
158 /** The type of compression used to store the bitmap. */
159 TBitmapfileCompression iCompression;
162 class TLineScanningPosition
168 TLineScanningPosition(TUint32* aSrcDataPtr): iSrcDataPtr((TUint8*)aSrcDataPtr), iCursorPos(0), iScanLineBuffer(NULL) {}
172 HBufC8* iScanLineBuffer;
175 class CShiftedFileStore;
177 BitGdi bitmap class (pseudo-CXxx class).
184 friend class CFbClient;
185 friend class CFbsBitmap;
186 friend class CBitmapObject;
187 friend class CFbsBitmapAsyncStreamer;
188 friend class CFbsBackgroundCompression;
189 friend class CleanupDelete<CBitwiseBitmap>;
191 IMPORT_C TUid Uid() const;
192 IMPORT_C void ExternalizeL(RWriteStream& aStream,const CFbsBitmap& aHandleBitmap) const;
193 IMPORT_C void ExternalizeRectangleL(RWriteStream& aStream,const TRect& aRect,const CFbsBitmap& aHandleBitmap) const;
194 IMPORT_C void InternalizeL(RReadStream& aStream);
195 IMPORT_C static void InternalizeHeaderL(RReadStream& aStream,SEpocBitmapHeader& aHeader);
196 IMPORT_C TSize SizeInPixels() const;
197 IMPORT_C TSize SizeInTwips() const;
198 IMPORT_C TDisplayMode DisplayMode() const;
199 IMPORT_C TInt HorizontalTwipsToPixels(TInt aTwips) const;
200 IMPORT_C TInt VerticalTwipsToPixels(TInt aTwips) const;
201 IMPORT_C TInt HorizontalPixelsToTwips(TInt aPixels) const;
202 IMPORT_C TInt VerticalPixelsToTwips(TInt aPixels) const;
203 IMPORT_C void GetPixel(TRgb& aColor,const TPoint& aPos,TUint32* aBase) const;
204 IMPORT_C TInt GetScanLinePtr(TUint32*& aSlptr, TPoint& aPixel,TInt aLength, TUint32* aBase, TLineScanningPosition& aLineScanningPosition) const;
205 IMPORT_C TInt GetScanLinePtr(TUint32*& aSlptr, TInt& aLength, TPoint& aPixel,TUint32* aBase, TLineScanningPosition& aLineScanningPosition) const;
206 IMPORT_C void GetScanLine(TDes8& aBuf,const TPoint& aPixel,TInt aLength,TBool aDither,const TPoint& aDitherOffset,TDisplayMode aDispMode,TUint32* aBase, TLineScanningPosition& aLineScanningPosition) const;
207 IMPORT_C void GetScanLine(TUint32*& aSlptr, TDes8& aBuf,const TPoint& aPixel,TInt aLength,TBool aDither,const TPoint& aDitherOffset,TDisplayMode aDispMode) const;
208 IMPORT_C void GetScanLine(TDes8& aBuf,const TPoint& aPixel,TInt aLength,TBool aDither,const TPoint& aDitherOffset,TDisplayMode aDispMode,TUint32* aBase) const;
209 IMPORT_C void GetVerticalScanLine(TDes8& aBuf,TInt aX,TBool aDither,const TPoint& aDitherOffset,TDisplayMode aDispMode,TUint32* aBase) const;
210 IMPORT_C void StretchScanLine(TDes8& aBuf,const TPoint& aPixel,TInt aClipStrchX,TInt aClipStrchLen,TInt aStretchLength,TInt aOrgX,TInt aOrgLen,const TPoint& aDitherOffset,TDisplayMode aDispMode,TUint32* aBase) const;
211 IMPORT_C void 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;
212 IMPORT_C TUint32* ScanLineAddress(TUint32* aBase,TUint aY) const;
213 IMPORT_C TBool IsMonochrome(TUint32* aBase) const;
214 IMPORT_C TBool IsLargeBitmap() const;
215 IMPORT_C TInt CompressData();
216 IMPORT_C TInt CompressData(TBitmapfileCompressionScheme aScheme);
217 IMPORT_C TInt CheckBackgroundCompressData();
218 IMPORT_C void SetCompressionBookmark(TLineScanningPosition& aLineScanningPosition, TUint32* aBase, const CFbsBitmap* aFbsBitmap);
219 IMPORT_C TInt HardwareBitmapHandle() const;
220 IMPORT_C TBool IsCompressedInRAM() const;
221 IMPORT_C TBool IsCompressed() const;
222 IMPORT_C TInt DataStride() const;
223 IMPORT_C SEpocBitmapHeader Header() const;
225 IMPORT_C void operator delete(TAny*);
226 void operator delete(TAny*, TAny*) {} // This function exists only to prevent a compiler warning
227 IMPORT_C CBitwiseBitmap(RHeap* aHeap,CChunkPile* aPile);
228 IMPORT_C ~CBitwiseBitmap();
229 IMPORT_C void Reset();
230 IMPORT_C TInt Construct(const TSize& aSize,TDisplayMode aDispMode,TUid aCreatorUid);
231 IMPORT_C void ConstructL(RFs& aFs,const TDesC& aFilename,TInt32 aId,TUint aFileOffset);
232 IMPORT_C void ConstructL(RFile& aFile,TInt32 aId,TUint aFileOffset);
233 IMPORT_C void ConstructL(CShiftedFileStore* aFileStore,TStreamId aStreamId);
234 IMPORT_C TInt CopyData(const CBitwiseBitmap& aSourceBitmap);
236 void GenerateLineFromCompressedTwelveBitData(TUint8* aDestBuffer, const TPoint& aPixel,TInt aLength,TUint32* aBase, TLineScanningPosition& aLineScanningPosition) const;
237 void GenerateLineFromCompressedEightBitData(TUint8* aDestBuffer, const TPoint& aPixel,TInt aLength,TUint32* aBase, TLineScanningPosition& aLineScanningPosition) const;
238 TUint8 GetGrayPixelEx(TInt aX,TUint32* aScanLineAddress) const;
239 TRgb GetRgbPixelEx(TInt aX,TUint32* aScanLineAddress) const;
240 void GetRgbPixelExMany(TInt aX,TUint32* aScanlinePtr,TUint32* aDest,TInt aLength) const;
241 void GetRgbPixelExMany16M(TInt aX,TUint32* aScanlinePtr,TUint8* aDest,TInt aLength) const;
242 void GetRgbPixelExMany16MAP(TInt aX,TUint32* aScanlinePtr,TUint32* aDest,TInt aLength) const;
243 void GetScanLineGray2(TDes8& aBuf,const TPoint& aPixel,TInt aLength,TBool aDither,const TPoint& aDitherOffset,TUint32* aScanlinePtr) const;
244 void GetScanLineGray4(TDes8& aBuf,const TPoint& aPixel,TInt aLength,TBool aDither,const TPoint& aDitherOffset,TUint32* aScanlinePtr) const;
245 void GetScanLineGray16(TDes8& aBuf,const TPoint& aPixel,TInt aLength,TUint32* aScanlinePtr) const;
246 void GetScanLineGray256(TDes8& aBuf,const TPoint& aPixel,TInt aLength,TUint32* aScanlinePtr) const;
247 void GetScanLineColor16(TDes8& aBuf,const TPoint& aPixel,TInt aLength,TUint32* aScanlinePtr) const;
248 void GetScanLineColor256(TDes8& aBuf,const TPoint& aPixel,TInt aLength,TUint32* aScanlinePtr) const;
249 void GetScanLineColor4K(TDes8& aBuf,const TPoint& aPixel,TInt aLength,TUint32* aScanlinePtr) const;
250 void GetScanLineColor64K(TDes8& aBuf,const TPoint& aPixel,TInt aLength,TUint32* aScanlinePtr) const;
251 void GetScanLineColor16M(TDes8& aBuf,const TPoint& aPixel,TInt aLength,TUint32* aScanlinePtr) const;
252 void GetScanLineColor16MU(TDes8& aBuf,const TPoint& aPixel,TInt aLength,TUint32* aScanlinePtr) const;
253 void GetScanLineColorRgb(TDes8& aBuf,const TPoint& aPixel,TInt aLength,TUint32* aScanlinePtr) const;
254 void GetScanLineExBits(TDes8& aBuf,TInt aX,TInt aLength,TUint32* aScanlinePtr) const;
255 void GetScanLineExBytes(TDes8& aBuf,TInt aX,TInt aLength,TUint32* aScanlinePtr) const;
256 void DoExternalizeDataCompressedL(RWriteStream& aStream,TUint8* aData,TInt aSizeInBytes) const;
257 void DoExternalizeByteDataCompressedL(RWriteStream& aStream,TUint8* aData,TInt aSizeInBytes) const;
258 void DoExternalizeTwelveBitDataCompressedL(RWriteStream& aStream,TUint8* aData,TInt aSizeInBytes) const;
259 void DoExternalizeSixteenBitDataCompressedL(RWriteStream& aStream,TUint8* aData,TInt aSizeInBytes) const;
260 void DoExternalizeTwentyFourBitDataCompressedL(RWriteStream& aStream,TUint8* aData,TInt aSizeInBytes) const;
261 void DoExternalizeThirtyTwoUBitDataCompressedL(RWriteStream& aStream,TUint8* aData,TInt aSizeInBytes) const;
262 void DoExternalizeThirtyTwoABitDataCompressedL(RWriteStream& aStream,TUint8* aData,TInt aSizeInBytes) const;
263 TInt SizeOfDataCompressed(TUint8* aData,TInt aSizeInBytes) const;
264 TInt SizeOfByteDataCompressed(TUint8* aData,TInt aSizeInBytes) const;
265 TInt SizeOfTwelveBitDataCompressed(TUint8* aData,TInt aSizeInBytes) const;
266 TInt SizeOfSixteenBitDataCompressed(TUint8* aData,TInt aSizeInBytes) const;
267 TInt SizeOfTwentyFourBitDataCompressed(TUint8* aData,TInt aSizeInBytes) const;
268 TInt SizeOfThirtyTwoUBitDataCompressed(TUint8* aData,TInt aSizeInBytes) const;
269 TInt SizeOfThirtyTwoABitDataCompressed(TUint8* aData,TInt aSizeInBytes) const;
270 TBool TrueColorPointerCompare(TUint8* aColorPointer,TUint8 aComponent1,TUint8 aComponent2,TUint8 aComponent3) const;
271 TBool ColorAlphaPointerCompare(TUint8* aColorPointer,TUint8 aComponent1,TUint8 aComponent2,TUint8 aComponent3,TUint8 aComponent4) const;
272 void DoInternalizeL(RReadStream& aStream,TInt aSrceSize,TUint32* aBase);
273 void DoInternalizeCompressedDataL(RReadStream& aStream,TInt aSrceSize,TUint32* aBase,TBitmapfileCompression aCompression);
274 void DoDecompressByteData(TUint8* aDestBuffer,TInt aDestSize,TUint8* aSrceBuffer,TInt aSrceSize);
276 void DoDecompressByteDataAltL(RReadStream& aStream,TInt aSrceSizeInBytes,TUint32* aBase);
277 void DoDecompressTwelveBitData(TUint8* aDestBuffer,TInt aDestSize,TUint8* aSrceBuffer,TInt aSrceSize);
278 void DoDecompressTwelveBitDataAltL(RReadStream& aStream,TInt aSrceSizeInBytes,TUint32* aBase);
279 void DoDecompressSixteenBitData(TUint8* aDestBuffer,TInt aDestSize,TUint8* aSrceBuffer,TInt aSrceSize);
280 void DoDecompressSixteenBitDataAltL(RReadStream& aStream,TInt aSrceSizeInBytes,TUint32* aBase);
281 void DoDecompressTwentyFourBitData(TUint8* aDestBuffer,TInt aDestSize,TUint8* aSrceBuffer,TInt aSrceSize);
282 void DoDecompressTwentyFourBitDataAltL(RReadStream& aStream,TInt aSrceSizeInBytes,TUint32* aBase);
283 void DoDecompressThirtyTwoUBitData(TUint8* aDestBuffer,TInt aDestSize,TUint8* aSrceBuffer,TInt aSrceSize);
284 void DoDecompressThirtyTwoUBitDataAltL(RReadStream& aStream,TInt aSrceSizeInBytes,TUint32* aBase);
285 void DoDecompressThirtyTwoABitData(TUint8* aDestBuffer,TInt aDestSize,TUint8* aSrceBuffer,TInt aSrceSize);
286 void DoDecompressThirtyTwoABitDataAltL(RReadStream& aStream,TInt aSrceSizeInBytes,TUint32* aBase);
287 void DoStretchScanLine(TDes8& aBuf,TInt x,TInt y,TInt aClipStrchX,TInt aClipStrchLen,TInt aStretchLength,TInt aOrgX,TInt aOrgLen,const TPoint& aDitherOffset,TDisplayMode aDispMode,TUint32* aBase,TLineScanningPosition& aLineScanningPosition) const;
288 void DoCompressScanLine(TDes8& aBuf,TInt x,TInt y,TInt aClipStrchX,TInt aClipStrchLen,TInt aStretchLength,TInt aOrgX,TInt aOrgLen,const TPoint& aDitherOffset,TDisplayMode aDispMode,TUint32* aBase,TLineScanningPosition& aLineScanningPosition) const;
289 TUint32 HashTo1bpp(TUint32 aGray256,TBool aOddX,TBool aOddY) const;
290 TUint32 HashTo2bpp(TUint32 aGray256,TInt aDitherIndex) const;
291 TBool IsWordMonochrome(TUint32 aWord) const;
292 TUint32* DataAddress() const;
293 static void WhiteFill(TUint8* aData,TInt aDataSize,TDisplayMode aDispMode);
294 static TInt ByteWidth(TInt aPixelWidth,TDisplayMode aDispMode);
295 static TInt Bpp(TDisplayMode aDispMode);
296 static TInt IsColor(TDisplayMode aDispMode);
297 static TDisplayMode DisplayMode(TInt aBpp,TInt aColor);
298 static TBitmapfileCompression CompressionType(TInt aBpp, TInt aColor);
299 TInt DoGetScanLinePtr(TUint32*& aSlptr, TPoint& aPixel,TInt aLength, TUint32* aBase, TLineScanningPosition& aLineScanningPosition) const;
300 void GenerateLineFromCompressedSixteenBitData(TUint8* aDestBuffer, const TPoint& aPixel,TInt aLength, TUint32* aBase, TLineScanningPosition& aLineScanningPosition) const;
301 TDisplayMode InitialDisplayMode() const;
302 TInt SetDisplayMode(TDisplayMode aDisplayMode, TUint32* aDataAddress);
303 TInt DisplayModeArgCheck(TDisplayMode aDisplayMode, TUint32* aDataAddress) const;
304 void ChangeDisplayMode( TDisplayMode aNewDisplayMode,
305 TInt aScanLineWidthNew,
306 TUint8* aDataAddrNew,
307 TUint32* aDataAddress,
311 void UpdateBitmapProperties(TDisplayMode aNewDisplayMode);
312 TInt SwapWidthAndHeight(TUint32* aDataAddress);
313 void GenerateLineFromCompressed24BitData(TUint8* aDestBuffer, const TPoint& aPixel,TInt aLength, TUint32* aBase, TLineScanningPosition& aLineScanningPosition) const;
314 void GenerateLineFromCompressed32UBitData(TUint8* aDestBuffer, const TPoint& aPixel,TInt aLength, TUint32* aBase, TLineScanningPosition& aLineScanningPosition) const;
315 void GenerateLineFromCompressed32ABitData(TUint8* aDestBuffer, const TPoint& aPixel,TInt aLength, TUint32* aBase, TLineScanningPosition& aLineScanningPosition) const;
316 void AdjustXCoord(TInt& aX) const;
317 void GetLineScanPos(TLineScanningPosition& aLineScanPos,
318 const TCompressionBookMark*& aComprBookMark,
319 const TUint8* aBase) const;
320 void UpdateBookMark(const TLineScanningPosition& aLineScanPos,
321 TCompressionBookMark* aComprBookMark,
322 const TUint8* aBase) const;
323 void GetScanLineColor16MA(TDes8& aBuf,const TPoint& aPixel,TInt aLength,TUint32* aScanlinePtr) const;
324 void GetScanLineColor16MAP(TDes8& aBuf,const TPoint& aPixel,TInt aLength,TUint32* aScanlinePtr) const;
325 TInt PaletteCompress();
326 void GenerateLineFromPaletteCompressedData(TUint8* aDestBuffer, const TPoint& aPixel,TInt aLength, TUint32* aBase, TLineScanningPosition& aLineScanningPosition) const;
327 TUint PaletteBitsPerPixel(TInt aNumColors) const;
328 TUint PaletteBytesPerPixel(TInt aBitsPerPixel) const;
329 void PaletteDecodeAndAssignGeneric(TUint8* aDataPtr, TUint32* aPalettePtr, TUint8*& aDestPtr, TUint aStartPixel, TUint aEndPixel, TUint aCompressedPixelsPerByte, TUint aCompressedBitsPerPixel) const;
330 static void PaletteDecode1PixelPerByte(TUint8* aDataPtr, TUint32* aPalettePtr, TUint8*& aDestPtr, TAssignFunction aAssignFunction);
331 static void PaletteDecode2PixelPerByte(TUint8* aDataPtr, TUint32* aPalettePtr, TUint8*& aDestPtr, TAssignFunction aAssignFunction);
332 static void PaletteDecode4PixelPerByte(TUint8* aDataPtr, TUint32* aPalettePtr, TUint8*& aDestPtr, TAssignFunction aAssignFunction);
333 static void PaletteDecode8PixelPerByte(TUint8* aDataPtr, TUint32* aPalettePtr, TUint8*& aDestPtr, TAssignFunction aAssignFunction);
334 static void PaletteAssign16BitColor(TUint8*& aDestPtr, TUint32 aColor);
335 static void PaletteAssign24BitColor(TUint8*& aDestPtr, TUint32 aColor);
336 static void PaletteAssign32BitColor(TUint8*& aDestPtr, TUint32 aColor);
337 void UpdatePaddingData(TUint32* aData);
338 static void CheckHeaderIsValidL(const SEpocBitmapHeader& aHeader);
339 static TInt CompressedFormatInfo(TDisplayMode aDispMode, TInt& aBytesPerPack, TInt& aBytesPerCompressed);
340 static void BitmapFill32(TUint32* aDestPtr32, TInt aCount, TUint32 aValue32);
341 static inline void BitmapFill16(TUint16* aDestPtr16, TInt aCount, TUint16 aValue16);
346 TSettings(TDisplayMode aDisplayMode);
347 void SetDisplayModes(TDisplayMode aDisplayMode);
348 void SetCurrentDisplayMode(TDisplayMode aDisplayMode);
349 TDisplayMode CurrentDisplayMode() const;
350 TDisplayMode InitialDisplayMode() const;
351 inline void SetDirtyBitmap();
352 inline TBool IsDirtyBitmap() const;
355 EBitmapFlagDirty = 0x10000 // set in the old bitmap when a re-allocating operation has created a new bitmap
362 SEpocBitmapHeader iHeader;
363 struct TExtra // used only in builds with SYMBIAN_DEBUG_FBS_LOCKHEAP defined
365 TInt iLockCount; // number of calls to CFbsBitmap::LockHeap() not balanced by calls to CFbsBitmap::UnlockHeap()
366 TThreadId iThreadId; // id of thread that called CFbsBitmap::LockHeap() if any
368 *iExtra; // only for bitmaps created in RAM, set to 0xFFFFFFFF for ROM bitmaps by the tool chain
369 TInt iDataOffset; // offset in bytes from "this" or from base of bitmap chunk, or hardware bitmap handle
370 TBool iIsCompressedInRAM; //flag indicating whether CompressData has been called