Small fixes and tidy-ups spotted by cppcheck.
[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 "config.h"
21 #include "colour_conversion.h"
22 #include "util.h"
23 #include "md5_digester.h"
24 #include <dcp/colour_matrix.h>
25 #include <dcp/raw_convert.h>
26 #include <dcp/gamma_transfer_function.h>
27 #include <dcp/modified_gamma_transfer_function.h>
28 #include <libcxml/cxml.h>
29 #include <libxml++/libxml++.h>
30
31 #include "i18n.h"
32
33 using std::list;
34 using std::string;
35 using std::cout;
36 using std::vector;
37 using boost::shared_ptr;
38 using boost::optional;
39 using boost::dynamic_pointer_cast;
40 using dcp::raw_convert;
41
42 ColourConversion::ColourConversion ()
43         : dcp::ColourConversion (dcp::ColourConversion::srgb_to_xyz ())
44 {
45         
46 }
47
48 ColourConversion::ColourConversion (dcp::ColourConversion conversion_)
49         : dcp::ColourConversion (conversion_)
50 {
51         
52 }
53
54 ColourConversion::ColourConversion (cxml::NodePtr node, int version)
55 {
56         shared_ptr<dcp::TransferFunction> in;
57
58         if (version >= 32) {
59
60                 /* Version 2.x */
61
62                 cxml::ConstNodePtr in_node = node->node_child ("InputTransferFunction");
63                 string in_type = in_node->string_child ("Type");
64                 if (in_type == "Gamma") {
65                         _in.reset (new dcp::GammaTransferFunction (false, in_node->number_child<double> ("Gamma")));
66                 } else if (in_type == "ModifiedGamma") {
67                         _in.reset (new dcp::ModifiedGammaTransferFunction (
68                                            false,
69                                            in_node->number_child<double> ("Power"),
70                                            in_node->number_child<double> ("Threshold"),
71                                            in_node->number_child<double> ("A"),
72                                            in_node->number_child<double> ("B")
73                                            ));
74                 }
75
76         } else {
77
78                 /* Version 1.x */
79                 
80                 if (node->bool_child ("InputGammaLinearised")) {
81                         _in.reset (new dcp::ModifiedGammaTransferFunction (false, node->number_child<float> ("InputGamma"), 0.04045, 0.055, 12.92));
82                 } else {
83                         _in.reset (new dcp::GammaTransferFunction (false, node->number_child<float> ("InputGamma")));
84                 }
85         }
86
87         list<cxml::NodePtr> m = node->node_children ("Matrix");
88         for (list<cxml::NodePtr>::iterator i = m.begin(); i != m.end(); ++i) {
89                 int const ti = (*i)->number_attribute<int> ("i");
90                 int const tj = (*i)->number_attribute<int> ("j");
91                 _matrix(ti, tj) = raw_convert<double> ((*i)->content ());
92         }
93         
94         _out.reset (new dcp::GammaTransferFunction (true, node->number_child<double> ("OutputGamma")));
95 }
96
97 boost::optional<ColourConversion>
98 ColourConversion::from_xml (cxml::NodePtr node, int version)
99 {
100         if (!node->optional_node_child ("InputTransferFunction")) {
101                 return boost::optional<ColourConversion> ();
102         }
103
104         return ColourConversion (node, version);
105 }
106
107 void
108 ColourConversion::as_xml (xmlpp::Node* node) const
109 {
110         xmlpp::Node* in_node = node->add_child ("InputTransferFunction");
111         if (dynamic_pointer_cast<const dcp::GammaTransferFunction> (_in)) {
112                 shared_ptr<const dcp::GammaTransferFunction> tf = dynamic_pointer_cast<const dcp::GammaTransferFunction> (_in);
113                 in_node->add_child("Type")->add_child_text ("Gamma");
114                 in_node->add_child("Gamma")->add_child_text (raw_convert<string> (tf->gamma ()));
115         } else if (dynamic_pointer_cast<const dcp::ModifiedGammaTransferFunction> (_in)) {
116                 shared_ptr<const dcp::ModifiedGammaTransferFunction> tf = dynamic_pointer_cast<const dcp::ModifiedGammaTransferFunction> (_in);
117                 in_node->add_child("Type")->add_child_text ("ModifiedGamma");
118                 in_node->add_child("Power")->add_child_text (raw_convert<string> (tf->power ()));
119                 in_node->add_child("Threshold")->add_child_text (raw_convert<string> (tf->threshold ()));
120                 in_node->add_child("A")->add_child_text (raw_convert<string> (tf->A ()));
121                 in_node->add_child("B")->add_child_text (raw_convert<string> (tf->B ()));
122         }
123
124         boost::numeric::ublas::matrix<double> matrix = _matrix;
125         for (int i = 0; i < 3; ++i) {
126                 for (int j = 0; j < 3; ++j) {
127                         xmlpp::Element* m = node->add_child("Matrix");
128                         m->set_attribute ("i", raw_convert<string> (i));
129                         m->set_attribute ("j", raw_convert<string> (j));
130                         m->add_child_text (raw_convert<string> (matrix (i, j)));
131                 }
132         }
133
134         node->add_child("OutputGamma")->add_child_text (raw_convert<string> (dynamic_pointer_cast<const dcp::GammaTransferFunction> (_out)->gamma ()));
135 }
136
137 optional<size_t>
138 ColourConversion::preset () const
139 {
140         vector<PresetColourConversion> presets = Config::instance()->colour_conversions ();
141         size_t i = 0;
142         while (i < presets.size() && (presets[i].conversion != *this)) {
143                 ++i;
144         }
145
146         if (i >= presets.size ()) {
147                 return optional<size_t> ();
148         }
149
150         return i;
151 }
152
153 string
154 ColourConversion::identifier () const
155 {
156         MD5Digester digester;
157
158         if (dynamic_pointer_cast<const dcp::GammaTransferFunction> (_in)) {
159                 shared_ptr<const dcp::GammaTransferFunction> tf = dynamic_pointer_cast<const dcp::GammaTransferFunction> (_in);
160                 digester.add (tf->gamma ());
161         } else if (dynamic_pointer_cast<const dcp::ModifiedGammaTransferFunction> (_in)) {
162                 shared_ptr<const dcp::ModifiedGammaTransferFunction> tf = dynamic_pointer_cast<const dcp::ModifiedGammaTransferFunction> (_in);
163                 digester.add (tf->power ());
164                 digester.add (tf->threshold ());
165                 digester.add (tf->A ());
166                 digester.add (tf->B ());
167         }
168
169         boost::numeric::ublas::matrix<double> matrix = _matrix;
170         for (int i = 0; i < 3; ++i) {
171                 for (int j = 0; j < 3; ++j) {
172                         digester.add (matrix (i, j));
173                 }
174         }
175
176         digester.add (dynamic_pointer_cast<const dcp::GammaTransferFunction> (_out)->gamma ());
177         
178         return digester.get ();
179 }
180
181 PresetColourConversion::PresetColourConversion ()
182         : name (_("Untitled"))
183 {
184
185 }
186
187 PresetColourConversion::PresetColourConversion (string n, dcp::ColourConversion conversion_)
188         : name (n)
189         , conversion (conversion_)
190 {
191
192 }
193
194 PresetColourConversion::PresetColourConversion (cxml::NodePtr node, int version)
195         : conversion (node, version)
196         , name (node->string_child ("Name"))
197 {
198
199 }
200
201 void
202 PresetColourConversion::as_xml (xmlpp::Node* node) const
203 {
204         conversion.as_xml (node);
205         node->add_child("Name")->add_child_text (name);
206 }
207
208 bool
209 operator== (ColourConversion const & a, ColourConversion const & b)
210 {
211         return a.about_equal (b, 1e-6);
212 }
213
214 bool
215 operator!= (ColourConversion const & a, ColourConversion const & b)
216 {
217         return !(a == b);
218 }