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