Add Reader classes to permit much more efficient DCP reading.
[libdcp.git] / src / colour_conversion.cc
1 /*
2     Copyright (C) 2014-2015 Carl Hetherington <cth@carlh.net>
3
4     This file is part of libdcp.
5
6     libdcp is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     libdcp is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with libdcp.  If not, see <http://www.gnu.org/licenses/>.
18
19 */
20
21 #include "colour_conversion.h"
22 #include "gamma_transfer_function.h"
23 #include "modified_gamma_transfer_function.h"
24 #include "colour_matrix.h"
25 #include "dcp_assert.h"
26 #include <boost/numeric/ublas/matrix.hpp>
27 #include <boost/numeric/ublas/lu.hpp>
28 #include <boost/numeric/ublas/io.hpp>
29
30 using boost::shared_ptr;
31 using boost::optional;
32 using namespace dcp;
33
34 ColourConversion const &
35 ColourConversion::srgb_to_xyz ()
36 {
37         static ColourConversion* c = new ColourConversion (
38                 shared_ptr<const TransferFunction> (new ModifiedGammaTransferFunction (2.4, 0.04045, 0.055, 12.92)),
39                 YUV_TO_RGB_REC601,
40                 Chromaticity (0.64, 0.33),
41                 Chromaticity (0.3, 0.6),
42                 Chromaticity (0.15, 0.06),
43                 /* D65 */
44                 Chromaticity (0.3127, 0.329),
45                 optional<Chromaticity> (),
46                 shared_ptr<const TransferFunction> (new GammaTransferFunction (2.6))
47                 );
48         return *c;
49 }
50
51 ColourConversion const &
52 ColourConversion::rec601_to_xyz ()
53 {
54         static ColourConversion* c = new ColourConversion (
55                 shared_ptr<const TransferFunction> (new GammaTransferFunction (2.2)),
56                 YUV_TO_RGB_REC601,
57                 Chromaticity (0.64, 0.33),
58                 Chromaticity (0.3, 0.6),
59                 Chromaticity (0.15, 0.06),
60                 /* D65 */
61                 Chromaticity (0.3127, 0.329),
62                 optional<Chromaticity> (),
63                 shared_ptr<const TransferFunction> (new GammaTransferFunction (2.6))
64                 );
65         return *c;
66 }
67
68 ColourConversion const &
69 ColourConversion::rec709_to_xyz ()
70 {
71         static ColourConversion* c = new ColourConversion (
72                 shared_ptr<const TransferFunction> (new GammaTransferFunction (2.2)),
73                 YUV_TO_RGB_REC709,
74                 Chromaticity (0.64, 0.33),
75                 Chromaticity (0.3, 0.6),
76                 Chromaticity (0.15, 0.06),
77                 /* D65 */
78                 Chromaticity (0.3127, 0.329),
79                 optional<Chromaticity> (),
80                 shared_ptr<const TransferFunction> (new GammaTransferFunction (2.6))
81                 );
82         return *c;
83 }
84
85 ColourConversion const &
86 ColourConversion::p3_to_xyz ()
87 {
88         static ColourConversion* c = new ColourConversion (
89                 shared_ptr<const TransferFunction> (new GammaTransferFunction (2.6)),
90                 YUV_TO_RGB_REC709,
91                 Chromaticity (0.68, 0.32),
92                 Chromaticity (0.265, 0.69),
93                 Chromaticity (0.15, 0.06),
94                 Chromaticity (0.314, 0.351),
95                 optional<Chromaticity> (),
96                 shared_ptr<const TransferFunction> (new GammaTransferFunction (2.6))
97                 );
98         return *c;
99 }
100
101 ColourConversion const &
102 ColourConversion::rec1886_to_xyz ()
103 {
104         /* According to Olivier on DCP-o-matic bug #832, Rec. 1886 is Rec. 709 with
105            2.4 gamma, so here goes ...
106         */
107         static ColourConversion* c = new ColourConversion (
108                 shared_ptr<const TransferFunction> (new GammaTransferFunction (2.4)),
109                 YUV_TO_RGB_REC709,
110                 Chromaticity (0.64, 0.33),
111                 Chromaticity (0.3, 0.6),
112                 Chromaticity (0.15, 0.06),
113                 /* D65 */
114                 Chromaticity (0.3127, 0.329),
115                 optional<Chromaticity> (),
116                 shared_ptr<const TransferFunction> (new GammaTransferFunction (2.6))
117                 );
118         return *c;
119 }
120
121 ColourConversion const &
122 ColourConversion::rec2020_to_xyz ()
123 {
124         /* From Wikipedia */
125         static ColourConversion* c = new ColourConversion (
126                 shared_ptr<const TransferFunction> (new ModifiedGammaTransferFunction (1 / 0.45, 0.08145, 0.0993, 4.5)),
127                 YUV_TO_RGB_REC709,
128                 Chromaticity (0.708, 0.292),
129                 Chromaticity (0.170, 0.797),
130                 Chromaticity (0.131, 0.046),
131                 /* D65 */
132                 Chromaticity (0.3127, 0.329),
133                 optional<Chromaticity> (),
134                 shared_ptr<const TransferFunction> (new GammaTransferFunction (2.6))
135                 );
136         return *c;
137 }
138
139 ColourConversion::ColourConversion (
140         shared_ptr<const TransferFunction> in,
141         YUVToRGB yuv_to_rgb,
142         Chromaticity red,
143         Chromaticity green,
144         Chromaticity blue,
145         Chromaticity white,
146         optional<Chromaticity> adjusted_white,
147         shared_ptr<const TransferFunction> out
148         )
149         : _in (in)
150         , _yuv_to_rgb (yuv_to_rgb)
151         , _red (red)
152         , _green (green)
153         , _blue (blue)
154         , _white (white)
155         , _adjusted_white (adjusted_white)
156         , _out (out)
157 {
158
159 }
160
161 bool
162 ColourConversion::about_equal (ColourConversion const & other, float epsilon) const
163 {
164         if (!_in->about_equal (other._in, epsilon) ||
165             _yuv_to_rgb != other._yuv_to_rgb ||
166             !_red.about_equal (other._red, epsilon) ||
167             !_green.about_equal (other._green, epsilon) ||
168             !_blue.about_equal (other._blue, epsilon) ||
169             !_white.about_equal (other._white, epsilon) ||
170             !_out->about_equal (other._out, epsilon)) {
171                 return false;
172         }
173
174         if (!_adjusted_white && !other._adjusted_white) {
175                 return true;
176         }
177
178         if (
179                 _adjusted_white && other._adjusted_white &&
180                 fabs (_adjusted_white.get().x - other._adjusted_white.get().x) < epsilon &&
181                 fabs (_adjusted_white.get().y - other._adjusted_white.get().y) < epsilon
182                 ) {
183                 return true;
184         }
185
186         /* Otherwise one has an adjusted white and other hasn't, or they both have but different */
187         return false;
188 }
189
190 boost::numeric::ublas::matrix<double>
191 ColourConversion::rgb_to_xyz () const
192 {
193         /* See doc/design/colour.tex */
194
195         double const D = (_red.x - _white.x) * (_white.y - _blue.y) - (_white.x - _blue.x) * (_red.y - _white.y);
196         double const E = (_white.x - _green.x) * (_red.y - _white.y) - (_red.x - _white.x) * (_white.y - _green.y);
197         double const F = (_white.x - _green.x) * (_white.y - _blue.y) - (_white.x - _blue.x) * (_white.y - _green.y);
198         double const P = _red.y + _green.y * D / F + _blue.y * E / F;
199
200         boost::numeric::ublas::matrix<double> C (3, 3);
201         C(0, 0) = _red.x / P;
202         C(0, 1) = _green.x * D / (F * P);
203         C(0, 2) = _blue.x * E / (F * P);
204         C(1, 0) = _red.y / P;
205         C(1, 1) = _green.y * D / (F * P);
206         C(1, 2) = _blue.y * E / (F * P);
207         C(2, 0) = _red.z() / P;
208         C(2, 1) = _green.z() * D / (F * P);
209         C(2, 2) = _blue.z() * E / (F * P);
210         return C;
211 }
212
213 boost::numeric::ublas::matrix<double>
214 ColourConversion::xyz_to_rgb () const
215 {
216         boost::numeric::ublas::matrix<double> A (rgb_to_xyz ());
217
218         /* permutation matrix for the LU-factorization */
219         boost::numeric::ublas::permutation_matrix<std::size_t> pm (A.size1 ());
220
221         /* perform LU-factorization */
222         int const r = lu_factorize (A, pm);
223         DCP_ASSERT (r == 0);
224
225         /* create identity matrix of inverse */
226         boost::numeric::ublas::matrix<double> xyz_to_rgb (3, 3);
227         xyz_to_rgb.assign (boost::numeric::ublas::identity_matrix<double> (A.size1 ()));
228
229         /* backsubstitute to get the inverse */
230         lu_substitute (A, pm, xyz_to_rgb);
231
232         return xyz_to_rgb;
233 }
234
235 boost::numeric::ublas::matrix<double>
236 ColourConversion::bradford () const
237 {
238         if (!_adjusted_white || fabs (_adjusted_white.get().x) < 1e-6 || fabs (_adjusted_white.get().y) < 1e-6) {
239                 boost::numeric::ublas::matrix<double> B = boost::numeric::ublas::zero_matrix<double> (3, 3);
240                 B(0, 0) = 1;
241                 B(1, 1) = 1;
242                 B(2, 2) = 1;
243                 return B;
244         }
245
246         /* See doc/design/colour.tex */
247
248         boost::numeric::ublas::matrix<double> M (3, 3);
249         M(0, 0) = 0.8951;
250         M(0, 1) = 0.2664;
251         M(0, 2) = -0.1614;
252         M(1, 0) = -0.7502;
253         M(1, 1) = 1.7135;
254         M(1, 2) = 0.0367;
255         M(2, 0) = 0.0389;
256         M(2, 1) = -0.0685;
257         M(2, 2) = 1.0296;
258
259         boost::numeric::ublas::matrix<double> Mi (3, 3);
260         Mi(0, 0) = 0.9869929055;
261         Mi(0, 1) = -0.1470542564;
262         Mi(0, 2) = 0.1599626517;
263         Mi(1, 0) = 0.4323052697;
264         Mi(1, 1) = 0.5183602715;
265         Mi(1, 2) = 0.0492912282;
266         Mi(2, 0) = -0.0085286646;
267         Mi(2, 1) = 0.0400428217;
268         Mi(2, 2) = 0.9684866958;
269
270         boost::numeric::ublas::matrix<double> Gp (3, 1);
271         Gp(0, 0) = _white.x / _white.y;
272         Gp(1, 0) = 1;
273         Gp(2, 0) = (1 - _white.x - _white.y) / _white.y;
274
275         boost::numeric::ublas::matrix<double> G = boost::numeric::ublas::prod (M, Gp);
276
277         boost::numeric::ublas::matrix<double> Hp (3, 1);
278         Hp(0, 0) = _adjusted_white.get().x / _adjusted_white.get().y;
279         Hp(1, 0) = 1;
280         Hp(2, 0) = (1 - _adjusted_white.get().x - _adjusted_white.get().y) / _adjusted_white.get().y;
281
282         boost::numeric::ublas::matrix<double> H = boost::numeric::ublas::prod (M, Hp);
283
284         boost::numeric::ublas::matrix<double> C = boost::numeric::ublas::zero_matrix<double> (3, 3);
285         C(0, 0) = H(0, 0) / G(0, 0);
286         C(1, 1) = H(1, 0) / G(1, 0);
287         C(2, 2) = H(2, 0) / G(2, 0);
288
289         boost::numeric::ublas::matrix<double> CM = boost::numeric::ublas::prod (C, M);
290         return boost::numeric::ublas::prod (Mi, CM);
291 }