2 Copyright (C) 2014-2015 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/>.
21 #include "colour_conversion.h"
22 #include "gamma_transfer_function.h"
23 #include "modified_gamma_transfer_function.h"
24 #include "colour_matrix.h"
25 #include "dcp_assert.h"
26 #include <boost/numeric/ublas/matrix.hpp>
27 #include <boost/numeric/ublas/lu.hpp>
28 #include <boost/numeric/ublas/io.hpp>
30 using boost::shared_ptr;
31 using boost::optional;
34 ColourConversion const &
35 ColourConversion::srgb_to_xyz ()
37 static ColourConversion* c = new ColourConversion (
38 shared_ptr<const TransferFunction> (new ModifiedGammaTransferFunction (2.4, 0.04045, 0.055, 12.92)),
40 Chromaticity (0.64, 0.33),
41 Chromaticity (0.3, 0.6),
42 Chromaticity (0.15, 0.06),
44 Chromaticity (0.3127, 0.329),
45 optional<Chromaticity> (),
46 shared_ptr<const TransferFunction> (new GammaTransferFunction (2.6))
51 ColourConversion const &
52 ColourConversion::rec601_to_xyz ()
54 static ColourConversion* c = new ColourConversion (
55 shared_ptr<const TransferFunction> (new GammaTransferFunction (2.2)),
57 Chromaticity (0.64, 0.33),
58 Chromaticity (0.3, 0.6),
59 Chromaticity (0.15, 0.06),
61 Chromaticity (0.3127, 0.329),
62 optional<Chromaticity> (),
63 shared_ptr<const TransferFunction> (new GammaTransferFunction (2.6))
68 ColourConversion const &
69 ColourConversion::rec709_to_xyz ()
71 static ColourConversion* c = new ColourConversion (
72 shared_ptr<const TransferFunction> (new GammaTransferFunction (2.2)),
74 Chromaticity (0.64, 0.33),
75 Chromaticity (0.3, 0.6),
76 Chromaticity (0.15, 0.06),
78 Chromaticity (0.3127, 0.329),
79 optional<Chromaticity> (),
80 shared_ptr<const TransferFunction> (new GammaTransferFunction (2.6))
85 ColourConversion const &
86 ColourConversion::p3_to_xyz ()
88 static ColourConversion* c = new ColourConversion (
89 shared_ptr<const TransferFunction> (new GammaTransferFunction (2.6)),
91 Chromaticity (0.68, 0.32),
92 Chromaticity (0.265, 0.69),
93 Chromaticity (0.15, 0.06),
94 Chromaticity (0.314, 0.351),
95 optional<Chromaticity> (),
96 shared_ptr<const TransferFunction> (new GammaTransferFunction (2.6))
101 ColourConversion const &
102 ColourConversion::rec1886_to_xyz ()
104 /* According to Olivier on DCP-o-matic bug #832, Rec. 1886 is Rec. 709 with
105 2.4 gamma, so here goes ...
107 static ColourConversion* c = new ColourConversion (
108 shared_ptr<const TransferFunction> (new GammaTransferFunction (2.4)),
110 Chromaticity (0.64, 0.33),
111 Chromaticity (0.3, 0.6),
112 Chromaticity (0.15, 0.06),
114 Chromaticity (0.3127, 0.329),
115 optional<Chromaticity> (),
116 shared_ptr<const TransferFunction> (new GammaTransferFunction (2.6))
121 ColourConversion const &
122 ColourConversion::rec2020_to_xyz ()
125 static ColourConversion* c = new ColourConversion (
126 shared_ptr<const TransferFunction> (new ModifiedGammaTransferFunction (1 / 0.45, 0.08145, 0.0993, 4.5)),
128 Chromaticity (0.708, 0.292),
129 Chromaticity (0.170, 0.797),
130 Chromaticity (0.131, 0.046),
132 Chromaticity (0.3127, 0.329),
133 optional<Chromaticity> (),
134 shared_ptr<const TransferFunction> (new GammaTransferFunction (2.6))
139 ColourConversion::ColourConversion (
140 shared_ptr<const TransferFunction> in,
146 optional<Chromaticity> adjusted_white,
147 shared_ptr<const TransferFunction> out
150 , _yuv_to_rgb (yuv_to_rgb)
155 , _adjusted_white (adjusted_white)
162 ColourConversion::about_equal (ColourConversion const & other, float epsilon) const
164 if (!_in->about_equal (other._in, epsilon) ||
165 _yuv_to_rgb != other._yuv_to_rgb ||
166 !_red.about_equal (other._red, epsilon) ||
167 !_green.about_equal (other._green, epsilon) ||
168 !_blue.about_equal (other._blue, epsilon) ||
169 !_white.about_equal (other._white, epsilon) ||
170 !_out->about_equal (other._out, epsilon)) {
174 if (!_adjusted_white && !other._adjusted_white) {
179 _adjusted_white && other._adjusted_white &&
180 fabs (_adjusted_white.get().x - other._adjusted_white.get().x) < epsilon &&
181 fabs (_adjusted_white.get().y - other._adjusted_white.get().y) < epsilon
186 /* Otherwise one has an adjusted white and other hasn't, or they both have but different */
190 boost::numeric::ublas::matrix<double>
191 ColourConversion::rgb_to_xyz () const
193 /* See doc/design/colour.tex */
195 double const D = (_red.x - _white.x) * (_white.y - _blue.y) - (_white.x - _blue.x) * (_red.y - _white.y);
196 double const E = (_white.x - _green.x) * (_red.y - _white.y) - (_red.x - _white.x) * (_white.y - _green.y);
197 double const F = (_white.x - _green.x) * (_white.y - _blue.y) - (_white.x - _blue.x) * (_white.y - _green.y);
198 double const P = _red.y + _green.y * D / F + _blue.y * E / F;
200 boost::numeric::ublas::matrix<double> C (3, 3);
201 C(0, 0) = _red.x / P;
202 C(0, 1) = _green.x * D / (F * P);
203 C(0, 2) = _blue.x * E / (F * P);
204 C(1, 0) = _red.y / P;
205 C(1, 1) = _green.y * D / (F * P);
206 C(1, 2) = _blue.y * E / (F * P);
207 C(2, 0) = _red.z() / P;
208 C(2, 1) = _green.z() * D / (F * P);
209 C(2, 2) = _blue.z() * E / (F * P);
213 boost::numeric::ublas::matrix<double>
214 ColourConversion::xyz_to_rgb () const
216 boost::numeric::ublas::matrix<double> A (rgb_to_xyz ());
218 /* permutation matrix for the LU-factorization */
219 boost::numeric::ublas::permutation_matrix<std::size_t> pm (A.size1 ());
221 /* perform LU-factorization */
222 int const r = lu_factorize (A, pm);
225 /* create identity matrix of inverse */
226 boost::numeric::ublas::matrix<double> xyz_to_rgb (3, 3);
227 xyz_to_rgb.assign (boost::numeric::ublas::identity_matrix<double> (A.size1 ()));
229 /* backsubstitute to get the inverse */
230 lu_substitute (A, pm, xyz_to_rgb);
235 boost::numeric::ublas::matrix<double>
236 ColourConversion::bradford () const
238 if (!_adjusted_white || fabs (_adjusted_white.get().x) < 1e-6 || fabs (_adjusted_white.get().y) < 1e-6) {
239 boost::numeric::ublas::matrix<double> B = boost::numeric::ublas::zero_matrix<double> (3, 3);
246 /* See doc/design/colour.tex */
248 boost::numeric::ublas::matrix<double> M (3, 3);
259 boost::numeric::ublas::matrix<double> Mi (3, 3);
260 Mi(0, 0) = 0.9869929055;
261 Mi(0, 1) = -0.1470542564;
262 Mi(0, 2) = 0.1599626517;
263 Mi(1, 0) = 0.4323052697;
264 Mi(1, 1) = 0.5183602715;
265 Mi(1, 2) = 0.0492912282;
266 Mi(2, 0) = -0.0085286646;
267 Mi(2, 1) = 0.0400428217;
268 Mi(2, 2) = 0.9684866958;
270 boost::numeric::ublas::matrix<double> Gp (3, 1);
271 Gp(0, 0) = _white.x / _white.y;
273 Gp(2, 0) = (1 - _white.x - _white.y) / _white.y;
275 boost::numeric::ublas::matrix<double> G = boost::numeric::ublas::prod (M, Gp);
277 boost::numeric::ublas::matrix<double> Hp (3, 1);
278 Hp(0, 0) = _adjusted_white.get().x / _adjusted_white.get().y;
280 Hp(2, 0) = (1 - _adjusted_white.get().x - _adjusted_white.get().y) / _adjusted_white.get().y;
282 boost::numeric::ublas::matrix<double> H = boost::numeric::ublas::prod (M, Hp);
284 boost::numeric::ublas::matrix<double> C = boost::numeric::ublas::zero_matrix<double> (3, 3);
285 C(0, 0) = H(0, 0) / G(0, 0);
286 C(1, 1) = H(1, 0) / G(1, 0);
287 C(2, 2) = H(2, 0) / G(2, 0);
289 boost::numeric::ublas::matrix<double> CM = boost::numeric::ublas::prod (C, M);
290 return boost::numeric::ublas::prod (Mi, CM);