FIXME: Remove all use of add_child() from xmlpp.
[dcpomatic.git] / src / lib / colour_conversion.cc
1 /*
2     Copyright (C) 2013-2021 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
22 #include "colour_conversion.h"
23 #include "config.h"
24 #include "digester.h"
25 #include "util.h"
26 #include <dcp/chromaticity.h>
27 #include <dcp/gamma_transfer_function.h>
28 #include <dcp/identity_transfer_function.h>
29 #include <dcp/modified_gamma_transfer_function.h>
30 #include <dcp/raw_convert.h>
31 #include <dcp/s_gamut3_transfer_function.h>
32 #include <dcp/warnings.h>
33 #include <libcxml/cxml.h>
34 LIBDCP_DISABLE_WARNINGS
35 #include <libxml++/libxml++.h>
36 LIBDCP_ENABLE_WARNINGS
37 #include <iostream>
38
39 #include "i18n.h"
40
41
42 using std::cout;
43 using std::dynamic_pointer_cast;
44 using std::list;
45 using std::make_shared;
46 using std::shared_ptr;
47 using std::string;
48 using std::vector;
49 using boost::optional;
50 using dcp::raw_convert;
51
52
53 vector<PresetColourConversion> PresetColourConversion::_presets;
54
55
56 ColourConversion::ColourConversion ()
57         : dcp::ColourConversion (dcp::ColourConversion::srgb_to_xyz ())
58 {
59
60 }
61
62 ColourConversion::ColourConversion (dcp::ColourConversion conversion_)
63         : dcp::ColourConversion (conversion_)
64 {
65
66 }
67
68 ColourConversion::ColourConversion (cxml::NodePtr node, int version)
69 {
70         shared_ptr<dcp::TransferFunction> in;
71
72         if (version >= 32) {
73
74                 /* Version 2.x */
75
76                 auto in_node = node->node_child ("InputTransferFunction");
77                 auto in_type = in_node->string_child ("Type");
78                 if (in_type == "Gamma") {
79                         _in = make_shared<dcp::GammaTransferFunction>(in_node->number_child<double> ("Gamma"));
80                 } else if (in_type == "ModifiedGamma") {
81                         _in = make_shared<dcp::ModifiedGammaTransferFunction>(
82                                            in_node->number_child<double>("Power"),
83                                            in_node->number_child<double>("Threshold"),
84                                            in_node->number_child<double>("A"),
85                                            in_node->number_child<double>("B")
86                                            );
87                 } else if (in_type == "SGamut3") {
88                         _in = make_shared<dcp::SGamut3TransferFunction>();
89                 }
90
91         } else {
92
93                 /* Version 1.x */
94
95                 if (node->bool_child ("InputGammaLinearised")) {
96                         _in.reset (new dcp::ModifiedGammaTransferFunction (node->number_child<double> ("InputGamma"), 0.04045, 0.055, 12.92));
97                 } else {
98                         _in.reset (new dcp::GammaTransferFunction (node->number_child<double> ("InputGamma")));
99                 }
100         }
101
102         _yuv_to_rgb = static_cast<dcp::YUVToRGB>(node->optional_number_child<int>("YUVToRGB").get_value_or(static_cast<int>(dcp::YUVToRGB::REC601)));
103
104         auto m = node->node_children ("Matrix");
105         if (!m.empty ()) {
106                 /* Read in old <Matrix> nodes and convert them to chromaticities */
107                 boost::numeric::ublas::matrix<double> C (3, 3);
108                 for (auto i: m) {
109                         int const ti = i->number_attribute<int>("i");
110                         int const tj = i->number_attribute<int>("j");
111                         C(ti, tj) = raw_convert<double>(i->content());
112                 }
113
114                 double const rd = C(0, 0) + C(1, 0) + C(2, 0);
115                 _red = dcp::Chromaticity (C(0, 0) / rd, C(1, 0) / rd);
116                 double const gd = C(0, 1) + C(1, 1) + C(2, 1);
117                 _green = dcp::Chromaticity (C(0, 1) / gd, C(1, 1) / gd);
118                 double const bd = C(0, 2) + C(1, 2) + C(2, 2);
119                 _blue = dcp::Chromaticity (C(0, 2) / bd, C(1, 2) / bd);
120                 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);
121                 _white = dcp::Chromaticity ((C(0, 0) + C(0, 1) + C(0, 2)) / wd, (C(1, 0) + C(1, 1) + C(1, 2)) / wd);
122         } else {
123                 /* New-style chromaticities */
124                 _red = dcp::Chromaticity (node->number_child<double> ("RedX"), node->number_child<double> ("RedY"));
125                 _green = dcp::Chromaticity (node->number_child<double> ("GreenX"), node->number_child<double> ("GreenY"));
126                 _blue = dcp::Chromaticity (node->number_child<double> ("BlueX"), node->number_child<double> ("BlueY"));
127                 _white = dcp::Chromaticity (node->number_child<double> ("WhiteX"), node->number_child<double> ("WhiteY"));
128                 if (node->optional_node_child ("AdjustedWhiteX")) {
129                         _adjusted_white = dcp::Chromaticity (
130                                 node->number_child<double> ("AdjustedWhiteX"), node->number_child<double> ("AdjustedWhiteY")
131                                 );
132                 }
133         }
134
135         auto gamma = node->optional_number_child<double>("OutputGamma");
136         if (gamma) {
137                 _out = make_shared<dcp::GammaTransferFunction>(node->number_child<double>("OutputGamma"));
138         } else {
139                 _out = make_shared<dcp::IdentityTransferFunction>();
140         }
141 }
142
143 boost::optional<ColourConversion>
144 ColourConversion::from_xml (cxml::NodePtr node, int version)
145 {
146         if (!node->optional_node_child ("InputTransferFunction")) {
147                 return boost::optional<ColourConversion> ();
148         }
149
150         return ColourConversion (node, version);
151 }
152
153 void
154 ColourConversion::as_xml(xmlpp::Element* element) const
155 {
156         auto in_node = cxml::add_child(element, "InputTransferFunction");
157         if (dynamic_pointer_cast<const dcp::GammaTransferFunction> (_in)) {
158                 auto tf = dynamic_pointer_cast<const dcp::GammaTransferFunction> (_in);
159                 cxml::add_text_child(in_node, "Type", "Gamma");
160                 cxml::add_text_child(in_node, "Gamma", raw_convert<string>(tf->gamma()));
161         } else if (dynamic_pointer_cast<const dcp::ModifiedGammaTransferFunction> (_in)) {
162                 auto tf = dynamic_pointer_cast<const dcp::ModifiedGammaTransferFunction> (_in);
163                 cxml::add_text_child(in_node, "Type", "ModifiedGamma");
164                 cxml::add_text_child(in_node, "Power", raw_convert<string>(tf->power ()));
165                 cxml::add_text_child(in_node, "Threshold", raw_convert<string>(tf->threshold ()));
166                 cxml::add_text_child(in_node, "A", raw_convert<string>(tf->A()));
167                 cxml::add_text_child(in_node, "B", raw_convert<string>(tf->B()));
168         } else if (dynamic_pointer_cast<const dcp::SGamut3TransferFunction>(_in)) {
169                 cxml::add_text_child(in_node, "Type", "SGamut3");
170         }
171
172         cxml::add_text_child(element, "YUVToRGB", raw_convert<string>(static_cast<int>(_yuv_to_rgb)));
173         cxml::add_text_child(element, "RedX", raw_convert<string>(_red.x));
174         cxml::add_text_child(element, "RedY", raw_convert<string>(_red.y));
175         cxml::add_text_child(element, "GreenX", raw_convert<string>(_green.x));
176         cxml::add_text_child(element, "GreenY", raw_convert<string>(_green.y));
177         cxml::add_text_child(element, "BlueX", raw_convert<string>(_blue.x));
178         cxml::add_text_child(element, "BlueY", raw_convert<string>(_blue.y));
179         cxml::add_text_child(element, "WhiteX", raw_convert<string>(_white.x));
180         cxml::add_text_child(element, "WhiteY", raw_convert<string>(_white.y));
181         if (_adjusted_white) {
182                 cxml::add_text_child(element, "AdjustedWhiteX", raw_convert<string>(_adjusted_white.get().x));
183                 cxml::add_text_child(element, "AdjustedWhiteY", raw_convert<string>(_adjusted_white.get().y));
184         }
185
186         if (auto gf = dynamic_pointer_cast<const dcp::GammaTransferFunction>(_out)) {
187                 cxml::add_text_child(element, "OutputGamma", raw_convert<string>(gf->gamma()));
188         }
189 }
190
191 optional<size_t>
192 ColourConversion::preset () const
193 {
194         auto presets = PresetColourConversion::all ();
195         size_t i = 0;
196         while (i < presets.size() && presets[i].conversion != *this) {
197                 ++i;
198         }
199
200         if (i >= presets.size ()) {
201                 return {};
202         }
203
204         return i;
205 }
206
207 string
208 ColourConversion::identifier () const
209 {
210         Digester digester;
211
212         if (dynamic_pointer_cast<const dcp::GammaTransferFunction> (_in)) {
213                 shared_ptr<const dcp::GammaTransferFunction> tf = dynamic_pointer_cast<const dcp::GammaTransferFunction> (_in);
214                 digester.add (tf->gamma ());
215         } else if (dynamic_pointer_cast<const dcp::ModifiedGammaTransferFunction> (_in)) {
216                 shared_ptr<const dcp::ModifiedGammaTransferFunction> tf = dynamic_pointer_cast<const dcp::ModifiedGammaTransferFunction> (_in);
217                 digester.add (tf->power ());
218                 digester.add (tf->threshold ());
219                 digester.add (tf->A ());
220                 digester.add (tf->B ());
221         }
222
223         digester.add (_red.x);
224         digester.add (_red.y);
225         digester.add (_green.x);
226         digester.add (_green.y);
227         digester.add (_blue.x);
228         digester.add (_blue.y);
229         digester.add (_white.x);
230         digester.add (_white.y);
231
232         if (_adjusted_white) {
233                 digester.add (_adjusted_white.get().x);
234                 digester.add (_adjusted_white.get().y);
235         }
236
237         digester.add(static_cast<int>(_yuv_to_rgb));
238
239         auto gf = dynamic_pointer_cast<const dcp::GammaTransferFunction> (_out);
240         if (gf) {
241                 digester.add (gf->gamma ());
242         }
243
244         return digester.get ();
245 }
246
247 PresetColourConversion::PresetColourConversion ()
248         : name (_("Untitled"))
249 {
250
251 }
252
253 PresetColourConversion::PresetColourConversion (string n, string i, dcp::ColourConversion conversion_)
254         : conversion (conversion_)
255         , name (n)
256         , id (i)
257 {
258
259 }
260
261 PresetColourConversion::PresetColourConversion (cxml::NodePtr node, int version)
262         : conversion (node, version)
263         , name (node->string_child ("Name"))
264 {
265
266 }
267
268 bool
269 operator== (ColourConversion const & a, ColourConversion const & b)
270 {
271         return a.about_equal (b, 1e-6);
272 }
273
274 bool
275 operator!= (ColourConversion const & a, ColourConversion const & b)
276 {
277         return !(a == b);
278 }
279
280 bool
281 operator== (PresetColourConversion const & a, PresetColourConversion const & b)
282 {
283         return a.name == b.name && a.conversion == b.conversion;
284 }
285
286 void
287 PresetColourConversion::setup_colour_conversion_presets ()
288 {
289         _presets.push_back (PresetColourConversion (_("sRGB"), "srgb", dcp::ColourConversion::srgb_to_xyz ()));
290         _presets.push_back (PresetColourConversion (_("Rec. 601"), "rec601", dcp::ColourConversion::rec601_to_xyz ()));
291         _presets.push_back (PresetColourConversion (_("Rec. 709"), "rec709", dcp::ColourConversion::rec709_to_xyz ()));
292         _presets.push_back (PresetColourConversion (_("P3"), "p3", dcp::ColourConversion::p3_to_xyz ()));
293         _presets.push_back (PresetColourConversion (_("Rec. 1886"), "rec1886", dcp::ColourConversion::rec1886_to_xyz ()));
294         _presets.push_back (PresetColourConversion (_("Rec. 2020"), "rec2020", dcp::ColourConversion::rec2020_to_xyz ()));
295         _presets.push_back (PresetColourConversion (_("S-Gamut3/S-Log3"), "sgamut3", dcp::ColourConversion::s_gamut3_to_xyz ()));
296 }
297
298 PresetColourConversion
299 PresetColourConversion::from_id (string s)
300 {
301         for (auto const& i: _presets) {
302                 if (i.id == s) {
303                         return i;
304                 }
305         }
306
307         DCPOMATIC_ASSERT (false);
308 }