|
1 /* |
|
2 * Copyright (c) 2002 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 "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: Implementation of class CYuv2Rgb24. |
|
15 * YUV to EColor16M colorspace converter concrete classes |
|
16 * |
|
17 */ |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 // EXTERNAL RESOURCES |
|
24 |
|
25 |
|
26 // Include Files |
|
27 |
|
28 #include <e32math.h> |
|
29 #include "yuvconverter.h" |
|
30 #include "yuv2rgb24.h" |
|
31 #include "brightnesscontrast.h" |
|
32 |
|
33 |
|
34 // MEMBER FUNCTIONS |
|
35 |
|
36 |
|
37 //============================================================================= |
|
38 |
|
39 /* |
|
40 ----------------------------------------------------------------------------- |
|
41 |
|
42 CYuv2Rgb24 |
|
43 |
|
44 CYuv2Rgb24() |
|
45 |
|
46 Standard C++ constructor |
|
47 |
|
48 ----------------------------------------------------------------------------- |
|
49 */ |
|
50 |
|
51 CYuv2Rgb24::CYuv2Rgb24() |
|
52 { |
|
53 iRgbLookupTable = 0; |
|
54 iGamma = 65536; |
|
55 iBrightnessContrast = KMedBrightnessContrastIndex; |
|
56 |
|
57 } |
|
58 |
|
59 |
|
60 |
|
61 /* |
|
62 ----------------------------------------------------------------------------- |
|
63 |
|
64 CYuv2Rgb24 |
|
65 |
|
66 ~CYuv2Rgb24() |
|
67 |
|
68 Standard C++ destructor |
|
69 |
|
70 ----------------------------------------------------------------------------- |
|
71 */ |
|
72 |
|
73 CYuv2Rgb24::~CYuv2Rgb24() |
|
74 { |
|
75 User::Free(iRgbLookupTable); |
|
76 } |
|
77 |
|
78 |
|
79 |
|
80 /* |
|
81 ----------------------------------------------------------------------------- |
|
82 |
|
83 CYuv2Rgb24 |
|
84 |
|
85 ConstructL() |
|
86 |
|
87 Standard Symbian OS second-phase constructor. Initializes the object. |
|
88 |
|
89 ----------------------------------------------------------------------------- |
|
90 */ |
|
91 |
|
92 void CYuv2Rgb24::ConstructL(TUint aWidth, TUint aHeight, TUint aMaxWidth, TUint aMaxHeight) |
|
93 { |
|
94 // Remember the dimensions |
|
95 // __ASSERT_ALWAYS(((aWidth & 1) == 0) && ((aHeight & 1) == 0), |
|
96 // User::Leave(KErrArgument)); |
|
97 iWidth = aWidth; |
|
98 iHeight = aHeight; |
|
99 if ( iWidth > aMaxWidth ) { |
|
100 iCropWidth = (iWidth-aMaxWidth)/2; |
|
101 iWidth = aMaxWidth; |
|
102 } |
|
103 else { |
|
104 iCropWidth = 0; |
|
105 } |
|
106 if ( iHeight > aMaxHeight ) { |
|
107 iCropHeight = (iHeight-aMaxHeight)/2; |
|
108 iHeight = aMaxHeight; |
|
109 } |
|
110 else { |
|
111 iCropHeight = 0; |
|
112 } |
|
113 |
|
114 // Allocate the RGB saturate/gamma lookup table |
|
115 iRgbLookupTable = (TUint8*) User::AllocL(ESaturateLength); |
|
116 |
|
117 // Initialize brightness & contrast value, this will calculate the conversion table |
|
118 // Since this uses the median index, it makes no difference if the preferred |
|
119 // enhancement is this or gamma. Furthermore, changes to the value will be done using |
|
120 // the appropriate method. |
|
121 SetBrightnessContrast(KMaxBCInputIndex/2); |
|
122 } |
|
123 |
|
124 |
|
125 |
|
126 /* |
|
127 ----------------------------------------------------------------------------- |
|
128 |
|
129 CYuv2Rgb24 |
|
130 |
|
131 SetGamma() |
|
132 |
|
133 Sets the conversion gamma value and recalculates the look-up table |
|
134 |
|
135 ----------------------------------------------------------------------------- |
|
136 */ |
|
137 |
|
138 void CYuv2Rgb24::SetGamma(TInt aGamma) |
|
139 { |
|
140 TInt i, v; |
|
141 TReal vNorm; |
|
142 |
|
143 // Remember gamma and convert it to floating point |
|
144 iGamma = aGamma; |
|
145 TReal fGamma = TReal(iGamma) / TReal(65536); |
|
146 |
|
147 // Calculate table entries for all possible RGB values: |
|
148 for ( i = 0; i < ESaturateLength; i++ ) |
|
149 { |
|
150 // Actual RGB value for this table index |
|
151 v = i - ESaturateOffset; |
|
152 |
|
153 // Saturate if <0 or >255, otherwise calculate gamma |
|
154 if ( v < 0 ) |
|
155 v = 0; |
|
156 else if ( v > 255 ) |
|
157 v = 255; |
|
158 else |
|
159 { |
|
160 // Normalize v: |
|
161 vNorm = TReal(v) / TReal(255); |
|
162 |
|
163 // Gamma-correct: v = v ^ gamma |
|
164 Math::Pow(vNorm, vNorm, fGamma); |
|
165 |
|
166 // Scale back to [0..255] and clamp: |
|
167 vNorm = (TReal(255) * vNorm) + 0.5; |
|
168 v = (TInt) vNorm; |
|
169 if ( v < 0 ) v = 0; |
|
170 if ( v > 255 ) v = 255; |
|
171 } |
|
172 |
|
173 // 24bpp RGB has range [0..255] for all components, store to table: |
|
174 iRgbLookupTable[i] = (TUint8) v; |
|
175 } |
|
176 } |
|
177 |
|
178 /* |
|
179 ----------------------------------------------------------------------------- |
|
180 |
|
181 CYuv2Rgb24 |
|
182 |
|
183 SetBrightnessContrast() |
|
184 |
|
185 Sets the index to the predefined brightness&contrast lookup table |
|
186 (KBrightnessContrastEnhParam) and recalculates the RGB look-up table |
|
187 The algorithm was developed by IMAAMI for Kenny display. |
|
188 |
|
189 ----------------------------------------------------------------------------- |
|
190 */ |
|
191 void CYuv2Rgb24::SetBrightnessContrast(TInt aBCIndex) |
|
192 { |
|
193 TInt i, v; |
|
194 TReal vNorm; |
|
195 |
|
196 // Convert & remember brightness-contrast index. aBCIndex == 0 to KMaxBCInputIndex. |
|
197 iBrightnessContrast = (aBCIndex*KMaxBrightnessContrastIndex)/KMaxBCInputIndex; |
|
198 |
|
199 // Calculate table entries for all possible RGB values: |
|
200 for ( i = 0; i < ESaturateLength; i++ ) |
|
201 { |
|
202 // Actual RGB value for this table index |
|
203 v = 298 * (i - ESaturateOffset - 16) / 256; |
|
204 // (see Convert()) |
|
205 |
|
206 // Saturate if <0 or >255, otherwise calculate value |
|
207 if ( v < 0 ) |
|
208 v = 0; |
|
209 else if ( v > 255 ) |
|
210 v = 255; |
|
211 else |
|
212 { |
|
213 |
|
214 // Normalize v: |
|
215 vNorm = TReal(v) / TReal(255); |
|
216 |
|
217 vNorm = KBrightnessContrastEnhParam[iBrightnessContrast].a * vNorm + KBrightnessContrastEnhParam[iBrightnessContrast].b; |
|
218 if ( vNorm < 0 ) |
|
219 vNorm = 0; |
|
220 else if ( vNorm > 1 ) |
|
221 vNorm = 1; |
|
222 Math::Pow( vNorm, vNorm, KBrightnessContrastEnhParam[iBrightnessContrast].g ); |
|
223 |
|
224 // Scale back to [0..255] and clamp: |
|
225 vNorm = (TReal(255) * vNorm) + 0.5; |
|
226 v = (TInt) vNorm; |
|
227 if ( v < 0 ) v = 0; |
|
228 if ( v > 255 ) v = 255; |
|
229 } |
|
230 |
|
231 // 24bpp RGB has range [0..255] for all components, store to table: |
|
232 iRgbLookupTable[i] = (TUint8) v; |
|
233 } |
|
234 } |
|
235 |
|
236 |
|
237 /* |
|
238 ----------------------------------------------------------------------------- |
|
239 |
|
240 CYuv2Rgb24 |
|
241 |
|
242 Convert() |
|
243 |
|
244 Converts a YUV frame to a EColor16M frame |
|
245 |
|
246 ----------------------------------------------------------------------------- |
|
247 */ |
|
248 |
|
249 void CYuv2Rgb24::Convert(const TUint8 *aYBuf, const TUint8 *aUBuf, |
|
250 const TUint8 *aVBuf, |
|
251 TUint aBufWidth, TUint aBufHeight, |
|
252 TUint8 *aTarget, TUint aTargetScanlineLength) |
|
253 { |
|
254 TUint cols; |
|
255 TUint rows = iHeight; |
|
256 TUint8 *target; |
|
257 TUint8 *target2; |
|
258 const TUint8 *yb, *yb2; |
|
259 TInt rc, gc, bc; |
|
260 TInt y; |
|
261 TInt uval, vval; |
|
262 TUint8 val; |
|
263 |
|
264 |
|
265 __ASSERT_ALWAYS((aBufWidth >= iWidth) && (aBufHeight >= iHeight), |
|
266 User::Invariant()); |
|
267 |
|
268 // Cropping needed? |
|
269 if ( iCropWidth > 0 ) { |
|
270 //sets offset to buffers; from now on increments below will always result the same offset, since the increment is aBufWidth |
|
271 aYBuf += iCropWidth; |
|
272 aUBuf += iCropWidth/2; |
|
273 aVBuf += iCropWidth/2; |
|
274 } |
|
275 if ( iCropHeight > 0 ) { |
|
276 //skip lines on top |
|
277 aYBuf += iCropHeight*aBufWidth; |
|
278 aUBuf += (iCropHeight/2)*aBufWidth/2; |
|
279 aVBuf += (iCropHeight/2)*aBufWidth/2; |
|
280 } |
|
281 // We don't interpolate the chrominance values at all, since that way we |
|
282 // can save a lot of multiplications. This actually doesn't affect the |
|
283 // subjective picture quality much, if at all, with natural images. |
|
284 |
|
285 // Conversion is done 2x2 pixels at a time |
|
286 |
|
287 // Luminance-only conversion? |
|
288 if ( (aUBuf != NULL) && (aVBuf != NULL) ) |
|
289 { |
|
290 // Full conversion |
|
291 |
|
292 // Convert all rows, two at a time |
|
293 while ( rows ) |
|
294 { |
|
295 // Convert all pixels in this row, two at a time |
|
296 cols = iWidth; |
|
297 target = aTarget; |
|
298 target2 = aTarget + aTargetScanlineLength; |
|
299 yb = aYBuf; |
|
300 yb2 = aYBuf + aBufWidth; |
|
301 |
|
302 while ( cols ) |
|
303 { |
|
304 // Charles Poynton: Color FAQ |
|
305 // (http://www.inforamp.net/~poynton/ColorFAQ.html) |
|
306 // 30. How do I encode Y'CBCR components from computer R'G'B' ? |
|
307 |
|
308 // [normalized] |
|
309 // R = 1.1643828125 * (Y-16) + 1.59602734375 * (Cr-128) |
|
310 // G = 1.1643828125 * (Y-16) + -0.39178515625 * (Cb-128) + -0.81296875 * (Cr-128) |
|
311 // B = 1.1643828125 * (Y-16) + 2.01723046875 * (Cb-128) |
|
312 |
|
313 // We'll use fixed-point with 16 bits of fractional part for |
|
314 // accuracy. Besides, 24bpp RGB is not likely to be used in |
|
315 // low-CPU devices in the near future... |
|
316 |
|
317 // Red chrominance part for this 2x2 block: |
|
318 vval = ((TInt) aVBuf[0]) - 128; |
|
319 rc = 104597 * vval; |
|
320 |
|
321 // Green chrominance: |
|
322 uval = ((TInt) aUBuf[0]) - 128; |
|
323 gc = -25676*uval - 53279*vval; |
|
324 |
|
325 // Blue chrominance: |
|
326 bc = 132201 * uval; |
|
327 |
|
328 // Upper left pixel y part for all components: |
|
329 y = 76309 * (((TInt) yb[0]) - 16) + 32768; // round up |
|
330 |
|
331 // Calculate components and store: |
|
332 // Bitmap format: bbbbbbbb gggggggg rrrrrrrr |
|
333 target[0] = iRgbLookupTable[((y+bc) >> 16) + ESaturateOffset]; |
|
334 target[1] = iRgbLookupTable[((y+gc) >> 16) + ESaturateOffset]; |
|
335 target[2] = iRgbLookupTable[((y+rc) >> 16) + ESaturateOffset]; |
|
336 |
|
337 // Upper right pixel: |
|
338 y = 76309 * (((TInt) yb[1]) - 16) + 32768; |
|
339 target[3] = iRgbLookupTable[((y+bc) >> 16) + ESaturateOffset]; |
|
340 target[4] = iRgbLookupTable[((y+gc) >> 16) + ESaturateOffset]; |
|
341 target[5] = iRgbLookupTable[((y+rc) >> 16) + ESaturateOffset]; |
|
342 |
|
343 // Lower left: |
|
344 y = 76309 * (((TInt) yb2[0]) - 16) + 32768; |
|
345 target2[0] = iRgbLookupTable[((y+bc) >> 16) + ESaturateOffset]; |
|
346 target2[1] = iRgbLookupTable[((y+gc) >> 16) + ESaturateOffset]; |
|
347 target2[2] = iRgbLookupTable[((y+rc) >> 16) + ESaturateOffset]; |
|
348 |
|
349 // Lower right: |
|
350 y = 76309 * (((TInt) yb2[1]) - 16) + 32768; |
|
351 target2[3] = iRgbLookupTable[((y+bc) >> 16) + ESaturateOffset]; |
|
352 target2[4] = iRgbLookupTable[((y+gc) >> 16) + ESaturateOffset]; |
|
353 target2[5] = iRgbLookupTable[((y+rc) >> 16) + ESaturateOffset]; |
|
354 |
|
355 // Next two pixels: |
|
356 target += 6; |
|
357 target2 += 6; |
|
358 yb += 2; |
|
359 yb2 += 2; |
|
360 aUBuf++; |
|
361 aVBuf++; |
|
362 cols -= 2; |
|
363 } |
|
364 |
|
365 // Next rows |
|
366 rows -= 2; |
|
367 aYBuf += 2*aBufWidth; |
|
368 aUBuf += (aBufWidth - iWidth)/2; |
|
369 aVBuf += (aBufWidth - iWidth)/2; |
|
370 aTarget += 2*aTargetScanlineLength; |
|
371 } |
|
372 } |
|
373 else |
|
374 { |
|
375 // No chrominance given, do a luminance-only conversion |
|
376 |
|
377 // Convert all rows |
|
378 while ( rows ) |
|
379 { |
|
380 // Convert all pixels in this row, two at a time |
|
381 cols = iWidth; |
|
382 target = aTarget; |
|
383 |
|
384 while ( cols ) |
|
385 { |
|
386 // Do a pixel: |
|
387 y = 76309 * (((TInt) aYBuf[0]) - 16) + 32768; |
|
388 val = iRgbLookupTable[(y >> 16) + ESaturateOffset]; |
|
389 target[0] = val; |
|
390 target[1] = val; |
|
391 target[2] = val; |
|
392 |
|
393 // And another one: |
|
394 y = 76309 * (((TInt) aYBuf[1]) - 16) + 32768; |
|
395 val = iRgbLookupTable[(y >> 16) + ESaturateOffset]; |
|
396 target[3] = val; |
|
397 target[4] = val; |
|
398 target[5] = val; |
|
399 |
|
400 // Next two pixels: |
|
401 target += 6; |
|
402 aYBuf += 2; |
|
403 cols -= 2; |
|
404 } |
|
405 |
|
406 // Next row |
|
407 rows--; |
|
408 aYBuf += aBufWidth - iWidth; |
|
409 aTarget += aTargetScanlineLength; |
|
410 } |
|
411 } |
|
412 } |
|
413 |
|
414 |
|
415 |
|
416 |
|
417 |
|
418 // End of File |