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