46 #include <boost/numeric/ublas/matrix.hpp>
47 #include <boost/numeric/ublas/lu.hpp>
48 #include <boost/numeric/ublas/io.hpp>
51 using std::shared_ptr;
52 using std::make_shared;
53 using boost::optional;
58 ColourConversion::srgb_to_xyz ()
61 make_shared<ModifiedGammaTransferFunction>(2.4, 0.04045, 0.055, 12.92),
67 optional<Chromaticity> (),
68 make_shared<GammaTransferFunction>(2.6)
74 ColourConversion::rec601_to_xyz ()
77 make_shared<GammaTransferFunction>(2.2),
83 optional<Chromaticity> (),
84 make_shared<GammaTransferFunction>(2.6)
90 ColourConversion::rec709_to_xyz ()
93 make_shared<GammaTransferFunction>(2.2),
99 optional<Chromaticity> (),
100 make_shared<GammaTransferFunction>(2.6)
106 ColourConversion::p3_to_xyz ()
109 make_shared<GammaTransferFunction>(2.6),
115 optional<Chromaticity> (),
116 make_shared<GammaTransferFunction>(2.6)
122 ColourConversion::rec1886_to_xyz ()
128 make_shared<GammaTransferFunction>(2.4),
133 Chromaticity::D65 (),
134 optional<Chromaticity> (),
135 make_shared<GammaTransferFunction>(2.6)
141 ColourConversion::rec2020_to_xyz ()
144 make_shared<GammaTransferFunction>(2.4),
149 Chromaticity::D65 (),
150 optional<Chromaticity> (),
151 make_shared<GammaTransferFunction>(2.6)
161 make_shared<SGamut3TransferFunction>(),
166 Chromaticity::D65 (),
167 optional<Chromaticity> (),
168 make_shared<IdentityTransferFunction>()
175 ColourConversion::ColourConversion (
176 shared_ptr<const TransferFunction> in,
182 optional<Chromaticity> adjusted_white,
183 shared_ptr<const TransferFunction> out
186 , _yuv_to_rgb (yuv_to_rgb)
191 , _adjusted_white (adjusted_white)
199 ColourConversion::about_equal (
ColourConversion const & other,
float epsilon)
const
201 if (!
_in->about_equal (other.
_in, epsilon) ||
230 boost::numeric::ublas::matrix<double>
231 ColourConversion::rgb_to_xyz ()
const
235 double const D = (_red.x - _white.x) * (_white.y - _blue.y) - (_white.x - _blue.x) * (_red.y - _white.y);
236 double const E = (_white.x - _green.x) * (_red.y - _white.y) - (_red.x - _white.x) * (_white.y - _green.y);
237 double const F = (_white.x - _green.x) * (_white.y - _blue.y) - (_white.x - _blue.x) * (_white.y - _green.y);
238 double const P = _red.y + _green.y * D / F + _blue.y * E / F;
240 boost::numeric::ublas::matrix<double> C (3, 3);
241 C(0, 0) = _red.x / P;
242 C(0, 1) = _green.x * D / (F * P);
243 C(0, 2) = _blue.x * E / (F * P);
244 C(1, 0) = _red.y / P;
245 C(1, 1) = _green.y * D / (F * P);
246 C(1, 2) = _blue.y * E / (F * P);
247 C(2, 0) = _red.z() / P;
248 C(2, 1) = _green.z() * D / (F * P);
249 C(2, 2) = _blue.z() * E / (F * P);
254 boost::numeric::ublas::matrix<double>
255 ColourConversion::xyz_to_rgb ()
const
257 boost::numeric::ublas::matrix<double> A (rgb_to_xyz());
260 boost::numeric::ublas::permutation_matrix<std::size_t> pm (A.size1());
263 int const r = lu_factorize (A, pm);
267 boost::numeric::ublas::matrix<double> xyz_to_rgb (3, 3);
268 xyz_to_rgb.assign (boost::numeric::ublas::identity_matrix<double> (A.size1()));
271 lu_substitute (A, pm, xyz_to_rgb);
277 boost::numeric::ublas::matrix<double>
278 ColourConversion::bradford ()
const
281 boost::numeric::ublas::matrix<double> B = boost::numeric::ublas::zero_matrix<double> (3, 3);
290 boost::numeric::ublas::matrix<double> M (3, 3);
301 boost::numeric::ublas::matrix<double> Mi (3, 3);
302 Mi(0, 0) = 0.9869929055;
303 Mi(0, 1) = -0.1470542564;
304 Mi(0, 2) = 0.1599626517;
305 Mi(1, 0) = 0.4323052697;
306 Mi(1, 1) = 0.5183602715;
307 Mi(1, 2) = 0.0492912282;
308 Mi(2, 0) = -0.0085286646;
309 Mi(2, 1) = 0.0400428217;
310 Mi(2, 2) = 0.9684866958;
312 boost::numeric::ublas::matrix<double> Gp (3, 1);
313 Gp(0, 0) = _white.x / _white.y;
315 Gp(2, 0) = (1 - _white.x - _white.y) / _white.y;
317 boost::numeric::ublas::matrix<double> G = boost::numeric::ublas::prod (M, Gp);
319 boost::numeric::ublas::matrix<double> Hp (3, 1);
324 boost::numeric::ublas::matrix<double> H = boost::numeric::ublas::prod (M, Hp);
326 boost::numeric::ublas::matrix<double> C = boost::numeric::ublas::zero_matrix<double> (3, 3);
327 C(0, 0) = H(0, 0) / G(0, 0);
328 C(1, 1) = H(1, 0) / G(1, 0);
329 C(2, 2) = H(2, 0) / G(2, 0);
331 boost::numeric::ublas::matrix<double> CM = boost::numeric::ublas::prod (C, M);
332 return boost::numeric::ublas::prod (Mi, CM);
A representation of a x,y,z chromaticity, where z = 1 - x - y.
bool about_equal(Chromaticity const &other, float epsilon) const
A representation of all the parameters involved the colourspace conversion of a YUV image to XYZ (via...
std::shared_ptr< const TransferFunction > _out
static ColourConversion const & s_gamut3_to_xyz()
boost::optional< Chromaticity > _adjusted_white
std::shared_ptr< const TransferFunction > _in
GammaTransferFunction class.
IdentityTransferFunction class.
ModifiedGammaTransferFunction class.
Namespace for everything in libdcp.
SGamut3TransferFunction class.