sbsv2/raptor/test/smoke_suite/test_resources/mmp/mmp2/src/ImageUtils.h
changeset 3 e1eecf4d390d
equal deleted inserted replaced
1:be27ed110b50 3:e1eecf4d390d
       
     1 /*
       
     2 * Copyright (c) 2002-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of the License "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description: 
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #ifndef __ImageUtils_h
       
    20 #define __ImageUtils_h
       
    21 
       
    22 /*Template class CleanupResetAndDestroy
       
    23  *
       
    24  * Shamelessly copied from CleanupClose to clean up
       
    25  * the array of implementation information from the cleanup stack.
       
    26  */
       
    27 
       
    28 template <class T>
       
    29 class CleanupResetAndDestroy
       
    30 	{
       
    31 public:
       
    32 	inline static void PushL(T& aRef);
       
    33 private:
       
    34 	static void ResetAndDestroy(TAny *aPtr);
       
    35 	};
       
    36 template <class T>
       
    37 inline void CleanupResetAndDestroyPushL(T& aRef);
       
    38 
       
    39 
       
    40 template <class T>
       
    41 inline void CleanupResetAndDestroy<T>::PushL(T& aRef)
       
    42 	{CleanupStack::PushL(TCleanupItem(&ResetAndDestroy,&aRef));}
       
    43 template <class T>
       
    44 void CleanupResetAndDestroy<T>::ResetAndDestroy(TAny *aPtr)
       
    45 	{(STATIC_CAST(T*,aPtr))->ResetAndDestroy();}
       
    46 template <class T>
       
    47 inline void CleanupResetAndDestroyPushL(T& aRef)
       
    48 	{CleanupResetAndDestroy<T>::PushL(aRef);}
       
    49 
       
    50 //
       
    51 // PtrReadUtil - utility class with methods for standard 
       
    52 //            reading stuff from a TUint8* string
       
    53 //
       
    54 
       
    55 class PtrReadUtil
       
    56 	{
       
    57 public:
       
    58 	// This calls decode from TUint8*
       
    59 	static TInt8 ReadInt8(const TUint8* aPtr);
       
    60 	static TUint8 ReadUint8(const TUint8* aPtr);
       
    61 	static TInt16 ReadInt16(const TUint8* aPtr);
       
    62 	static TInt16 ReadBigEndianInt16(const TUint8* aPtr);
       
    63 	static TUint16 ReadUint16(const TUint8* aPtr);
       
    64 	static TUint16 ReadBigEndianUint16(const TUint8* aPtr);
       
    65 	static TInt32 ReadInt32(const TUint8* aPtr);
       
    66 	static TInt32 ReadBigEndianInt32(const TUint8* aPtr);
       
    67 	static TUint32 ReadUint32(const TUint8* aPtr);
       
    68 	static TUint32 ReadBigEndianUint32(const TUint8* aPtr);
       
    69 	// these calls also increment the pointer
       
    70 	static TInt8 ReadInt8Inc(const TUint8*& aPtr);
       
    71 	static TUint8 ReadUint8Inc(const TUint8*& aPtr);
       
    72 	static TInt16 ReadInt16Inc(const TUint8*& aPtr);
       
    73 	static TInt16 ReadBigEndianInt16Inc(const TUint8*& aPtr);
       
    74 	static TUint16 ReadUint16Inc(const TUint8*& aPtr);
       
    75 	static TUint16 ReadBigEndianUint16Inc(const TUint8*& aPtr);
       
    76 	static TInt32 ReadInt32Inc(const TUint8*& aPtr);
       
    77 	static TInt32 ReadBigEndianInt32Inc(const TUint8*& aPtr);
       
    78 	static TUint32 ReadUint32Inc(const TUint8*& aPtr);
       
    79 	static TUint32 ReadBigEndianUint32Inc(const TUint8*& aPtr);
       
    80 	};
       
    81 
       
    82 inline TUint8 PtrReadUtil::ReadUint8(const TUint8* aPtr)
       
    83 	{
       
    84 	return *aPtr ;
       
    85 	}
       
    86 
       
    87 inline TInt8 PtrReadUtil::ReadInt8(const TUint8* aPtr)
       
    88 	{
       
    89 	return TInt8(ReadUint8(aPtr));
       
    90 	}
       
    91 
       
    92 inline TUint16 PtrReadUtil::ReadUint16(const TUint8* aPtr)
       
    93 	{
       
    94 	return TUint16(aPtr[0] | (aPtr[1]<<8));
       
    95 	}
       
    96 
       
    97 inline TInt16 PtrReadUtil::ReadInt16(const TUint8* aPtr)
       
    98 	{
       
    99 	return TInt16(ReadUint16(aPtr));
       
   100 	}
       
   101 
       
   102 inline TUint32 PtrReadUtil::ReadUint32(const TUint8* aPtr)
       
   103 	{
       
   104 	return TUint32(aPtr[0] | (aPtr[1]<<8) | (aPtr[2]<<16) | (aPtr[3]<<24));
       
   105 	}
       
   106 
       
   107 inline TInt32 PtrReadUtil::ReadInt32(const TUint8* aPtr)
       
   108 	{
       
   109 	return TInt32(ReadUint32(aPtr));
       
   110 	}
       
   111 
       
   112 inline TUint16 PtrReadUtil::ReadBigEndianUint16(const TUint8* aPtr)
       
   113 	{
       
   114 	return TUint16((aPtr[0]<<8) | aPtr[1]);
       
   115 	}
       
   116 
       
   117 inline TInt16 PtrReadUtil::ReadBigEndianInt16(const TUint8* aPtr)
       
   118 	{
       
   119 	return TInt16(ReadBigEndianUint16(aPtr));
       
   120 	}
       
   121 
       
   122 inline TUint32 PtrReadUtil::ReadBigEndianUint32(const TUint8* aPtr)
       
   123 	{
       
   124 	return TUint32((aPtr[0]<<24) | (aPtr[1]<<16) | (aPtr[2]<<8) | aPtr[3]);
       
   125 	}
       
   126 
       
   127 inline TInt32 PtrReadUtil::ReadBigEndianInt32(const TUint8* aPtr)
       
   128 	{
       
   129 	return TInt32(ReadBigEndianInt32(aPtr));
       
   130 	}
       
   131 
       
   132 inline TInt8 PtrReadUtil::ReadInt8Inc(const TUint8*& aPtr)
       
   133 	{
       
   134 	TInt8 result = ReadInt8(aPtr);
       
   135 	aPtr += 1;
       
   136 	return result;
       
   137 	}
       
   138 
       
   139 inline TUint8 PtrReadUtil::ReadUint8Inc(const TUint8*& aPtr)
       
   140 	{
       
   141 	TUint8 result = ReadUint8(aPtr);
       
   142 	aPtr += 1;
       
   143 	return result;
       
   144 	}
       
   145 
       
   146 inline TInt16 PtrReadUtil::ReadInt16Inc(const TUint8*& aPtr)
       
   147 	{
       
   148 	TInt16 result = ReadInt16(aPtr);
       
   149 	aPtr += 2;
       
   150 	return result;
       
   151 	}
       
   152 
       
   153 inline TUint16 PtrReadUtil::ReadUint16Inc(const TUint8*& aPtr)
       
   154 	{
       
   155 	TUint16 result = ReadUint16(aPtr);
       
   156 	aPtr += 2;
       
   157 	return result;
       
   158 	}
       
   159 
       
   160 inline TInt16 PtrReadUtil::ReadBigEndianInt16Inc(const TUint8*& aPtr)
       
   161 	{
       
   162 	TInt16 result = ReadBigEndianInt16(aPtr);
       
   163 	aPtr += 2;
       
   164 	return result;
       
   165 	}
       
   166 
       
   167 inline TUint16 PtrReadUtil::ReadBigEndianUint16Inc(const TUint8*& aPtr)
       
   168 	{
       
   169 	TUint16 result = ReadBigEndianUint16(aPtr);
       
   170 	aPtr += 2;
       
   171 	return result;
       
   172 	}
       
   173 
       
   174 inline TInt32 PtrReadUtil::ReadInt32Inc(const TUint8*& aPtr)
       
   175 	{
       
   176 	TInt32 result = ReadInt32(aPtr);
       
   177 	aPtr += 4;
       
   178 	return result;
       
   179 	}
       
   180 
       
   181 inline TUint32 PtrReadUtil::ReadUint32Inc(const TUint8*& aPtr)
       
   182 	{
       
   183 	TUint32 result = ReadUint32(aPtr);
       
   184 	aPtr += 4;
       
   185 	return result;
       
   186 	}
       
   187 
       
   188 inline TInt32 PtrReadUtil::ReadBigEndianInt32Inc(const TUint8*& aPtr)
       
   189 	{
       
   190 	TInt32 result = ReadBigEndianInt32(aPtr);
       
   191 	aPtr += 4;
       
   192 	return result;
       
   193 	}
       
   194 
       
   195 inline TUint32 PtrReadUtil::ReadBigEndianUint32Inc(const TUint8*& aPtr)
       
   196 	{
       
   197 	TUint32 result = ReadBigEndianUint32(aPtr);
       
   198 	aPtr += 4;
       
   199 	return result;
       
   200 	}
       
   201 
       
   202 class PtrWriteUtil
       
   203 	{
       
   204 public:
       
   205 	static void WriteInt8(TUint8* aPtr, TInt aData);
       
   206 	static void WriteInt16(TUint8* aPtr, TInt aData);
       
   207 	static void WriteInt32(TUint8* aPtr, TInt aData);
       
   208 	// Big endian version
       
   209 	static void WriteBigEndianInt32(TUint8* aPtr, TInt32 aData);
       
   210 	static void WriteBigEndianInt16(TUint8* aPtr, TInt aData);
       
   211 	};
       
   212 
       
   213 inline void PtrWriteUtil::WriteInt8(TUint8* aPtr, TInt aData)
       
   214 	{
       
   215 	aPtr[0] = TUint8(aData);
       
   216 	}
       
   217 
       
   218 inline void PtrWriteUtil::WriteInt16(TUint8* aPtr, TInt aData)
       
   219 	{
       
   220 	aPtr[0] = TUint8(aData);
       
   221 	aPtr[1] = TUint8(aData>>8);
       
   222 	}
       
   223 
       
   224 inline void PtrWriteUtil::WriteInt32(TUint8* aPtr, TInt aData)
       
   225 	{
       
   226 	aPtr[0] = TUint8(aData);
       
   227 	aPtr[1] = TUint8(aData>>8);
       
   228 	aPtr[2] = TUint8(aData>>16);
       
   229 	aPtr[3] = TUint8(aData>>24);
       
   230 	}
       
   231 
       
   232 inline void PtrWriteUtil::WriteBigEndianInt32(TUint8* aPtr, TInt32 aData)
       
   233 	{
       
   234 	aPtr[0] = TUint8(aData>>24);
       
   235 	aPtr[1] = TUint8(aData>>16);
       
   236 	aPtr[2] = TUint8(aData>>8);
       
   237 	aPtr[3] = TUint8(aData);
       
   238 	}
       
   239 
       
   240 inline void PtrWriteUtil::WriteBigEndianInt16(TUint8* aPtr, TInt aData)
       
   241 	{
       
   242 	aPtr[0] = TUint8(aData>>8);
       
   243 	aPtr[1] = TUint8(aData);
       
   244 	}
       
   245 
       
   246 class ColorCcomponent
       
   247 	{
       
   248 public:
       
   249 	static TInt ClampColorComponent(TInt value);
       
   250 	};
       
   251 
       
   252 inline TInt ColorCcomponent::ClampColorComponent(TInt value)
       
   253 	{
       
   254 	return (value < 0) ? 0 : (value > 255) ? 255 : value;
       
   255 	}
       
   256 
       
   257 
       
   258 //
       
   259 // The following routines have been copied from Graphics subsystem.
       
   260 // They deal with alpha to premultiplied alpha and viceversa conversions.
       
   261 // The original files are: blendingalgorithms.h and blendingalgorithms.inl
       
   262 //
       
   263 
       
   264 const TUint32 KRBMask = 0x00ff00ff;
       
   265 const TUint32 KAGMask = 0xff00ff00;
       
   266 const TUint32 KGMask  = 0x0000ff00;
       
   267 const TUint32 KAMask  = 0xff000000;
       
   268 const TUint32 KRBBias = 0x00800080;
       
   269 const TUint32 KGBias  = 0x00008000;
       
   270 
       
   271 
       
   272 /**
       
   273 Premultiplies the color channel values with the Alpha channel value.
       
   274 Alpha value remains unchanged. An approximation is used in the operation where the division
       
   275 by 255 is approximated by a shift-by-8-bits operation (i.e. division by 256).
       
   276 @param	aPixel	The 32 bit pixel value to be pre-multiplied.
       
   277 @return	The PMA value.
       
   278 @internalTechnology
       
   279 @released
       
   280 */
       
   281 inline TUint32 NonPMA2PMAPixel(TUint32 aPixel)
       
   282 	{
       
   283 	TUint8 tA = (TUint8)(aPixel >> 24);
       
   284 	if (tA==0)
       
   285 		{ 
       
   286 		return 0;
       
   287 		}
       
   288 	if (tA==0xff) 
       
   289 		{
       
   290 		return aPixel;
       
   291 		}
       
   292 
       
   293 	// Use a bias value of 128 rather than 255, but also add 1/256 of the numerator 
       
   294 	// before dividing the sum by 256.
       
   295 
       
   296 	TUint32 scaledRB = (aPixel & KRBMask) * tA + KRBBias;
       
   297 	scaledRB = (scaledRB + ( (scaledRB >> 8) & KRBMask) ) >> 8;
       
   298 	TUint32 scaledG = (aPixel & KGMask ) * tA + KGBias;
       
   299 	scaledG = (scaledG + (scaledG >> 8)) >> 8;
       
   300 	
       
   301 	return (aPixel & KAMask) | (scaledRB & KRBMask) | (scaledG & KGMask);
       
   302 	}
       
   303 
       
   304 
       
   305 /**
       
   306 Divives the PMA pixel color channels with the Alpha value, to convert them to non-PMA format.
       
   307 Alpha value remains unchanged.
       
   308 @param	aPixel	the premultiplied 32 bit pixel value.
       
   309 @param	aNormTable	The lookup table used to do the normalisation (the table converts the division
       
   310 					to multiplication operation).
       
   311 					The table is usually obtainable by a call to the method:
       
   312 					PtrTo16BitNormalisationTable, which is defined in lookuptable.dll(.lib).
       
   313 					The lookup table for normalised alpha is compluted using this equation: 
       
   314 					Table[index] = (255*256) / index (where index is an 8 bit value).
       
   315 @return The NON-PMA 32 bit pixel value.
       
   316 @internalTechnology
       
   317 @released
       
   318 */
       
   319 inline TUint32 PMA2NonPMAPixel(TUint32 aPixel, const TUint16* aNormTable)
       
   320 	{
       
   321 	TUint8 alpha = (TUint8)(aPixel >> 24);
       
   322 	if (alpha==0)
       
   323 		{ 
       
   324 		return 0;
       
   325 		}
       
   326 	if (alpha==0xff) 
       
   327 		{
       
   328 		return aPixel;
       
   329 		}
       
   330 	TUint16 norm = aNormTable[alpha];
       
   331 	TUint32 norm_rb = (((aPixel & KRBMask) * norm) >> 8) & KRBMask;
       
   332 	TUint32 norm_g =  (((aPixel & KGMask ) * norm) >> 8) & KGMask;
       
   333 	
       
   334 	return ((aPixel & KAMask) | norm_rb | norm_g);
       
   335 	}
       
   336 
       
   337 
       
   338 /**
       
   339 In-place version of NonPMA2PMAPixel.
       
   340 @see NonPMA2PMAPixel
       
   341 @internalTechnology
       
   342 @released
       
   343 */
       
   344 inline void Convert2PMA(TUint32& aInOutValue)
       
   345 	{
       
   346 	aInOutValue = NonPMA2PMAPixel(aInOutValue);
       
   347 	}
       
   348 
       
   349 
       
   350 /**
       
   351 In-place version of PMA2NonPMAPixel
       
   352 @see PMA2NonPMAPixel
       
   353 @internalTechnology
       
   354 @released
       
   355 */
       
   356 inline void Convert2NonPMA(TUint32& aInOutValue, const TUint16* aNormTable)
       
   357 	{
       
   358 	aInOutValue = PMA2NonPMAPixel(aInOutValue, aNormTable);
       
   359 	}
       
   360 
       
   361 
       
   362 #endif  // __ImageUtils_h