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 "openjpeg_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"
32 using boost::shared_ptr;
33 using boost::optional;
36 #define DCI_COEFFICIENT (48.0 / 52.37)
38 /** Convert an XYZ image to RGBA.
39 * @param xyz_image Image in XYZ.
40 * @param conversion Colour conversion to use.
41 * @param argb Buffer to fill with RGBA data. The format of the data is:
44 * Byte /- 0 -------|- 1 --------|- 2 --------|- 3 --------|- 4 --------|- 5 --------| ...
45 * |(0, 0) Blue|(0, 0)Green |(0, 0) Red |(0, 0) Alpha|(0, 1) Blue |(0, 1) Green| ...
48 * So that the first byte is the blue component of the pixel at x=0, y=0, the second
49 * is the green component, and so on.
51 * Lines are packed so that the second row directly follows the first.
55 boost::shared_ptr<const OpenJPEGImage> xyz_image,
56 ColourConversion const & conversion,
60 int const max_colour = pow (2, 12) - 1;
70 int* xyz_x = xyz_image->data (0);
71 int* xyz_y = xyz_image->data (1);
72 int* xyz_z = xyz_image->data (2);
74 double const * lut_in = conversion.out()->lut (12, false);
75 double const * lut_out = conversion.in()->lut (16, true);
76 boost::numeric::ublas::matrix<double> const matrix = conversion.xyz_to_rgb ();
78 int const height = xyz_image->size().height;
79 int const width = xyz_image->size().width;
81 for (int y = 0; y < height; ++y) {
82 uint8_t* argb_line = argb;
83 for (int x = 0; x < width; ++x) {
85 DCP_ASSERT (*xyz_x >= 0 && *xyz_y >= 0 && *xyz_z >= 0 && *xyz_x < 4096 && *xyz_y < 4096 && *xyz_z < 4096);
88 s.x = lut_in[*xyz_x++];
89 s.y = lut_in[*xyz_y++];
90 s.z = lut_in[*xyz_z++];
93 s.x /= DCI_COEFFICIENT;
94 s.y /= DCI_COEFFICIENT;
95 s.z /= DCI_COEFFICIENT;
98 d.r = ((s.x * matrix(0, 0)) + (s.y * matrix(0, 1)) + (s.z * matrix(0, 2)));
99 d.g = ((s.x * matrix(1, 0)) + (s.y * matrix(1, 1)) + (s.z * matrix(1, 2)));
100 d.b = ((s.x * matrix(2, 0)) + (s.y * matrix(2, 1)) + (s.z * matrix(2, 2)));
102 d.r = min (d.r, 1.0);
103 d.r = max (d.r, 0.0);
105 d.g = min (d.g, 1.0);
106 d.g = max (d.g, 0.0);
108 d.b = min (d.b, 1.0);
109 d.b = max (d.b, 0.0);
112 *argb_line++ = lut_out[int(rint(d.b * max_colour))] * 0xff;
113 *argb_line++ = lut_out[int(rint(d.g * max_colour))] * 0xff;
114 *argb_line++ = lut_out[int(rint(d.r * max_colour))] * 0xff;
118 /* 4 bytes per pixel */
123 /** Convert an XYZ image to 48bpp RGB.
124 * @param xyz_image Frame in XYZ.
125 * @param conversion Colour conversion to use.
126 * @param rgb Buffer to fill with RGB data. Format is packed RGB
127 * 16:16:16, 48bpp, 16R, 16G, 16B, with the 2-byte value for each
128 * R/G/B component stored as little-endian; i.e. AV_PIX_FMT_RGB48LE.
129 * @param stride Stride for RGB data in bytes.
130 * @param note Optional handler for any notes that may be made during the conversion (e.g. when clamping occurs).
134 shared_ptr<const OpenJPEGImage> xyz_image,
135 ColourConversion const & conversion,
138 optional<NoteHandler> note
149 /* These should be 12-bit values from 0-4095 */
150 int* xyz_x = xyz_image->data (0);
151 int* xyz_y = xyz_image->data (1);
152 int* xyz_z = xyz_image->data (2);
154 double const * lut_in = conversion.out()->lut (12, false);
155 double const * lut_out = conversion.in()->lut (16, true);
156 boost::numeric::ublas::matrix<double> const matrix = conversion.xyz_to_rgb ();
158 for (int y = 0; y < xyz_image->size().height; ++y) {
159 uint16_t* rgb_line = reinterpret_cast<uint16_t*> (rgb + y * stride);
160 for (int x = 0; x < xyz_image->size().width; ++x) {
166 if (cx < 0 || cx > 4095) {
168 note.get() (DCP_NOTE, String::compose ("XYZ value %1 out of range", cx));
170 cx = max (min (cx, 4095), 0);
173 if (cy < 0 || cy > 4095) {
175 note.get() (DCP_NOTE, String::compose ("XYZ value %1 out of range", cy));
177 cy = max (min (cy, 4095), 0);
180 if (cz < 0 || cz > 4095) {
182 note.get() (DCP_NOTE, String::compose ("XYZ value %1 out of range", cz));
184 cz = max (min (cz, 4095), 0);
193 s.x /= DCI_COEFFICIENT;
194 s.y /= DCI_COEFFICIENT;
195 s.z /= DCI_COEFFICIENT;
198 d.r = ((s.x * matrix(0, 0)) + (s.y * matrix(0, 1)) + (s.z * matrix(0, 2)));
199 d.g = ((s.x * matrix(1, 0)) + (s.y * matrix(1, 1)) + (s.z * matrix(1, 2)));
200 d.b = ((s.x * matrix(2, 0)) + (s.y * matrix(2, 1)) + (s.z * matrix(2, 2)));
202 d.r = min (d.r, 1.0);
203 d.r = max (d.r, 0.0);
205 d.g = min (d.g, 1.0);
206 d.g = max (d.g, 0.0);
208 d.b = min (d.b, 1.0);
209 d.b = max (d.b, 0.0);
211 *rgb_line++ = rint(lut_out[int(rint(d.r * 65535))] * 65535);
212 *rgb_line++ = rint(lut_out[int(rint(d.g * 65535))] * 65535);
213 *rgb_line++ = rint(lut_out[int(rint(d.b * 65535))] * 65535);
218 /** @param rgb RGB data; packed RGB 16:16:16, 48bpp, 16R, 16G, 16B,
219 * with the 2-byte value for each R/G/B component stored as
220 * little-endian; i.e. AV_PIX_FMT_RGB48LE.
221 * @param size of RGB image in pixels.
222 * @param stride of RGB data in pixels.
224 shared_ptr<dcp::OpenJPEGImage>
229 ColourConversion const & conversion,
230 optional<NoteHandler> note
233 shared_ptr<OpenJPEGImage> xyz (new OpenJPEGImage (size));
247 double const * lut_in = conversion.in()->lut (12, false);
248 double const * lut_out = conversion.out()->lut (16, true);
249 boost::numeric::ublas::matrix<double> const rgb_to_xyz = conversion.rgb_to_xyz ();
250 boost::numeric::ublas::matrix<double> const bradford = conversion.bradford ();
254 for (int y = 0; y < size.height; ++y) {
255 uint16_t const * p = reinterpret_cast<uint16_t const *> (rgb + y * stride);
256 for (int x = 0; x < size.width; ++x) {
258 /* In gamma LUT (converting 16-bit to 12-bit) */
259 s.r = lut_in[*p++ >> 4];
260 s.g = lut_in[*p++ >> 4];
261 s.b = lut_in[*p++ >> 4];
263 /* RGB to XYZ Matrix */
264 d.x = ((s.r * rgb_to_xyz(0, 0)) + (s.g * rgb_to_xyz(0, 1)) + (s.b * rgb_to_xyz(0, 2)));
265 d.y = ((s.r * rgb_to_xyz(1, 0)) + (s.g * rgb_to_xyz(1, 1)) + (s.b * rgb_to_xyz(1, 2)));
266 d.z = ((s.r * rgb_to_xyz(2, 0)) + (s.g * rgb_to_xyz(2, 1)) + (s.b * rgb_to_xyz(2, 2)));
268 e.x = ((d.x * bradford(0, 0)) + (d.y * bradford(0, 1)) + (d.z * bradford(0, 2)));
269 e.y = ((d.x * bradford(1, 0)) + (d.y * bradford(1, 1)) + (d.z * bradford(1, 2)));
270 e.z = ((d.x * bradford(2, 0)) + (d.y * bradford(2, 1)) + (d.z * bradford(2, 2)));
273 e.x = e.x * DCI_COEFFICIENT * 65535;
274 e.y = e.y * DCI_COEFFICIENT * 65535;
275 e.z = e.z * DCI_COEFFICIENT * 65535;
279 if (e.x < 0 || e.y < 0 || e.z < 0 || e.x > 65535 || e.y > 65535 || e.z > 65535) {
283 e.x = max (0.0, e.x);
284 e.y = max (0.0, e.y);
285 e.z = max (0.0, e.z);
286 e.x = min (65535.0, e.x);
287 e.y = min (65535.0, e.y);
288 e.z = min (65535.0, e.z);
291 xyz->data(0)[jn] = lut_out[int(rint(e.x))] * 4095;
292 xyz->data(1)[jn] = lut_out[int(rint(e.y))] * 4095;
293 xyz->data(2)[jn] = lut_out[int(rint(e.z))] * 4095;
299 if (clamped && note) {
300 note.get() (DCP_NOTE, String::compose ("%1 XYZ value(s) clamped", clamped));
307 /** @param xyz_16 XYZ image data in packed 16:16:16, 48bpp, 16X, 16Y,
308 * 16Z, with the 2-byte value for each X/Y/Z component stored as
311 shared_ptr<dcp::OpenJPEGImage>
312 dcp::xyz_to_xyz (uint8_t const * xyz_16, dcp::Size size, int stride)
314 shared_ptr<OpenJPEGImage> xyz_12 (new OpenJPEGImage (size));
317 for (int y = 0; y < size.height; ++y) {
318 uint16_t const * p = reinterpret_cast<uint16_t const *> (xyz_16 + y * stride);
319 for (int x = 0; x < size.width; ++x) {
320 /* Truncate 16-bit to 12-bit */
321 xyz_12->data(0)[jn] = *p++ >> 4;
322 xyz_12->data(1)[jn] = *p++ >> 4;
323 xyz_12->data(2)[jn] = *p++ >> 4;