Add basics of colourspace conversion bypass (#266).
[dcpomatic.git] / src / lib / colour_conversion.cc
1 /*
2     Copyright (C) 2013 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 <libxml++/libxml++.h>
21 #include <libdcp/colour_matrix.h>
22 #include <libdcp/raw_convert.h>
23 #include <libcxml/cxml.h>
24 #include "config.h"
25 #include "colour_conversion.h"
26 #include "util.h"
27 #include "md5_digester.h"
28
29 #include "i18n.h"
30
31 using std::list;
32 using std::string;
33 using std::cout;
34 using std::vector;
35 using boost::shared_ptr;
36 using boost::optional;
37 using libdcp::raw_convert;
38
39 ColourConversion::ColourConversion ()
40         : input_gamma (2.4)
41         , input_gamma_linearised (true)
42         , matrix (3, 3)
43         , output_gamma (2.6)
44 {
45         for (int i = 0; i < 3; ++i) {
46                 for (int j = 0; j < 3; ++j) {
47                         matrix (i, j) = libdcp::colour_matrix::srgb_to_xyz[i][j];
48                 }
49         }
50 }
51
52 ColourConversion::ColourConversion (double i, bool il, double const m[3][3], double o)
53         : input_gamma (i)
54         , input_gamma_linearised (il)
55         , matrix (3, 3)
56         , output_gamma (o)
57 {
58         for (int i = 0; i < 3; ++i) {
59                 for (int j = 0; j < 3; ++j) {
60                         matrix (i, j) = m[i][j];
61                 }
62         }
63 }
64
65 ColourConversion::ColourConversion (cxml::NodePtr node)
66         : matrix (3, 3)
67 {
68         input_gamma = node->number_child<double> ("InputGamma");
69         input_gamma_linearised = node->bool_child ("InputGammaLinearised");
70
71         for (int i = 0; i < 3; ++i) {
72                 for (int j = 0; j < 3; ++j) {
73                         matrix (i, j) = 0;
74                 }
75         }
76
77         list<cxml::NodePtr> m = node->node_children ("Matrix");
78         for (list<cxml::NodePtr>::iterator i = m.begin(); i != m.end(); ++i) {
79                 int const ti = (*i)->number_attribute<int> ("i");
80                 int const tj = (*i)->number_attribute<int> ("j");
81                 matrix(ti, tj) = raw_convert<double> ((*i)->content ());
82         }
83
84         output_gamma = node->number_child<double> ("OutputGamma");
85 }
86
87 boost::optional<ColourConversion>
88 ColourConversion::from_xml (cxml::NodePtr node)
89 {
90         if (!node->optional_node_child ("InputGamma")) {
91                 return boost::optional<ColourConversion> ();
92         }
93
94         return ColourConversion (node);
95 }
96
97 void
98 ColourConversion::as_xml (xmlpp::Node* node) const
99 {
100         node->add_child("InputGamma")->add_child_text (raw_convert<string> (input_gamma));
101         node->add_child("InputGammaLinearised")->add_child_text (input_gamma_linearised ? "1" : "0");
102
103         for (int i = 0; i < 3; ++i) {
104                 for (int j = 0; j < 3; ++j) {
105                         xmlpp::Element* m = node->add_child("Matrix");
106                         m->set_attribute ("i", raw_convert<string> (i));
107                         m->set_attribute ("j", raw_convert<string> (j));
108                         m->add_child_text (raw_convert<string> (matrix (i, j)));
109                 }
110         }
111
112         node->add_child("OutputGamma")->add_child_text (raw_convert<string> (output_gamma));
113 }
114
115 optional<size_t>
116 ColourConversion::preset () const
117 {
118         vector<PresetColourConversion> presets = Config::instance()->colour_conversions ();
119         size_t i = 0;
120         while (i < presets.size() && (presets[i].conversion != *this)) {
121                 ++i;
122         }
123
124         if (i >= presets.size ()) {
125                 return optional<size_t> ();
126         }
127
128         return i;
129 }
130
131 string
132 ColourConversion::identifier () const
133 {
134         MD5Digester digester;
135         
136         digester.add (input_gamma);
137         digester.add (input_gamma_linearised);
138         for (int i = 0; i < 3; ++i) {
139                 for (int j = 0; j < 3; ++j) {
140                         digester.add (matrix (i, j));
141                 }
142         }
143         digester.add (output_gamma);
144         
145         return digester.get ();
146 }
147
148 PresetColourConversion::PresetColourConversion ()
149         : name (_("Untitled"))
150 {
151
152 }
153
154 PresetColourConversion::PresetColourConversion (string n, double i, bool il, double const m[3][3], double o)
155         : name (n)
156         , conversion (i, il, m, o)
157 {
158
159 }
160
161 PresetColourConversion::PresetColourConversion (cxml::NodePtr node)
162         : conversion (node)
163 {
164         name = node->string_child ("Name");
165 }
166
167 void
168 PresetColourConversion::as_xml (xmlpp::Node* node) const
169 {
170         conversion.as_xml (node);
171         node->add_child("Name")->add_child_text (name);
172 }
173
174 static bool
175 about_equal (double a, double b)
176 {
177         static const double eps = 1e-6;
178         return fabs (a - b) < eps;
179 }
180
181 bool
182 operator== (ColourConversion const & a, ColourConversion const & b)
183 {
184         if (
185                 !about_equal (a.input_gamma, b.input_gamma) ||
186                 a.input_gamma_linearised != b.input_gamma_linearised ||
187                 !about_equal (a.output_gamma, b.output_gamma)) {
188                 return false;
189         }
190
191         for (int i = 0; i < 3; ++i) {
192                 for (int j = 0; j < 3; ++j) {
193                         if (!about_equal (a.matrix (i, j), b.matrix (i, j))) {
194                                 return false;
195                         }
196                 }
197         }
198
199         return true;
200 }
201
202 bool
203 operator!= (ColourConversion const & a, ColourConversion const & b)
204 {
205         return !(a == b);
206 }