Merge master.
[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 <dcp/colour_matrix.h>
22 #include <dcp/raw_convert.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::stringstream;
33 using std::cout;
34 using std::vector;
35 using boost::shared_ptr;
36 using boost::optional;
37 using dcp::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) = dcp::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 void
88 ColourConversion::as_xml (xmlpp::Node* node) const
89 {
90         node->add_child("InputGamma")->add_child_text (raw_convert<string> (input_gamma));
91         node->add_child("InputGammaLinearised")->add_child_text (input_gamma_linearised ? "1" : "0");
92
93         for (int i = 0; i < 3; ++i) {
94                 for (int j = 0; j < 3; ++j) {
95                         xmlpp::Element* m = node->add_child("Matrix");
96                         m->set_attribute ("i", raw_convert<string> (i));
97                         m->set_attribute ("j", raw_convert<string> (j));
98                         m->add_child_text (raw_convert<string> (matrix (i, j)));
99                 }
100         }
101
102         node->add_child("OutputGamma")->add_child_text (raw_convert<string> (output_gamma));
103 }
104
105 optional<size_t>
106 ColourConversion::preset () const
107 {
108         vector<PresetColourConversion> presets = Config::instance()->colour_conversions ();
109         size_t i = 0;
110         while (i < presets.size() && (presets[i].conversion != *this)) {
111                 ++i;
112         }
113
114         if (i >= presets.size ()) {
115                 return optional<size_t> ();
116         }
117
118         return i;
119 }
120
121 string
122 ColourConversion::identifier () const
123 {
124         double numbers[12];
125
126         int n = 0;
127         numbers[n++] = input_gamma;
128         numbers[n++] = input_gamma_linearised;
129         for (int i = 0; i < 3; ++i) {
130                 for (int j = 0; j < 3; ++j) {
131                         numbers[n++] = matrix (i, j);
132                 }
133         }
134         numbers[n++] = output_gamma;
135
136         assert (n == 12);
137
138         return md5_digest (numbers, 12 * sizeof (double));
139 }
140
141 PresetColourConversion::PresetColourConversion ()
142         : name (_("Untitled"))
143 {
144
145 }
146
147 PresetColourConversion::PresetColourConversion (string n, double i, bool il, double const m[3][3], double o)
148         : name (n)
149         , conversion (i, il, m, o)
150 {
151
152 }
153
154 PresetColourConversion::PresetColourConversion (cxml::NodePtr node)
155         : conversion (node)
156 {
157         name = node->string_child ("Name");
158 }
159
160 void
161 PresetColourConversion::as_xml (xmlpp::Node* node) const
162 {
163         conversion.as_xml (node);
164         node->add_child("Name")->add_child_text (name);
165 }
166
167 static bool
168 about_equal (double a, double b)
169 {
170         static const double eps = 1e-6;
171         return fabs (a - b) < eps;
172 }
173
174 bool
175 operator== (ColourConversion const & a, ColourConversion const & b)
176 {
177         if (
178                 !about_equal (a.input_gamma, b.input_gamma) ||
179                 a.input_gamma_linearised != b.input_gamma_linearised ||
180                 !about_equal (a.output_gamma, b.output_gamma)) {
181                 return false;
182         }
183
184         for (int i = 0; i < 3; ++i) {
185                 for (int j = 0; j < 3; ++j) {
186                         if (!about_equal (a.matrix (i, j), b.matrix (i, j))) {
187                                 return false;
188                         }
189                 }
190         }
191
192         return true;
193 }
194
195 bool
196 operator!= (ColourConversion const & a, ColourConversion const & b)
197 {
198         return !(a == b);
199 }