54f6174ca61a77d5be608e98b37f3d7f0da21783
[libdcp.git] / test / rgb_xyz_test.cc
1 /*
2     Copyright (C) 2014-2019 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 #include "rgb_xyz.h"
35 #include "openjpeg_image.h"
36 #include "colour_conversion.h"
37 #include <boost/test/unit_test.hpp>
38 #include <boost/bind.hpp>
39 #include <boost/scoped_array.hpp>
40
41 using std::max;
42 using std::list;
43 using std::string;
44 using std::cout;
45 using std::shared_ptr;
46 using std::make_shared;
47 using boost::optional;
48 using boost::scoped_array;
49
50 /** Convert a test image from sRGB to XYZ and check that the transforms are right */
51 BOOST_AUTO_TEST_CASE (rgb_xyz_test)
52 {
53         srand (0);
54         dcp::Size const size (640, 480);
55
56         scoped_array<uint8_t> rgb (new uint8_t[size.width * size.height * 6]);
57         for (int y = 0; y < size.height; ++y) {
58                 uint16_t* p = reinterpret_cast<uint16_t*> (rgb.get() + y * size.width * 6);
59                 for (int x = 0; x < size.width; ++x) {
60                         /* Write a 12-bit random number for each component */
61                         for (int c = 0; c < 3; ++c) {
62                                 *p = (rand () & 0xfff) << 4;
63                                 ++p;
64                         }
65                 }
66         }
67
68         auto xyz = dcp::rgb_to_xyz (rgb.get(), size, size.width * 6, dcp::ColourConversion::srgb_to_xyz());
69
70         for (int y = 0; y < size.height; ++y) {
71                 uint16_t* p = reinterpret_cast<uint16_t*> (rgb.get() + y * size.width * 6);
72                 for (int x = 0; x < size.width; ++x) {
73
74                         double cr = *p++ / 65535.0;
75                         double cg = *p++ / 65535.0;
76                         double cb = *p++ / 65535.0;
77
78                         /* Input gamma */
79
80                         if (cr < 0.04045) {
81                                 cr /= 12.92;
82                         } else {
83                                 cr = pow ((cr + 0.055) / 1.055, 2.4);
84                         }
85
86                         if (cg < 0.04045) {
87                                 cg /= 12.92;
88                         } else {
89                                 cg = pow ((cg + 0.055) / 1.055, 2.4);
90                         }
91
92                         if (cb < 0.04045) {
93                                 cb /= 12.92;
94                         } else {
95                                 cb = pow ((cb + 0.055) / 1.055, 2.4);
96                         }
97
98                         /* Matrix */
99
100                         double cx = cr * 0.4124564 + cg * 0.3575761 + cb * 0.1804375;
101                         double cy = cr * 0.2126729 + cg * 0.7151522 + cb * 0.0721750;
102                         double cz = cr * 0.0193339 + cg * 0.1191920 + cb * 0.9503041;
103
104                         /* Compand */
105
106                         cx *= 48 / 52.37;
107                         cy *= 48 / 52.37;
108                         cz *= 48 / 52.37;
109
110                         /* Output gamma */
111
112                         cx = pow (cx, 1 / 2.6);
113                         cy = pow (cy, 1 / 2.6);
114                         cz = pow (cz, 1 / 2.6);
115
116                         BOOST_REQUIRE_CLOSE (cx * 4095, xyz->data(0)[y * size.width + x], 1);
117                         BOOST_REQUIRE_CLOSE (cy * 4095, xyz->data(1)[y * size.width + x], 1);
118                         BOOST_REQUIRE_CLOSE (cz * 4095, xyz->data(2)[y * size.width + x], 1);
119                 }
120         }
121 }
122
123 static list<string> notes;
124
125 static void
126 note_handler (dcp::NoteType n, string s)
127 {
128         BOOST_REQUIRE_EQUAL (n, dcp::NoteType::NOTE);
129         notes.push_back (s);
130 }
131
132 /** Check that xyz_to_rgb clamps XYZ values correctly */
133 BOOST_AUTO_TEST_CASE (xyz_rgb_range_test)
134 {
135         auto xyz = make_shared<dcp::OpenJPEGImage>(dcp::Size(2, 2));
136
137         xyz->data(0)[0] = -4;
138         xyz->data(0)[1] = 6901;
139         xyz->data(0)[2] = 0;
140         xyz->data(0)[3] = 4095;
141         xyz->data(1)[0] = -4;
142         xyz->data(1)[1] = 6901;
143         xyz->data(1)[2] = 0;
144         xyz->data(1)[3] = 4095;
145         xyz->data(2)[0] = -4;
146         xyz->data(2)[1] = 6901;
147         xyz->data(2)[2] = 0;
148         xyz->data(2)[3] = 4095;
149
150         scoped_array<uint8_t> rgb (new uint8_t[2 * 2 * 6]);
151
152         notes.clear ();
153         dcp::xyz_to_rgb (
154                 xyz, dcp::ColourConversion::srgb_to_xyz (), rgb.get(), 2 * 6, boost::optional<dcp::NoteHandler> (boost::bind (&note_handler, _1, _2))
155                 );
156
157         /* The 6 out-of-range samples should have been noted */
158         BOOST_REQUIRE_EQUAL (notes.size(), 6);
159         auto i = notes.begin ();
160         BOOST_REQUIRE_EQUAL (*i++, "XYZ value -4 out of range");
161         BOOST_REQUIRE_EQUAL (*i++, "XYZ value -4 out of range");
162         BOOST_REQUIRE_EQUAL (*i++, "XYZ value -4 out of range");
163         BOOST_REQUIRE_EQUAL (*i++, "XYZ value 6901 out of range");
164         BOOST_REQUIRE_EQUAL (*i++, "XYZ value 6901 out of range");
165         BOOST_REQUIRE_EQUAL (*i++, "XYZ value 6901 out of range");
166
167         /* And those samples should have been clamped, so check that they give the same result
168            as inputs at the extremes (0 and 4095).
169         */
170
171         auto buffer = reinterpret_cast<uint16_t*> (rgb.get ());
172         BOOST_REQUIRE_EQUAL (buffer[0 * 3 + 0], buffer[2 * 3 + 1]);
173         BOOST_REQUIRE_EQUAL (buffer[0 * 3 + 1], buffer[2 * 3 + 1]);
174         BOOST_REQUIRE_EQUAL (buffer[0 * 3 + 2], buffer[2 * 3 + 2]);
175
176         BOOST_REQUIRE_EQUAL (buffer[1 * 3 + 0], buffer[3 * 3 + 0]);
177         BOOST_REQUIRE_EQUAL (buffer[1 * 3 + 1], buffer[3 * 3 + 1]);
178         BOOST_REQUIRE_EQUAL (buffer[1 * 3 + 2], buffer[3 * 3 + 2]);
179 }
180
181 /** Convert an image from RGB to XYZ and back again */
182 BOOST_AUTO_TEST_CASE (rgb_xyz_round_trip_test)
183 {
184         srand (0);
185         dcp::Size const size (640, 480);
186
187         scoped_array<uint8_t> rgb (new uint8_t[size.width * size.height * 6]);
188         for (int y = 0; y < size.height; ++y) {
189                 uint16_t* p = reinterpret_cast<uint16_t*> (rgb.get() + y * size.width * 6);
190                 for (int x = 0; x < size.width; ++x) {
191                         /* Write a 12-bit random number for each component */
192                         for (int c = 0; c < 3; ++c) {
193                                 *p = (rand () & 0xfff) << 4;
194                                 ++p;
195                         }
196                 }
197         }
198
199         shared_ptr<dcp::OpenJPEGImage> xyz = dcp::rgb_to_xyz (rgb.get(), size, size.width * 6, dcp::ColourConversion::srgb_to_xyz ());
200         scoped_array<uint8_t> back (new uint8_t[size.width * size.height * 6]);
201         dcp::xyz_to_rgb (xyz, dcp::ColourConversion::srgb_to_xyz (), back.get(), size.width * 6);
202
203 #if 0
204         uint16_t* p = reinterpret_cast<uint16_t*> (rgb.get ());
205         uint16_t* q = reinterpret_cast<uint16_t*> (back.get ());
206         for (int i = 0; i < (size.width * size.height); ++i) {
207                 /* XXX: doesn't quite work */
208                 // BOOST_REQUIRE_EQUAL (*p++, *q++);
209         }
210 #endif
211 }