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