1e293f7884d6b03ab8b1b45f965958d7b7dc4b56
[libdcp.git] / src / colour_conversion.h
1 /*
2     Copyright (C) 2014-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  src/colour_conversion.h
21  *  @brief ColourConversion class.
22  */
23
24 #ifndef DCP_COLOUR_CONVERSION_H
25 #define DCP_COLOUR_CONVERSION_H
26
27 #include "chromaticity.h"
28 #include <boost/shared_ptr.hpp>
29 #include <boost/numeric/ublas/matrix.hpp>
30 #include <boost/optional.hpp>
31
32 namespace dcp {
33
34 class TransferFunction;
35
36 enum YUVToRGB {
37         YUV_TO_RGB_REC601,
38         YUV_TO_RGB_REC709,
39         YUV_TO_RGB_COUNT
40 };
41
42 /** @class ColourConversion
43  *  @brief A representation of all the parameters involved the colourspace conversion
44  *  of a YUV image to XYZ (via RGB).
45  */
46 class ColourConversion
47 {
48 public:
49         ColourConversion ()
50         {}
51
52         ColourConversion (
53                 boost::shared_ptr<const TransferFunction> in,
54                 YUVToRGB yuv_to_rgb,
55                 Chromaticity red,
56                 Chromaticity green,
57                 Chromaticity blue,
58                 Chromaticity white,
59                 boost::optional<Chromaticity> adjusted_white,
60                 boost::shared_ptr<const TransferFunction> out
61                 );
62
63         boost::shared_ptr<const TransferFunction> in () const {
64                 return _in;
65         }
66
67         YUVToRGB yuv_to_rgb () const {
68                 return _yuv_to_rgb;
69         }
70
71         Chromaticity red () const {
72                 return _red;
73         }
74
75         Chromaticity green () const {
76                 return _green;
77         }
78
79         Chromaticity blue () const {
80                 return _blue;
81         }
82
83         Chromaticity white () const {
84                 return _white;
85         }
86
87         boost::optional<Chromaticity> adjusted_white () const {
88                 return _adjusted_white;
89         }
90
91         boost::shared_ptr<const TransferFunction> out () const {
92                 return _out;
93         }
94
95         void set_in (boost::shared_ptr<const TransferFunction> f) {
96                 _in = f;
97         }
98
99         void set_yuv_to_rgb (YUVToRGB y) {
100                 _yuv_to_rgb = y;
101         }
102
103         void set_red (Chromaticity red) {
104                 _red = red;
105         }
106
107         void set_green (Chromaticity green) {
108                 _green = green;
109         }
110
111         void set_blue (Chromaticity blue) {
112                 _blue = blue;
113         }
114
115         void set_white (Chromaticity white) {
116                 _white = white;
117         }
118
119         void set_adjusted_white (Chromaticity adjusted_white) {
120                 _adjusted_white = adjusted_white;
121         }
122
123         void unset_adjusted_white () {
124                 _adjusted_white = boost::optional<Chromaticity> ();
125         }
126
127         void set_out (boost::shared_ptr<const TransferFunction> f) {
128                 _out = f;
129         }
130
131         bool about_equal (ColourConversion const & other, float epsilon) const;
132
133         boost::numeric::ublas::matrix<double> rgb_to_xyz () const;
134         boost::numeric::ublas::matrix<double> xyz_to_rgb () const;
135         boost::numeric::ublas::matrix<double> bradford () const;
136
137         static ColourConversion const & srgb_to_xyz ();
138         static ColourConversion const & rec601_to_xyz ();
139         static ColourConversion const & rec709_to_xyz ();
140         static ColourConversion const & p3_to_xyz ();
141
142 protected:
143         /** Input transfer function (probably a gamma function, or something similar) */
144         boost::shared_ptr<const TransferFunction> _in;
145         /** Conversion to use from YUV to RGB */
146         YUVToRGB _yuv_to_rgb;
147         Chromaticity _red;
148         Chromaticity _green;
149         Chromaticity _blue;
150         Chromaticity _white;
151         /** White point that we are adjusting to using a Bradford matrix */
152         boost::optional<Chromaticity> _adjusted_white;
153         /** Output transfer function (probably an inverse gamma function, or something similar) */
154         boost::shared_ptr<const TransferFunction> _out;
155 };
156
157 }
158
159 #endif