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