2 Copyright (C) 2013 Carl Hetherington <cth@carlh.net>
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 #include "colour_conversion.h"
23 #include "md5_digester.h"
24 #include "raw_convert.h"
25 #include <dcp/chromaticity.h>
26 #include <dcp/colour_matrix.h>
27 #include <dcp/gamma_transfer_function.h>
28 #include <dcp/modified_gamma_transfer_function.h>
29 #include <libcxml/cxml.h>
30 #include <libxml++/libxml++.h>
38 using boost::shared_ptr;
39 using boost::optional;
40 using boost::dynamic_pointer_cast;
42 ColourConversion::ColourConversion ()
43 : dcp::ColourConversion (dcp::ColourConversion::srgb_to_xyz ())
48 ColourConversion::ColourConversion (dcp::ColourConversion conversion_)
49 : dcp::ColourConversion (conversion_)
54 ColourConversion::ColourConversion (cxml::NodePtr node, int version)
56 shared_ptr<dcp::TransferFunction> in;
62 cxml::ConstNodePtr in_node = node->node_child ("InputTransferFunction");
63 string in_type = in_node->string_child ("Type");
64 if (in_type == "Gamma") {
65 _in.reset (new dcp::GammaTransferFunction (in_node->number_child<double> ("Gamma")));
66 } else if (in_type == "ModifiedGamma") {
67 _in.reset (new dcp::ModifiedGammaTransferFunction (
68 in_node->number_child<double> ("Power"),
69 in_node->number_child<double> ("Threshold"),
70 in_node->number_child<double> ("A"),
71 in_node->number_child<double> ("B")
79 if (node->bool_child ("InputGammaLinearised")) {
80 _in.reset (new dcp::ModifiedGammaTransferFunction (node->number_child<float> ("InputGamma"), 0.04045, 0.055, 12.92));
82 _in.reset (new dcp::GammaTransferFunction (node->number_child<float> ("InputGamma")));
86 _yuv_to_rgb = static_cast<dcp::YUVToRGB> (node->optional_number_child<int>("YUVToRGB").get_value_or (dcp::YUV_TO_RGB_REC601));
88 list<cxml::NodePtr> m = node->node_children ("Matrix");
90 /* Read in old <Matrix> nodes and convert them to chromaticities */
91 boost::numeric::ublas::matrix<double> C (3, 3);
92 for (list<cxml::NodePtr>::iterator i = m.begin(); i != m.end(); ++i) {
93 int const ti = (*i)->number_attribute<int> ("i");
94 int const tj = (*i)->number_attribute<int> ("j");
95 C(ti, tj) = raw_convert<double> ((*i)->content ());
98 double const rd = C(0, 0) + C(1, 0) + C(2, 0);
99 _red = dcp::Chromaticity (C(0, 0) / rd, C(1, 0) / rd);
100 double const gd = C(0, 1) + C(1, 1) + C(2, 1);
101 _green = dcp::Chromaticity (C(0, 1) / gd, C(1, 1) / gd);
102 double const bd = C(0, 2) + C(1, 2) + C(2, 2);
103 _blue = dcp::Chromaticity (C(0, 2) / bd, C(1, 2) / bd);
104 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);
105 _white = dcp::Chromaticity ((C(0, 0) + C(0, 1) + C(0, 2)) / wd, (C(1, 0) + C(1, 1) + C(1, 2)) / wd);
107 /* New-style chromaticities */
108 _red = dcp::Chromaticity (node->number_child<double> ("RedX"), node->number_child<double> ("RedY"));
109 _green = dcp::Chromaticity (node->number_child<double> ("GreenX"), node->number_child<double> ("GreenY"));
110 _blue = dcp::Chromaticity (node->number_child<double> ("BlueX"), node->number_child<double> ("BlueY"));
111 _white = dcp::Chromaticity (node->number_child<double> ("WhiteX"), node->number_child<double> ("WhiteY"));
112 if (node->optional_node_child ("AdjustedWhiteX")) {
113 _adjusted_white = dcp::Chromaticity (
114 node->number_child<double> ("AdjustedWhiteX"), node->number_child<double> ("AdjustedWhiteY")
119 _out.reset (new dcp::GammaTransferFunction (node->number_child<double> ("OutputGamma")));
122 boost::optional<ColourConversion>
123 ColourConversion::from_xml (cxml::NodePtr node, int version)
125 if (!node->optional_node_child ("InputTransferFunction")) {
126 return boost::optional<ColourConversion> ();
129 return ColourConversion (node, version);
133 ColourConversion::as_xml (xmlpp::Node* node) const
135 xmlpp::Node* in_node = node->add_child ("InputTransferFunction");
136 if (dynamic_pointer_cast<const dcp::GammaTransferFunction> (_in)) {
137 shared_ptr<const dcp::GammaTransferFunction> tf = dynamic_pointer_cast<const dcp::GammaTransferFunction> (_in);
138 in_node->add_child("Type")->add_child_text ("Gamma");
139 in_node->add_child("Gamma")->add_child_text (raw_convert<string> (tf->gamma ()));
140 } else if (dynamic_pointer_cast<const dcp::ModifiedGammaTransferFunction> (_in)) {
141 shared_ptr<const dcp::ModifiedGammaTransferFunction> tf = dynamic_pointer_cast<const dcp::ModifiedGammaTransferFunction> (_in);
142 in_node->add_child("Type")->add_child_text ("ModifiedGamma");
143 in_node->add_child("Power")->add_child_text (raw_convert<string> (tf->power ()));
144 in_node->add_child("Threshold")->add_child_text (raw_convert<string> (tf->threshold ()));
145 in_node->add_child("A")->add_child_text (raw_convert<string> (tf->A ()));
146 in_node->add_child("B")->add_child_text (raw_convert<string> (tf->B ()));
149 node->add_child("RedX")->add_child_text (raw_convert<string> (_red.x));
150 node->add_child("RedY")->add_child_text (raw_convert<string> (_red.y));
151 node->add_child("GreenX")->add_child_text (raw_convert<string> (_green.x));
152 node->add_child("GreenY")->add_child_text (raw_convert<string> (_green.y));
153 node->add_child("BlueX")->add_child_text (raw_convert<string> (_blue.x));
154 node->add_child("BlueY")->add_child_text (raw_convert<string> (_blue.y));
155 node->add_child("WhiteX")->add_child_text (raw_convert<string> (_white.x));
156 node->add_child("WhiteY")->add_child_text (raw_convert<string> (_white.y));
157 if (_adjusted_white) {
158 node->add_child("AdjustedWhiteX")->add_child_text (raw_convert<string> (_adjusted_white.get().x));
159 node->add_child("AdjustedWhiteY")->add_child_text (raw_convert<string> (_adjusted_white.get().y));
162 node->add_child("OutputGamma")->add_child_text (raw_convert<string> (dynamic_pointer_cast<const dcp::GammaTransferFunction> (_out)->gamma ()));
166 ColourConversion::preset () const
168 vector<PresetColourConversion> presets = Config::instance()->colour_conversions ();
170 while (i < presets.size() && (presets[i].conversion != *this)) {
174 if (i >= presets.size ()) {
175 return optional<size_t> ();
182 ColourConversion::identifier () const
184 MD5Digester digester;
186 if (dynamic_pointer_cast<const dcp::GammaTransferFunction> (_in)) {
187 shared_ptr<const dcp::GammaTransferFunction> tf = dynamic_pointer_cast<const dcp::GammaTransferFunction> (_in);
188 digester.add (tf->gamma ());
189 } else if (dynamic_pointer_cast<const dcp::ModifiedGammaTransferFunction> (_in)) {
190 shared_ptr<const dcp::ModifiedGammaTransferFunction> tf = dynamic_pointer_cast<const dcp::ModifiedGammaTransferFunction> (_in);
191 digester.add (tf->power ());
192 digester.add (tf->threshold ());
193 digester.add (tf->A ());
194 digester.add (tf->B ());
197 digester.add (_red.x);
198 digester.add (_red.y);
199 digester.add (_green.x);
200 digester.add (_green.y);
201 digester.add (_blue.x);
202 digester.add (_blue.y);
203 digester.add (_white.x);
204 digester.add (_white.y);
206 if (_adjusted_white) {
207 digester.add (_adjusted_white.get().x);
208 digester.add (_adjusted_white.get().y);
211 digester.add (dynamic_pointer_cast<const dcp::GammaTransferFunction> (_out)->gamma ());
213 return digester.get ();
216 PresetColourConversion::PresetColourConversion ()
217 : name (_("Untitled"))
222 PresetColourConversion::PresetColourConversion (string n, dcp::ColourConversion conversion_)
223 : conversion (conversion_)
229 PresetColourConversion::PresetColourConversion (cxml::NodePtr node, int version)
230 : conversion (node, version)
231 , name (node->string_child ("Name"))
237 PresetColourConversion::as_xml (xmlpp::Node* node) const
239 conversion.as_xml (node);
240 node->add_child("Name")->add_child_text (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;