Cleanup: move EqualityOptions into its own file.
[libdcp.git] / test / rgb_xyz_test.cc
1 /*
2     Copyright (C) 2014-2021 Carl Hetherington <cth@carlh.net>
3
4     This file is part of libdcp.
5
6     libdcp is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     libdcp is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with libdcp.  If not, see <http://www.gnu.org/licenses/>.
18
19     In addition, as a special exception, the copyright holders give
20     permission to link the code of portions of this program with the
21     OpenSSL library under certain conditions as described in each
22     individual source file, and distribute linked combinations
23     including the two.
24
25     You must obey the GNU General Public License in all respects
26     for all of the code used other than OpenSSL.  If you modify
27     file(s) with this exception, you may extend this exception to your
28     version of the file(s), but you are not obligated to do so.  If you
29     do not wish to do so, delete this exception statement from your
30     version.  If you delete this exception statement from all source
31     files in the program, then also delete it here.
32 */
33
34
35 #include "colour_conversion.h"
36 #include "openjpeg_image.h"
37 #include "piecewise_lut.h"
38 #include "rgb_xyz.h"
39 #include "stream_operators.h"
40 #include <boost/bind.hpp>
41 #include <boost/random.hpp>
42 #include <boost/scoped_array.hpp>
43 #include <boost/test/unit_test.hpp>
44
45
46 using std::cout;
47 using std::list;
48 using std::make_shared;
49 using std::max;
50 using std::shared_ptr;
51 using std::string;
52 using boost::optional;
53 using boost::scoped_array;
54
55
56 static
57 void
58 rgb_xyz_test_case (std::function<void (uint16_t*)> write_pixel)
59 {
60         srand (0);
61         dcp::Size const size (640, 480);
62
63         scoped_array<uint8_t> rgb (new uint8_t[size.width * size.height * 6]);
64         for (int y = 0; y < size.height; ++y) {
65                 uint16_t* p = reinterpret_cast<uint16_t*> (rgb.get() + y * size.width * 6);
66                 for (int x = 0; x < size.width; ++x) {
67                         write_pixel (p);
68                         p += 3;
69                 }
70         }
71
72         auto xyz = dcp::rgb_to_xyz (rgb.get(), size, size.width * 6, dcp::ColourConversion::srgb_to_xyz());
73
74         for (int y = 0; y < size.height; ++y) {
75                 uint16_t* p = reinterpret_cast<uint16_t*> (rgb.get() + y * size.width * 6);
76                 for (int x = 0; x < size.width; ++x) {
77
78                         double cr = *p++ / 65535.0;
79                         double cg = *p++ / 65535.0;
80                         double cb = *p++ / 65535.0;
81
82                         /* Input gamma */
83
84                         if (cr < 0.04045) {
85                                 cr /= 12.92;
86                         } else {
87                                 cr = pow ((cr + 0.055) / 1.055, 2.4);
88                         }
89
90                         if (cg < 0.04045) {
91                                 cg /= 12.92;
92                         } else {
93                                 cg = pow ((cg + 0.055) / 1.055, 2.4);
94                         }
95
96                         if (cb < 0.04045) {
97                                 cb /= 12.92;
98                         } else {
99                                 cb = pow ((cb + 0.055) / 1.055, 2.4);
100                         }
101
102                         /* Matrix */
103
104                         double cx = cr * 0.4124564 + cg * 0.3575761 + cb * 0.1804375;
105                         double cy = cr * 0.2126729 + cg * 0.7151522 + cb * 0.0721750;
106                         double cz = cr * 0.0193339 + cg * 0.1191920 + cb * 0.9503041;
107
108                         /* Compand */
109
110                         cx *= 48 / 52.37;
111                         cy *= 48 / 52.37;
112                         cz *= 48 / 52.37;
113
114                         /* Output gamma */
115
116                         cx = pow (cx, 1 / 2.6);
117                         cy = pow (cy, 1 / 2.6);
118                         cz = pow (cz, 1 / 2.6);
119
120                         BOOST_REQUIRE_CLOSE (cx * 4095, xyz->data(0)[y * size.width + x], 1);
121                         BOOST_REQUIRE_CLOSE (cy * 4095, xyz->data(1)[y * size.width + x], 1);
122                         BOOST_REQUIRE_CLOSE (cz * 4095, xyz->data(2)[y * size.width + x], 1);
123                 }
124         }
125 }
126
127
128 /** Convert a test image from sRGB to XYZ and check that the transforms are right */
129 BOOST_AUTO_TEST_CASE (rgb_xyz_test)
130 {
131         {
132                 int counter = 0;
133                 rgb_xyz_test_case ([&counter](uint16_t* p) {
134                         p[0] = p[1] = p[2] = (counter << 4);
135                         ++counter;
136                         if (counter >= 4096) {
137                                 counter = 0;
138                         }
139                 });
140         }
141
142         boost::random::mt19937 rng(1);
143         boost::random::uniform_int_distribution<> dist(0, 4095);
144
145         rgb_xyz_test_case ([&rng, &dist](uint16_t* p) {
146                            p[0] = dist(rng) << 4;
147                            p[1] = dist(rng) << 4;
148                            p[2] = dist(rng) << 4;
149                            });
150 }
151
152
153 /** Check the piecewise LUT that is used for inverse gamma calculation */
154 BOOST_AUTO_TEST_CASE (rgb_xyz_lut_test)
155 {
156         auto conversion = dcp::ColourConversion::rec709_to_xyz();
157         auto lut = dcp::make_inverse_gamma_lut(conversion.out());
158
159         for (double x = 0; x < 1; x += 0.000001) {
160                 BOOST_CHECK(std::abs(lrint(lut.lookup(x) * 4095) - lrint(pow(x, 1 / 2.6) * 4095)) < 2);
161         }
162 }
163
164
165 static list<string> notes;
166
167 static void
168 note_handler (dcp::NoteType n, string s)
169 {
170         BOOST_REQUIRE_EQUAL (n, dcp::NoteType::NOTE);
171         notes.push_back (s);
172 }
173
174 /** Check that xyz_to_rgb clamps XYZ values correctly */
175 BOOST_AUTO_TEST_CASE (xyz_rgb_range_test)
176 {
177         auto xyz = make_shared<dcp::OpenJPEGImage>(dcp::Size(2, 2));
178
179         xyz->data(0)[0] = -4;
180         xyz->data(0)[1] = 6901;
181         xyz->data(0)[2] = 0;
182         xyz->data(0)[3] = 4095;
183         xyz->data(1)[0] = -4;
184         xyz->data(1)[1] = 6901;
185         xyz->data(1)[2] = 0;
186         xyz->data(1)[3] = 4095;
187         xyz->data(2)[0] = -4;
188         xyz->data(2)[1] = 6901;
189         xyz->data(2)[2] = 0;
190         xyz->data(2)[3] = 4095;
191
192         scoped_array<uint8_t> rgb (new uint8_t[2 * 2 * 6]);
193
194         notes.clear ();
195         dcp::xyz_to_rgb (
196                 xyz, dcp::ColourConversion::srgb_to_xyz (), rgb.get(), 2 * 6, boost::optional<dcp::NoteHandler> (boost::bind (&note_handler, _1, _2))
197                 );
198
199         /* The 6 out-of-range samples should have been noted */
200         BOOST_REQUIRE_EQUAL (notes.size(), 6U);
201         auto i = notes.begin ();
202         BOOST_REQUIRE_EQUAL (*i++, "XYZ value -4 out of range");
203         BOOST_REQUIRE_EQUAL (*i++, "XYZ value -4 out of range");
204         BOOST_REQUIRE_EQUAL (*i++, "XYZ value -4 out of range");
205         BOOST_REQUIRE_EQUAL (*i++, "XYZ value 6901 out of range");
206         BOOST_REQUIRE_EQUAL (*i++, "XYZ value 6901 out of range");
207         BOOST_REQUIRE_EQUAL (*i++, "XYZ value 6901 out of range");
208
209         /* And those samples should have been clamped, so check that they give the same result
210            as inputs at the extremes (0 and 4095).
211         */
212
213         auto buffer = reinterpret_cast<uint16_t*> (rgb.get ());
214         BOOST_REQUIRE_EQUAL (buffer[0 * 3 + 0], buffer[2 * 3 + 1]);
215         BOOST_REQUIRE_EQUAL (buffer[0 * 3 + 1], buffer[2 * 3 + 1]);
216         BOOST_REQUIRE_EQUAL (buffer[0 * 3 + 2], buffer[2 * 3 + 2]);
217
218         BOOST_REQUIRE_EQUAL (buffer[1 * 3 + 0], buffer[3 * 3 + 0]);
219         BOOST_REQUIRE_EQUAL (buffer[1 * 3 + 1], buffer[3 * 3 + 1]);
220         BOOST_REQUIRE_EQUAL (buffer[1 * 3 + 2], buffer[3 * 3 + 2]);
221 }
222
223 /** Convert an image from RGB to XYZ and back again */
224 BOOST_AUTO_TEST_CASE (rgb_xyz_round_trip_test)
225 {
226         srand (0);
227         dcp::Size const size (640, 480);
228
229         scoped_array<uint8_t> rgb (new uint8_t[size.width * size.height * 6]);
230         for (int y = 0; y < size.height; ++y) {
231                 uint16_t* p = reinterpret_cast<uint16_t*> (rgb.get() + y * size.width * 6);
232                 for (int x = 0; x < size.width; ++x) {
233                         /* Write a 12-bit random number for each component */
234                         for (int c = 0; c < 3; ++c) {
235                                 *p = (rand () & 0xfff) << 4;
236                                 ++p;
237                         }
238                 }
239         }
240
241         shared_ptr<dcp::OpenJPEGImage> xyz = dcp::rgb_to_xyz (rgb.get(), size, size.width * 6, dcp::ColourConversion::srgb_to_xyz ());
242         scoped_array<uint8_t> back (new uint8_t[size.width * size.height * 6]);
243         dcp::xyz_to_rgb (xyz, dcp::ColourConversion::srgb_to_xyz (), back.get(), size.width * 6);
244
245 #if 0
246         uint16_t* p = reinterpret_cast<uint16_t*> (rgb.get ());
247         uint16_t* q = reinterpret_cast<uint16_t*> (back.get ());
248         for (int i = 0; i < (size.width * size.height); ++i) {
249                 /* XXX: doesn't quite work */
250                 // BOOST_REQUIRE_EQUAL (*p++, *q++);
251         }
252 #endif
253 }