Adapt for changes to libdcp.
[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)
55 {
56         shared_ptr<dcp::TransferFunction> in;
57         
58         cxml::ConstNodePtr in_node = node->node_child ("InputTransferFunction");
59         string in_type = in_node->string_child ("Type");
60         if (in_type == "Gamma") {
61                 _in.reset (new dcp::GammaTransferFunction (false, in_node->number_child<double> ("Gamma")));
62         } else if (in_type == "ModifiedGamma") {
63                 _in.reset (new dcp::ModifiedGammaTransferFunction (
64                                    false,
65                                    in_node->number_child<double> ("Power"),
66                                    in_node->number_child<double> ("Threshold"),
67                                    in_node->number_child<double> ("A"),
68                                    in_node->number_child<double> ("B")
69                                    ));
70         }
71
72         list<cxml::NodePtr> m = node->node_children ("Matrix");
73         for (list<cxml::NodePtr>::iterator i = m.begin(); i != m.end(); ++i) {
74                 int const ti = (*i)->number_attribute<int> ("i");
75                 int const tj = (*i)->number_attribute<int> ("j");
76                 _matrix(ti, tj) = raw_convert<double> ((*i)->content ());
77         }
78
79         _out.reset (new dcp::GammaTransferFunction (true, node->number_child<double> ("OutputGamma")));
80 }
81
82 boost::optional<ColourConversion>
83 ColourConversion::from_xml (cxml::NodePtr node)
84 {
85         if (!node->optional_node_child ("InputTransferFunction")) {
86                 return boost::optional<ColourConversion> ();
87         }
88
89         return ColourConversion (node);
90 }
91
92 void
93 ColourConversion::as_xml (xmlpp::Node* node) const
94 {
95         xmlpp::Node* in_node = node->add_child ("InputTransferFunction");
96         if (dynamic_pointer_cast<const dcp::GammaTransferFunction> (_in)) {
97                 shared_ptr<const dcp::GammaTransferFunction> tf = dynamic_pointer_cast<const dcp::GammaTransferFunction> (_in);
98                 in_node->add_child("Type")->add_child_text ("Gamma");
99                 in_node->add_child("Gamma")->add_child_text (raw_convert<string> (tf->gamma ()));
100         } else if (dynamic_pointer_cast<const dcp::ModifiedGammaTransferFunction> (_in)) {
101                 shared_ptr<const dcp::ModifiedGammaTransferFunction> tf = dynamic_pointer_cast<const dcp::ModifiedGammaTransferFunction> (_in);
102                 in_node->add_child("Type")->add_child_text ("ModifiedGamma");
103                 in_node->add_child("Power")->add_child_text (raw_convert<string> (tf->power ()));
104                 in_node->add_child("Threshold")->add_child_text (raw_convert<string> (tf->threshold ()));
105                 in_node->add_child("A")->add_child_text (raw_convert<string> (tf->A ()));
106                 in_node->add_child("B")->add_child_text (raw_convert<string> (tf->B ()));
107         }
108
109         boost::numeric::ublas::matrix<double> matrix = _matrix;
110         for (int i = 0; i < 3; ++i) {
111                 for (int j = 0; j < 3; ++j) {
112                         xmlpp::Element* m = node->add_child("Matrix");
113                         m->set_attribute ("i", raw_convert<string> (i));
114                         m->set_attribute ("j", raw_convert<string> (j));
115                         m->add_child_text (raw_convert<string> (matrix (i, j)));
116                 }
117         }
118
119         node->add_child("OutputGamma")->add_child_text (raw_convert<string> (dynamic_pointer_cast<const dcp::GammaTransferFunction> (_out)->gamma ()));
120 }
121
122 optional<size_t>
123 ColourConversion::preset () const
124 {
125         vector<PresetColourConversion> presets = Config::instance()->colour_conversions ();
126         size_t i = 0;
127         while (i < presets.size() && (presets[i].conversion != *this)) {
128                 ++i;
129         }
130
131         if (i >= presets.size ()) {
132                 return optional<size_t> ();
133         }
134
135         return i;
136 }
137
138 string
139 ColourConversion::identifier () const
140 {
141         MD5Digester digester;
142
143         if (dynamic_pointer_cast<const dcp::GammaTransferFunction> (_in)) {
144                 shared_ptr<const dcp::GammaTransferFunction> tf = dynamic_pointer_cast<const dcp::GammaTransferFunction> (_in);
145                 digester.add (tf->gamma ());
146         } else if (dynamic_pointer_cast<const dcp::ModifiedGammaTransferFunction> (_in)) {
147                 shared_ptr<const dcp::ModifiedGammaTransferFunction> tf = dynamic_pointer_cast<const dcp::ModifiedGammaTransferFunction> (_in);
148                 digester.add (tf->power ());
149                 digester.add (tf->threshold ());
150                 digester.add (tf->A ());
151                 digester.add (tf->B ());
152         }
153
154         boost::numeric::ublas::matrix<double> matrix = _matrix;
155         for (int i = 0; i < 3; ++i) {
156                 for (int j = 0; j < 3; ++j) {
157                         digester.add (matrix (i, j));
158                 }
159         }
160
161         digester.add (dynamic_pointer_cast<const dcp::GammaTransferFunction> (_out)->gamma ());
162         
163         return digester.get ();
164 }
165
166 PresetColourConversion::PresetColourConversion ()
167         : name (_("Untitled"))
168 {
169
170 }
171
172 PresetColourConversion::PresetColourConversion (string n, dcp::ColourConversion conversion_)
173         : name (n)
174         , conversion (conversion_)
175 {
176
177 }
178
179 PresetColourConversion::PresetColourConversion (cxml::NodePtr node)
180         : conversion (node)
181 {
182         name = node->string_child ("Name");
183 }
184
185 void
186 PresetColourConversion::as_xml (xmlpp::Node* node) const
187 {
188         conversion.as_xml (node);
189         node->add_child("Name")->add_child_text (name);
190 }
191
192 bool
193 operator== (ColourConversion const & a, ColourConversion const & b)
194 {
195         return a.about_equal (b, 1e-6);
196 }
197
198 bool
199 operator!= (ColourConversion const & a, ColourConversion const & b)
200 {
201         return !(a == b);
202 }