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.
20 #include <libxml++/libxml++.h>
21 #include <libdcp/colour_matrix.h>
22 #include <libdcp/raw_convert.h>
23 #include <libcxml/cxml.h>
25 #include "colour_conversion.h"
34 using boost::shared_ptr;
35 using boost::optional;
36 using libdcp::raw_convert;
38 ColourConversion::ColourConversion ()
40 , input_gamma_linearised (true)
44 for (int i = 0; i < 3; ++i) {
45 for (int j = 0; j < 3; ++j) {
46 matrix (i, j) = libdcp::colour_matrix::srgb_to_xyz[i][j];
51 ColourConversion::ColourConversion (double i, bool il, double const m[3][3], double o)
53 , input_gamma_linearised (il)
57 for (int i = 0; i < 3; ++i) {
58 for (int j = 0; j < 3; ++j) {
59 matrix (i, j) = m[i][j];
64 ColourConversion::ColourConversion (cxml::NodePtr node)
67 input_gamma = node->number_child<double> ("InputGamma");
68 input_gamma_linearised = node->bool_child ("InputGammaLinearised");
70 for (int i = 0; i < 3; ++i) {
71 for (int j = 0; j < 3; ++j) {
76 list<cxml::NodePtr> m = node->node_children ("Matrix");
77 for (list<cxml::NodePtr>::iterator i = m.begin(); i != m.end(); ++i) {
78 int const ti = (*i)->number_attribute<int> ("i");
79 int const tj = (*i)->number_attribute<int> ("j");
80 matrix(ti, tj) = raw_convert<double> ((*i)->content ());
83 output_gamma = node->number_child<double> ("OutputGamma");
87 ColourConversion::as_xml (xmlpp::Node* node) const
89 node->add_child("InputGamma")->add_child_text (raw_convert<string> (input_gamma));
90 node->add_child("InputGammaLinearised")->add_child_text (input_gamma_linearised ? "1" : "0");
92 for (int i = 0; i < 3; ++i) {
93 for (int j = 0; j < 3; ++j) {
94 xmlpp::Element* m = node->add_child("Matrix");
95 m->set_attribute ("i", raw_convert<string> (i));
96 m->set_attribute ("j", raw_convert<string> (j));
97 m->add_child_text (raw_convert<string> (matrix (i, j)));
101 node->add_child("OutputGamma")->add_child_text (raw_convert<string> (output_gamma));
105 ColourConversion::preset () const
107 vector<PresetColourConversion> presets = Config::instance()->colour_conversions ();
109 while (i < presets.size() && (presets[i].conversion != *this)) {
113 if (i >= presets.size ()) {
114 return optional<size_t> ();
121 ColourConversion::identifier () const
126 numbers[n++] = input_gamma;
127 numbers[n++] = input_gamma_linearised;
128 for (int i = 0; i < 3; ++i) {
129 for (int j = 0; j < 3; ++j) {
130 numbers[n++] = matrix (i, j);
133 numbers[n++] = output_gamma;
137 return md5_digest (numbers, 12 * sizeof (double));
140 PresetColourConversion::PresetColourConversion ()
141 : name (_("Untitled"))
146 PresetColourConversion::PresetColourConversion (string n, double i, bool il, double const m[3][3], double o)
148 , conversion (i, il, m, o)
153 PresetColourConversion::PresetColourConversion (cxml::NodePtr node)
156 name = node->string_child ("Name");
160 PresetColourConversion::as_xml (xmlpp::Node* node) const
162 conversion.as_xml (node);
163 node->add_child("Name")->add_child_text (name);
167 about_equal (double a, double b)
169 static const double eps = 1e-6;
170 return fabs (a - b) < eps;
174 operator== (ColourConversion const & a, ColourConversion const & b)
177 !about_equal (a.input_gamma, b.input_gamma) ||
178 a.input_gamma_linearised != b.input_gamma_linearised ||
179 !about_equal (a.output_gamma, b.output_gamma)) {
183 for (int i = 0; i < 3; ++i) {
184 for (int j = 0; j < 3; ++j) {
185 if (!about_equal (a.matrix (i, j), b.matrix (i, j))) {
195 operator!= (ColourConversion const & a, ColourConversion const & b)