HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ImfRgbaYca.h
Go to the documentation of this file.
1 //
2 // SPDX-License-Identifier: BSD-3-Clause
3 // Copyright (c) Contributors to the OpenEXR Project.
4 //
5 
6 #ifndef INCLUDED_IMF_RGBA_YCA_H
7 #define INCLUDED_IMF_RGBA_YCA_H
8 
9 //-----------------------------------------------------------------------------
10 //
11 // Conversion between RGBA (red, green, blue alpha)
12 // and YCA (luminance, subsampled chroma, alpha) data:
13 //
14 // Luminance, Y, is computed as a weighted sum of R, G, and B:
15 //
16 // Y = yw.x * R + yw.y * G + yw.z * B
17 //
18 // Function computeYw() computes a set of RGB-to-Y weights, yw,
19 // from a set of primary and white point chromaticities.
20 //
21 // Chroma, C, consists of two components, RY and BY:
22 //
23 // RY = (R - Y) / Y
24 // BY = (B - Y) / Y
25 //
26 // For efficiency, the x and y subsampling rates for chroma are
27 // hardwired to 2, and the chroma subsampling and reconstruction
28 // filters are fixed 27-pixel wide windowed sinc functions.
29 //
30 // Starting with an image that has RGBA data for all pixels,
31 //
32 // RGBA RGBA RGBA RGBA ... RGBA RGBA
33 // RGBA RGBA RGBA RGBA ... RGBA RGBA
34 // RGBA RGBA RGBA RGBA ... RGBA RGBA
35 // RGBA RGBA RGBA RGBA ... RGBA RGBA
36 // ...
37 // RGBA RGBA RGBA RGBA ... RGBA RGBA
38 // RGBA RGBA RGBA RGBA ... RGBA RGBA
39 //
40 // function RGBAtoYCA() converts the pixels to YCA format:
41 //
42 // YCA YCA YCA YCA ... YCA YCA
43 // YCA YCA YCA YCA ... YCA YCA
44 // YCA YCA YCA YCA ... YCA YCA
45 // YCA YCA YCA YCA ... YCA YCA
46 // ...
47 // YCA YCA YCA YCA ... YCA YCA
48 // YCA YCA YCA YCA ... YCA YCA
49 //
50 // Next, decimateChomaHoriz() eliminates the chroma values from
51 // the odd-numbered pixels in every scan line:
52 //
53 // YCA YA YCA YA ... YCA YA
54 // YCA YA YCA YA ... YCA YA
55 // YCA YA YCA YA ... YCA YA
56 // YCA YA YCA YA ... YCA YA
57 // ...
58 // YCA YA YCA YA ... YCA YA
59 // YCA YA YCA YA ... YCA YA
60 //
61 // decimateChromaVert() eliminates all chroma values from the
62 // odd-numbered scan lines:
63 //
64 // YCA YA YCA YA ... YCA YA
65 // YA YA YA YA ... YA YA
66 // YCA YA YCA YA ... YCA YA
67 // YA YA YA YA ... YA YA
68 // ...
69 // YCA YA YCA YA ... YCA YA
70 // YA YA YA YA ... YA YA
71 //
72 // Finally, roundYCA() reduces the precision of the luminance
73 // and chroma values so that the pixel data shrink more when
74 // they are saved in a compressed file.
75 //
76 // The output of roundYCA() can be converted back to a set
77 // of RGBA pixel data that is visually very similar to the
78 // original RGBA image, by calling reconstructChromaHoriz(),
79 // reconstructChromaVert(), YCAtoRGBA(), and finally
80 // fixSaturation().
81 //
82 //-----------------------------------------------------------------------------
83 
84 #include "ImfExport.h"
85 #include "ImfNamespace.h"
86 
87 #include "ImfChromaticities.h"
88 #include "ImfRgba.h"
89 
91 
92 namespace RgbaYca
93 {
94 
95 //
96 // Width of the chroma subsampling and reconstruction filters
97 //
98 
99 static const int N = 27;
100 static const int N2 = N / 2;
101 
102 //
103 // Convert a set of primary chromaticities into a set of weighting
104 // factors for computing a pixels's luminance, Y, from R, G and B
105 //
106 
109 
110 //
111 // Convert an array of n RGBA pixels, rgbaIn, to YCA (luminance/chroma/alpha):
112 //
113 // ycaOut[i].g = Y (rgbaIn[i]);
114 // ycaOut[i].r = RY (rgbaIn[i]);
115 // ycaOut[i].b = BY (rgbaIn[i]);
116 // ycaOut[i].a = aIsValid? rgbaIn[i].a: 1
117 //
118 // yw is a set of RGB-to-Y weighting factors, as computed by computeYw().
119 //
120 
122 void RGBAtoYCA (
123  const IMATH_NAMESPACE::V3f& yw,
124  int n,
125  bool aIsValid,
126  const Rgba rgbaIn[/*n*/],
127  Rgba ycaOut[/*n*/]);
128 
129 //
130 // Perform horizontal low-pass filtering and subsampling of
131 // the chroma channels of an array of n pixels. In order
132 // to avoid indexing off the ends of the input array during
133 // low-pass filtering, ycaIn must have N2 extra pixels at
134 // both ends. Before calling decimateChromaHoriz(), the extra
135 // pixels should be filled with copies of the first and last
136 // "real" input pixel.
137 //
138 
140 void
141 decimateChromaHoriz (int n, const Rgba ycaIn[/*n+N-1*/], Rgba ycaOut[/*n*/]);
142 
143 //
144 // Perform vertical chroma channel low-pass filtering and subsampling.
145 // N scan lines of input pixels are combined into a single scan line
146 // of output pixels.
147 //
148 
150 void decimateChromaVert (int n, const Rgba* const ycaIn[N], Rgba ycaOut[/*n*/]);
151 
152 //
153 // Round the luminance and chroma channels of an array of YCA
154 // pixels that has already been filtered and subsampled.
155 // The signifcands of the pixels' luminance and chroma values
156 // are rounded to roundY and roundC bits respectively.
157 //
158 
160 void roundYCA (
161  int n,
162  unsigned int roundY,
163  unsigned int roundC,
164  const Rgba ycaIn[/*n*/],
165  Rgba ycaOut[/*n*/]);
166 
167 //
168 // For a scan line that has valid chroma data only for every other pixel,
169 // reconstruct the missing chroma values.
170 //
171 
173 void
174 reconstructChromaHoriz (int n, const Rgba ycaIn[/*n+N-1*/], Rgba ycaOut[/*n*/]);
175 
176 //
177 // For a scan line that has only luminance and no valid chroma data,
178 // reconstruct chroma from the surronding N scan lines.
179 //
180 
182 void
183 reconstructChromaVert (int n, const Rgba* const ycaIn[N], Rgba ycaOut[/*n*/]);
184 
185 //
186 // Convert an array of n YCA (luminance/chroma/alpha) pixels to RGBA.
187 // This function is the inverse of RGBAtoYCA().
188 // yw is a set of RGB-to-Y weighting factors, as computed by computeYw().
189 //
190 
192 void YCAtoRGBA (
193  const IMATH_NAMESPACE::V3f& yw,
194  int n,
195  const Rgba ycaIn[/*n*/],
196  Rgba rgbaOut[/*n*/]);
197 
198 //
199 // Eliminate super-saturated pixels:
200 //
201 // Converting an image from RGBA to YCA, low-pass filtering chroma,
202 // and converting the result back to RGBA can produce pixels with
203 // super-saturated colors, where one or two of the RGB components
204 // become zero or negative. (The low-pass and reconstruction filters
205 // introduce some amount of ringing into the chroma components.
206 // This can lead to negative RGB values near high-contrast edges.)
207 //
208 // The fixSaturation() function finds super-saturated pixels and
209 // corrects them by desaturating their colors while maintaining
210 // their luminance. fixSaturation() takes three adjacent input
211 // scan lines, rgbaIn[0], rgbaIn[1], rgbaIn[2], adjusts the
212 // saturation of rgbaIn[1], and stores the result in rgbaOut.
213 //
214 
216 void fixSaturation (
217  const IMATH_NAMESPACE::V3f& yw,
218  int n,
219  const Rgba* const rgbaIn[3],
220  Rgba rgbaOut[/*n*/]);
221 
222 } // namespace RgbaYca
224 
225 #endif
IMF_EXPORT void decimateChromaVert(int n, const Rgba *const ycaIn[N], Rgba ycaOut[])
#define OPENEXR_IMF_INTERNAL_NAMESPACE_HEADER_EXIT
Definition: ImfNamespace.h:83
IMF_EXPORT void decimateChromaHoriz(int n, const Rgba ycaIn[], Rgba ycaOut[])
GLdouble n
Definition: glcorearb.h:2008
IMF_EXPORT void RGBAtoYCA(const IMATH_NAMESPACE::V3f &yw, int n, bool aIsValid, const Rgba rgbaIn[], Rgba ycaOut[])
IMF_EXPORT void fixSaturation(const IMATH_NAMESPACE::V3f &yw, int n, const Rgba *const rgbaIn[3], Rgba rgbaOut[])
#define IMF_EXPORT
Definition: ImfExport.h:54
IMF_EXPORT void reconstructChromaHoriz(int n, const Rgba ycaIn[], Rgba ycaOut[])
Vec3< float > V3f
Vec3 of float.
Definition: ImathVec.h:849
IMF_EXPORT void YCAtoRGBA(const IMATH_NAMESPACE::V3f &yw, int n, const Rgba ycaIn[], Rgba rgbaOut[])
IMF_EXPORT IMATH_NAMESPACE::V3f computeYw(const Chromaticities &cr)
#define OPENEXR_IMF_INTERNAL_NAMESPACE_HEADER_ENTER
Definition: ImfNamespace.h:80
Definition: ImfRgba.h:26
IMF_EXPORT void reconstructChromaVert(int n, const Rgba *const ycaIn[N], Rgba ycaOut[])
IMF_EXPORT void roundYCA(int n, unsigned int roundY, unsigned int roundC, const Rgba ycaIn[], Rgba ycaOut[])