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