No-op: whitespace.
[libdcp.git] / src / colour_conversion.cc
1 /*
2     Copyright (C) 2014 Carl Hetherington <cth@carlh.net>
3
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.
8
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.
13
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.
17
18 */
19
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>
28
29 using boost::shared_ptr;
30 using boost::optional;
31 using namespace dcp;
32
33 ColourConversion const &
34 ColourConversion::srgb_to_xyz ()
35 {
36         static ColourConversion* c = new ColourConversion (
37                 shared_ptr<const TransferFunction> (new ModifiedGammaTransferFunction (2.4, 0.04045, 0.055, 12.92)),
38                 YUV_TO_RGB_REC601,
39                 Chromaticity (0.64, 0.33),
40                 Chromaticity (0.3, 0.6),
41                 Chromaticity (0.15, 0.06),
42                 /* D65 */
43                 Chromaticity (0.3127, 0.329),
44                 optional<Chromaticity> (),
45                 shared_ptr<const TransferFunction> (new GammaTransferFunction (2.6))
46                 );
47         return *c;
48 }
49
50 ColourConversion const &
51 ColourConversion::rec601_to_xyz ()
52 {
53         static ColourConversion* c = new ColourConversion (
54                 shared_ptr<const TransferFunction> (new ModifiedGammaTransferFunction (1 / 0.45, 0.081, 0.099, 4.5)),
55                 YUV_TO_RGB_REC601,
56                 Chromaticity (0.64, 0.33),
57                 Chromaticity (0.3, 0.6),
58                 Chromaticity (0.15, 0.06),
59                 /* D65 */
60                 Chromaticity (0.3127, 0.329),
61                 optional<Chromaticity> (),
62                 shared_ptr<const TransferFunction> (new GammaTransferFunction (2.6))
63                 );
64         return *c;
65 }
66
67 ColourConversion const &
68 ColourConversion::rec709_to_xyz ()
69 {
70         static ColourConversion* c = new ColourConversion (
71                 shared_ptr<const TransferFunction> (new ModifiedGammaTransferFunction (1 / 0.45, 0.081, 0.099, 4.5)),
72                 YUV_TO_RGB_REC709,
73                 Chromaticity (0.64, 0.33),
74                 Chromaticity (0.3, 0.6),
75                 Chromaticity (0.15, 0.06),
76                 /* D65 */
77                 Chromaticity (0.3127, 0.329),
78                 optional<Chromaticity> (),
79                 shared_ptr<const TransferFunction> (new GammaTransferFunction (2.6))
80                 );
81         return *c;
82 }
83
84 ColourConversion const &
85 ColourConversion::p3_to_xyz ()
86 {
87         static ColourConversion* c = new ColourConversion (
88                 shared_ptr<const TransferFunction> (new GammaTransferFunction (2.6)),
89                 YUV_TO_RGB_REC709,
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))
96                 );
97         return *c;
98 }
99
100 ColourConversion::ColourConversion (
101         shared_ptr<const TransferFunction> in,
102         YUVToRGB yuv_to_rgb,
103         Chromaticity red,
104         Chromaticity green,
105         Chromaticity blue,
106         Chromaticity white,
107         optional<Chromaticity> adjusted_white,
108         shared_ptr<const TransferFunction> out
109         )
110         : _in (in)
111         , _yuv_to_rgb (yuv_to_rgb)
112         , _red (red)
113         , _green (green)
114         , _blue (blue)
115         , _white (white)
116         , _adjusted_white (adjusted_white)
117         , _out (out)
118 {
119
120 }
121
122 bool
123 ColourConversion::about_equal (ColourConversion const & other, float epsilon) const
124 {
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)) {
132                 return false;
133         }
134
135         if (!_adjusted_white && !other._adjusted_white) {
136                 return true;
137         }
138
139         if (
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
143                 ) {
144                 return true;
145         }
146
147         /* Otherwise one has an adjusted white and other hasn't, or they both have but different */
148         return false;
149 }
150
151 boost::numeric::ublas::matrix<double>
152 ColourConversion::rgb_to_xyz () const
153 {
154         /* See doc/design/colour.tex */
155
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;
160
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);
171         return C;
172 }
173
174 boost::numeric::ublas::matrix<double>
175 ColourConversion::xyz_to_rgb () const
176 {
177         boost::numeric::ublas::matrix<double> A (rgb_to_xyz ());
178
179         /* permutation matrix for the LU-factorization */
180         boost::numeric::ublas::permutation_matrix<std::size_t> pm (A.size1 ());
181
182         /* perform LU-factorization */
183         int const r = lu_factorize (A, pm);
184         DCP_ASSERT (r == 0);
185
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 ()));
189
190         /* backsubstitute to get the inverse */
191         lu_substitute (A, pm, xyz_to_rgb);
192
193         return xyz_to_rgb;
194 }
195
196 boost::numeric::ublas::matrix<double>
197 ColourConversion::bradford () const
198 {
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);
201                 B(0, 0) = 1;
202                 B(1, 1) = 1;
203                 B(2, 2) = 1;
204                 return B;
205         }
206
207         /* See doc/design/colour.tex */
208
209         boost::numeric::ublas::matrix<double> M (3, 3);
210         M(0, 0) = 0.8951;
211         M(0, 1) = 0.2664;
212         M(0, 2) = -0.1614;
213         M(1, 0) = -0.7502;
214         M(1, 1) = 1.7135;
215         M(1, 2) = 0.0367;
216         M(2, 0) = 0.0389;
217         M(2, 1) = -0.0685;
218         M(2, 2) = 1.0296;
219
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;
230
231         boost::numeric::ublas::matrix<double> Gp (3, 1);
232         Gp(0, 0) = _white.x / _white.y;
233         Gp(1, 0) = 1;
234         Gp(2, 0) = (1 - _white.x - _white.y) / _white.y;
235
236         boost::numeric::ublas::matrix<double> G = boost::numeric::ublas::prod (M, Gp);
237
238         boost::numeric::ublas::matrix<double> Hp (3, 1);
239         Hp(0, 0) = _adjusted_white.get().x / _adjusted_white.get().y;
240         Hp(1, 0) = 1;
241         Hp(2, 0) = (1 - _adjusted_white.get().x - _adjusted_white.get().y) / _adjusted_white.get().y;
242
243         boost::numeric::ublas::matrix<double> H = boost::numeric::ublas::prod (M, Hp);
244
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);
249
250         boost::numeric::ublas::matrix<double> CM = boost::numeric::ublas::prod (C, M);
251         return boost::numeric::ublas::prod (Mi, CM);
252 }