1 //////////////////////////////////////////////////////////////////////////////
2 //
3 // Copyright (c) 2004, Industrial Light & Magic, a division of Lucasfilm
4 // Entertainment Company Ltd. Portions contributed and copyright held by
5 // others as indicated. All rights reserved.
6 //
7 // Redistribution and use in source and binary forms, with or without
8 // modification, are permitted provided that the following conditions are
9 // met:
10 //
11 // * Redistributions of source code must retain the above
12 // copyright notice, this list of conditions and the following
13 // disclaimer.
14 //
15 // * Redistributions in binary form must reproduce the above
16 // copyright notice, this list of conditions and the following
17 // disclaimer in the documentation and/or other materials provided with
18 // the distribution.
19 //
20 // * Neither the name of Industrial Light & Magic nor the names of
21 // any other contributors to this software may be used to endorse or
22 // promote products derived from this software without specific prior
23 // written permission.
24 //
25 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
26 // IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
27 // THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
29 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 //
37 //////////////////////////////////////////////////////////////////////////////
38
39 //-----------------------------------------------------------------------------
40 //
41 // Conversion between RGBA and YCA data.
42 //
43 //-----------------------------------------------------------------------------
44
45 #include <ImfRgbaYca.h>
46 #include <assert.h>
47 #include <algorithm>
48
49 using namespace IMATH_NAMESPACE;
50 using namespace std;
51 #include "ImfNamespace.h"
52
53 OPENEXR_IMF_INTERNAL_NAMESPACE_SOURCE_ENTER
54
55 namespace RgbaYca {
56
57
58 V3f
computeYw(const Chromaticities & cr)59 computeYw (const Chromaticities &cr)
60 {
61 M44f m = RGBtoXYZ (cr, 1);
62 return V3f (m[0][1], m[1][1], m[2][1]) / (m[0][1] + m[1][1] + m[2][1]);
63 }
64
65
66 void
RGBAtoYCA(const V3f & yw,int n,bool aIsValid,const Rgba rgbaIn[],Rgba ycaOut[])67 RGBAtoYCA (const V3f &yw,
68 int n,
69 bool aIsValid,
70 const Rgba rgbaIn[/*n*/],
71 Rgba ycaOut[/*n*/])
72 {
73 for (int i = 0; i < n; ++i)
74 {
75 Rgba in = rgbaIn[i];
76 Rgba &out = ycaOut[i];
77
78 //
79 // Conversion to YCA and subsequent chroma subsampling
80 // work only if R, G and B are finite and non-negative.
81 //
82
83 if (!in.r.isFinite() || in.r < 0)
84 in.r = 0;
85
86 if (!in.g.isFinite() || in.g < 0)
87 in.g = 0;
88
89 if (!in.b.isFinite() || in.b < 0)
90 in.b = 0;
91
92 if (in.r == in.g && in.g == in.b)
93 {
94 //
95 // Special case -- R, G and B are equal. To avoid rounding
96 // errors, we explicitly set the output luminance channel
97 // to G, and the chroma channels to 0.
98 //
99 // The special cases here and in YCAtoRGBA() ensure that
100 // converting black-and white images from RGBA to YCA and
101 // back is lossless.
102 //
103
104 out.r = 0;
105 out.g = in.g;
106 out.b = 0;
107 }
108 else
109 {
110 out.g = in.r * yw.x + in.g * yw.y + in.b * yw.z;
111
112 float Y = out.g;
113
114 if (abs (in.r - Y) < HALF_MAX * Y)
115 out.r = (in.r - Y) / Y;
116 else
117 out.r = 0;
118
119 if (abs (in.b - Y) < HALF_MAX * Y)
120 out.b = (in.b - Y) / Y;
121 else
122 out.b = 0;
123 }
124
125 if (aIsValid)
126 out.a = in.a;
127 else
128 out.a = 1;
129 }
130 }
131
132
133 void
decimateChromaHoriz(int n,const Rgba ycaIn[],Rgba ycaOut[])134 decimateChromaHoriz (int n,
135 const Rgba ycaIn[/*n+N-1*/],
136 Rgba ycaOut[/*n*/])
137 {
138 #ifdef DEBUG
139 assert (ycaIn != ycaOut);
140 #endif
141
142 int begin = N2;
143 int end = begin + n;
144
145 for (int i = begin, j = 0; i < end; ++i, ++j)
146 {
147 if ((j & 1) == 0)
148 {
149 ycaOut[j].r = ycaIn[i - 13].r * 0.001064f +
150 ycaIn[i - 11].r * -0.003771f +
151 ycaIn[i - 9].r * 0.009801f +
152 ycaIn[i - 7].r * -0.021586f +
153 ycaIn[i - 5].r * 0.043978f +
154 ycaIn[i - 3].r * -0.093067f +
155 ycaIn[i - 1].r * 0.313659f +
156 ycaIn[i ].r * 0.499846f +
157 ycaIn[i + 1].r * 0.313659f +
158 ycaIn[i + 3].r * -0.093067f +
159 ycaIn[i + 5].r * 0.043978f +
160 ycaIn[i + 7].r * -0.021586f +
161 ycaIn[i + 9].r * 0.009801f +
162 ycaIn[i + 11].r * -0.003771f +
163 ycaIn[i + 13].r * 0.001064f;
164
165 ycaOut[j].b = ycaIn[i - 13].b * 0.001064f +
166 ycaIn[i - 11].b * -0.003771f +
167 ycaIn[i - 9].b * 0.009801f +
168 ycaIn[i - 7].b * -0.021586f +
169 ycaIn[i - 5].b * 0.043978f +
170 ycaIn[i - 3].b * -0.093067f +
171 ycaIn[i - 1].b * 0.313659f +
172 ycaIn[i ].b * 0.499846f +
173 ycaIn[i + 1].b * 0.313659f +
174 ycaIn[i + 3].b * -0.093067f +
175 ycaIn[i + 5].b * 0.043978f +
176 ycaIn[i + 7].b * -0.021586f +
177 ycaIn[i + 9].b * 0.009801f +
178 ycaIn[i + 11].b * -0.003771f +
179 ycaIn[i + 13].b * 0.001064f;
180 }
181
182 ycaOut[j].g = ycaIn[i].g;
183 ycaOut[j].a = ycaIn[i].a;
184 }
185 }
186
187
188 void
decimateChromaVert(int n,const Rgba * const ycaIn[N],Rgba ycaOut[])189 decimateChromaVert (int n,
190 const Rgba * const ycaIn[N],
191 Rgba ycaOut[/*n*/])
192 {
193 for (int i = 0; i < n; ++i)
194 {
195 if ((i & 1) == 0)
196 {
197 ycaOut[i].r = ycaIn[ 0][i].r * 0.001064f +
198 ycaIn[ 2][i].r * -0.003771f +
199 ycaIn[ 4][i].r * 0.009801f +
200 ycaIn[ 6][i].r * -0.021586f +
201 ycaIn[ 8][i].r * 0.043978f +
202 ycaIn[10][i].r * -0.093067f +
203 ycaIn[12][i].r * 0.313659f +
204 ycaIn[13][i].r * 0.499846f +
205 ycaIn[14][i].r * 0.313659f +
206 ycaIn[16][i].r * -0.093067f +
207 ycaIn[18][i].r * 0.043978f +
208 ycaIn[20][i].r * -0.021586f +
209 ycaIn[22][i].r * 0.009801f +
210 ycaIn[24][i].r * -0.003771f +
211 ycaIn[26][i].r * 0.001064f;
212
213 ycaOut[i].b = ycaIn[ 0][i].b * 0.001064f +
214 ycaIn[ 2][i].b * -0.003771f +
215 ycaIn[ 4][i].b * 0.009801f +
216 ycaIn[ 6][i].b * -0.021586f +
217 ycaIn[ 8][i].b * 0.043978f +
218 ycaIn[10][i].b * -0.093067f +
219 ycaIn[12][i].b * 0.313659f +
220 ycaIn[13][i].b * 0.499846f +
221 ycaIn[14][i].b * 0.313659f +
222 ycaIn[16][i].b * -0.093067f +
223 ycaIn[18][i].b * 0.043978f +
224 ycaIn[20][i].b * -0.021586f +
225 ycaIn[22][i].b * 0.009801f +
226 ycaIn[24][i].b * -0.003771f +
227 ycaIn[26][i].b * 0.001064f;
228 }
229
230 ycaOut[i].g = ycaIn[13][i].g;
231 ycaOut[i].a = ycaIn[13][i].a;
232 }
233 }
234
235
236 void
roundYCA(int n,unsigned int roundY,unsigned int roundC,const Rgba ycaIn[],Rgba ycaOut[])237 roundYCA (int n,
238 unsigned int roundY,
239 unsigned int roundC,
240 const Rgba ycaIn[/*n*/],
241 Rgba ycaOut[/*n*/])
242 {
243 for (int i = 0; i < n; ++i)
244 {
245 ycaOut[i].g = ycaIn[i].g.round (roundY);
246 ycaOut[i].a = ycaIn[i].a;
247
248 if ((i & 1) == 0)
249 {
250 ycaOut[i].r = ycaIn[i].r.round (roundC);
251 ycaOut[i].b = ycaIn[i].b.round (roundC);
252 }
253 }
254 }
255
256
257 void
reconstructChromaHoriz(int n,const Rgba ycaIn[],Rgba ycaOut[])258 reconstructChromaHoriz (int n,
259 const Rgba ycaIn[/*n+N-1*/],
260 Rgba ycaOut[/*n*/])
261 {
262 #ifdef DEBUG
263 assert (ycaIn != ycaOut);
264 #endif
265
266 int begin = N2;
267 int end = begin + n;
268
269 for (int i = begin, j = 0; i < end; ++i, ++j)
270 {
271 if (j & 1)
272 {
273 ycaOut[j].r = ycaIn[i - 13].r * 0.002128f +
274 ycaIn[i - 11].r * -0.007540f +
275 ycaIn[i - 9].r * 0.019597f +
276 ycaIn[i - 7].r * -0.043159f +
277 ycaIn[i - 5].r * 0.087929f +
278 ycaIn[i - 3].r * -0.186077f +
279 ycaIn[i - 1].r * 0.627123f +
280 ycaIn[i + 1].r * 0.627123f +
281 ycaIn[i + 3].r * -0.186077f +
282 ycaIn[i + 5].r * 0.087929f +
283 ycaIn[i + 7].r * -0.043159f +
284 ycaIn[i + 9].r * 0.019597f +
285 ycaIn[i + 11].r * -0.007540f +
286 ycaIn[i + 13].r * 0.002128f;
287
288 ycaOut[j].b = ycaIn[i - 13].b * 0.002128f +
289 ycaIn[i - 11].b * -0.007540f +
290 ycaIn[i - 9].b * 0.019597f +
291 ycaIn[i - 7].b * -0.043159f +
292 ycaIn[i - 5].b * 0.087929f +
293 ycaIn[i - 3].b * -0.186077f +
294 ycaIn[i - 1].b * 0.627123f +
295 ycaIn[i + 1].b * 0.627123f +
296 ycaIn[i + 3].b * -0.186077f +
297 ycaIn[i + 5].b * 0.087929f +
298 ycaIn[i + 7].b * -0.043159f +
299 ycaIn[i + 9].b * 0.019597f +
300 ycaIn[i + 11].b * -0.007540f +
301 ycaIn[i + 13].b * 0.002128f;
302 }
303 else
304 {
305 ycaOut[j].r = ycaIn[i].r;
306 ycaOut[j].b = ycaIn[i].b;
307 }
308
309 ycaOut[j].g = ycaIn[i].g;
310 ycaOut[j].a = ycaIn[i].a;
311 }
312 }
313
314
315 void
reconstructChromaVert(int n,const Rgba * const ycaIn[N],Rgba ycaOut[])316 reconstructChromaVert (int n,
317 const Rgba * const ycaIn[N],
318 Rgba ycaOut[/*n*/])
319 {
320 for (int i = 0; i < n; ++i)
321 {
322 ycaOut[i].r = ycaIn[ 0][i].r * 0.002128f +
323 ycaIn[ 2][i].r * -0.007540f +
324 ycaIn[ 4][i].r * 0.019597f +
325 ycaIn[ 6][i].r * -0.043159f +
326 ycaIn[ 8][i].r * 0.087929f +
327 ycaIn[10][i].r * -0.186077f +
328 ycaIn[12][i].r * 0.627123f +
329 ycaIn[14][i].r * 0.627123f +
330 ycaIn[16][i].r * -0.186077f +
331 ycaIn[18][i].r * 0.087929f +
332 ycaIn[20][i].r * -0.043159f +
333 ycaIn[22][i].r * 0.019597f +
334 ycaIn[24][i].r * -0.007540f +
335 ycaIn[26][i].r * 0.002128f;
336
337 ycaOut[i].b = ycaIn[ 0][i].b * 0.002128f +
338 ycaIn[ 2][i].b * -0.007540f +
339 ycaIn[ 4][i].b * 0.019597f +
340 ycaIn[ 6][i].b * -0.043159f +
341 ycaIn[ 8][i].b * 0.087929f +
342 ycaIn[10][i].b * -0.186077f +
343 ycaIn[12][i].b * 0.627123f +
344 ycaIn[14][i].b * 0.627123f +
345 ycaIn[16][i].b * -0.186077f +
346 ycaIn[18][i].b * 0.087929f +
347 ycaIn[20][i].b * -0.043159f +
348 ycaIn[22][i].b * 0.019597f +
349 ycaIn[24][i].b * -0.007540f +
350 ycaIn[26][i].b * 0.002128f;
351
352 ycaOut[i].g = ycaIn[13][i].g;
353 ycaOut[i].a = ycaIn[13][i].a;
354 }
355 }
356
357
358 void
YCAtoRGBA(const IMATH_NAMESPACE::V3f & yw,int n,const Rgba ycaIn[],Rgba rgbaOut[])359 YCAtoRGBA (const IMATH_NAMESPACE::V3f &yw,
360 int n,
361 const Rgba ycaIn[/*n*/],
362 Rgba rgbaOut[/*n*/])
363 {
364 for (int i = 0; i < n; ++i)
365 {
366 const Rgba &in = ycaIn[i];
367 Rgba &out = rgbaOut[i];
368
369 if (in.r == 0 && in.b == 0)
370 {
371 //
372 // Special case -- both chroma channels are 0. To avoid
373 // rounding errors, we explicitly set the output R, G and B
374 // channels equal to the input luminance.
375 //
376 // The special cases here and in RGBAtoYCA() ensure that
377 // converting black-and white images from RGBA to YCA and
378 // back is lossless.
379 //
380
381 out.r = in.g;
382 out.g = in.g;
383 out.b = in.g;
384 out.a = in.a;
385 }
386 else
387 {
388 float Y = in.g;
389 float r = (in.r + 1) * Y;
390 float b = (in.b + 1) * Y;
391 float g = (Y - r * yw.x - b * yw.z) / yw.y;
392
393 out.r = r;
394 out.g = g;
395 out.b = b;
396 out.a = in.a;
397 }
398 }
399 }
400
401
402 namespace {
403
404 inline float
saturation(const Rgba & in)405 saturation (const Rgba &in)
406 {
407 float rgbMax = max (in.r, max (in.g, in.b));
408 float rgbMin = min (in.r, min (in.g, in.b));
409
410 if (rgbMax > 0)
411 return 1 - rgbMin / rgbMax;
412 else
413 return 0;
414 }
415
416
417 void
desaturate(const Rgba & in,float f,const V3f & yw,Rgba & out)418 desaturate (const Rgba &in, float f, const V3f &yw, Rgba &out)
419 {
420 float rgbMax = max (in.r, max (in.g, in.b));
421
422 out.r = max (float (rgbMax - (rgbMax - in.r) * f), 0.0f);
423 out.g = max (float (rgbMax - (rgbMax - in.g) * f), 0.0f);
424 out.b = max (float (rgbMax - (rgbMax - in.b) * f), 0.0f);
425 out.a = in.a;
426
427 float Yin = in.r * yw.x + in.g * yw.y + in.b * yw.z;
428 float Yout = out.r * yw.x + out.g * yw.y + out.b * yw.z;
429
430 if (Yout > 0)
431 {
432 out.r *= Yin / Yout;
433 out.g *= Yin / Yout;
434 out.b *= Yin / Yout;
435 }
436 }
437
438 } // namespace
439
440
441 void
fixSaturation(const IMATH_NAMESPACE::V3f & yw,int n,const Rgba * const rgbaIn[3],Rgba rgbaOut[])442 fixSaturation (const IMATH_NAMESPACE::V3f &yw,
443 int n,
444 const Rgba * const rgbaIn[3],
445 Rgba rgbaOut[/*n*/])
446 {
447 float neighborA2 = saturation (rgbaIn[0][0]);
448 float neighborA1 = neighborA2;
449
450 float neighborB2 = saturation (rgbaIn[2][0]);
451 float neighborB1 = neighborB2;
452
453 for (int i = 0; i < n; ++i)
454 {
455 float neighborA0 = neighborA1;
456 neighborA1 = neighborA2;
457
458 float neighborB0 = neighborB1;
459 neighborB1 = neighborB2;
460
461 if (i < n - 1)
462 {
463 neighborA2 = saturation (rgbaIn[0][i + 1]);
464 neighborB2 = saturation (rgbaIn[2][i + 1]);
465 }
466
467 //
468 // A0 A1 A2
469 // rgbaOut[i]
470 // B0 B1 B2
471 //
472
473 float sMean = min (1.0f, 0.25f * (neighborA0 + neighborA2 +
474 neighborB0 + neighborB2));
475
476 const Rgba &in = rgbaIn[1][i];
477 Rgba &out = rgbaOut[i];
478
479 float s = saturation (in);
480
481 if (s > sMean)
482 {
483 float sMax = min (1.0f, 1 - (1 - sMean) * 0.25f);
484
485 if (s > sMax)
486 {
487 desaturate (in, sMax / s, yw, out);
488 continue;
489 }
490 }
491
492 out = in;
493 }
494 }
495
496 } // namespace RgbaYca
497 OPENEXR_IMF_INTERNAL_NAMESPACE_SOURCE_EXIT
498