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