2 // Copyright 2012 Chung-Lin Wen, Davide Anastasia
4 // Distributed under the Boost Software License, Version 1.0
5 // See accompanying file LICENSE_1_0.txt or copy at
6 // http://www.boost.org/LICENSE_1_0.txt
8 #ifndef BOOST_GIL_EXTENSION_TOOLBOX_COLOR_SPACES_XYZ_HPP
9 #define BOOST_GIL_EXTENSION_TOOLBOX_COLOR_SPACES_XYZ_HPP
11 #include <boost/gil/color_convert.hpp>
12 #include <boost/gil/typedefs.hpp>
13 #include <boost/gil/detail/mp11.hpp>
15 namespace boost{ namespace gil {
17 /// \addtogroup ColorNameModel
19 namespace xyz_color_space
21 /// \brief x Color Component
23 /// \brief y Color Component
25 /// \brief z Color Component
30 /// \ingroup ColorSpaceModel
31 using xyz_t = mp11::mp_list
38 /// \ingroup LayoutModel
39 using xyz_layout_t = layout<xyz_t>;
41 GIL_DEFINE_ALL_TYPEDEFS(32f, float32_t, xyz)
43 /// \ingroup ColorConvert
45 /// <a href="http://www.brucelindbloom.com/index.html?Eqn_RGB_XYZ_Matrix.html">Link</a>
46 /// \note rgb_t is assumed to be sRGB D65
48 struct default_color_converter_impl< rgb_t, xyz_t >
52 float32_t inverse_companding(float32_t sample) const
54 if ( sample > 0.04045f )
56 return powf((( sample + 0.055f ) / 1.055f ), 2.4f);
60 return ( sample / 12.92f );
65 template <typename P1, typename P2>
66 void operator()( const P1& src, P2& dst ) const
68 using namespace xyz_color_space;
72 channel_convert<float32_t>(get_color(src, red_t()))));
75 channel_convert<float32_t>(get_color(src, green_t()))));
78 channel_convert<float32_t>(get_color(src, blue_t()))));
80 get_color( dst, x_t() ) =
84 get_color( dst, y_t() ) =
88 get_color( dst, z_t() ) =
95 /// \ingroup ColorConvert
98 struct default_color_converter_impl<xyz_t,rgb_t>
102 float32_t companding(float32_t sample) const
104 if ( sample > 0.0031308f )
106 return ( 1.055f * powf( sample, 1.f/2.4f ) - 0.055f );
110 return ( 12.92f * sample );
115 template <typename P1, typename P2>
116 void operator()( const P1& src, P2& dst) const
118 using namespace xyz_color_space;
120 // Note: ideally channel_convert should be compiled out, because xyz_t
121 // is float32_t natively only
122 float32_t x( channel_convert<float32_t>( get_color( src, x_t() ) ) );
123 float32_t y( channel_convert<float32_t>( get_color( src, y_t() ) ) );
124 float32_t z( channel_convert<float32_t>( get_color( src, z_t() ) ) );
126 get_color(dst,red_t()) =
127 channel_convert<typename color_element_type<P2, red_t>::type>(
128 companding( x * 3.2404542f +
132 get_color(dst,green_t()) =
133 channel_convert<typename color_element_type<P2, green_t>::type>(
134 companding( x * -0.9692660f +
138 get_color(dst,blue_t()) =
139 channel_convert<typename color_element_type<P2, blue_t>::type>(
140 companding( x * 0.0556434f +
150 #endif // BOOST_GIL_EXTENSION_TOOLBOX_COLOR_SPACES_XYZ_HPP