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