No-op: whitespace.
[libdcp.git] / src / rgb_xyz.cc
1 /*
2     Copyright (C) 2013-2014 Carl Hetherington <cth@carlh.net>
3
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.
8
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.
13
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.
17
18 */
19
20 #include "rgb_xyz.h"
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"
27 #include <cmath>
28
29 using std::min;
30 using std::max;
31 using std::cout;
32 using boost::shared_ptr;
33 using boost::optional;
34 using namespace dcp;
35
36 #define DCI_COEFFICIENT (48.0 / 52.37)
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:
42  *
43  *  <pre>
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| ...
46  *  </pre>
47  *
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.
50  *
51  *  Lines are packed so that the second row directly follows the first.
52  */
53 void
54 dcp::xyz_to_rgba (
55         boost::shared_ptr<const OpenJPEGImage> xyz_image,
56         ColourConversion const & conversion,
57         uint8_t* argb
58         )
59 {
60         int const max_colour = pow (2, 12) - 1;
61
62         struct {
63                 double x, y, z;
64         } s;
65
66         struct {
67                 double r, g, b;
68         } d;
69
70         int* xyz_x = xyz_image->data (0);
71         int* xyz_y = xyz_image->data (1);
72         int* xyz_z = xyz_image->data (2);
73
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 ();
77
78         int const height = xyz_image->size().height;
79         int const width = xyz_image->size().width;
80
81         for (int y = 0; y < height; ++y) {
82                 uint8_t* argb_line = argb;
83                 for (int x = 0; x < width; ++x) {
84
85                         DCP_ASSERT (*xyz_x >= 0 && *xyz_y >= 0 && *xyz_z >= 0 && *xyz_x < 4096 && *xyz_y < 4096 && *xyz_z < 4096);
86
87                         /* In gamma LUT */
88                         s.x = lut_in[*xyz_x++];
89                         s.y = lut_in[*xyz_y++];
90                         s.z = lut_in[*xyz_z++];
91
92                         /* DCI companding */
93                         s.x /= DCI_COEFFICIENT;
94                         s.y /= DCI_COEFFICIENT;
95                         s.z /= DCI_COEFFICIENT;
96
97                         /* XYZ to RGB */
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)));
101
102                         d.r = min (d.r, 1.0);
103                         d.r = max (d.r, 0.0);
104
105                         d.g = min (d.g, 1.0);
106                         d.g = max (d.g, 0.0);
107
108                         d.b = min (d.b, 1.0);
109                         d.b = max (d.b, 0.0);
110
111                         /* Out gamma LUT */
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;
115                         *argb_line++ = 0xff;
116                 }
117
118                 /* 4 bytes per pixel */
119                 argb += width * 4;
120         }
121 }
122
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).
131  */
132 void
133 dcp::xyz_to_rgb (
134         shared_ptr<const OpenJPEGImage> xyz_image,
135         ColourConversion const & conversion,
136         uint8_t* rgb,
137         int stride,
138         optional<NoteHandler> note
139         )
140 {
141         struct {
142                 double x, y, z;
143         } s;
144
145         struct {
146                 double r, g, b;
147         } d;
148
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);
153
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 ();
157
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) {
161
162                         int cx = *xyz_x++;
163                         int cy = *xyz_y++;
164                         int cz = *xyz_z++;
165
166                         if (cx < 0 || cx > 4095) {
167                                 if (note) {
168                                         note.get() (DCP_NOTE, String::compose ("XYZ value %1 out of range", cx));
169                                 }
170                                 cx = max (min (cx, 4095), 0);
171                         }
172
173                         if (cy < 0 || cy > 4095) {
174                                 if (note) {
175                                         note.get() (DCP_NOTE, String::compose ("XYZ value %1 out of range", cy));
176                                 }
177                                 cy = max (min (cy, 4095), 0);
178                         }
179
180                         if (cz < 0 || cz > 4095) {
181                                 if (note) {
182                                         note.get() (DCP_NOTE, String::compose ("XYZ value %1 out of range", cz));
183                                 }
184                                 cz = max (min (cz, 4095), 0);
185                         }
186
187                         /* In gamma LUT */
188                         s.x = lut_in[cx];
189                         s.y = lut_in[cy];
190                         s.z = lut_in[cz];
191
192                         /* DCI companding */
193                         s.x /= DCI_COEFFICIENT;
194                         s.y /= DCI_COEFFICIENT;
195                         s.z /= DCI_COEFFICIENT;
196
197                         /* XYZ to RGB */
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)));
201
202                         d.r = min (d.r, 1.0);
203                         d.r = max (d.r, 0.0);
204
205                         d.g = min (d.g, 1.0);
206                         d.g = max (d.g, 0.0);
207
208                         d.b = min (d.b, 1.0);
209                         d.b = max (d.b, 0.0);
210
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);
214                 }
215         }
216 }
217
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.
223  */
224 shared_ptr<dcp::OpenJPEGImage>
225 dcp::rgb_to_xyz (
226         uint8_t const * rgb,
227         dcp::Size size,
228         int stride,
229         ColourConversion const & conversion,
230         optional<NoteHandler> note
231         )
232 {
233         shared_ptr<OpenJPEGImage> xyz (new OpenJPEGImage (size));
234
235         struct {
236                 double r, g, b;
237         } s;
238
239         struct {
240                 double x, y, z;
241         } d;
242
243         struct {
244                 double x, y, z;
245         } e;
246
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 ();
251
252         int clamped = 0;
253         int jn = 0;
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) {
257
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];
262
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)));
267
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)));
271
272                         /* DCI companding */
273                         e.x = e.x * DCI_COEFFICIENT * 65535;
274                         e.y = e.y * DCI_COEFFICIENT * 65535;
275                         e.z = e.z * DCI_COEFFICIENT * 65535;
276
277                         /* Clamp */
278
279                         if (e.x < 0 || e.y < 0 || e.z < 0 || e.x > 65535 || e.y > 65535 || e.z > 65535) {
280                                 ++clamped;
281                         }
282
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);
289
290                         /* Out gamma LUT */
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;
294
295                         ++jn;
296                 }
297         }
298
299         if (clamped && note) {
300                 note.get() (DCP_NOTE, String::compose ("%1 XYZ value(s) clamped", clamped));
301         }
302
303         return xyz;
304 }
305
306
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
309  *  little-endian.
310  */
311 shared_ptr<dcp::OpenJPEGImage>
312 dcp::xyz_to_xyz (uint8_t const * xyz_16, dcp::Size size, int stride)
313 {
314         shared_ptr<OpenJPEGImage> xyz_12 (new OpenJPEGImage (size));
315
316         int jn = 0;
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;
324                         ++jn;
325                 }
326         }
327
328         return xyz_12;
329 }