Accessor for ClosedCaptionsDialog.
[dcpomatic.git] / src / lib / colour_conversion.cc
1 /*
2     Copyright (C) 2013-2015 Carl Hetherington <cth@carlh.net>
3
4     This file is part of DCP-o-matic.
5
6     DCP-o-matic is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     DCP-o-matic is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with DCP-o-matic.  If not, see <http://www.gnu.org/licenses/>.
18
19 */
20
21 #include "config.h"
22 #include "colour_conversion.h"
23 #include "util.h"
24 #include "digester.h"
25 #include <dcp/raw_convert.h>
26 #include <dcp/chromaticity.h>
27 #include <dcp/gamma_transfer_function.h>
28 #include <dcp/modified_gamma_transfer_function.h>
29 #include <dcp/identity_transfer_function.h>
30 #include <dcp/s_gamut3_transfer_function.h>
31 #include <libcxml/cxml.h>
32 #include <libxml++/libxml++.h>
33 #include <boost/foreach.hpp>
34 #include <iostream>
35
36 #include "i18n.h"
37
38 using std::list;
39 using std::string;
40 using std::cout;
41 using std::vector;
42 using boost::shared_ptr;
43 using boost::optional;
44 using boost::dynamic_pointer_cast;
45 using dcp::raw_convert;
46
47 vector<PresetColourConversion> PresetColourConversion::_presets;
48
49 ColourConversion::ColourConversion ()
50         : dcp::ColourConversion (dcp::ColourConversion::srgb_to_xyz ())
51 {
52
53 }
54
55 ColourConversion::ColourConversion (dcp::ColourConversion conversion_)
56         : dcp::ColourConversion (conversion_)
57 {
58
59 }
60
61 ColourConversion::ColourConversion (cxml::NodePtr node, int version)
62 {
63         shared_ptr<dcp::TransferFunction> in;
64
65         if (version >= 32) {
66
67                 /* Version 2.x */
68
69                 cxml::ConstNodePtr in_node = node->node_child ("InputTransferFunction");
70                 string in_type = in_node->string_child ("Type");
71                 if (in_type == "Gamma") {
72                         _in.reset (new dcp::GammaTransferFunction (in_node->number_child<double> ("Gamma")));
73                 } else if (in_type == "ModifiedGamma") {
74                         _in.reset (new dcp::ModifiedGammaTransferFunction (
75                                            in_node->number_child<double> ("Power"),
76                                            in_node->number_child<double> ("Threshold"),
77                                            in_node->number_child<double> ("A"),
78                                            in_node->number_child<double> ("B")
79                                            ));
80                 } else if (in_type == "SGamut3") {
81                         _in.reset (new dcp::SGamut3TransferFunction ());
82                 }
83
84         } else {
85
86                 /* Version 1.x */
87
88                 if (node->bool_child ("InputGammaLinearised")) {
89                         _in.reset (new dcp::ModifiedGammaTransferFunction (node->number_child<double> ("InputGamma"), 0.04045, 0.055, 12.92));
90                 } else {
91                         _in.reset (new dcp::GammaTransferFunction (node->number_child<double> ("InputGamma")));
92                 }
93         }
94
95         _yuv_to_rgb = static_cast<dcp::YUVToRGB> (node->optional_number_child<int>("YUVToRGB").get_value_or (dcp::YUV_TO_RGB_REC601));
96
97         list<cxml::NodePtr> m = node->node_children ("Matrix");
98         if (!m.empty ()) {
99                 /* Read in old <Matrix> nodes and convert them to chromaticities */
100                 boost::numeric::ublas::matrix<double> C (3, 3);
101                 for (list<cxml::NodePtr>::iterator i = m.begin(); i != m.end(); ++i) {
102                         int const ti = (*i)->number_attribute<int> ("i");
103                         int const tj = (*i)->number_attribute<int> ("j");
104                         C(ti, tj) = raw_convert<double> ((*i)->content ());
105                 }
106
107                 double const rd = C(0, 0) + C(1, 0) + C(2, 0);
108                 _red = dcp::Chromaticity (C(0, 0) / rd, C(1, 0) / rd);
109                 double const gd = C(0, 1) + C(1, 1) + C(2, 1);
110                 _green = dcp::Chromaticity (C(0, 1) / gd, C(1, 1) / gd);
111                 double const bd = C(0, 2) + C(1, 2) + C(2, 2);
112                 _blue = dcp::Chromaticity (C(0, 2) / bd, C(1, 2) / bd);
113                 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);
114                 _white = dcp::Chromaticity ((C(0, 0) + C(0, 1) + C(0, 2)) / wd, (C(1, 0) + C(1, 1) + C(1, 2)) / wd);
115         } else {
116                 /* New-style chromaticities */
117                 _red = dcp::Chromaticity (node->number_child<double> ("RedX"), node->number_child<double> ("RedY"));
118                 _green = dcp::Chromaticity (node->number_child<double> ("GreenX"), node->number_child<double> ("GreenY"));
119                 _blue = dcp::Chromaticity (node->number_child<double> ("BlueX"), node->number_child<double> ("BlueY"));
120                 _white = dcp::Chromaticity (node->number_child<double> ("WhiteX"), node->number_child<double> ("WhiteY"));
121                 if (node->optional_node_child ("AdjustedWhiteX")) {
122                         _adjusted_white = dcp::Chromaticity (
123                                 node->number_child<double> ("AdjustedWhiteX"), node->number_child<double> ("AdjustedWhiteY")
124                                 );
125                 }
126         }
127
128         optional<double> gamma = node->optional_number_child<double> ("OutputGamma");
129         if (gamma) {
130                 _out.reset (new dcp::GammaTransferFunction (node->number_child<double> ("OutputGamma")));
131         } else {
132                 _out.reset (new dcp::IdentityTransferFunction ());
133         }
134 }
135
136 boost::optional<ColourConversion>
137 ColourConversion::from_xml (cxml::NodePtr node, int version)
138 {
139         if (!node->optional_node_child ("InputTransferFunction")) {
140                 return boost::optional<ColourConversion> ();
141         }
142
143         return ColourConversion (node, version);
144 }
145
146 void
147 ColourConversion::as_xml (xmlpp::Node* node) const
148 {
149         xmlpp::Node* in_node = node->add_child ("InputTransferFunction");
150         if (dynamic_pointer_cast<const dcp::GammaTransferFunction> (_in)) {
151                 shared_ptr<const dcp::GammaTransferFunction> tf = dynamic_pointer_cast<const dcp::GammaTransferFunction> (_in);
152                 in_node->add_child("Type")->add_child_text ("Gamma");
153                 in_node->add_child("Gamma")->add_child_text (raw_convert<string> (tf->gamma ()));
154         } else if (dynamic_pointer_cast<const dcp::ModifiedGammaTransferFunction> (_in)) {
155                 shared_ptr<const dcp::ModifiedGammaTransferFunction> tf = dynamic_pointer_cast<const dcp::ModifiedGammaTransferFunction> (_in);
156                 in_node->add_child("Type")->add_child_text ("ModifiedGamma");
157                 in_node->add_child("Power")->add_child_text (raw_convert<string> (tf->power ()));
158                 in_node->add_child("Threshold")->add_child_text (raw_convert<string> (tf->threshold ()));
159                 in_node->add_child("A")->add_child_text (raw_convert<string> (tf->A ()));
160                 in_node->add_child("B")->add_child_text (raw_convert<string> (tf->B ()));
161         } else if (dynamic_pointer_cast<const dcp::SGamut3TransferFunction> (_in)) {
162                 in_node->add_child("Type")->add_child_text ("SGamut3");
163         }
164
165         node->add_child("YUVToRGB")->add_child_text (raw_convert<string> (static_cast<int> (_yuv_to_rgb)));
166         node->add_child("RedX")->add_child_text (raw_convert<string> (_red.x));
167         node->add_child("RedY")->add_child_text (raw_convert<string> (_red.y));
168         node->add_child("GreenX")->add_child_text (raw_convert<string> (_green.x));
169         node->add_child("GreenY")->add_child_text (raw_convert<string> (_green.y));
170         node->add_child("BlueX")->add_child_text (raw_convert<string> (_blue.x));
171         node->add_child("BlueY")->add_child_text (raw_convert<string> (_blue.y));
172         node->add_child("WhiteX")->add_child_text (raw_convert<string> (_white.x));
173         node->add_child("WhiteY")->add_child_text (raw_convert<string> (_white.y));
174         if (_adjusted_white) {
175                 node->add_child("AdjustedWhiteX")->add_child_text (raw_convert<string> (_adjusted_white.get().x));
176                 node->add_child("AdjustedWhiteY")->add_child_text (raw_convert<string> (_adjusted_white.get().y));
177         }
178
179         if (dynamic_pointer_cast<const dcp::GammaTransferFunction> (_out)) {
180                 shared_ptr<const dcp::GammaTransferFunction> gf = dynamic_pointer_cast<const dcp::GammaTransferFunction> (_out);
181                 node->add_child("OutputGamma")->add_child_text (raw_convert<string> (gf->gamma ()));
182         }
183 }
184
185 optional<size_t>
186 ColourConversion::preset () const
187 {
188         vector<PresetColourConversion> presets = PresetColourConversion::all ();
189         size_t i = 0;
190         while (i < presets.size() && presets[i].conversion != *this) {
191                 ++i;
192         }
193
194         if (i >= presets.size ()) {
195                 return optional<size_t> ();
196         }
197
198         return i;
199 }
200
201 string
202 ColourConversion::identifier () const
203 {
204         Digester digester;
205
206         if (dynamic_pointer_cast<const dcp::GammaTransferFunction> (_in)) {
207                 shared_ptr<const dcp::GammaTransferFunction> tf = dynamic_pointer_cast<const dcp::GammaTransferFunction> (_in);
208                 digester.add (tf->gamma ());
209         } else if (dynamic_pointer_cast<const dcp::ModifiedGammaTransferFunction> (_in)) {
210                 shared_ptr<const dcp::ModifiedGammaTransferFunction> tf = dynamic_pointer_cast<const dcp::ModifiedGammaTransferFunction> (_in);
211                 digester.add (tf->power ());
212                 digester.add (tf->threshold ());
213                 digester.add (tf->A ());
214                 digester.add (tf->B ());
215         }
216
217         digester.add (_red.x);
218         digester.add (_red.y);
219         digester.add (_green.x);
220         digester.add (_green.y);
221         digester.add (_blue.x);
222         digester.add (_blue.y);
223         digester.add (_white.x);
224         digester.add (_white.y);
225
226         if (_adjusted_white) {
227                 digester.add (_adjusted_white.get().x);
228                 digester.add (_adjusted_white.get().y);
229         }
230
231         shared_ptr<const dcp::GammaTransferFunction> gf = dynamic_pointer_cast<const dcp::GammaTransferFunction> (_out);
232         if (gf) {
233                 digester.add (gf->gamma ());
234         }
235
236         return digester.get ();
237 }
238
239 PresetColourConversion::PresetColourConversion ()
240         : name (_("Untitled"))
241 {
242
243 }
244
245 PresetColourConversion::PresetColourConversion (string n, string i, dcp::ColourConversion conversion_)
246         : conversion (conversion_)
247         , name (n)
248         , id (i)
249 {
250
251 }
252
253 PresetColourConversion::PresetColourConversion (cxml::NodePtr node, int version)
254         : conversion (node, version)
255         , name (node->string_child ("Name"))
256 {
257
258 }
259
260 bool
261 operator== (ColourConversion const & a, ColourConversion const & b)
262 {
263         return a.about_equal (b, 1e-6);
264 }
265
266 bool
267 operator!= (ColourConversion const & a, ColourConversion const & b)
268 {
269         return !(a == b);
270 }
271
272 bool
273 operator== (PresetColourConversion const & a, PresetColourConversion const & b)
274 {
275         return a.name == b.name && a.conversion == b.conversion;
276 }
277
278 void
279 PresetColourConversion::setup_colour_conversion_presets ()
280 {
281         _presets.push_back (PresetColourConversion (_("sRGB"), "srgb", dcp::ColourConversion::srgb_to_xyz ()));
282         _presets.push_back (PresetColourConversion (_("Rec. 601"), "rec601", dcp::ColourConversion::rec601_to_xyz ()));
283         _presets.push_back (PresetColourConversion (_("Rec. 709"), "rec709", dcp::ColourConversion::rec709_to_xyz ()));
284         _presets.push_back (PresetColourConversion (_("P3"), "p3", dcp::ColourConversion::p3_to_xyz ()));
285         _presets.push_back (PresetColourConversion (_("Rec. 1886"), "rec1886", dcp::ColourConversion::rec1886_to_xyz ()));
286         _presets.push_back (PresetColourConversion (_("Rec. 2020"), "rec2020", dcp::ColourConversion::rec2020_to_xyz ()));
287         _presets.push_back (PresetColourConversion (_("S-Gamut3/S-Log3"), "sgamut3", dcp::ColourConversion::s_gamut3_to_xyz ()));
288 }
289
290 PresetColourConversion
291 PresetColourConversion::from_id (string s)
292 {
293         BOOST_FOREACH (PresetColourConversion const& i, _presets) {
294                 if (i.id == s) {
295                         return i;
296                 }
297         }
298
299         DCPOMATIC_ASSERT (false);
300 }