2 Copyright (C) 2013-2015 Carl Hetherington <cth@carlh.net>
4 This file is part of DCP-o-matic.
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.
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.
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/>.
22 #include "colour_conversion.h"
25 #include "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 <libcxml/cxml.h>
31 #include <libxml++/libxml++.h>
32 #include <boost/foreach.hpp>
41 using boost::shared_ptr;
42 using boost::optional;
43 using boost::dynamic_pointer_cast;
45 vector<PresetColourConversion> PresetColourConversion::_presets;
47 ColourConversion::ColourConversion ()
48 : dcp::ColourConversion (dcp::ColourConversion::srgb_to_xyz ())
53 ColourConversion::ColourConversion (dcp::ColourConversion conversion_)
54 : dcp::ColourConversion (conversion_)
59 ColourConversion::ColourConversion (cxml::NodePtr node, int version)
61 shared_ptr<dcp::TransferFunction> in;
67 cxml::ConstNodePtr in_node = node->node_child ("InputTransferFunction");
68 string in_type = in_node->string_child ("Type");
69 if (in_type == "Gamma") {
70 _in.reset (new dcp::GammaTransferFunction (in_node->number_child<double> ("Gamma")));
71 } else if (in_type == "ModifiedGamma") {
72 _in.reset (new dcp::ModifiedGammaTransferFunction (
73 in_node->number_child<double> ("Power"),
74 in_node->number_child<double> ("Threshold"),
75 in_node->number_child<double> ("A"),
76 in_node->number_child<double> ("B")
84 if (node->bool_child ("InputGammaLinearised")) {
85 _in.reset (new dcp::ModifiedGammaTransferFunction (node->number_child<double> ("InputGamma"), 0.04045, 0.055, 12.92));
87 _in.reset (new dcp::GammaTransferFunction (node->number_child<double> ("InputGamma")));
91 _yuv_to_rgb = static_cast<dcp::YUVToRGB> (node->optional_number_child<int>("YUVToRGB").get_value_or (dcp::YUV_TO_RGB_REC601));
93 list<cxml::NodePtr> m = node->node_children ("Matrix");
95 /* Read in old <Matrix> nodes and convert them to chromaticities */
96 boost::numeric::ublas::matrix<double> C (3, 3);
97 for (list<cxml::NodePtr>::iterator i = m.begin(); i != m.end(); ++i) {
98 int const ti = (*i)->number_attribute<int> ("i");
99 int const tj = (*i)->number_attribute<int> ("j");
100 C(ti, tj) = raw_convert<double> ((*i)->content ());
103 double const rd = C(0, 0) + C(1, 0) + C(2, 0);
104 _red = dcp::Chromaticity (C(0, 0) / rd, C(1, 0) / rd);
105 double const gd = C(0, 1) + C(1, 1) + C(2, 1);
106 _green = dcp::Chromaticity (C(0, 1) / gd, C(1, 1) / gd);
107 double const bd = C(0, 2) + C(1, 2) + C(2, 2);
108 _blue = dcp::Chromaticity (C(0, 2) / bd, C(1, 2) / bd);
109 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);
110 _white = dcp::Chromaticity ((C(0, 0) + C(0, 1) + C(0, 2)) / wd, (C(1, 0) + C(1, 1) + C(1, 2)) / wd);
112 /* New-style chromaticities */
113 _red = dcp::Chromaticity (node->number_child<double> ("RedX"), node->number_child<double> ("RedY"));
114 _green = dcp::Chromaticity (node->number_child<double> ("GreenX"), node->number_child<double> ("GreenY"));
115 _blue = dcp::Chromaticity (node->number_child<double> ("BlueX"), node->number_child<double> ("BlueY"));
116 _white = dcp::Chromaticity (node->number_child<double> ("WhiteX"), node->number_child<double> ("WhiteY"));
117 if (node->optional_node_child ("AdjustedWhiteX")) {
118 _adjusted_white = dcp::Chromaticity (
119 node->number_child<double> ("AdjustedWhiteX"), node->number_child<double> ("AdjustedWhiteY")
124 _out.reset (new dcp::GammaTransferFunction (node->number_child<double> ("OutputGamma")));
127 boost::optional<ColourConversion>
128 ColourConversion::from_xml (cxml::NodePtr node, int version)
130 if (!node->optional_node_child ("InputTransferFunction")) {
131 return boost::optional<ColourConversion> ();
134 return ColourConversion (node, version);
138 ColourConversion::as_xml (xmlpp::Node* node) const
140 xmlpp::Node* in_node = node->add_child ("InputTransferFunction");
141 if (dynamic_pointer_cast<const dcp::GammaTransferFunction> (_in)) {
142 shared_ptr<const dcp::GammaTransferFunction> tf = dynamic_pointer_cast<const dcp::GammaTransferFunction> (_in);
143 in_node->add_child("Type")->add_child_text ("Gamma");
144 in_node->add_child("Gamma")->add_child_text (raw_convert<string> (tf->gamma ()));
145 } else if (dynamic_pointer_cast<const dcp::ModifiedGammaTransferFunction> (_in)) {
146 shared_ptr<const dcp::ModifiedGammaTransferFunction> tf = dynamic_pointer_cast<const dcp::ModifiedGammaTransferFunction> (_in);
147 in_node->add_child("Type")->add_child_text ("ModifiedGamma");
148 in_node->add_child("Power")->add_child_text (raw_convert<string> (tf->power ()));
149 in_node->add_child("Threshold")->add_child_text (raw_convert<string> (tf->threshold ()));
150 in_node->add_child("A")->add_child_text (raw_convert<string> (tf->A ()));
151 in_node->add_child("B")->add_child_text (raw_convert<string> (tf->B ()));
154 node->add_child("YUVToRGB")->add_child_text (raw_convert<string> (_yuv_to_rgb));
155 node->add_child("RedX")->add_child_text (raw_convert<string> (_red.x));
156 node->add_child("RedY")->add_child_text (raw_convert<string> (_red.y));
157 node->add_child("GreenX")->add_child_text (raw_convert<string> (_green.x));
158 node->add_child("GreenY")->add_child_text (raw_convert<string> (_green.y));
159 node->add_child("BlueX")->add_child_text (raw_convert<string> (_blue.x));
160 node->add_child("BlueY")->add_child_text (raw_convert<string> (_blue.y));
161 node->add_child("WhiteX")->add_child_text (raw_convert<string> (_white.x));
162 node->add_child("WhiteY")->add_child_text (raw_convert<string> (_white.y));
163 if (_adjusted_white) {
164 node->add_child("AdjustedWhiteX")->add_child_text (raw_convert<string> (_adjusted_white.get().x));
165 node->add_child("AdjustedWhiteY")->add_child_text (raw_convert<string> (_adjusted_white.get().y));
168 node->add_child("OutputGamma")->add_child_text (raw_convert<string> (dynamic_pointer_cast<const dcp::GammaTransferFunction> (_out)->gamma ()));
172 ColourConversion::preset () const
174 vector<PresetColourConversion> presets = PresetColourConversion::all ();
176 while (i < presets.size() && presets[i].conversion != *this) {
180 if (i >= presets.size ()) {
181 return optional<size_t> ();
188 ColourConversion::identifier () const
192 if (dynamic_pointer_cast<const dcp::GammaTransferFunction> (_in)) {
193 shared_ptr<const dcp::GammaTransferFunction> tf = dynamic_pointer_cast<const dcp::GammaTransferFunction> (_in);
194 digester.add (tf->gamma ());
195 } else if (dynamic_pointer_cast<const dcp::ModifiedGammaTransferFunction> (_in)) {
196 shared_ptr<const dcp::ModifiedGammaTransferFunction> tf = dynamic_pointer_cast<const dcp::ModifiedGammaTransferFunction> (_in);
197 digester.add (tf->power ());
198 digester.add (tf->threshold ());
199 digester.add (tf->A ());
200 digester.add (tf->B ());
203 digester.add (_red.x);
204 digester.add (_red.y);
205 digester.add (_green.x);
206 digester.add (_green.y);
207 digester.add (_blue.x);
208 digester.add (_blue.y);
209 digester.add (_white.x);
210 digester.add (_white.y);
212 if (_adjusted_white) {
213 digester.add (_adjusted_white.get().x);
214 digester.add (_adjusted_white.get().y);
217 digester.add (dynamic_pointer_cast<const dcp::GammaTransferFunction> (_out)->gamma ());
219 return digester.get ();
222 PresetColourConversion::PresetColourConversion ()
223 : name (_("Untitled"))
228 PresetColourConversion::PresetColourConversion (string n, string i, dcp::ColourConversion conversion_)
229 : conversion (conversion_)
236 PresetColourConversion::PresetColourConversion (cxml::NodePtr node, int version)
237 : conversion (node, version)
238 , name (node->string_child ("Name"))
244 operator== (ColourConversion const & a, ColourConversion const & b)
246 return a.about_equal (b, 1e-6);
250 operator!= (ColourConversion const & a, ColourConversion const & b)
256 operator== (PresetColourConversion const & a, PresetColourConversion const & b)
258 return a.name == b.name && a.conversion == b.conversion;
262 PresetColourConversion::setup_colour_conversion_presets ()
264 _presets.push_back (PresetColourConversion (_("sRGB"), "srgb", dcp::ColourConversion::srgb_to_xyz ()));
265 _presets.push_back (PresetColourConversion (_("Rec. 601"), "rec601", dcp::ColourConversion::rec601_to_xyz ()));
266 _presets.push_back (PresetColourConversion (_("Rec. 709"), "rec709", dcp::ColourConversion::rec709_to_xyz ()));
267 _presets.push_back (PresetColourConversion (_("P3"), "p3", dcp::ColourConversion::p3_to_xyz ()));
268 _presets.push_back (PresetColourConversion (_("Rec. 1886"), "rec1886", dcp::ColourConversion::rec1886_to_xyz ()));
269 _presets.push_back (PresetColourConversion (_("Rec. 2020"), "rec2020", dcp::ColourConversion::rec2020_to_xyz ()));
272 PresetColourConversion
273 PresetColourConversion::from_id (string s)
275 BOOST_FOREACH (PresetColourConversion const& i, _presets) {
281 DCPOMATIC_ASSERT (false);