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