Ignore parameters in PKL type strings when checking them.
[libdcp.git] / test / colour_conversion_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 "gamma_transfer_function.h"
35 #include "colour_conversion.h"
36 #include "modified_gamma_transfer_function.h"
37 #include <boost/test/unit_test.hpp>
38 #include <cmath>
39
40 using std::pow;
41 using std::shared_ptr;
42 using namespace dcp;
43
44 static void
45 check_gamma (shared_ptr<const TransferFunction> tf, int bit_depth, bool inverse, float gamma)
46 {
47         double const * lut = tf->lut (bit_depth, inverse);
48         int const count = rint (pow (2.0, bit_depth));
49
50         for (int i = 0; i < count; ++i) {
51                 BOOST_CHECK_CLOSE (lut[i], pow (float(i) / (count - 1), gamma), 0.001);
52         }
53 }
54
55 static void
56 check_modified_gamma (shared_ptr<const TransferFunction> tf, int bit_depth, bool inverse, double power, double threshold, double A, double B)
57 {
58         double const * lut = tf->lut (bit_depth, inverse);
59         int const count = rint (pow (2.0, bit_depth));
60
61         for (int i = 0; i < count; ++i) {
62                 double const x = double(i) / (count - 1);
63                 if (x > threshold) {
64                         BOOST_CHECK_CLOSE (lut[i], pow ((x + A) / (1 + A), power), 0.001);
65                 } else {
66                         BOOST_CHECK_CLOSE (lut[i], (x / B), 0.001);
67                 }
68         }
69 }
70
71 /** Check that the gamma correction LUTs are right for sRGB */
72 BOOST_AUTO_TEST_CASE (colour_conversion_test1)
73 {
74         ColourConversion cc = ColourConversion::srgb_to_xyz ();
75
76         check_modified_gamma (cc.in(), 8, false, 2.4, 0.04045, 0.055, 12.92);
77         check_modified_gamma (cc.in(), 12, false, 2.4, 0.04045, 0.055, 12.92);
78         check_modified_gamma (cc.in(), 16, false, 2.4, 0.04045, 0.055, 12.92);
79
80         check_gamma (cc.out(), 8, true, 1 / 2.6);
81         check_gamma (cc.out(), 12, true, 1 / 2.6);
82         check_gamma (cc.out(), 16, true, 1 / 2.6);
83 }
84
85 /** Check that the gamma correction LUTs are right for REC709 */
86 BOOST_AUTO_TEST_CASE (colour_conversion_test2)
87 {
88         ColourConversion cc = ColourConversion::rec709_to_xyz ();
89
90         check_gamma (cc.in(), 8, false, 2.2);
91         check_gamma (cc.in(), 12, false, 2.2);
92         check_gamma (cc.in(), 16, false, 2.2);
93
94         check_gamma (cc.out(), 8, true, 1 / 2.6);
95         check_gamma (cc.out(), 12, true, 1 / 2.6);
96         check_gamma (cc.out(), 16, true, 1 / 2.6);
97 }
98
99 /** Check that the xyz_to_rgb matrix is the inverse of the rgb_to_xyz one */
100 BOOST_AUTO_TEST_CASE (colour_conversion_matrix_test)
101 {
102         ColourConversion c = ColourConversion::srgb_to_xyz ();
103
104         boost::numeric::ublas::matrix<double> A = c.rgb_to_xyz ();
105         boost::numeric::ublas::matrix<double> B = c.xyz_to_rgb ();
106
107         BOOST_CHECK_CLOSE (A(0, 0) * B(0, 0) + A(0, 1) * B(1, 0) + A(0, 2) * B(2, 0), 1, 0.1);
108         BOOST_CHECK (fabs (A(0, 0) * B(0, 1) + A(0, 1) * B(1, 1) + A(0, 2) * B(2, 1)) < 1e-6);
109         BOOST_CHECK (fabs (A(0, 0) * B(0, 2) + A(0, 1) * B(1, 2) + A(0, 2) * B(2, 2)) < 1e-6);
110
111         BOOST_CHECK (fabs (A(1, 0) * B(0, 0) + A(1, 1) * B(1, 0) + A(1, 2) * B(2, 0)) < 1e-6);
112         BOOST_CHECK_CLOSE (A(1, 0) * B(0, 1) + A(1, 1) * B(1, 1) + A(1, 2) * B(2, 1), 1, 0.1);
113         BOOST_CHECK (fabs (A(1, 0) * B(0, 2) + A(1, 1) * B(1, 2) + A(1, 2) * B(2, 2)) < 1e-6);
114
115         BOOST_CHECK (fabs (A(2, 0) * B(0, 0) + A(2, 1) * B(1, 0) + A(2, 2) * B(2, 0)) < 1e-6);
116         BOOST_CHECK (fabs (A(2, 0) * B(0, 1) + A(2, 1) * B(1, 1) + A(2, 2) * B(2, 1)) < 1e-6);
117         BOOST_CHECK_CLOSE (A(2, 0) * B(0, 2) + A(2, 1) * B(1, 2) + A(2, 2) * B(2, 2), 1, 0.1);
118 }
119
120 BOOST_AUTO_TEST_CASE (colour_conversion_bradford_test)
121 {
122         ColourConversion c = ColourConversion::srgb_to_xyz ();
123
124         /* CIE "A" illuminant from http://www.brucelindbloom.com/index.html?Eqn_ChromAdapt.html
125            un-normalised using a factor k where k = 1 / (1 + x + z)
126         */
127         c.set_adjusted_white (Chromaticity (0.447576324, 0.407443172));
128
129         boost::numeric::ublas::matrix<double> b = c.bradford ();
130
131         /* Check the conversion matrix against the one quoted on brucelindbloom.com */
132         BOOST_CHECK_CLOSE (b(0, 0), 1.2164557, 0.1);
133         BOOST_CHECK_CLOSE (b(0, 1), 0.1109905, 0.1);
134         BOOST_CHECK_CLOSE (b(0, 2), -0.1549325, 0.1);
135         BOOST_CHECK_CLOSE (b(1, 0), 0.1533326, 0.1);
136         BOOST_CHECK_CLOSE (b(1, 1), 0.9152313, 0.1);
137         BOOST_CHECK_CLOSE (b(1, 2), -0.0559953, 0.1);
138         BOOST_CHECK_CLOSE (b(2, 0), -0.0239469, 0.1);
139         BOOST_CHECK_CLOSE (b(2, 1), 0.0358984, 0.1);
140         BOOST_CHECK_CLOSE (b(2, 2), 0.3147529, 0.1);
141
142         /* Same for CIE "B" illuminant */
143         c.set_adjusted_white (Chromaticity (0.99072 * 0.351747305, 0.351747305));
144
145         b = c.bradford ();
146
147         BOOST_CHECK_CLOSE (b(0, 0), 1.0641402, 0.1);
148         BOOST_CHECK_CLOSE (b(0, 1), 0.0325780, 0.1);
149         BOOST_CHECK_CLOSE (b(0, 2), -0.0489436, 0.1);
150         BOOST_CHECK_CLOSE (b(1, 0), 0.0446103, 0.1);
151         BOOST_CHECK_CLOSE (b(1, 1), 0.9766379, 0.1);
152         BOOST_CHECK_CLOSE (b(1, 2), -0.0174854, 0.1);
153         BOOST_CHECK_CLOSE (b(2, 0), -0.0078485, 0.1);
154         BOOST_CHECK_CLOSE (b(2, 1), 0.0119945, 0.1);
155         BOOST_CHECK_CLOSE (b(2, 2), 0.7785377, 0.1);
156 }