Add another unit test.
[dcpomatic.git] / test / colour_conversion_test.cc
1 /*
2     Copyright (C) 2013-2015 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 /** @file  test/colour_conversion_test.cc
21  *  @brief Various tests of ColourConversion.
22  */
23
24 #include "lib/colour_conversion.h"
25 #include "lib/film.h"
26 #include <dcp/colour_matrix.h>
27 #include <dcp/gamma_transfer_function.h>
28 #include <libxml++/libxml++.h>
29 #include <boost/test/unit_test.hpp>
30 #include <boost/foreach.hpp>
31
32 using std::cout;
33 using boost::shared_ptr;
34
35 BOOST_AUTO_TEST_CASE (colour_conversion_test1)
36 {
37         ColourConversion A (dcp::ColourConversion::srgb_to_xyz ());
38         ColourConversion B (dcp::ColourConversion::rec709_to_xyz ());
39
40         BOOST_CHECK_EQUAL (A.identifier(), "9840c601d2775bf1b3847254bbaa36a9");
41         BOOST_CHECK_EQUAL (B.identifier(), "58151ac92fdf333663a62c9a8ba5c5f4");
42 }
43
44 BOOST_AUTO_TEST_CASE (colour_conversion_test2)
45 {
46         ColourConversion A (dcp::ColourConversion::srgb_to_xyz ());
47         xmlpp::Document doc;
48         xmlpp::Element* root = doc.create_root_node ("Test");
49         A.as_xml (root);
50         BOOST_CHECK_EQUAL (
51                 doc.write_to_string_formatted ("UTF-8"),
52                 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
53                 "<Test>\n"
54                 "  <InputTransferFunction>\n"
55                 "    <Type>ModifiedGamma</Type>\n"
56                 "    <Power>2.4</Power>\n"
57                 "    <Threshold>0.04045</Threshold>\n"
58                 "    <A>0.055</A>\n"
59                 "    <B>12.92</B>\n"
60                 "  </InputTransferFunction>\n"
61                 "  <YUVToRGB>0</YUVToRGB>\n"
62                 "  <RedX>0.64</RedX>\n"
63                 "  <RedY>0.33</RedY>\n"
64                 "  <GreenX>0.3</GreenX>\n"
65                 "  <GreenY>0.6</GreenY>\n"
66                 "  <BlueX>0.15</BlueX>\n"
67                 "  <BlueY>0.06</BlueY>\n"
68                 "  <WhiteX>0.3127</WhiteX>\n"
69                 "  <WhiteY>0.329</WhiteY>\n"
70                 "  <OutputGamma>2.6</OutputGamma>\n"
71                 "</Test>\n"
72                 );
73 }
74
75 BOOST_AUTO_TEST_CASE (colour_conversion_test3)
76 {
77         ColourConversion A (dcp::ColourConversion::rec709_to_xyz ());
78         xmlpp::Document doc;
79         xmlpp::Element* root = doc.create_root_node ("Test");
80         A.as_xml (root);
81         BOOST_CHECK_EQUAL (
82                 doc.write_to_string_formatted ("UTF-8"),
83                 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
84                 "<Test>\n"
85                 "  <InputTransferFunction>\n"
86                 "    <Type>Gamma</Type>\n"
87                 "    <Gamma>2.2</Gamma>\n"
88                 "  </InputTransferFunction>\n"
89                 "  <YUVToRGB>1</YUVToRGB>\n"
90                 "  <RedX>0.64</RedX>\n"
91                 "  <RedY>0.33</RedY>\n"
92                 "  <GreenX>0.3</GreenX>\n"
93                 "  <GreenY>0.6</GreenY>\n"
94                 "  <BlueX>0.15</BlueX>\n"
95                 "  <BlueY>0.06</BlueY>\n"
96                 "  <WhiteX>0.3127</WhiteX>\n"
97                 "  <WhiteY>0.329</WhiteY>\n"
98                 "  <OutputGamma>2.6</OutputGamma>\n"
99                 "</Test>\n"
100                 );
101 }
102
103 /** Test a round trip via the XML representation */
104 BOOST_AUTO_TEST_CASE (colour_conversion_test4)
105 {
106         BOOST_FOREACH (PresetColourConversion const & i, PresetColourConversion::all ()) {
107                 xmlpp::Document out;
108                 xmlpp::Element* out_root = out.create_root_node ("Test");
109                 i.conversion.as_xml (out_root);
110                 shared_ptr<cxml::Document> in (new cxml::Document ("Test"));
111                 in->read_string (out.write_to_string ("UTF-8"));
112                 BOOST_CHECK (ColourConversion::from_xml (in, Film::current_state_version).get () == i.conversion);
113         }
114 }