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