Try to fix static initialisation order problems.
[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
26 using std::max;
27 using boost::shared_ptr;
28
29 class SimpleImage : public dcp::Image
30 {
31 public:
32         SimpleImage (dcp::Size size)
33                 : Image (size)
34         {
35                 /* 48bpp */
36                 _stride[0] = _size.width * 6;
37                 _data[0] = new uint8_t[size.height * stride()[0]];
38         }
39
40         ~SimpleImage ()
41         {
42                 delete[] _data[0];
43         }
44
45         uint8_t * const * data () const {
46                 return _data;
47         }
48
49         int const * stride () const {
50                 return _stride;
51         }
52
53 private:
54         uint8_t* _data[1];
55         int _stride[1];
56 };
57
58 /** Convert a test image from sRGB to XYZ and check that the transforms are right */
59 BOOST_AUTO_TEST_CASE (rgb_xyz_test)
60 {
61         unsigned int seed = 0;
62         dcp::Size const size (640, 480);
63
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;
71                                 ++p;
72                         }
73                 }
74         }
75
76         shared_ptr<dcp::XYZFrame> xyz = dcp::rgb_to_xyz (rgb, dcp::ColourConversion::srgb_to_xyz ());
77
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) {
81
82                         double cr = *p++ / 65535.0;
83                         double cg = *p++ / 65535.0;
84                         double cb = *p++ / 65535.0;
85
86                         /* Input gamma */
87
88                         if (cr < 0.04045) {
89                                 cr /= 12.92;
90                         } else {
91                                 cr = pow ((cr + 0.055) / 1.055, 2.4);
92                         }
93
94                         if (cg < 0.04045) {
95                                 cg /= 12.92;
96                         } else {
97                                 cg = pow ((cg + 0.055) / 1.055, 2.4);
98                         }
99
100                         if (cb < 0.04045) {
101                                 cb /= 12.92;
102                         } else {
103                                 cb = pow ((cb + 0.055) / 1.055, 2.4);
104                         }
105
106                         /* Matrix */
107
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;
111
112                         /* Compand */
113
114                         cx *= 48 / 52.37;
115                         cy *= 48 / 52.37;
116                         cz *= 48 / 52.37;
117
118                         /* Output gamma */
119
120                         cx = pow (cx, 1 / 2.6);
121                         cy = pow (cy, 1 / 2.6);
122                         cz = pow (cz, 1 / 2.6);
123
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);
127                 }
128         }
129 }