2 Copyright (C) 2014 Carl Hetherington <cth@carlh.net>
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.
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.
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.
22 #include "xyz_frame.h"
23 #include "colour_conversion.h"
24 #include <boost/test/unit_test.hpp>
27 using boost::shared_ptr;
29 class SimpleImage : public dcp::Image
32 SimpleImage (dcp::Size size)
36 _stride[0] = _size.width * 6;
37 _data[0] = new uint8_t[size.height * stride()[0]];
45 uint8_t * const * data () const {
49 int const * stride () const {
58 /** Convert a test image from sRGB to XYZ and check that the transforms are right */
59 BOOST_AUTO_TEST_CASE (rgb_xyz_test)
61 unsigned int seed = 0;
62 dcp::Size const size (640, 480);
64 shared_ptr<const dcp::Image> rgb (new SimpleImage (size));
65 for (int y = 0; y < size.height; ++y) {
66 uint16_t* p = reinterpret_cast<uint16_t*> (rgb->data()[0] + y * rgb->stride()[0]);
67 for (int x = 0; x < size.width; ++x) {
68 /* Write a 12-bit random number for each component */
69 for (int c = 0; c < 3; ++c) {
70 *p = (rand_r (&seed) & 0xfff) << 4;
76 shared_ptr<dcp::XYZFrame> xyz = dcp::rgb_to_xyz (rgb, dcp::ColourConversion::srgb_to_xyz ());
78 for (int y = 0; y < size.height; ++y) {
79 uint16_t* p = reinterpret_cast<uint16_t*> (rgb->data()[0] + y * rgb->stride()[0]);
80 for (int x = 0; x < size.width; ++x) {
82 double cr = *p++ / 65535.0;
83 double cg = *p++ / 65535.0;
84 double cb = *p++ / 65535.0;
91 cr = pow ((cr + 0.055) / 1.055, 2.4);
97 cg = pow ((cg + 0.055) / 1.055, 2.4);
103 cb = pow ((cb + 0.055) / 1.055, 2.4);
108 double cx = cr * 0.4124564 + cg * 0.3575761 + cb * 0.1804375;
109 double cy = cr * 0.2126729 + cg * 0.7151522 + cb * 0.0721750;
110 double cz = cr * 0.0193339 + cg * 0.1191920 + cb * 0.9503041;
120 cx = pow (cx, 1 / 2.6);
121 cy = pow (cy, 1 / 2.6);
122 cz = pow (cz, 1 / 2.6);
124 BOOST_REQUIRE_CLOSE (cx * 4095, xyz->data(0)[y * size.width + x], 1);
125 BOOST_REQUIRE_CLOSE (cy * 4095, xyz->data(1)[y * size.width + x], 1);
126 BOOST_REQUIRE_CLOSE (cz * 4095, xyz->data(2)[y * size.width + x], 1);