Properly remove JSON server; remove some unused usings; remove some unnecessary uses...
[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 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         MD5Digester digester;
125         
126         digester.add (input_gamma);
127         digester.add (input_gamma_linearised);
128         for (int i = 0; i < 3; ++i) {
129                 for (int j = 0; j < 3; ++j) {
130                         digester.add (matrix (i, j));
131                 }
132         }
133         digester.add (output_gamma);
134         
135         return digester.get ();
136 }
137
138 PresetColourConversion::PresetColourConversion ()
139         : name (_("Untitled"))
140 {
141
142 }
143
144 PresetColourConversion::PresetColourConversion (string n, double i, bool il, double const m[3][3], double o)
145         : name (n)
146         , conversion (i, il, m, o)
147 {
148
149 }
150
151 PresetColourConversion::PresetColourConversion (cxml::NodePtr node)
152         : conversion (node)
153 {
154         name = node->string_child ("Name");
155 }
156
157 void
158 PresetColourConversion::as_xml (xmlpp::Node* node) const
159 {
160         conversion.as_xml (node);
161         node->add_child("Name")->add_child_text (name);
162 }
163
164 static bool
165 about_equal (double a, double b)
166 {
167         static const double eps = 1e-6;
168         return fabs (a - b) < eps;
169 }
170
171 bool
172 operator== (ColourConversion const & a, ColourConversion const & b)
173 {
174         if (
175                 !about_equal (a.input_gamma, b.input_gamma) ||
176                 a.input_gamma_linearised != b.input_gamma_linearised ||
177                 !about_equal (a.output_gamma, b.output_gamma)) {
178                 return false;
179         }
180
181         for (int i = 0; i < 3; ++i) {
182                 for (int j = 0; j < 3; ++j) {
183                         if (!about_equal (a.matrix (i, j), b.matrix (i, j))) {
184                                 return false;
185                         }
186                 }
187         }
188
189         return true;
190 }
191
192 bool
193 operator!= (ColourConversion const & a, ColourConversion const & b)
194 {
195         return !(a == b);
196 }