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