2 Copyright (C) 2013-2014 Carl Hetherington <cth@carlh.net>
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 #include "xyz_image.h"
22 #include "colour_matrix.h"
23 #include "colour_conversion.h"
24 #include "transfer_function.h"
25 #include "dcp_assert.h"
26 #include "compose.hpp"
31 using boost::shared_ptr;
32 using boost::optional;
35 #define DCI_COEFFICIENT (48.0 / 52.37)
37 /** Convert an XYZ image to RGBA.
38 * @param xyz_image Image in XYZ.
39 * @param conversion Colour conversion to use.
40 * @param argb Buffer to fill with RGBA data. The format of the data is:
43 * Byte /- 0 -------|- 1 --------|- 2 --------|- 3 --------|- 4 --------|- 5 --------| ...
44 * |(0, 0) Blue|(0, 0)Green |(0, 0) Red |(0, 0) Alpha|(0, 1) Blue |(0, 1) Green| ...
47 * So that the first byte is the blue component of the pixel at x=0, y=0, the second
48 * is the green component, and so on.
50 * Lines are packed so that the second row directly follows the first.
54 boost::shared_ptr<const XYZImage> xyz_image,
55 ColourConversion const & conversion,
59 int const max_colour = pow (2, 12) - 1;
69 int* xyz_x = xyz_image->data (0);
70 int* xyz_y = xyz_image->data (1);
71 int* xyz_z = xyz_image->data (2);
73 double const * lut_in = conversion.in()->lut (16, true);
74 double const * lut_out = conversion.out()->lut (12, false);
75 boost::numeric::ublas::matrix<double> const matrix = conversion.xyz_to_rgb ();
77 int const height = xyz_image->size().height;
78 int const width = xyz_image->size().width;
80 for (int y = 0; y < height; ++y) {
81 uint8_t* argb_line = argb;
82 for (int x = 0; x < width; ++x) {
84 DCP_ASSERT (*xyz_x >= 0 && *xyz_y >= 0 && *xyz_z >= 0 && *xyz_x < 4096 && *xyz_y < 4096 && *xyz_z < 4096);
87 s.x = lut_in[*xyz_x++];
88 s.y = lut_in[*xyz_y++];
89 s.z = lut_in[*xyz_z++];
92 s.x /= DCI_COEFFICIENT;
93 s.y /= DCI_COEFFICIENT;
94 s.z /= DCI_COEFFICIENT;
97 d.r = ((s.x * matrix(0, 0)) + (s.y * matrix(0, 1)) + (s.z * matrix(0, 2)));
98 d.g = ((s.x * matrix(1, 0)) + (s.y * matrix(1, 1)) + (s.z * matrix(1, 2)));
99 d.b = ((s.x * matrix(2, 0)) + (s.y * matrix(2, 1)) + (s.z * matrix(2, 2)));
101 d.r = min (d.r, 1.0);
102 d.r = max (d.r, 0.0);
104 d.g = min (d.g, 1.0);
105 d.g = max (d.g, 0.0);
107 d.b = min (d.b, 1.0);
108 d.b = max (d.b, 0.0);
111 *argb_line++ = lut_out[int(rint(d.b * max_colour))] * 0xff;
112 *argb_line++ = lut_out[int(rint(d.g * max_colour))] * 0xff;
113 *argb_line++ = lut_out[int(rint(d.r * max_colour))] * 0xff;
117 /* 4 bytes per pixel */
122 /** Convert an XYZ image to 48bpp RGB.
123 * @param xyz_image Frame in XYZ.
124 * @param conversion Colour conversion to use.
125 * @param rgb Buffer to fill with RGB data. Format is packed RGB
126 * 16:16:16, 48bpp, 16R, 16G, 16B, with the 2-byte value for each
127 * R/G/B component stored as little-endian; i.e. AV_PIX_FMT_RGB48LE.
128 * @param stride Stride for RGB data in bytes.
129 * @param note Optional handler for any notes that may be made during the conversion (e.g. when clamping occurs).
133 shared_ptr<const XYZImage> xyz_image,
134 ColourConversion const & conversion,
137 optional<NoteHandler> note
148 /* These should be 12-bit values from 0-4095 */
149 int* xyz_x = xyz_image->data (0);
150 int* xyz_y = xyz_image->data (1);
151 int* xyz_z = xyz_image->data (2);
153 double const * lut_in = conversion.in()->lut (12, true);
154 double const * lut_out = conversion.out()->lut (16, false);
155 boost::numeric::ublas::matrix<double> const matrix = conversion.xyz_to_rgb ();
157 for (int y = 0; y < xyz_image->size().height; ++y) {
158 uint16_t* rgb_line = reinterpret_cast<uint16_t*> (rgb + y * stride);
159 for (int x = 0; x < xyz_image->size().width; ++x) {
165 if (cx < 0 || cx > 4095) {
167 note.get() (DCP_NOTE, String::compose ("XYZ value %1 out of range", cx));
169 cx = max (min (cx, 4095), 0);
172 if (cy < 0 || cy > 4095) {
174 note.get() (DCP_NOTE, String::compose ("XYZ value %1 out of range", cy));
176 cy = max (min (cy, 4095), 0);
179 if (cz < 0 || cz > 4095) {
181 note.get() (DCP_NOTE, String::compose ("XYZ value %1 out of range", cz));
183 cz = max (min (cz, 4095), 0);
192 s.x /= DCI_COEFFICIENT;
193 s.y /= DCI_COEFFICIENT;
194 s.z /= DCI_COEFFICIENT;
197 d.r = ((s.x * matrix(0, 0)) + (s.y * matrix(0, 1)) + (s.z * matrix(0, 2)));
198 d.g = ((s.x * matrix(1, 0)) + (s.y * matrix(1, 1)) + (s.z * matrix(1, 2)));
199 d.b = ((s.x * matrix(2, 0)) + (s.y * matrix(2, 1)) + (s.z * matrix(2, 2)));
201 d.r = min (d.r, 1.0);
202 d.r = max (d.r, 0.0);
204 d.g = min (d.g, 1.0);
205 d.g = max (d.g, 0.0);
207 d.b = min (d.b, 1.0);
208 d.b = max (d.b, 0.0);
210 *rgb_line++ = rint(lut_out[int(rint(d.r * 65535))] * 65535);
211 *rgb_line++ = rint(lut_out[int(rint(d.g * 65535))] * 65535);
212 *rgb_line++ = rint(lut_out[int(rint(d.b * 65535))] * 65535);
217 /** @param rgb RGB data; packed RGB 16:16:16, 48bpp, 16R, 16G, 16B,
218 * with the 2-byte value for each R/G/B component stored as
219 * little-endian; i.e. AV_PIX_FMT_RGB48LE.
220 * @param size of RGB image in pixels.
221 * @param stride of RGB data in bytes.
223 shared_ptr<dcp::XYZImage>
228 ColourConversion const & conversion
231 shared_ptr<XYZImage> xyz (new XYZImage (size));
245 double const * lut_in = conversion.in()->lut (12, false);
246 double const * lut_out = conversion.out()->lut (16, true);
247 boost::numeric::ublas::matrix<double> const rgb_to_xyz = conversion.rgb_to_xyz ();
248 boost::numeric::ublas::matrix<double> const bradford = conversion.bradford ();
251 for (int y = 0; y < size.height; ++y) {
252 uint16_t const * p = reinterpret_cast<uint16_t const *> (rgb + y * stride);
253 for (int x = 0; x < size.width; ++x) {
255 /* In gamma LUT (converting 16-bit to 12-bit) */
256 s.r = lut_in[*p++ >> 4];
257 s.g = lut_in[*p++ >> 4];
258 s.b = lut_in[*p++ >> 4];
260 /* RGB to XYZ Matrix */
261 d.x = ((s.r * rgb_to_xyz(0, 0)) + (s.g * rgb_to_xyz(0, 1)) + (s.b * rgb_to_xyz(0, 2)));
262 d.y = ((s.r * rgb_to_xyz(1, 0)) + (s.g * rgb_to_xyz(1, 1)) + (s.b * rgb_to_xyz(1, 2)));
263 d.z = ((s.r * rgb_to_xyz(2, 0)) + (s.g * rgb_to_xyz(2, 1)) + (s.b * rgb_to_xyz(2, 2)));
265 e.x = ((d.x * bradford(0, 0)) + (d.y * bradford(0, 1)) + (d.z * bradford(0, 2)));
266 e.y = ((d.x * bradford(1, 0)) + (d.y * bradford(1, 1)) + (d.z * bradford(1, 2)));
267 e.z = ((d.x * bradford(2, 0)) + (d.y * bradford(2, 1)) + (d.z * bradford(2, 2)));
270 e.x = e.x * DCI_COEFFICIENT * 65535;
271 e.y = e.y * DCI_COEFFICIENT * 65535;
272 e.z = e.z * DCI_COEFFICIENT * 65535;
274 DCP_ASSERT (e.x >= 0 && e.x < 65536);
275 DCP_ASSERT (e.y >= 0 && e.y < 65536);
276 DCP_ASSERT (e.z >= 0 && e.z < 65536);
279 xyz->data(0)[jn] = lut_out[int(rint(e.x))] * 4095;
280 xyz->data(1)[jn] = lut_out[int(rint(e.y))] * 4095;
281 xyz->data(2)[jn] = lut_out[int(rint(e.z))] * 4095;
291 /** @param xyz_16 XYZ image data in packed 16:16:16, 48bpp, 16X, 16Y,
292 * 16Z, with the 2-byte value for each X/Y/Z component stored as
295 shared_ptr<dcp::XYZImage>
296 dcp::xyz_to_xyz (uint8_t const * xyz_16, dcp::Size size, int stride)
298 shared_ptr<XYZImage> xyz_12 (new XYZImage (size));
301 for (int y = 0; y < size.height; ++y) {
302 uint16_t const * p = reinterpret_cast<uint16_t const *> (xyz_16 + y * stride);
303 for (int x = 0; x < size.width; ++x) {
304 /* Truncate 16-bit to 12-bit */
305 xyz_12->data(0)[jn] = *p++ >> 4;
306 xyz_12->data(1)[jn] = *p++ >> 4;
307 xyz_12->data(2)[jn] = *p++ >> 4;