2 Copyright (C) 2014 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/>.
20 #include "gamma_transfer_function.h"
21 #include "colour_conversion.h"
22 #include "modified_gamma_transfer_function.h"
23 #include <boost/test/unit_test.hpp>
27 using boost::shared_ptr;
31 check_gamma (shared_ptr<const TransferFunction> tf, int bit_depth, bool inverse, float gamma)
33 double const * lut = tf->lut (bit_depth, inverse);
34 int const count = rint (pow (2.0, bit_depth));
36 for (int i = 0; i < count; ++i) {
37 BOOST_CHECK_CLOSE (lut[i], pow (float(i) / (count - 1), gamma), 0.001);
42 check_modified_gamma (shared_ptr<const TransferFunction> tf, int bit_depth, bool inverse, double power, double threshold, double A, double B)
44 double const * lut = tf->lut (bit_depth, inverse);
45 int const count = rint (pow (2.0, bit_depth));
47 for (int i = 0; i < count; ++i) {
48 double const x = double(i) / (count - 1);
50 BOOST_CHECK_CLOSE (lut[i], pow ((x + A) / (1 + A), power), 0.001);
52 BOOST_CHECK_CLOSE (lut[i], (x / B), 0.001);
57 /** Check that the gamma correction LUTs are right for sRGB */
58 BOOST_AUTO_TEST_CASE (colour_conversion_test1)
60 ColourConversion cc = ColourConversion::srgb_to_xyz ();
62 check_modified_gamma (cc.in(), 8, false, 2.4, 0.04045, 0.055, 12.92);
63 check_modified_gamma (cc.in(), 12, false, 2.4, 0.04045, 0.055, 12.92);
64 check_modified_gamma (cc.in(), 16, false, 2.4, 0.04045, 0.055, 12.92);
66 check_gamma (cc.out(), 8, true, 1 / 2.6);
67 check_gamma (cc.out(), 12, true, 1 / 2.6);
68 check_gamma (cc.out(), 16, true, 1 / 2.6);
71 /** Check that the gamma correction LUTs are right for REC709 */
72 BOOST_AUTO_TEST_CASE (colour_conversion_test2)
74 ColourConversion cc = ColourConversion::rec709_to_xyz ();
76 check_gamma (cc.in(), 8, false, 2.2);
77 check_gamma (cc.in(), 12, false, 2.2);
78 check_gamma (cc.in(), 16, false, 2.2);
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 xyz_to_rgb matrix is the inverse of the rgb_to_xyz one */
86 BOOST_AUTO_TEST_CASE (colour_conversion_matrix_test)
88 ColourConversion c = ColourConversion::srgb_to_xyz ();
90 boost::numeric::ublas::matrix<double> A = c.rgb_to_xyz ();
91 boost::numeric::ublas::matrix<double> B = c.xyz_to_rgb ();
93 BOOST_CHECK_CLOSE (A(0, 0) * B(0, 0) + A(0, 1) * B(1, 0) + A(0, 2) * B(2, 0), 1, 0.1);
94 BOOST_CHECK (fabs (A(0, 0) * B(0, 1) + A(0, 1) * B(1, 1) + A(0, 2) * B(2, 1)) < 1e-6);
95 BOOST_CHECK (fabs (A(0, 0) * B(0, 2) + A(0, 1) * B(1, 2) + A(0, 2) * B(2, 2)) < 1e-6);
97 BOOST_CHECK (fabs (A(1, 0) * B(0, 0) + A(1, 1) * B(1, 0) + A(1, 2) * B(2, 0)) < 1e-6);
98 BOOST_CHECK_CLOSE (A(1, 0) * B(0, 1) + A(1, 1) * B(1, 1) + A(1, 2) * B(2, 1), 1, 0.1);
99 BOOST_CHECK (fabs (A(1, 0) * B(0, 2) + A(1, 1) * B(1, 2) + A(1, 2) * B(2, 2)) < 1e-6);
101 BOOST_CHECK (fabs (A(2, 0) * B(0, 0) + A(2, 1) * B(1, 0) + A(2, 2) * B(2, 0)) < 1e-6);
102 BOOST_CHECK (fabs (A(2, 0) * B(0, 1) + A(2, 1) * B(1, 1) + A(2, 2) * B(2, 1)) < 1e-6);
103 BOOST_CHECK_CLOSE (A(2, 0) * B(0, 2) + A(2, 1) * B(1, 2) + A(2, 2) * B(2, 2), 1, 0.1);
106 BOOST_AUTO_TEST_CASE (colour_conversion_bradford_test)
108 ColourConversion c = ColourConversion::srgb_to_xyz ();
110 /* CIE "A" illuminant from http://www.brucelindbloom.com/index.html?Eqn_ChromAdapt.html
111 un-normalised using a factor k where k = 1 / (1 + x + z)
113 c.set_adjusted_white (Chromaticity (0.447576324, 0.407443172));
115 boost::numeric::ublas::matrix<double> b = c.bradford ();
117 /* Check the conversion matrix against the one quoted on brucelindbloom.com */
118 BOOST_CHECK_CLOSE (b(0, 0), 1.2164557, 0.1);
119 BOOST_CHECK_CLOSE (b(0, 1), 0.1109905, 0.1);
120 BOOST_CHECK_CLOSE (b(0, 2), -0.1549325, 0.1);
121 BOOST_CHECK_CLOSE (b(1, 0), 0.1533326, 0.1);
122 BOOST_CHECK_CLOSE (b(1, 1), 0.9152313, 0.1);
123 BOOST_CHECK_CLOSE (b(1, 2), -0.0559953, 0.1);
124 BOOST_CHECK_CLOSE (b(2, 0), -0.0239469, 0.1);
125 BOOST_CHECK_CLOSE (b(2, 1), 0.0358984, 0.1);
126 BOOST_CHECK_CLOSE (b(2, 2), 0.3147529, 0.1);
128 /* Same for CIE "B" illuminant */
129 c.set_adjusted_white (Chromaticity (0.99072 * 0.351747305, 0.351747305));
133 BOOST_CHECK_CLOSE (b(0, 0), 1.0641402, 0.1);
134 BOOST_CHECK_CLOSE (b(0, 1), 0.0325780, 0.1);
135 BOOST_CHECK_CLOSE (b(0, 2), -0.0489436, 0.1);
136 BOOST_CHECK_CLOSE (b(1, 0), 0.0446103, 0.1);
137 BOOST_CHECK_CLOSE (b(1, 1), 0.9766379, 0.1);
138 BOOST_CHECK_CLOSE (b(1, 2), -0.0174854, 0.1);
139 BOOST_CHECK_CLOSE (b(2, 0), -0.0078485, 0.1);
140 BOOST_CHECK_CLOSE (b(2, 1), 0.0119945, 0.1);
141 BOOST_CHECK_CLOSE (b(2, 2), 0.7785377, 0.1);