2 Copyright (C) 2014-2019 Carl Hetherington <cth@carlh.net>
4 This file is part of libdcp.
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.
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.
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/>.
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
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.
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>
41 using boost::shared_ptr;
45 check_gamma (shared_ptr<const TransferFunction> tf, int bit_depth, bool inverse, float gamma)
47 double const * lut = tf->lut (bit_depth, inverse);
48 int const count = rint (pow (2.0, bit_depth));
50 for (int i = 0; i < count; ++i) {
51 BOOST_CHECK_CLOSE (lut[i], pow (float(i) / (count - 1), gamma), 0.001);
56 check_modified_gamma (shared_ptr<const TransferFunction> tf, int bit_depth, bool inverse, double power, double threshold, double A, double B)
58 double const * lut = tf->lut (bit_depth, inverse);
59 int const count = rint (pow (2.0, bit_depth));
61 for (int i = 0; i < count; ++i) {
62 double const x = double(i) / (count - 1);
64 BOOST_CHECK_CLOSE (lut[i], pow ((x + A) / (1 + A), power), 0.001);
66 BOOST_CHECK_CLOSE (lut[i], (x / B), 0.001);
71 /** Check that the gamma correction LUTs are right for sRGB */
72 BOOST_AUTO_TEST_CASE (colour_conversion_test1)
74 ColourConversion cc = ColourConversion::srgb_to_xyz ();
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);
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);
85 /** Check that the gamma correction LUTs are right for REC709 */
86 BOOST_AUTO_TEST_CASE (colour_conversion_test2)
88 ColourConversion cc = ColourConversion::rec709_to_xyz ();
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);
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);
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)
102 ColourConversion c = ColourConversion::srgb_to_xyz ();
104 boost::numeric::ublas::matrix<double> A = c.rgb_to_xyz ();
105 boost::numeric::ublas::matrix<double> B = c.xyz_to_rgb ();
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);
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);
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);
120 BOOST_AUTO_TEST_CASE (colour_conversion_bradford_test)
122 ColourConversion c = ColourConversion::srgb_to_xyz ();
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)
127 c.set_adjusted_white (Chromaticity (0.447576324, 0.407443172));
129 boost::numeric::ublas::matrix<double> b = c.bradford ();
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);
142 /* Same for CIE "B" illuminant */
143 c.set_adjusted_white (Chromaticity (0.99072 * 0.351747305, 0.351747305));
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);