Rename XYZImage -> OpenJPEGImage.
[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 boost::shared_ptr;
32 using boost::optional;
33 using namespace dcp;
34
35 #define DCI_COEFFICIENT (48.0 / 52.37)
36
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:
41  *
42  *  <pre>
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| ...
45  *  </pre>
46  *
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.
49  *
50  *  Lines are packed so that the second row directly follows the first.
51  */
52 void
53 dcp::xyz_to_rgba (
54         boost::shared_ptr<const OpenJPEGImage> xyz_image,
55         ColourConversion const & conversion,
56         uint8_t* argb
57         )
58 {
59         int const max_colour = pow (2, 12) - 1;
60
61         struct {
62                 double x, y, z;
63         } s;
64         
65         struct {
66                 double r, g, b;
67         } d;
68         
69         int* xyz_x = xyz_image->data (0);
70         int* xyz_y = xyz_image->data (1);
71         int* xyz_z = xyz_image->data (2);
72
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 ();
76
77         int const height = xyz_image->size().height;
78         int const width = xyz_image->size().width;
79         
80         for (int y = 0; y < height; ++y) {
81                 uint8_t* argb_line = argb;
82                 for (int x = 0; x < width; ++x) {
83
84                         DCP_ASSERT (*xyz_x >= 0 && *xyz_y >= 0 && *xyz_z >= 0 && *xyz_x < 4096 && *xyz_y < 4096 && *xyz_z < 4096);
85                         
86                         /* In gamma LUT */
87                         s.x = lut_in[*xyz_x++];
88                         s.y = lut_in[*xyz_y++];
89                         s.z = lut_in[*xyz_z++];
90
91                         /* DCI companding */
92                         s.x /= DCI_COEFFICIENT;
93                         s.y /= DCI_COEFFICIENT;
94                         s.z /= DCI_COEFFICIENT;
95
96                         /* XYZ to RGB */
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)));
100                         
101                         d.r = min (d.r, 1.0);
102                         d.r = max (d.r, 0.0);
103                         
104                         d.g = min (d.g, 1.0);
105                         d.g = max (d.g, 0.0);
106                         
107                         d.b = min (d.b, 1.0);
108                         d.b = max (d.b, 0.0);
109                         
110                         /* Out gamma LUT */
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;
114                         *argb_line++ = 0xff;
115                 }
116
117                 /* 4 bytes per pixel */
118                 argb += width * 4;
119         }
120 }
121
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).
130  */
131 void
132 dcp::xyz_to_rgb (
133         shared_ptr<const OpenJPEGImage> xyz_image,
134         ColourConversion const & conversion,
135         uint8_t* rgb,
136         int stride,
137         optional<NoteHandler> note
138         )
139 {
140         struct {
141                 double x, y, z;
142         } s;
143         
144         struct {
145                 double r, g, b;
146         } d;
147
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);
152
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 ();
156
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) {
160
161                         int cx = *xyz_x++;
162                         int cy = *xyz_y++;
163                         int cz = *xyz_z++;
164
165                         if (cx < 0 || cx > 4095) {
166                                 if (note) {
167                                         note.get() (DCP_NOTE, String::compose ("XYZ value %1 out of range", cx));
168                                 }
169                                 cx = max (min (cx, 4095), 0);
170                         }
171
172                         if (cy < 0 || cy > 4095) {
173                                 if (note) {
174                                         note.get() (DCP_NOTE, String::compose ("XYZ value %1 out of range", cy));
175                                 }
176                                 cy = max (min (cy, 4095), 0);
177                         }
178
179                         if (cz < 0 || cz > 4095) {
180                                 if (note) {
181                                         note.get() (DCP_NOTE, String::compose ("XYZ value %1 out of range", cz));
182                                 }
183                                 cz = max (min (cz, 4095), 0);
184                         }
185                         
186                         /* In gamma LUT */
187                         s.x = lut_in[cx];
188                         s.y = lut_in[cy];
189                         s.z = lut_in[cz];
190
191                         /* DCI companding */
192                         s.x /= DCI_COEFFICIENT;
193                         s.y /= DCI_COEFFICIENT;
194                         s.z /= DCI_COEFFICIENT;
195
196                         /* XYZ to RGB */
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)));
200                         
201                         d.r = min (d.r, 1.0);
202                         d.r = max (d.r, 0.0);
203                         
204                         d.g = min (d.g, 1.0);
205                         d.g = max (d.g, 0.0);
206                         
207                         d.b = min (d.b, 1.0);
208                         d.b = max (d.b, 0.0);
209
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);
213                 }
214         }
215 }
216
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 pixels.
222  */
223 shared_ptr<dcp::OpenJPEGImage>
224 dcp::rgb_to_xyz (
225         uint8_t const * rgb,
226         dcp::Size size,
227         int stride,
228         ColourConversion const & conversion
229         )
230 {
231         shared_ptr<OpenJPEGImage> xyz (new OpenJPEGImage (size));
232
233         struct {
234                 double r, g, b;
235         } s;
236
237         struct {
238                 double x, y, z;
239         } d;
240
241         struct {
242                 double x, y, z;
243         } e;
244         
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 ();
249
250         int jn = 0;
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) {
254
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];
259
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)));
264
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)));
268                         
269                         /* DCI companding */
270                         e.x = e.x * DCI_COEFFICIENT * 65535;
271                         e.y = e.y * DCI_COEFFICIENT * 65535;
272                         e.z = e.z * DCI_COEFFICIENT * 65535;
273
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);
277                         
278                         /* Out gamma LUT */
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;
282
283                         ++jn;
284                 }
285         }
286
287         return xyz;
288 }
289
290
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
293  *  little-endian.
294  */
295 shared_ptr<dcp::OpenJPEGImage>
296 dcp::xyz_to_xyz (uint8_t const * xyz_16, dcp::Size size, int stride)
297 {
298         shared_ptr<OpenJPEGImage> xyz_12 (new OpenJPEGImage (size));
299
300         int jn = 0;
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;
308                         ++jn;
309                 }
310         }
311         
312         return xyz_12;
313 }