2 Copyright (C) 2014 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 ModifiedGammaTransferFunction (1 / 0.45, 0.081, 0.099, 4.5)),
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 ModifiedGammaTransferFunction (1 / 0.45, 0.081, 0.099, 4.5)),
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::ColourConversion (
101 shared_ptr<const TransferFunction> in,
107 optional<Chromaticity> adjusted_white,
108 shared_ptr<const TransferFunction> out
111 , _yuv_to_rgb (yuv_to_rgb)
116 , _adjusted_white (adjusted_white)
123 ColourConversion::about_equal (ColourConversion const & other, float epsilon) const
125 if (!_in->about_equal (other._in, epsilon) ||
126 _yuv_to_rgb != other._yuv_to_rgb ||
127 !_red.about_equal (other._red, epsilon) ||
128 !_green.about_equal (other._green, epsilon) ||
129 !_blue.about_equal (other._blue, epsilon) ||
130 !_white.about_equal (other._white, epsilon) ||
131 !_out->about_equal (other._out, epsilon)) {
135 if (!_adjusted_white && !other._adjusted_white) {
140 _adjusted_white && other._adjusted_white &&
141 fabs (_adjusted_white.get().x - other._adjusted_white.get().x) < epsilon &&
142 fabs (_adjusted_white.get().y - other._adjusted_white.get().y) < epsilon
147 /* Otherwise one has an adjusted white and other hasn't, or they both have but different */
151 boost::numeric::ublas::matrix<double>
152 ColourConversion::rgb_to_xyz () const
154 /* See doc/design/colour.tex */
156 double const D = (_red.x - _white.x) * (_white.y - _blue.y) - (_white.x - _blue.x) * (_red.y - _white.y);
157 double const E = (_white.x - _green.x) * (_red.y - _white.y) - (_red.x - _white.x) * (_white.y - _green.y);
158 double const F = (_white.x - _green.x) * (_white.y - _blue.y) - (_white.x - _blue.x) * (_white.y - _green.y);
159 double const P = _red.y + _green.y * D / F + _blue.y * E / F;
161 boost::numeric::ublas::matrix<double> C (3, 3);
162 C(0, 0) = _red.x / P;
163 C(0, 1) = _green.x * D / (F * P);
164 C(0, 2) = _blue.x * E / (F * P);
165 C(1, 0) = _red.y / P;
166 C(1, 1) = _green.y * D / (F * P);
167 C(1, 2) = _blue.y * E / (F * P);
168 C(2, 0) = _red.z() / P;
169 C(2, 1) = _green.z() * D / (F * P);
170 C(2, 2) = _blue.z() * E / (F * P);
174 boost::numeric::ublas::matrix<double>
175 ColourConversion::xyz_to_rgb () const
177 boost::numeric::ublas::matrix<double> A (rgb_to_xyz ());
179 /* permutation matrix for the LU-factorization */
180 boost::numeric::ublas::permutation_matrix<std::size_t> pm (A.size1 ());
182 /* perform LU-factorization */
183 int const r = lu_factorize (A, pm);
186 /* create identity matrix of inverse */
187 boost::numeric::ublas::matrix<double> xyz_to_rgb (3, 3);
188 xyz_to_rgb.assign (boost::numeric::ublas::identity_matrix<double> (A.size1 ()));
190 /* backsubstitute to get the inverse */
191 lu_substitute (A, pm, xyz_to_rgb);
196 boost::numeric::ublas::matrix<double>
197 ColourConversion::bradford () const
199 if (!_adjusted_white || fabs (_adjusted_white.get().x) < 1e-6 || fabs (_adjusted_white.get().y) < 1e-6) {
200 boost::numeric::ublas::matrix<double> B = boost::numeric::ublas::zero_matrix<double> (3, 3);
207 /* See doc/design/colour.tex */
209 boost::numeric::ublas::matrix<double> M (3, 3);
220 boost::numeric::ublas::matrix<double> Mi (3, 3);
221 Mi(0, 0) = 0.9869929055;
222 Mi(0, 1) = -0.1470542564;
223 Mi(0, 2) = 0.1599626517;
224 Mi(1, 0) = 0.4323052697;
225 Mi(1, 1) = 0.5183602715;
226 Mi(1, 2) = 0.0492912282;
227 Mi(2, 0) = -0.0085286646;
228 Mi(2, 1) = 0.0400428217;
229 Mi(2, 2) = 0.9684866958;
231 boost::numeric::ublas::matrix<double> Gp (3, 1);
232 Gp(0, 0) = _white.x / _white.y;
234 Gp(2, 0) = (1 - _white.x - _white.y) / _white.y;
236 boost::numeric::ublas::matrix<double> G = boost::numeric::ublas::prod (M, Gp);
238 boost::numeric::ublas::matrix<double> Hp (3, 1);
239 Hp(0, 0) = _adjusted_white.get().x / _adjusted_white.get().y;
241 Hp(2, 0) = (1 - _adjusted_white.get().x - _adjusted_white.get().y) / _adjusted_white.get().y;
243 boost::numeric::ublas::matrix<double> H = boost::numeric::ublas::prod (M, Hp);
245 boost::numeric::ublas::matrix<double> C = boost::numeric::ublas::zero_matrix<double> (3, 3);
246 C(0, 0) = H(0, 0) / G(0, 0);
247 C(1, 1) = H(1, 0) / G(1, 0);
248 C(2, 2) = H(2, 0) / G(2, 0);
250 boost::numeric::ublas::matrix<double> CM = boost::numeric::ublas::prod (C, M);
251 return boost::numeric::ublas::prod (Mi, CM);