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::ColourConversion (
85 shared_ptr<const TransferFunction> in,
91 optional<Chromaticity> adjusted_white,
92 shared_ptr<const TransferFunction> out
95 , _yuv_to_rgb (yuv_to_rgb)
100 , _adjusted_white (adjusted_white)
107 ColourConversion::about_equal (ColourConversion const & other, float epsilon) const
109 if (!_in->about_equal (other._in, epsilon) ||
110 _yuv_to_rgb != other._yuv_to_rgb ||
111 !_red.about_equal (other._red, epsilon) ||
112 !_green.about_equal (other._green, epsilon) ||
113 !_blue.about_equal (other._blue, epsilon) ||
114 !_white.about_equal (other._white, epsilon) ||
115 !_out->about_equal (other._out, epsilon)) {
119 if (!_adjusted_white && !other._adjusted_white) {
124 _adjusted_white && other._adjusted_white &&
125 fabs (_adjusted_white.get().x - other._adjusted_white.get().x) < epsilon &&
126 fabs (_adjusted_white.get().y - other._adjusted_white.get().y) < epsilon
131 /* Otherwise one has an adjusted white and other hasn't, or they both have but different */
135 boost::numeric::ublas::matrix<double>
136 ColourConversion::rgb_to_xyz () const
138 /* See doc/design/colour.tex */
140 double const D = (_red.x - _white.x) * (_white.y - _blue.y) - (_white.x - _blue.x) * (_red.y - _white.y);
141 double const E = (_white.x - _green.x) * (_red.y - _white.y) - (_red.x - _white.x) * (_white.y - _green.y);
142 double const F = (_white.x - _green.x) * (_white.y - _blue.y) - (_white.x - _blue.x) * (_white.y - _green.y);
143 double const P = _red.y + _green.y * D / F + _blue.y * E / F;
145 boost::numeric::ublas::matrix<double> C (3, 3);
146 C(0, 0) = _red.x / P;
147 C(0, 1) = _green.x * D / (F * P);
148 C(0, 2) = _blue.x * E / (F * P);
149 C(1, 0) = _red.y / P;
150 C(1, 1) = _green.y * D / (F * P);
151 C(1, 2) = _blue.y * E / (F * P);
152 C(2, 0) = _red.z() / P;
153 C(2, 1) = _green.z() * D / (F * P);
154 C(2, 2) = _blue.z() * E / (F * P);
158 boost::numeric::ublas::matrix<double>
159 ColourConversion::xyz_to_rgb () const
161 boost::numeric::ublas::matrix<double> A (rgb_to_xyz ());
163 /* permutation matrix for the LU-factorization */
164 boost::numeric::ublas::permutation_matrix<std::size_t> pm (A.size1 ());
166 /* perform LU-factorization */
167 int const r = lu_factorize (A, pm);
170 /* create identity matrix of inverse */
171 boost::numeric::ublas::matrix<double> xyz_to_rgb;
172 xyz_to_rgb.assign (boost::numeric::ublas::identity_matrix<double> (A.size1 ()));
174 /* backsubstitute to get the inverse */
175 lu_substitute (A, pm, xyz_to_rgb);
180 boost::numeric::ublas::matrix<double>
181 ColourConversion::bradford () const
183 if (!_adjusted_white || fabs (_adjusted_white.get().x) < 1e-6 || fabs (_adjusted_white.get().y) < 1e-6) {
184 boost::numeric::ublas::matrix<double> B = boost::numeric::ublas::zero_matrix<double> (3, 3);
191 /* See doc/design/colour.tex */
193 boost::numeric::ublas::matrix<double> M (3, 3);
204 boost::numeric::ublas::matrix<double> Mi (3, 3);
205 Mi(0, 0) = 0.9869929055;
206 Mi(0, 1) = -0.1470542564;
207 Mi(0, 2) = 0.1599626517;
208 Mi(1, 0) = 0.4323052697;
209 Mi(1, 1) = 0.5183602715;
210 Mi(1, 2) = 0.0492912282;
211 Mi(2, 0) = -0.0085286646;
212 Mi(2, 1) = 0.0400428217;
213 Mi(2, 2) = 0.9684866958;
215 boost::numeric::ublas::matrix<double> Gp (3, 1);
216 Gp(0, 0) = _white.x / _white.y;
218 Gp(2, 0) = (1 - _white.x - _white.y) / _white.y;
220 boost::numeric::ublas::matrix<double> G = boost::numeric::ublas::prod (M, Gp);
222 boost::numeric::ublas::matrix<double> Hp (3, 1);
223 Hp(0, 0) = _adjusted_white.get().x / _adjusted_white.get().y;
225 Hp(2, 0) = (1 - _adjusted_white.get().x - _adjusted_white.get().y) / _adjusted_white.get().y;
227 boost::numeric::ublas::matrix<double> H = boost::numeric::ublas::prod (M, Hp);
229 boost::numeric::ublas::matrix<double> C = boost::numeric::ublas::zero_matrix<double> (3, 3);
230 C(0, 0) = H(0, 0) / G(0, 0);
231 C(1, 1) = H(1, 0) / G(1, 0);
232 C(2, 2) = H(2, 0) / G(2, 0);
234 boost::numeric::ublas::matrix<double> CM = boost::numeric::ublas::prod (C, M);
235 return boost::numeric::ublas::prod (Mi, CM);