os/graphics/fbs/fontandbitmapserver/sfbs/BitmapCompr.inl
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
sl@0
     1
// Copyright (c) 2003-2009 Nokia Corporation and/or its subsidiary(-ies).
sl@0
     2
// All rights reserved.
sl@0
     3
// This component and the accompanying materials are made available
sl@0
     4
// under the terms of "Eclipse Public License v1.0"
sl@0
     5
// which accompanies this distribution, and is available
sl@0
     6
// at the URL "http://www.eclipse.org/legal/epl-v10.html".
sl@0
     7
//
sl@0
     8
// Initial Contributors:
sl@0
     9
// Nokia Corporation - initial contribution.
sl@0
    10
//
sl@0
    11
// Contributors:
sl@0
    12
//
sl@0
    13
// Description:
sl@0
    14
//
sl@0
    15
sl@0
    16
/**
sl@0
    17
 
sl@0
    18
 The created class instance doesn't take the ownership on aBase parameter.
sl@0
    19
 @internalComponent
sl@0
    20
 @pre aBase != NULL
sl@0
    21
 @param aBase Points to the beginning of compressed bitmap data
sl@0
    22
 @param aComprDataBytes Total amount of compressed bitmap bytes
sl@0
    23
 @param aCanAdjustLineScanPos If not EFalse - scanline position can be adjusted calling
sl@0
    24
 AdjustLineScanningPosition function.
sl@0
    25
 @see AdjustLineScanningPosition(TLineScanningPosition& aLineScanPos, const TUint32* aBase, TInt aBitmapWidth, TInt aStartPos, TInt aCompressedDataBytes);
sl@0
    26
*/
sl@0
    27
template <TInt BPP, TInt BPP_DEST> 
sl@0
    28
TScanLineDecompressor<BPP, BPP_DEST>::TScanLineDecompressor(const TUint32* aBase, 
sl@0
    29
												  TInt aComprDataBytes, 
sl@0
    30
												  TBool aCanAdjustLineScanPos) :
sl@0
    31
	iBase(aBase),
sl@0
    32
	iComprDataBytes(aComprDataBytes),
sl@0
    33
	iCanAdjustLineScanPos(aCanAdjustLineScanPos)
sl@0
    34
	{
sl@0
    35
	}
sl@0
    36
sl@0
    37
/**
sl@0
    38
The method calculates the start byte position (as a index) in decompressed bitmap data .
sl@0
    39
@internalComponent
sl@0
    40
@param aPixel The decompression starts from this pixel
sl@0
    41
@param aByteWidth Scanline width in bytes
sl@0
    42
@return Calculated start position.
sl@0
    43
*/
sl@0
    44
template <TInt BPP, TInt BPP_DEST> 
sl@0
    45
TInt TScanLineDecompressor<BPP, BPP_DEST>::CalcStartPos(const TPoint& aPixel, TInt aByteWidth) const
sl@0
    46
	{
sl@0
    47
	return aPixel.iY * aByteWidth + aPixel.iX * BPP;
sl@0
    48
	}
sl@0
    49
sl@0
    50
/**
sl@0
    51
The method calculates the start byte position (as a index) in decompressed bitmap data .
sl@0
    52
@internalComponent
sl@0
    53
@param aPixel The decompression starts from this pixel
sl@0
    54
@param aByteWidth Scanline width in bytes
sl@0
    55
@return Calculated start position.
sl@0
    56
*/
sl@0
    57
template <TInt BPP, TInt BPP_DEST> 
sl@0
    58
TInt TScanLineDecompressor<BPP, BPP_DEST>::CalcDestStartPos(const TPoint& aPixel, TInt aByteWidth) const
sl@0
    59
	{
sl@0
    60
	return aPixel.iY * aByteWidth + aPixel.iX * BPP_DEST;
sl@0
    61
	}
sl@0
    62
sl@0
    63
/**
sl@0
    64
The method calculates the end byte position (as a index) in decompressed bitmap data .
sl@0
    65
@internalComponent
sl@0
    66
@param aLength Length in pixels of bitmap data we want to decompress.
sl@0
    67
@param aPixel The decompression starts from this pixel
sl@0
    68
@param aByteWidth Scanline width in bytes
sl@0
    69
@return Calculated end position.
sl@0
    70
*/
sl@0
    71
template <TInt BPP, TInt BPP_DEST> 
sl@0
    72
TInt TScanLineDecompressor<BPP, BPP_DEST>::CalcEndPos(TInt aLength, const TPoint& aPixel, 
sl@0
    73
											TInt aByteWidth) const
sl@0
    74
	{
sl@0
    75
	return aPixel.iY * aByteWidth + (aPixel.iX + aLength) * BPP_DEST;
sl@0
    76
	}
sl@0
    77
sl@0
    78
/**
sl@0
    79
The method calculates a pointer to a place in aDestBuffer where the 
sl@0
    80
copying of decompressed bitmap data starts.
sl@0
    81
@internalComponent
sl@0
    82
@param aDestBuffer Points to the start of the destination buffer.
sl@0
    83
@param aPixel The decompression starts from this pixel
sl@0
    84
@return A pointer to a place in aDestBuffer where the 
sl@0
    85
copying of decompressed bitmap data starts.
sl@0
    86
*/
sl@0
    87
template <TInt BPP, TInt BPP_DEST> 
sl@0
    88
TUint8* TScanLineDecompressor<BPP, BPP_DEST>::CalcDestPtr(const TUint8* aDestBuffer, 
sl@0
    89
												const TPoint& aPixel) const
sl@0
    90
	{
sl@0
    91
	return const_cast <TUint8*> (aDestBuffer + aPixel.iX * BPP_DEST);
sl@0
    92
	}
sl@0
    93
sl@0
    94
/**
sl@0
    95
The method calculates a pointer to a place in aDestBuffer where the copying
sl@0
    96
of the decompressed data stops.
sl@0
    97
@internalComponent
sl@0
    98
@param aDestPtr A pointer to a place in aDestBuffer where the 
sl@0
    99
copying of decompressed bitmap data starts - CalcDestPtr() return value.
sl@0
   100
@param aPixel The decompression starts from this pixel
sl@0
   101
@param aByteWidth Scanline width in bytes
sl@0
   102
@param aLength Length in pixels of bitmap data we want to decompress.
sl@0
   103
@param aStartPos CalcStartPos() method return value.
sl@0
   104
@return A pointer to a place in aDestBuffer where the 
sl@0
   105
copying of decompressed bitmap data stops.
sl@0
   106
@see CalcDestPtr
sl@0
   107
@see CalcStartPos
sl@0
   108
*/
sl@0
   109
template <TInt BPP, TInt BPP_DEST> 
sl@0
   110
TUint8* TScanLineDecompressor<BPP, BPP_DEST>::CalcDestPtrLimit(const TUint8* aDestPtr, 
sl@0
   111
													 const TPoint& aPixel, TInt aByteWidth, 
sl@0
   112
													 TInt aLength, TInt aStartPos) const
sl@0
   113
	{
sl@0
   114
	return const_cast <TUint8*> (aDestPtr + CalcEndPos(aLength, aPixel, aByteWidth) - aStartPos);
sl@0
   115
	}
sl@0
   116
sl@0
   117
/**
sl@0
   118
The method performs a quick find of the correct start position.
sl@0
   119
It uses the fact that aLineScanPos parameter should be recalculated after each positioning 
sl@0
   120
in the compresed bitmap data and might be used in the next call of decompression methods.
sl@0
   121
@internalComponent
sl@0
   122
@param aLineScanPos Saved information about the last used position in the compressed data
sl@0
   123
@param aByteWidth Scanline width in bytes
sl@0
   124
@param aStartPos CalcStartPos() method return value.
sl@0
   125
@return A pointer to a position in compressed bitmap data, where the decompression  
sl@0
   126
starts from
sl@0
   127
@see CalcStartPos
sl@0
   128
*/
sl@0
   129
template <TInt BPP, TInt BPP_DEST> 
sl@0
   130
TUint8* TScanLineDecompressor<BPP, BPP_DEST>::AdjustLineScanningPosition(TLineScanningPosition& aLineScanPos, 
sl@0
   131
															   TInt aByteWidth, 
sl@0
   132
															   TInt aStartPos) const
sl@0
   133
	{
sl@0
   134
	TUint8* srcPtr = NULL;
sl@0
   135
	// Fast find the correct position to start	
sl@0
   136
	if(iCanAdjustLineScanPos)
sl@0
   137
		{
sl@0
   138
		::AdjustLineScanningPosition(aLineScanPos, iBase, aByteWidth, aStartPos, iComprDataBytes);
sl@0
   139
		}
sl@0
   140
	srcPtr = aLineScanPos.iSrcDataPtr;
sl@0
   141
	//Unfortunatelly we can't move fast srcPtr if aLineScanPos.iCursorPos > aStartPos, 
sl@0
   142
	//because we have 1 byte for the compressed data length and the data itself after the "length"
sl@0
   143
	//byte. With this information we can move srcPtr forward, not backward.
sl@0
   144
	if(aLineScanPos.iCursorPos > aStartPos) 
sl@0
   145
		{
sl@0
   146
		srcPtr = (TUint8*)iBase;
sl@0
   147
		aLineScanPos.iCursorPos = 0;
sl@0
   148
		}
sl@0
   149
	TInt count = TInt8(*srcPtr);//pixels count
sl@0
   150
	TInt length = (count < 0 ? -count : count + 1) * BPP;//length in bytes
sl@0
   151
	//If count < 0 - we have a sequence of abs(count) pixels - abs(count) * BPP bytes
sl@0
   152
	//If count >= 0 - we have (count + 1) pixels with the same color - (count + 1) * BPP bytes
sl@0
   153
	while((aLineScanPos.iCursorPos + length) <= aStartPos)
sl@0
   154
		{
sl@0
   155
		srcPtr += count < 0 ? length + 1 : (BPP + 1);//(BPP + 1) - bytes for the pixel + 1 byte for the length
sl@0
   156
		aLineScanPos.iCursorPos += length;
sl@0
   157
		count = TInt8(*srcPtr);
sl@0
   158
		length = (count < 0 ? -count : count + 1) * BPP;
sl@0
   159
		}
sl@0
   160
	//After executing of the previous "if" operator and "while" loop, srcPtr points to 
sl@0
   161
	//the nearest "length" byte before the start position in compressed bitmap data.
sl@0
   162
	return srcPtr;
sl@0
   163
	}
sl@0
   164
sl@0
   165
/**
sl@0
   166
This overloaded "()" operator used for decompression of bitmap scan lines.
sl@0
   167
@internalComponent
sl@0
   168
@param aDestBuffer Points to the destination buffer. After the call it fill be filled
sl@0
   169
with the decomperssed data.
sl@0
   170
@param aPixel The decompression starts from this pixel
sl@0
   171
@param aLineScanPos Saved information about the last used position in the compressed data
sl@0
   172
@param aByteWidth Scanline width in bytes
sl@0
   173
@param aLength Length of requested decompressed data - in pixels
sl@0
   174
*/
sl@0
   175
template <TInt BPP, TInt BPP_DEST> 
sl@0
   176
void TScanLineDecompressor<BPP, BPP_DEST>::operator()(TUint8* aDestBuffer, const TPoint& aPixel, 
sl@0
   177
											TLineScanningPosition& aLineScanPos, 
sl@0
   178
											TInt aByteWidth, TInt aDestByteWidth, TInt aLength) const
sl@0
   179
	{
sl@0
   180
	TInt startPos = CalcStartPos(aPixel, aByteWidth);
sl@0
   181
	TInt destStartPos = CalcDestStartPos(aPixel, aDestByteWidth);
sl@0
   182
	TUint8* destPtr = CalcDestPtr(aDestBuffer, aPixel);
sl@0
   183
	const TUint8* destPtrLimit = CalcDestPtrLimit(destPtr, aPixel, aDestByteWidth, aLength, destStartPos);
sl@0
   184
	//Fast find the correct position to start	
sl@0
   185
	TUint8* srcPtr = AdjustLineScanningPosition(aLineScanPos, aByteWidth, startPos);
sl@0
   186
	//Decompress each slot of compressed data which has the following format:
sl@0
   187
	// ...|"length byte"| "pixel value(s)" |...
sl@0
   188
	while(destPtr < destPtrLimit)
sl@0
   189
		{
sl@0
   190
		TInt pixelsInTheSlot = TInt8(*srcPtr++);
sl@0
   191
		TInt availDestSpaceInBytes = destPtrLimit - destPtr;
sl@0
   192
		TInt skipBytes = startPos - aLineScanPos.iCursorPos;
sl@0
   193
		if(pixelsInTheSlot < 0)
sl@0
   194
			{//a sequence of abs(pixelsInTheSlot) pixels with different color values
sl@0
   195
			TInt bytesInTheSlot = -pixelsInTheSlot * BPP;
sl@0
   196
			TUint8* start_pos = srcPtr;
sl@0
   197
			TInt bytesToCopy = bytesInTheSlot;
sl@0
   198
			if(skipBytes > 0) //Skip the pixels before the start possition
sl@0
   199
				{
sl@0
   200
				bytesToCopy -= skipBytes;
sl@0
   201
				start_pos += skipBytes;
sl@0
   202
				}
sl@0
   203
			if(BPP == BPP_DEST)
sl@0
   204
				{
sl@0
   205
				destPtr = Mem::Copy(destPtr, start_pos, ::Min(bytesToCopy, availDestSpaceInBytes));
sl@0
   206
				}
sl@0
   207
			else
sl@0
   208
				{
sl@0
   209
				destPtr = CopyBlockPixel(destPtr, start_pos, ::Min(bytesToCopy / BPP, availDestSpaceInBytes / BPP_DEST));
sl@0
   210
				}	
sl@0
   211
			//Move to the next "length" byte only if everything, 
sl@0
   212
			//controlled by the current "length" byte, is copied
sl@0
   213
			if((bytesToCopy / BPP) <= (availDestSpaceInBytes / BPP_DEST))
sl@0
   214
				{
sl@0
   215
				srcPtr += bytesInTheSlot;
sl@0
   216
				aLineScanPos.iCursorPos += bytesInTheSlot;
sl@0
   217
				}
sl@0
   218
			else
sl@0
   219
				{
sl@0
   220
				srcPtr--;
sl@0
   221
				}
sl@0
   222
			}
sl@0
   223
		else//the "else" part of - if(pixelsInTheSlot < 0)
sl@0
   224
			{//(pixelsInTheSlot + 1) pixels with the same color
sl@0
   225
			TInt pixelsToCopy = pixelsInTheSlot + 1;
sl@0
   226
			TInt bytesInTheSlot = pixelsToCopy * BPP;
sl@0
   227
			if(skipBytes > 0) //Skip the pixels before the start possition
sl@0
   228
				{
sl@0
   229
				pixelsToCopy -= skipBytes / BPP;
sl@0
   230
				}
sl@0
   231
			destPtr = CopyPixel(destPtr, srcPtr, ::Min(pixelsToCopy, availDestSpaceInBytes / BPP_DEST));
sl@0
   232
			//Move to the next "length" byte only if everything, 
sl@0
   233
			//controlled by the current "length" byte, is copied
sl@0
   234
			if((pixelsToCopy * BPP) <= availDestSpaceInBytes)
sl@0
   235
				{
sl@0
   236
				srcPtr += BPP;// += BPP - we have (count + 1) pixels with the same color - only BPP bytes are used.
sl@0
   237
				aLineScanPos.iCursorPos += bytesInTheSlot;
sl@0
   238
				}
sl@0
   239
			else
sl@0
   240
				{
sl@0
   241
				srcPtr--;
sl@0
   242
				}
sl@0
   243
			}//end of - if(pixelsCnt < 0)
sl@0
   244
		}//end of - while(destPtr < destPtrLimit)
sl@0
   245
	aLineScanPos.iSrcDataPtr = srcPtr;//update aLineScanPos.iSrcDataPtr - it will be used 
sl@0
   246
									  //when the method is called again.
sl@0
   247
	}
sl@0
   248
sl@0
   249