Merge branch '2.0' of ssh://main.carlh.net/home/carl/git/dcpomatic into 2.0
[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 "raw_convert.h"
25 #include <dcp/chromaticity.h>
26 #include <dcp/colour_matrix.h>
27 #include <dcp/gamma_transfer_function.h>
28 #include <dcp/modified_gamma_transfer_function.h>
29 #include <libcxml/cxml.h>
30 #include <libxml++/libxml++.h>
31
32 #include "i18n.h"
33
34 using std::list;
35 using std::string;
36 using std::cout;
37 using std::vector;
38 using boost::shared_ptr;
39 using boost::optional;
40 using boost::dynamic_pointer_cast;
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 (in_node->number_child<double> ("Gamma")));
66                 } else if (in_type == "ModifiedGamma") {
67                         _in.reset (new dcp::ModifiedGammaTransferFunction (
68                                            in_node->number_child<double> ("Power"),
69                                            in_node->number_child<double> ("Threshold"),
70                                            in_node->number_child<double> ("A"),
71                                            in_node->number_child<double> ("B")
72                                            ));
73                 }
74
75         } else {
76
77                 /* Version 1.x */
78                 
79                 if (node->bool_child ("InputGammaLinearised")) {
80                         _in.reset (new dcp::ModifiedGammaTransferFunction (node->number_child<float> ("InputGamma"), 0.04045, 0.055, 12.92));
81                 } else {
82                         _in.reset (new dcp::GammaTransferFunction (node->number_child<float> ("InputGamma")));
83                 }
84         }
85
86         _yuv_to_rgb = static_cast<dcp::YUVToRGB> (node->optional_number_child<int>("YUVToRGB").get_value_or (dcp::YUV_TO_RGB_REC601));
87         
88         list<cxml::NodePtr> m = node->node_children ("Matrix");
89         if (!m.empty ()) {
90                 /* Read in old <Matrix> nodes and convert them to chromaticities */
91                 boost::numeric::ublas::matrix<double> C (3, 3);
92                 for (list<cxml::NodePtr>::iterator i = m.begin(); i != m.end(); ++i) {
93                         int const ti = (*i)->number_attribute<int> ("i");
94                         int const tj = (*i)->number_attribute<int> ("j");
95                         C(ti, tj) = raw_convert<double> ((*i)->content ());
96                 }
97
98                 double const rd = C(0, 0) + C(1, 0) + C(2, 0);
99                 _red = dcp::Chromaticity (C(0, 0) / rd, C(1, 0) / rd);
100                 double const gd = C(0, 1) + C(1, 1) + C(2, 1);
101                 _green = dcp::Chromaticity (C(0, 1) / gd, C(1, 1) / gd);
102                 double const bd = C(0, 2) + C(1, 2) + C(2, 2);
103                 _blue = dcp::Chromaticity (C(0, 2) / bd, C(1, 2) / bd);
104                 double const wd = C(0, 0) + C(0, 1) + C(0, 2) + C(1, 0) + C(1, 1) + C(1, 2) + C(2, 0) + C(2, 1) + C(2, 2);
105                 _white = dcp::Chromaticity ((C(0, 0) + C(0, 1) + C(0, 2)) / wd, (C(1, 0) + C(1, 1) + C(1, 2)) / wd);
106         } else {
107                 /* New-style chromaticities */
108                 _red = dcp::Chromaticity (node->number_child<double> ("RedX"), node->number_child<double> ("RedY"));
109                 _green = dcp::Chromaticity (node->number_child<double> ("GreenX"), node->number_child<double> ("GreenY"));
110                 _blue = dcp::Chromaticity (node->number_child<double> ("BlueX"), node->number_child<double> ("BlueY"));
111                 _white = dcp::Chromaticity (node->number_child<double> ("WhiteX"), node->number_child<double> ("WhiteY"));
112                 if (node->optional_node_child ("AdjustedWhiteX")) {
113                         _adjusted_white = dcp::Chromaticity (
114                                 node->number_child<double> ("AdjustedWhiteX"), node->number_child<double> ("AdjustedWhiteY")
115                                 );
116                 }
117         }       
118         
119         _out.reset (new dcp::GammaTransferFunction (node->number_child<double> ("OutputGamma")));
120 }
121
122 boost::optional<ColourConversion>
123 ColourConversion::from_xml (cxml::NodePtr node, int version)
124 {
125         if (!node->optional_node_child ("InputTransferFunction")) {
126                 return boost::optional<ColourConversion> ();
127         }
128
129         return ColourConversion (node, version);
130 }
131
132 void
133 ColourConversion::as_xml (xmlpp::Node* node) const
134 {
135         xmlpp::Node* in_node = node->add_child ("InputTransferFunction");
136         if (dynamic_pointer_cast<const dcp::GammaTransferFunction> (_in)) {
137                 shared_ptr<const dcp::GammaTransferFunction> tf = dynamic_pointer_cast<const dcp::GammaTransferFunction> (_in);
138                 in_node->add_child("Type")->add_child_text ("Gamma");
139                 in_node->add_child("Gamma")->add_child_text (raw_convert<string> (tf->gamma ()));
140         } else if (dynamic_pointer_cast<const dcp::ModifiedGammaTransferFunction> (_in)) {
141                 shared_ptr<const dcp::ModifiedGammaTransferFunction> tf = dynamic_pointer_cast<const dcp::ModifiedGammaTransferFunction> (_in);
142                 in_node->add_child("Type")->add_child_text ("ModifiedGamma");
143                 in_node->add_child("Power")->add_child_text (raw_convert<string> (tf->power ()));
144                 in_node->add_child("Threshold")->add_child_text (raw_convert<string> (tf->threshold ()));
145                 in_node->add_child("A")->add_child_text (raw_convert<string> (tf->A ()));
146                 in_node->add_child("B")->add_child_text (raw_convert<string> (tf->B ()));
147         }
148
149         node->add_child("RedX")->add_child_text (raw_convert<string> (_red.x));
150         node->add_child("RedY")->add_child_text (raw_convert<string> (_red.y));
151         node->add_child("GreenX")->add_child_text (raw_convert<string> (_green.x));
152         node->add_child("GreenY")->add_child_text (raw_convert<string> (_green.y));
153         node->add_child("BlueX")->add_child_text (raw_convert<string> (_blue.x));
154         node->add_child("BlueY")->add_child_text (raw_convert<string> (_blue.y));
155         node->add_child("WhiteX")->add_child_text (raw_convert<string> (_white.x));
156         node->add_child("WhiteY")->add_child_text (raw_convert<string> (_white.y));
157         if (_adjusted_white) {
158                 node->add_child("AdjustedWhiteX")->add_child_text (raw_convert<string> (_adjusted_white.get().x));
159                 node->add_child("AdjustedWhiteY")->add_child_text (raw_convert<string> (_adjusted_white.get().y));
160         }
161
162         node->add_child("OutputGamma")->add_child_text (raw_convert<string> (dynamic_pointer_cast<const dcp::GammaTransferFunction> (_out)->gamma ()));
163 }
164
165 optional<size_t>
166 ColourConversion::preset () const
167 {
168         vector<PresetColourConversion> presets = Config::instance()->colour_conversions ();
169         size_t i = 0;
170         while (i < presets.size() && (presets[i].conversion != *this)) {
171                 ++i;
172         }
173
174         if (i >= presets.size ()) {
175                 return optional<size_t> ();
176         }
177
178         return i;
179 }
180
181 string
182 ColourConversion::identifier () const
183 {
184         MD5Digester digester;
185
186         if (dynamic_pointer_cast<const dcp::GammaTransferFunction> (_in)) {
187                 shared_ptr<const dcp::GammaTransferFunction> tf = dynamic_pointer_cast<const dcp::GammaTransferFunction> (_in);
188                 digester.add (tf->gamma ());
189         } else if (dynamic_pointer_cast<const dcp::ModifiedGammaTransferFunction> (_in)) {
190                 shared_ptr<const dcp::ModifiedGammaTransferFunction> tf = dynamic_pointer_cast<const dcp::ModifiedGammaTransferFunction> (_in);
191                 digester.add (tf->power ());
192                 digester.add (tf->threshold ());
193                 digester.add (tf->A ());
194                 digester.add (tf->B ());
195         }
196
197         digester.add (_red.x);
198         digester.add (_red.y);
199         digester.add (_green.x);
200         digester.add (_green.y);
201         digester.add (_blue.x);
202         digester.add (_blue.y);
203         digester.add (_white.x);
204         digester.add (_white.y);
205
206         if (_adjusted_white) {
207                 digester.add (_adjusted_white.get().x);
208                 digester.add (_adjusted_white.get().y);
209         }
210
211         digester.add (dynamic_pointer_cast<const dcp::GammaTransferFunction> (_out)->gamma ());
212         
213         return digester.get ();
214 }
215
216 PresetColourConversion::PresetColourConversion ()
217         : name (_("Untitled"))
218 {
219
220 }
221
222 PresetColourConversion::PresetColourConversion (string n, dcp::ColourConversion conversion_)
223         : conversion (conversion_)
224         , name (n)
225 {
226
227 }
228
229 PresetColourConversion::PresetColourConversion (cxml::NodePtr node, int version)
230         : conversion (node, version)
231         , name (node->string_child ("Name"))
232 {
233
234 }
235
236 void
237 PresetColourConversion::as_xml (xmlpp::Node* node) const
238 {
239         conversion.as_xml (node);
240         node->add_child("Name")->add_child_text (name);
241 }
242
243 bool
244 operator== (ColourConversion const & a, ColourConversion const & b)
245 {
246         return a.about_equal (b, 1e-6);
247 }
248
249 bool
250 operator!= (ColourConversion const & a, ColourConversion const & b)
251 {
252         return !(a == b);
253 }
254
255 bool
256 operator== (PresetColourConversion const & a, PresetColourConversion const & b)
257 {
258         return a.name == b.name && a.conversion == b.conversion;
259 }