Very basics of colour conversion configuration.
[dcpomatic.git] / src / lib / colour_conversion.cc
1 /*
2     Copyright (C) 2013 Carl Hetherington <cth@carlh.net>
3
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.
8
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.
13
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.
17
18 */
19
20 #include <boost/lexical_cast.hpp>
21 #include <libxml++/libxml++.h>
22 #include <libdcp/colour_matrix.h>
23 #include <libcxml/cxml.h>
24 #include "colour_conversion.h"
25
26 #include "i18n.h"
27
28 using std::list;
29 using std::string;
30 using boost::shared_ptr;
31 using boost::lexical_cast;
32
33 ColourConversion::ColourConversion ()
34         : name (_("Untitled"))
35         , input_gamma (2.4)
36         , input_gamma_linearised (true)
37         , matrix (3, 3)
38         , output_gamma (2.6)
39 {
40         for (int i = 0; i < 3; ++i) {
41                 for (int j = 0; j < 3; ++j) {
42                         matrix (i, j) = libdcp::colour_matrix::xyz_to_rgb[i][j];
43                 }
44         }
45 }
46
47 ColourConversion::ColourConversion (string n, float i, bool il, float const m[3][3], float o)
48         : name (n)
49         , input_gamma (i)
50         , input_gamma_linearised (il)
51         , matrix (3, 3)
52         , output_gamma (o)
53 {
54         for (int i = 0; i < 3; ++i) {
55                 for (int j = 0; j < 3; ++j) {
56                         matrix (i, j) = m[i][j];
57                 }
58         }
59 }
60
61 ColourConversion::ColourConversion (shared_ptr<cxml::Node> node)
62         : matrix (3, 3)
63 {
64         name = node->string_child ("Name");
65         input_gamma = node->number_child<float> ("InputGamma");
66         input_gamma_linearised = node->bool_child ("InputGammaLinearised");
67
68         for (int i = 0; i < 3; ++i) {
69                 for (int j = 0; j < 3; ++j) {
70                         matrix (i, j) = 0;
71                 }
72         }
73
74         list<shared_ptr<cxml::Node> > m = node->node_children ("Matrix");
75         for (list<shared_ptr<cxml::Node> >::iterator i = m.begin(); i != m.end(); ++i) {
76                 int const ti = (*i)->number_attribute<int> ("i");
77                 int const tj = (*i)->number_attribute<int> ("j");
78                 matrix(ti, tj) = lexical_cast<float> ((*i)->content ());
79         }
80
81         output_gamma = node->number_child<float> ("OutputGamma");
82 }
83
84 void
85 ColourConversion::as_xml (xmlpp::Node* node) const
86 {
87         node->add_child("Name")->add_child_text (name);
88         node->add_child("InputGamma")->add_child_text (lexical_cast<string> (input_gamma));
89         node->add_child("InputGammaLinearised")->add_child_text (input_gamma_linearised ? "1" : "0");
90
91         for (int i = 0; i < 3; ++i) {
92                 for (int j = 0; j < 3; ++j) {
93                         xmlpp::Element* m = node->add_child("Matrix");
94                         m->set_attribute ("i", lexical_cast<string> (i));
95                         m->set_attribute ("j", lexical_cast<string> (j));
96                         m->add_child_text (lexical_cast<string> (matrix (i, j)));
97                 }
98         }
99
100         node->add_child("OutputGamma")->add_child_text (lexical_cast<string> (output_gamma));
101 }