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