2 Copyright (C) 2014-2015 Carl Hetherington <cth@carlh.net>
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.
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.
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.
20 #include "colour_conversion.h"
21 #include "gamma_transfer_function.h"
22 #include "modified_gamma_transfer_function.h"
23 #include "colour_matrix.h"
24 #include "dcp_assert.h"
25 #include <boost/numeric/ublas/matrix.hpp>
26 #include <boost/numeric/ublas/lu.hpp>
27 #include <boost/numeric/ublas/io.hpp>
29 using boost::shared_ptr;
30 using boost::optional;
33 ColourConversion const &
34 ColourConversion::srgb_to_xyz ()
36 static ColourConversion* c = new ColourConversion (
37 shared_ptr<const TransferFunction> (new ModifiedGammaTransferFunction (2.4, 0.04045, 0.055, 12.92)),
39 Chromaticity (0.64, 0.33),
40 Chromaticity (0.3, 0.6),
41 Chromaticity (0.15, 0.06),
43 Chromaticity (0.3127, 0.329),
44 optional<Chromaticity> (),
45 shared_ptr<const TransferFunction> (new GammaTransferFunction (2.6))
50 ColourConversion const &
51 ColourConversion::rec601_to_xyz ()
53 static ColourConversion* c = new ColourConversion (
54 shared_ptr<const TransferFunction> (new GammaTransferFunction (2.2)),
56 Chromaticity (0.64, 0.33),
57 Chromaticity (0.3, 0.6),
58 Chromaticity (0.15, 0.06),
60 Chromaticity (0.3127, 0.329),
61 optional<Chromaticity> (),
62 shared_ptr<const TransferFunction> (new GammaTransferFunction (2.6))
67 ColourConversion const &
68 ColourConversion::rec709_to_xyz ()
70 static ColourConversion* c = new ColourConversion (
71 shared_ptr<const TransferFunction> (new GammaTransferFunction (2.2)),
73 Chromaticity (0.64, 0.33),
74 Chromaticity (0.3, 0.6),
75 Chromaticity (0.15, 0.06),
77 Chromaticity (0.3127, 0.329),
78 optional<Chromaticity> (),
79 shared_ptr<const TransferFunction> (new GammaTransferFunction (2.6))
84 ColourConversion const &
85 ColourConversion::p3_to_xyz ()
87 static ColourConversion* c = new ColourConversion (
88 shared_ptr<const TransferFunction> (new GammaTransferFunction (2.6)),
90 Chromaticity (0.68, 0.32),
91 Chromaticity (0.265, 0.69),
92 Chromaticity (0.15, 0.06),
93 Chromaticity (0.314, 0.351),
94 optional<Chromaticity> (),
95 shared_ptr<const TransferFunction> (new GammaTransferFunction (2.6))
100 ColourConversion const &
101 ColourConversion::rec1886_to_xyz ()
103 /* According to Olivier on DCP-o-matic bug #832, Rec. 1886 is Rec. 709 with
104 2.4 gamma, so here goes ...
106 static ColourConversion* c = new ColourConversion (
107 shared_ptr<const TransferFunction> (new GammaTransferFunction (2.4)),
109 Chromaticity (0.64, 0.33),
110 Chromaticity (0.3, 0.6),
111 Chromaticity (0.15, 0.06),
113 Chromaticity (0.3127, 0.329),
114 optional<Chromaticity> (),
115 shared_ptr<const TransferFunction> (new GammaTransferFunction (2.6))
120 ColourConversion const &
121 ColourConversion::rec2020_to_xyz ()
124 static ColourConversion* c = new ColourConversion (
125 shared_ptr<const TransferFunction> (new ModifiedGammaTransferFunction (1 / 0.45, 0.08145, 0.0993, 4.5)),
127 Chromaticity (0.708, 0.292),
128 Chromaticity (0.170, 0.797),
129 Chromaticity (0.131, 0.046),
131 Chromaticity (0.3127, 0.329),
132 optional<Chromaticity> (),
133 shared_ptr<const TransferFunction> (new GammaTransferFunction (2.6))
138 ColourConversion::ColourConversion (
139 shared_ptr<const TransferFunction> in,
145 optional<Chromaticity> adjusted_white,
146 shared_ptr<const TransferFunction> out
149 , _yuv_to_rgb (yuv_to_rgb)
154 , _adjusted_white (adjusted_white)
161 ColourConversion::about_equal (ColourConversion const & other, float epsilon) const
163 if (!_in->about_equal (other._in, epsilon) ||
164 _yuv_to_rgb != other._yuv_to_rgb ||
165 !_red.about_equal (other._red, epsilon) ||
166 !_green.about_equal (other._green, epsilon) ||
167 !_blue.about_equal (other._blue, epsilon) ||
168 !_white.about_equal (other._white, epsilon) ||
169 !_out->about_equal (other._out, epsilon)) {
173 if (!_adjusted_white && !other._adjusted_white) {
178 _adjusted_white && other._adjusted_white &&
179 fabs (_adjusted_white.get().x - other._adjusted_white.get().x) < epsilon &&
180 fabs (_adjusted_white.get().y - other._adjusted_white.get().y) < epsilon
185 /* Otherwise one has an adjusted white and other hasn't, or they both have but different */
189 boost::numeric::ublas::matrix<double>
190 ColourConversion::rgb_to_xyz () const
192 /* See doc/design/colour.tex */
194 double const D = (_red.x - _white.x) * (_white.y - _blue.y) - (_white.x - _blue.x) * (_red.y - _white.y);
195 double const E = (_white.x - _green.x) * (_red.y - _white.y) - (_red.x - _white.x) * (_white.y - _green.y);
196 double const F = (_white.x - _green.x) * (_white.y - _blue.y) - (_white.x - _blue.x) * (_white.y - _green.y);
197 double const P = _red.y + _green.y * D / F + _blue.y * E / F;
199 boost::numeric::ublas::matrix<double> C (3, 3);
200 C(0, 0) = _red.x / P;
201 C(0, 1) = _green.x * D / (F * P);
202 C(0, 2) = _blue.x * E / (F * P);
203 C(1, 0) = _red.y / P;
204 C(1, 1) = _green.y * D / (F * P);
205 C(1, 2) = _blue.y * E / (F * P);
206 C(2, 0) = _red.z() / P;
207 C(2, 1) = _green.z() * D / (F * P);
208 C(2, 2) = _blue.z() * E / (F * P);
212 boost::numeric::ublas::matrix<double>
213 ColourConversion::xyz_to_rgb () const
215 boost::numeric::ublas::matrix<double> A (rgb_to_xyz ());
217 /* permutation matrix for the LU-factorization */
218 boost::numeric::ublas::permutation_matrix<std::size_t> pm (A.size1 ());
220 /* perform LU-factorization */
221 int const r = lu_factorize (A, pm);
224 /* create identity matrix of inverse */
225 boost::numeric::ublas::matrix<double> xyz_to_rgb (3, 3);
226 xyz_to_rgb.assign (boost::numeric::ublas::identity_matrix<double> (A.size1 ()));
228 /* backsubstitute to get the inverse */
229 lu_substitute (A, pm, xyz_to_rgb);
234 boost::numeric::ublas::matrix<double>
235 ColourConversion::bradford () const
237 if (!_adjusted_white || fabs (_adjusted_white.get().x) < 1e-6 || fabs (_adjusted_white.get().y) < 1e-6) {
238 boost::numeric::ublas::matrix<double> B = boost::numeric::ublas::zero_matrix<double> (3, 3);
245 /* See doc/design/colour.tex */
247 boost::numeric::ublas::matrix<double> M (3, 3);
258 boost::numeric::ublas::matrix<double> Mi (3, 3);
259 Mi(0, 0) = 0.9869929055;
260 Mi(0, 1) = -0.1470542564;
261 Mi(0, 2) = 0.1599626517;
262 Mi(1, 0) = 0.4323052697;
263 Mi(1, 1) = 0.5183602715;
264 Mi(1, 2) = 0.0492912282;
265 Mi(2, 0) = -0.0085286646;
266 Mi(2, 1) = 0.0400428217;
267 Mi(2, 2) = 0.9684866958;
269 boost::numeric::ublas::matrix<double> Gp (3, 1);
270 Gp(0, 0) = _white.x / _white.y;
272 Gp(2, 0) = (1 - _white.x - _white.y) / _white.y;
274 boost::numeric::ublas::matrix<double> G = boost::numeric::ublas::prod (M, Gp);
276 boost::numeric::ublas::matrix<double> Hp (3, 1);
277 Hp(0, 0) = _adjusted_white.get().x / _adjusted_white.get().y;
279 Hp(2, 0) = (1 - _adjusted_white.get().x - _adjusted_white.get().y) / _adjusted_white.get().y;
281 boost::numeric::ublas::matrix<double> H = boost::numeric::ublas::prod (M, Hp);
283 boost::numeric::ublas::matrix<double> C = boost::numeric::ublas::zero_matrix<double> (3, 3);
284 C(0, 0) = H(0, 0) / G(0, 0);
285 C(1, 1) = H(1, 0) / G(1, 0);
286 C(2, 2) = H(2, 0) / G(2, 0);
288 boost::numeric::ublas::matrix<double> CM = boost::numeric::ublas::prod (C, M);
289 return boost::numeric::ublas::prod (Mi, CM);