Add somewhat speculative Rec 1886 and Rec 2020 colour conversions.
[libdcp.git] / src / colour_conversion.cc
1 /*
2     Copyright (C) 2014-2015 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 GammaTransferFunction (2.2)),
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 GammaTransferFunction (2.2)),
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 const &
101 ColourConversion::rec1886_to_xyz ()
102 {
103         /* According to Olivier on DCP-o-matic bug #832, Rec. 1886 is Rec. 709 with
104            2.4 gamma, so here goes ...
105         */
106         static ColourConversion* c = new ColourConversion (
107                 shared_ptr<const TransferFunction> (new GammaTransferFunction (2.4)),
108                 YUV_TO_RGB_REC709,
109                 Chromaticity (0.64, 0.33),
110                 Chromaticity (0.3, 0.6),
111                 Chromaticity (0.15, 0.06),
112                 /* D65 */
113                 Chromaticity (0.3127, 0.329),
114                 optional<Chromaticity> (),
115                 shared_ptr<const TransferFunction> (new GammaTransferFunction (2.6))
116                 );
117         return *c;
118 }
119
120 ColourConversion const &
121 ColourConversion::rec2020_to_xyz ()
122 {
123         /* From Wikipedia */
124         static ColourConversion* c = new ColourConversion (
125                 shared_ptr<const TransferFunction> (new ModifiedGammaTransferFunction (1 / 0.45, 0.08145, 0.0993, 4.5)),
126                 YUV_TO_RGB_REC709,
127                 Chromaticity (0.708, 0.292),
128                 Chromaticity (0.170, 0.797),
129                 Chromaticity (0.131, 0.046),
130                 /* D65 */
131                 Chromaticity (0.3127, 0.329),
132                 optional<Chromaticity> (),
133                 shared_ptr<const TransferFunction> (new GammaTransferFunction (2.6))
134                 );
135         return *c;
136 }
137
138 ColourConversion::ColourConversion (
139         shared_ptr<const TransferFunction> in,
140         YUVToRGB yuv_to_rgb,
141         Chromaticity red,
142         Chromaticity green,
143         Chromaticity blue,
144         Chromaticity white,
145         optional<Chromaticity> adjusted_white,
146         shared_ptr<const TransferFunction> out
147         )
148         : _in (in)
149         , _yuv_to_rgb (yuv_to_rgb)
150         , _red (red)
151         , _green (green)
152         , _blue (blue)
153         , _white (white)
154         , _adjusted_white (adjusted_white)
155         , _out (out)
156 {
157
158 }
159
160 bool
161 ColourConversion::about_equal (ColourConversion const & other, float epsilon) const
162 {
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)) {
170                 return false;
171         }
172
173         if (!_adjusted_white && !other._adjusted_white) {
174                 return true;
175         }
176
177         if (
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
181                 ) {
182                 return true;
183         }
184
185         /* Otherwise one has an adjusted white and other hasn't, or they both have but different */
186         return false;
187 }
188
189 boost::numeric::ublas::matrix<double>
190 ColourConversion::rgb_to_xyz () const
191 {
192         /* See doc/design/colour.tex */
193
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;
198
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);
209         return C;
210 }
211
212 boost::numeric::ublas::matrix<double>
213 ColourConversion::xyz_to_rgb () const
214 {
215         boost::numeric::ublas::matrix<double> A (rgb_to_xyz ());
216
217         /* permutation matrix for the LU-factorization */
218         boost::numeric::ublas::permutation_matrix<std::size_t> pm (A.size1 ());
219
220         /* perform LU-factorization */
221         int const r = lu_factorize (A, pm);
222         DCP_ASSERT (r == 0);
223
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 ()));
227
228         /* backsubstitute to get the inverse */
229         lu_substitute (A, pm, xyz_to_rgb);
230
231         return xyz_to_rgb;
232 }
233
234 boost::numeric::ublas::matrix<double>
235 ColourConversion::bradford () const
236 {
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);
239                 B(0, 0) = 1;
240                 B(1, 1) = 1;
241                 B(2, 2) = 1;
242                 return B;
243         }
244
245         /* See doc/design/colour.tex */
246
247         boost::numeric::ublas::matrix<double> M (3, 3);
248         M(0, 0) = 0.8951;
249         M(0, 1) = 0.2664;
250         M(0, 2) = -0.1614;
251         M(1, 0) = -0.7502;
252         M(1, 1) = 1.7135;
253         M(1, 2) = 0.0367;
254         M(2, 0) = 0.0389;
255         M(2, 1) = -0.0685;
256         M(2, 2) = 1.0296;
257
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;
268
269         boost::numeric::ublas::matrix<double> Gp (3, 1);
270         Gp(0, 0) = _white.x / _white.y;
271         Gp(1, 0) = 1;
272         Gp(2, 0) = (1 - _white.x - _white.y) / _white.y;
273
274         boost::numeric::ublas::matrix<double> G = boost::numeric::ublas::prod (M, Gp);
275
276         boost::numeric::ublas::matrix<double> Hp (3, 1);
277         Hp(0, 0) = _adjusted_white.get().x / _adjusted_white.get().y;
278         Hp(1, 0) = 1;
279         Hp(2, 0) = (1 - _adjusted_white.get().x - _adjusted_white.get().y) / _adjusted_white.get().y;
280
281         boost::numeric::ublas::matrix<double> H = boost::numeric::ublas::prod (M, Hp);
282
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);
287
288         boost::numeric::ublas::matrix<double> CM = boost::numeric::ublas::prod (C, M);
289         return boost::numeric::ublas::prod (Mi, CM);
290 }