Add colour conversion identifier to video identifier.
[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 <libdcp/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) = libdcp::colour_matrix::xyz_to_rgb[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 (shared_ptr<cxml::Node> node)
65         : matrix (3, 3)
66 {
67         input_gamma = node->number_child<double> ("InputGamma");
68         input_gamma_linearised = node->bool_child ("InputGammaLinearised");
69
70         for (int i = 0; i < 3; ++i) {
71                 for (int j = 0; j < 3; ++j) {
72                         matrix (i, j) = 0;
73                 }
74         }
75
76         list<shared_ptr<cxml::Node> > m = node->node_children ("Matrix");
77         for (list<shared_ptr<cxml::Node> >::iterator i = m.begin(); i != m.end(); ++i) {
78                 int const ti = (*i)->number_attribute<int> ("i");
79                 int const tj = (*i)->number_attribute<int> ("j");
80                 matrix(ti, tj) = lexical_cast<double> ((*i)->content ());
81         }
82
83         output_gamma = node->number_child<double> ("OutputGamma");
84 }
85
86 void
87 ColourConversion::as_xml (xmlpp::Node* node) const
88 {
89         node->add_child("InputGamma")->add_child_text (lexical_cast<string> (input_gamma));
90         node->add_child("InputGammaLinearised")->add_child_text (input_gamma_linearised ? "1" : "0");
91
92         for (int i = 0; i < 3; ++i) {
93                 for (int j = 0; j < 3; ++j) {
94                         xmlpp::Element* m = node->add_child("Matrix");
95                         m->set_attribute ("i", lexical_cast<string> (i));
96                         m->set_attribute ("j", lexical_cast<string> (j));
97                         m->add_child_text (lexical_cast<string> (matrix (i, j)));
98                 }
99         }
100
101         node->add_child("OutputGamma")->add_child_text (lexical_cast<string> (output_gamma));
102 }
103
104 optional<size_t>
105 ColourConversion::preset () const
106 {
107         vector<PresetColourConversion> presets = Config::instance()->colour_conversions ();
108         size_t i = 0;
109         while (i < presets.size() && (presets[i].conversion != *this)) {
110                 ++i;
111         }
112
113         if (i >= presets.size ()) {
114                 return optional<size_t> ();
115         }
116
117         return i;
118 }
119
120 string
121 ColourConversion::identifier () const
122 {
123         double numbers[12];
124
125         int n = 0;
126         numbers[n++] = input_gamma;
127         numbers[n++] = input_gamma_linearised;
128         for (int i = 0; i < 3; ++i) {
129                 for (int j = 0; j < 3; ++j) {
130                         numbers[n++] = matrix (i, j);
131                 }
132         }
133         numbers[n++] = output_gamma;
134
135         assert (n == 12);
136
137         return md5_digest (numbers, 12 * sizeof (double));
138 }
139
140 PresetColourConversion::PresetColourConversion ()
141         : name (_("Untitled"))
142 {
143
144 }
145
146 PresetColourConversion::PresetColourConversion (string n, double i, bool il, double const m[3][3], double o)
147         : name (n)
148         , conversion (i, il, m, o)
149 {
150
151 }
152
153 PresetColourConversion::PresetColourConversion (shared_ptr<cxml::Node> node)
154         : conversion (node)
155 {
156         name = node->string_child ("Name");
157 }
158
159 void
160 PresetColourConversion::as_xml (xmlpp::Node* node) const
161 {
162         conversion.as_xml (node);
163         node->add_child("Name")->add_child_text (name);
164 }
165
166 static bool
167 about_equal (double a, double b)
168 {
169         static const double eps = 1e-6;
170         return fabs (a - b) < eps;
171 }
172
173 bool
174 operator== (ColourConversion const & a, ColourConversion const & b)
175 {
176         if (
177                 !about_equal (a.input_gamma, b.input_gamma) ||
178                 a.input_gamma_linearised != b.input_gamma_linearised ||
179                 !about_equal (a.output_gamma, b.output_gamma)) {
180                 return false;
181         }
182
183         for (int i = 0; i < 3; ++i) {
184                 for (int j = 0; j < 3; ++j) {
185                         if (!about_equal (a.matrix (i, j), b.matrix (i, j))) {
186                                 return false;
187                         }
188                 }
189         }
190
191         return true;
192 }
193
194 bool
195 operator!= (ColourConversion const & a, ColourConversion const & b)
196 {
197         return !(a == b);
198 }