Change colourspace handling round a bit:
[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
25 using boost::shared_ptr;
26 using namespace dcp;
27
28 ColourConversion ColourConversion::srgb_to_xyz (
29         shared_ptr<const TransferFunction> (new ModifiedGammaTransferFunction (2.4, 0.04045, 0.055, 12.92)),
30         dcp::colour_matrix::srgb_to_xyz,
31         shared_ptr<const TransferFunction> (new GammaTransferFunction (2.6))
32         );
33
34 /* XXX: what sort of RGB is this...? */
35 ColourConversion ColourConversion::xyz_to_rgb (
36         shared_ptr<const TransferFunction> (new GammaTransferFunction (2.6)),
37         dcp::colour_matrix::xyz_to_rgb,
38         shared_ptr<const TransferFunction> (new GammaTransferFunction (2.2))
39         );
40
41 ColourConversion ColourConversion::rec709_to_xyz (
42         shared_ptr<const TransferFunction> (new ModifiedGammaTransferFunction (2.4, 0.081, 0.099, 4.5)),
43         dcp::colour_matrix::srgb_to_xyz,
44         shared_ptr<const TransferFunction> (new GammaTransferFunction (2.6))
45         );
46
47 ColourConversion::ColourConversion (
48         shared_ptr<const TransferFunction> in,
49         double const matrix[3][3],
50         shared_ptr<const TransferFunction> out
51         )
52         : _in (in)
53         , _matrix (3, 3)
54         , _out (out)
55 {
56         for (int i = 0; i < 3; ++i) {
57                 for (int j = 0; j < 3; ++j) {
58                         _matrix (i, j) = matrix[i][j];
59                 }
60         }
61 }
62
63 bool
64 ColourConversion::about_equal (ColourConversion const & other, float epsilon) const
65 {
66         for (int i = 0; i < 3; ++i) {
67                 for (int j = 0; j < 3; ++j) {
68                         if (fabs (_matrix(i, j) - other._matrix(i, j)) > epsilon) {
69                                 return false;
70                         }
71                 }
72         }
73
74         return _in->about_equal (other._in, epsilon) && _out->about_equal (other._out, epsilon);
75 }