imaging/imagingfws/src/ImageBitmapUtil.cpp
changeset 0 5752a19fdefe
equal deleted inserted replaced
-1:000000000000 0:5752a19fdefe
       
     1 // Copyright (c) 1999-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     2 // All rights reserved.
       
     3 // This component and the accompanying materials are made available
       
     4 // under the terms of "Eclipse Public License v1.0"
       
     5 // which accompanies this distribution, and is available
       
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 //
       
    15 
       
    16 #include <icl/imageprocessor.h>
       
    17 
       
    18 //
       
    19 // this file contains many performance-critical code, so use ARM instruction set for it
       
    20 //
       
    21 #ifdef __ARMCC__
       
    22 #pragma arm
       
    23 #pragma O3
       
    24 #pragma Otime
       
    25 #endif
       
    26 
       
    27 /**
       
    28  	@internalComponent
       
    29 */
       
    30 GLDEF_C void Panic(TImageBitmapUtilPanic aError);
       
    31 
       
    32 /**
       
    33   	@internalComponent
       
    34 */
       
    35 GLDEF_C TColorConvertor* CreateColorConvertorL(TDisplayMode aDisplayMode);
       
    36 
       
    37 /**
       
    38 Static factory function for creating instances of TColorConvertor derived classes
       
    39 based on the supplied display mode.
       
    40 
       
    41 @param  aDisplayMode
       
    42         The display mode. This determines the TColorConvertor derived type returned.
       
    43 
       
    44 @return A pointer to a fully constructed TColorConvertor derived object.
       
    45 
       
    46 @leave  KErrNotSupported
       
    47         The display mode is not supported.
       
    48 */
       
    49 EXPORT_C TColorConvertor* TColorConvertor::NewL(TDisplayMode aDisplayMode)
       
    50 	{
       
    51 	return CreateColorConvertorL(aDisplayMode);
       
    52 	}
       
    53 
       
    54 //
       
    55 // TImageBitmapUtil
       
    56 //
       
    57 
       
    58 /**
       
    59 Default constructor for this class.
       
    60 */
       
    61 EXPORT_C TImageBitmapUtil::TImageBitmapUtil():
       
    62 	iBitmap(NULL),
       
    63 	iBpp(0),
       
    64 	iBppShift(0),
       
    65 	iPixelShift(0),
       
    66 	iBitShift(0),
       
    67 	iScanlineWordLength(0)
       
    68 	{}
       
    69 
       
    70 /**
       
    71 Requests a lock for the current bitmap from the font & bitmap server and
       
    72 sets the current position in the bitmap to the first pixel.
       
    73 */
       
    74 EXPORT_C void TImageBitmapUtil::Begin()
       
    75 	{
       
    76 	ASSERT(iBitmap && iBitmap->Handle());
       
    77 
       
    78 	iBase.iWordPos = iBitmap->DataAddress();
       
    79 	}
       
    80 
       
    81 /**
       
    82 Requests a lock for the current bitmap from the font & bitmap server and
       
    83 sets the current position in the bitmap to aPosition.
       
    84 
       
    85 @param  aPosition
       
    86         The position to move to.
       
    87 
       
    88 @return A boolean indicating if the position was out of bounds. EFalse if the position was out of 
       
    89         bounds, otherwise ETrue.
       
    90 */
       
    91 EXPORT_C TBool TImageBitmapUtil::Begin(const TPoint& aPosition)
       
    92 	{
       
    93 	Begin();
       
    94 	if (!SetPos(aPosition))
       
    95 		{
       
    96 		End();
       
    97 		return EFalse;
       
    98 		}
       
    99 
       
   100 	return ETrue;
       
   101 	}
       
   102 
       
   103 /**
       
   104 Releases a lock previously acquired using TImageBitmapUtil::Begin().
       
   105 */
       
   106 EXPORT_C void TImageBitmapUtil::End()
       
   107 	{
       
   108 	}
       
   109 
       
   110 /**
       
   111 Sets the current bitmap to aBitmap.
       
   112 
       
   113 @param  aBitmap
       
   114         A pointer to the bitmap.
       
   115 
       
   116 @leave  KErrNotFound
       
   117         The bitmap or its handle is NULL or its display mode is not recognised.
       
   118 */
       
   119 EXPORT_C void TImageBitmapUtil::SetBitmapL(CFbsBitmap* aBitmap)
       
   120 	{
       
   121 	if (!aBitmap || !aBitmap->Handle())
       
   122 		User::Leave(KErrNotFound);
       
   123 	
       
   124 	if(aBitmap->ExtendedBitmapType()!=KNullUid)
       
   125 	    {
       
   126 	    User::Leave(KErrNotSupported);
       
   127 	    }
       
   128 
       
   129 	iBitmap = aBitmap;
       
   130 	iWordAccess = ETrue;
       
   131 
       
   132 	switch(iBitmap->DisplayMode())
       
   133 		{
       
   134 	case EGray2:
       
   135 		iBppShift = 0;
       
   136 		iMask = 1;
       
   137 		break;
       
   138 	case EGray4:
       
   139 		iBppShift = 1;
       
   140 		iMask = 3;
       
   141 		break;
       
   142 	case EGray16:
       
   143 	case EColor16:
       
   144 		iBppShift = 2;
       
   145 		iMask = 0xF;
       
   146 		break;
       
   147 	case EGray256:
       
   148 	case EColor256:
       
   149 		iBppShift = 3;
       
   150 		iMask = 0xFF;
       
   151 		break;
       
   152 	case EColor4K:
       
   153 	case EColor64K:
       
   154 		iBppShift = 4;
       
   155 		iMask = 0xFFFF;
       
   156 		break;
       
   157 	case EColor16M:
       
   158 		iWordAccess = EFalse;
       
   159 		break;
       
   160 	case EColor16MU:
       
   161 		iBppShift = 5;
       
   162 		iMask = 0xFF000000; // | with pixel value to set alpha channel to opaque
       
   163 		break;
       
   164 	case EColor16MA:		
       
   165 		iBppShift = 5;
       
   166 		iMask = 0x00000000; // | with pixel value to set alpha channel level
       
   167 		break;
       
   168 	default:
       
   169 		User::Leave(KErrNotFound);
       
   170 		break;
       
   171 		}
       
   172 
       
   173 	iBpp = 1<<iBppShift;
       
   174 	iPixelsPerWord = 32>>iBppShift;
       
   175 	iPixelShift = 5-iBppShift;
       
   176 
       
   177 	iSize = iBitmap->SizeInPixels();
       
   178 	iScanlineWordLength = CFbsBitmap::ScanLineLength(iSize.iWidth,iBitmap->DisplayMode()) / 4;
       
   179 	}
       
   180 
       
   181 /**
       
   182 Sets the pixel value at the current bitmap position using aPixelIndex.
       
   183 
       
   184 @post    
       
   185 The current position is updated.
       
   186 
       
   187 @param  aPixelIndex
       
   188         The pixel index.
       
   189 */
       
   190 EXPORT_C void TImageBitmapUtil::SetPixel(TUint32 aPixelIndex)
       
   191 	{
       
   192 	ASSERT(iPosition.iX < iSize.iWidth);
       
   193 
       
   194 	iPosition.iX++;
       
   195 
       
   196 	if (iWordAccess)
       
   197 		{
       
   198 		TUint32* dataPtr = iData.iWordPos;
       
   199 		switch(iBppShift)
       
   200 			{
       
   201 			case 5:
       
   202 				*dataPtr = aPixelIndex|iMask;
       
   203 				break;
       
   204 			default:
       
   205 				TInt bitShift = iBitShift;
       
   206 
       
   207 				*dataPtr &= ~(iMask << bitShift);
       
   208 				*dataPtr |= aPixelIndex << bitShift;
       
   209 
       
   210 				bitShift += iBpp;
       
   211 				if (bitShift >= 32)
       
   212 					{
       
   213 					bitShift = 0;
       
   214 					iData.iWordPos = dataPtr+1;
       
   215 					}
       
   216 				iBitShift = bitShift;
       
   217 				break;
       
   218 			}
       
   219 		}
       
   220 	else
       
   221 		{
       
   222 		TUint8* dataPtr = iData.iBytePos;
       
   223 
       
   224 		*dataPtr++ = TUint8(aPixelIndex);
       
   225 		*dataPtr++ = TUint8(aPixelIndex >> 8);
       
   226 		*dataPtr++ = TUint8(aPixelIndex >> 16);
       
   227 
       
   228 		iData.iBytePos = dataPtr;
       
   229 		}
       
   230 
       
   231 	}
       
   232 
       
   233 /**
       
   234 Sets an array of pixel values, starting at the current bitmap position using the
       
   235 values supplied in aPixelIndex.
       
   236 
       
   237 @post    
       
   238 The current position is updated.
       
   239 
       
   240 @param  aPixelIndex
       
   241         A pointer to the first element in the array.
       
   242 @param  aNumberOfPixels
       
   243         The number of elements in the array.
       
   244 */
       
   245 EXPORT_C void TImageBitmapUtil::SetPixels(TUint32* aPixelIndex,TInt aNumberOfPixels)
       
   246 	{
       
   247 	TInt newXPos = iPosition.iX + aNumberOfPixels;
       
   248 	ASSERT(newXPos <= iSize.iWidth);
       
   249 	
       
   250 	if (newXPos == iSize.iWidth)
       
   251 		{
       
   252 		iPosition.iX = 0;
       
   253 		iPosition.iY++;
       
   254 		}
       
   255 	else
       
   256 		{
       
   257 		iPosition.iX = newXPos;
       
   258 		}
       
   259 
       
   260 	if (iWordAccess)
       
   261 		{
       
   262 		TUint32*	dataPtr = iData.iWordPos;
       
   263 		TInt		bitShift = iBitShift;
       
   264 		TInt		bpp = iBpp;
       
   265 
       
   266 		// Do pixels in first data word (if first pixel isn't on 32 bit word boundary)
       
   267 
       
   268 		if(bitShift > 0)
       
   269 			{
       
   270 			TInt bitShiftLimit = bitShift+(aNumberOfPixels<<iBppShift);
       
   271 			if(bitShiftLimit>32)
       
   272 				bitShiftLimit = 32;
       
   273 
       
   274 			TInt numMaskBits = bitShiftLimit-bitShift;
       
   275 			aNumberOfPixels -= numMaskBits>>iBppShift;
       
   276 			TInt mask = (1<<numMaskBits)-1;	//mask has lowest 'numMaskBits' set
       
   277 
       
   278 			TInt wordValue = *dataPtr;
       
   279 
       
   280 			wordValue &= ~(mask << bitShift);	//zero the destination pixels we are about to write
       
   281 			do	{
       
   282 				wordValue |= *aPixelIndex++ << bitShift;
       
   283 				bitShift += bpp;
       
   284 				}
       
   285 			while(bitShift<bitShiftLimit);
       
   286 
       
   287 			*dataPtr = wordValue;
       
   288 
       
   289 			if(bitShift >= 32)
       
   290 				{
       
   291 				bitShift = 0;
       
   292 				dataPtr++;
       
   293 				}
       
   294 			}
       
   295 
       
   296 		// Do pixels which fill whole data words
       
   297 
       
   298 		TUint32* dataPtrLimit = dataPtr+(aNumberOfPixels >> iPixelShift);
       
   299 
       
   300 		aNumberOfPixels &= ~(0xFFFFFFFF<<iPixelShift);
       
   301 
       
   302 		if(dataPtr < dataPtrLimit)
       
   303 			{
       
   304 			switch(iBppShift)
       
   305 				{
       
   306 				case 5:
       
   307 					if(iMask == 0xFF000000)
       
   308 						{
       
   309 						do	{
       
   310 							*dataPtr++ = *aPixelIndex++|0xFF000000;
       
   311 							}
       
   312 						while(dataPtr < dataPtrLimit);
       
   313 						}
       
   314 					else
       
   315 						{
       
   316 						do	{
       
   317 							*dataPtr++ = *aPixelIndex++;
       
   318 							}
       
   319 						while(dataPtr < dataPtrLimit);
       
   320 						}
       
   321 					
       
   322 					break;
       
   323 
       
   324 				case 4:
       
   325 					do	{
       
   326 						TUint32 wordValue = *aPixelIndex++;
       
   327 						wordValue |= *aPixelIndex++ << 16;
       
   328 						*dataPtr++ = wordValue;
       
   329 						}
       
   330 					while(dataPtr < dataPtrLimit);
       
   331 					break;
       
   332 
       
   333 				case 3:
       
   334 					do	{
       
   335 						TUint32 wordValue = *aPixelIndex++;
       
   336 						wordValue |= *aPixelIndex++ << 8;
       
   337 						wordValue |= *aPixelIndex++ << 8*2;
       
   338 						wordValue |= *aPixelIndex++ << 8*3;
       
   339 						*dataPtr++ = wordValue;
       
   340 						}
       
   341 					while(dataPtr < dataPtrLimit);
       
   342 					break;
       
   343 
       
   344 				case 2:
       
   345 					do	{
       
   346 						TUint32 wordValue = *aPixelIndex++;
       
   347 						wordValue |= *aPixelIndex++ << 4;
       
   348 						wordValue |= *aPixelIndex++ << 4*2;
       
   349 						wordValue |= *aPixelIndex++ << 4*3;
       
   350 						wordValue |= *aPixelIndex++ << 4*4;
       
   351 						wordValue |= *aPixelIndex++ << 4*5;
       
   352 						wordValue |= *aPixelIndex++ << 4*6;
       
   353 						wordValue |= *aPixelIndex++ << 4*7;
       
   354 						*dataPtr++ = wordValue;
       
   355 						}
       
   356 					while(dataPtr < dataPtrLimit);
       
   357 					break;
       
   358 
       
   359 				case 1:
       
   360 					do	{
       
   361 						TUint32 wordValue = 0;
       
   362 						TInt pixelShift = 0;
       
   363 						do	{
       
   364 							TUint32 eightPixels = *aPixelIndex++;
       
   365 							eightPixels  |= *aPixelIndex++ << 2;
       
   366 							eightPixels  |= *aPixelIndex++ << 2*2;
       
   367 							eightPixels  |= *aPixelIndex++ << 2*3;
       
   368 							eightPixels  |= *aPixelIndex++ << 2*4;
       
   369 							eightPixels  |= *aPixelIndex++ << 2*5;
       
   370 							eightPixels  |= *aPixelIndex++ << 2*6;
       
   371 							eightPixels  |= *aPixelIndex++ << 2*7;
       
   372 							wordValue |= eightPixels << pixelShift;
       
   373 							pixelShift += 2*8;
       
   374 							}
       
   375 						while(pixelShift < 32);
       
   376 						*dataPtr++ = wordValue;
       
   377 						}
       
   378 					while(dataPtr < dataPtrLimit);
       
   379 					break;
       
   380 
       
   381 				case 0:
       
   382 					do	{
       
   383 						TUint32 wordValue = 0;
       
   384 						TInt pixelShift = 0;
       
   385 						do	{
       
   386 							TUint32 eightPixels = *aPixelIndex++;
       
   387 							eightPixels  |= *aPixelIndex++ << 1;
       
   388 							eightPixels  |= *aPixelIndex++ << 2;
       
   389 							eightPixels  |= *aPixelIndex++ << 3;
       
   390 							eightPixels  |= *aPixelIndex++ << 4;
       
   391 							eightPixels  |= *aPixelIndex++ << 5;
       
   392 							eightPixels  |= *aPixelIndex++ << 6;
       
   393 							eightPixels  |= *aPixelIndex++ << 7;
       
   394 							wordValue |= eightPixels << pixelShift;
       
   395 							pixelShift += 8;
       
   396 							}
       
   397 						while(pixelShift < 32);
       
   398 						*dataPtr++ = wordValue;
       
   399 						}
       
   400 					while(dataPtr < dataPtrLimit);
       
   401 					break;
       
   402 				default:
       
   403 					Panic(ECorrupt);
       
   404 					break;
       
   405 				}
       
   406 			}
       
   407 
       
   408 		// Do remaining pixels for colour depths that are < 32bit.
       
   409 
       
   410 		if(aNumberOfPixels)
       
   411 			{
       
   412 			TInt bitShiftLimit = aNumberOfPixels<<iBppShift;
       
   413 			TInt wordValue = *dataPtr;
       
   414 
       
   415 			wordValue &= 0xFFFFFFFF<<bitShiftLimit;		//zero the destination pixels we are about to write
       
   416 			do	{
       
   417 				wordValue |= *aPixelIndex++ << bitShift;
       
   418 				bitShift += bpp;
       
   419 				}
       
   420 			while(bitShift<bitShiftLimit);
       
   421 
       
   422 			*dataPtr = wordValue;
       
   423 			}
       
   424 
       
   425 		// Finished
       
   426 
       
   427 		iData.iWordPos = dataPtr;
       
   428 		iBitShift = bitShift;
       
   429 		}
       
   430 	else
       
   431 		{
       
   432 		TUint32* pixelPtrLimit = aPixelIndex + aNumberOfPixels;
       
   433 
       
   434 		TUint8* dataPtr = iData.iBytePos;
       
   435 
       
   436 		while (aPixelIndex < pixelPtrLimit)
       
   437 			{
       
   438 			TUint32 pixel = *aPixelIndex++;
       
   439 			*dataPtr++ = TUint8(pixel);
       
   440 			*dataPtr++ = TUint8(pixel >> 8);
       
   441 			*dataPtr++ = TUint8(pixel >> 16);
       
   442 			}
       
   443 
       
   444 		iData.iBytePos = dataPtr;
       
   445 		}
       
   446 
       
   447 	}
       
   448 
       
   449 /**
       
   450 Sets the current position in the bitmap to aPosition.
       
   451 
       
   452 @param  aPosition
       
   453         The position to move to.
       
   454 
       
   455 @return A boolean indicating if the position was out of bounds. EFalse if the position was out of 
       
   456         bounds, otherwise ETrue.
       
   457 */
       
   458 EXPORT_C TBool TImageBitmapUtil::SetPos(const TPoint& aPosition)
       
   459 	{
       
   460 	ASSERT(iBitmap);
       
   461 	ASSERT(iBase.iWordPos);
       
   462 
       
   463 	if (aPosition.iX < 0 || aPosition.iX >= iSize.iWidth || aPosition.iY < 0 || aPosition.iY >= iSize.iHeight)
       
   464 		return EFalse;
       
   465 
       
   466 	iData.iWordPos = iBase.iWordPos + (iScanlineWordLength * aPosition.iY);
       
   467 
       
   468 	if (iWordAccess)
       
   469 		{
       
   470 		iData.iWordPos += aPosition.iX >> iPixelShift;
       
   471 		iBitShift = (aPosition.iX << iBppShift) & 0x1f;
       
   472 		}
       
   473 	else
       
   474 		iData.iBytePos += (aPosition.iX << 1) + aPosition.iX;
       
   475 
       
   476 	iPosition = aPosition;
       
   477 	return ETrue;
       
   478 	}
       
   479