Fix missing version string when Popen communicate returns byte strings.
[libdcp.git] / src / colour_conversion.h
1 /*
2     Copyright (C) 2014-2015 Carl Hetherington <cth@carlh.net>
3
4     This file is part of libdcp.
5
6     libdcp 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     libdcp 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 libdcp.  If not, see <http://www.gnu.org/licenses/>.
18
19     In addition, as a special exception, the copyright holders give
20     permission to link the code of portions of this program with the
21     OpenSSL library under certain conditions as described in each
22     individual source file, and distribute linked combinations
23     including the two.
24
25     You must obey the GNU General Public License in all respects
26     for all of the code used other than OpenSSL.  If you modify
27     file(s) with this exception, you may extend this exception to your
28     version of the file(s), but you are not obligated to do so.  If you
29     do not wish to do so, delete this exception statement from your
30     version.  If you delete this exception statement from all source
31     files in the program, then also delete it here.
32 */
33
34 /** @file  src/colour_conversion.h
35  *  @brief ColourConversion class.
36  */
37
38 #ifndef DCP_COLOUR_CONVERSION_H
39 #define DCP_COLOUR_CONVERSION_H
40
41 #include "chromaticity.h"
42 #include <boost/shared_ptr.hpp>
43 #include <boost/numeric/ublas/matrix.hpp>
44 #include <boost/optional.hpp>
45
46 namespace dcp {
47
48 class TransferFunction;
49
50 enum YUVToRGB {
51         YUV_TO_RGB_REC601,
52         YUV_TO_RGB_REC709,
53         YUV_TO_RGB_COUNT
54 };
55
56 /** @class ColourConversion
57  *  @brief A representation of all the parameters involved the colourspace conversion
58  *  of a YUV image to XYZ (via RGB).
59  */
60 class ColourConversion
61 {
62 public:
63         ColourConversion ()
64                 : _yuv_to_rgb (YUV_TO_RGB_REC601)
65         {}
66
67         ColourConversion (
68                 boost::shared_ptr<const TransferFunction> in,
69                 YUVToRGB yuv_to_rgb,
70                 Chromaticity red,
71                 Chromaticity green,
72                 Chromaticity blue,
73                 Chromaticity white,
74                 boost::optional<Chromaticity> adjusted_white,
75                 boost::shared_ptr<const TransferFunction> out
76                 );
77
78         boost::shared_ptr<const TransferFunction> in () const {
79                 return _in;
80         }
81
82         YUVToRGB yuv_to_rgb () const {
83                 return _yuv_to_rgb;
84         }
85
86         Chromaticity red () const {
87                 return _red;
88         }
89
90         Chromaticity green () const {
91                 return _green;
92         }
93
94         Chromaticity blue () const {
95                 return _blue;
96         }
97
98         Chromaticity white () const {
99                 return _white;
100         }
101
102         boost::optional<Chromaticity> adjusted_white () const {
103                 return _adjusted_white;
104         }
105
106         boost::shared_ptr<const TransferFunction> out () const {
107                 return _out;
108         }
109
110         void set_in (boost::shared_ptr<const TransferFunction> f) {
111                 _in = f;
112         }
113
114         void set_yuv_to_rgb (YUVToRGB y) {
115                 _yuv_to_rgb = y;
116         }
117
118         void set_red (Chromaticity red) {
119                 _red = red;
120         }
121
122         void set_green (Chromaticity green) {
123                 _green = green;
124         }
125
126         void set_blue (Chromaticity blue) {
127                 _blue = blue;
128         }
129
130         void set_white (Chromaticity white) {
131                 _white = white;
132         }
133
134         void set_adjusted_white (Chromaticity adjusted_white) {
135                 _adjusted_white = adjusted_white;
136         }
137
138         void unset_adjusted_white () {
139                 _adjusted_white = boost::optional<Chromaticity> ();
140         }
141
142         void set_out (boost::shared_ptr<const TransferFunction> f) {
143                 _out = f;
144         }
145
146         bool about_equal (ColourConversion const & other, float epsilon) const;
147
148         boost::numeric::ublas::matrix<double> rgb_to_xyz () const;
149         boost::numeric::ublas::matrix<double> xyz_to_rgb () const;
150         boost::numeric::ublas::matrix<double> bradford () const;
151
152         static ColourConversion const & srgb_to_xyz ();
153         static ColourConversion const & rec601_to_xyz ();
154         static ColourConversion const & rec709_to_xyz ();
155         static ColourConversion const & p3_to_xyz ();
156         static ColourConversion const & rec1886_to_xyz ();
157         static ColourConversion const & rec2020_to_xyz ();
158         static ColourConversion const & s_gamut3_to_xyz ();
159
160 protected:
161         /** Input transfer function (probably a gamma function, or something similar) */
162         boost::shared_ptr<const TransferFunction> _in;
163         /** Conversion to use from YUV to RGB */
164         YUVToRGB _yuv_to_rgb;
165         Chromaticity _red;
166         Chromaticity _green;
167         Chromaticity _blue;
168         Chromaticity _white;
169         /** White point that we are adjusting to using a Bradford matrix */
170         boost::optional<Chromaticity> _adjusted_white;
171         /** Output transfer function (probably an inverse gamma function, or something similar) */
172         boost::shared_ptr<const TransferFunction> _out;
173 };
174
175 }
176
177 #endif