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