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