Debian Codegraph @

deps/boost/gil/extension/toolbox/hsv.hpp

raw
1 // Copyright 2004 Christian Henning.
2 // Distributed under the Boost Software License, Version 1.0. (See
3 // accompanying file LICENSE_1_0.txt or copy at
4 // http://www.boost.org/LICENSE_1_0.txt)
5
6 /*************************************************************************************************/
7
8 #ifndef GIL_HSV_H
9 #define GIL_HSV_H
10
11 ////////////////////////////////////////////////////////////////////////////////////////
12 /// \file
13 /// \brief Support for HSV color space
14 /// \author Christian Henning \n
15 ////////////////////////////////////////////////////////////////////////////////////////
16
17 #include <mapnik/warning.hpp>
18 MAPNIK_DISABLE_WARNING_PUSH
19 #include <mapnik/warning_ignore.hpp>
20 #if BOOST_VERSION >= 106900
21 #include <boost/gil.hpp>
22 #else
23 #include <boost/gil/gil_all.hpp>
24 #endif
25
26 MAPNIK_DISABLE_WARNING_POP
27
28 namespace boost { namespace gil {
29
30 /// \addtogroup ColorNameModel
31 /// \{
32 namespace hsv_color_space
33 {
34 /// \brief Hue
35 struct hue_t {};
36 /// \brief Saturation
37 struct saturation_t{};
38 /// \brief Value
39 struct value_t {};
40 }
41 /// \}
42
43 /// \ingroup ColorSpaceModel
44 typedef mpl::vector3< hsv_color_space::hue_t
45 , hsv_color_space::saturation_t
46 , hsv_color_space::value_t
47 > hsv_t;
48
49 /// \ingroup LayoutModel
50 typedef layout<hsv_t> hsv_layout_t;
51
52
53 #if BOOST_VERSION >= 106800
54 using bits32 = uint32_t;
55 using bits32f = float32_t;
56 GIL_DEFINE_ALL_TYPEDEFS( 32f, float32_t, hsv )
57 #else
58 GIL_DEFINE_ALL_TYPEDEFS( 32f, hsv );
59 #endif
60
61 /// \ingroup ColorConvert
62 /// \brief RGB to HSV
63 template <>
64 struct default_color_converter_impl< rgb_t, hsv_t >
65 {
66 template <typename P1, typename P2>
67 void operator()( const P1& src, P2& dst ) const
68 {
69 using namespace hsv_color_space;
70
71 // only bits32f for hsv is supported
72 bits32f temp_red = channel_convert<bits32f>( get_color( src, red_t() ));
73 bits32f temp_green = channel_convert<bits32f>( get_color( src, green_t() ));
74 bits32f temp_blue = channel_convert<bits32f>( get_color( src, blue_t() ));
75
76 bits32f hue, saturation, value;
77 bits32f min_color, max_color;
78
79 if( temp_red < temp_green )
80 {
81 min_color = std::min( temp_blue, temp_red );
82 max_color = std::max( temp_blue, temp_green );
83 }
84 else
85 {
86 min_color = std::min( temp_blue, temp_green );
87 max_color = std::max( temp_blue, temp_red );
88 }
89
90 value = max_color;
91
92 bits32f diff = max_color - min_color;
93
94 if( max_color < 0.0001f )
95 {
96 saturation = 0.f;
97 }
98 else
99 {
100 saturation = diff / max_color;
101 }
102
103
104 if( saturation < 0.0001f )
105 {
106 //it doesn't matter what value it has
107 hue = 0.f;
108 }
109 else
110 {
111 if( temp_red == max_color )
112 {
113 hue = ( temp_green - temp_blue )
114 / diff;
115 }
116 else if( temp_green == max_color )
117 {
118 hue = 2.f + ( temp_blue - temp_red )
119 / diff;
120 }
121 else
122 {
123 hue = 4.f + ( temp_red - temp_green )
124 / diff;
125 }
126
127 //to bring it to a number between 0 and 1
128 hue /= 6.f;
129
130 if( hue < 0.f )
131 {
132 hue++;
133 }
134 }
135
136 get_color( dst, hue_t() ) = hue;
137 get_color( dst, saturation_t() ) = saturation;
138 get_color( dst, value_t() ) = value;
139 }
140 };
141
142 /// \ingroup ColorConvert
143 /// \brief HSV to RGB
144 template <>
145 struct default_color_converter_impl<hsv_t,rgb_t>
146 {
147 template <typename P1, typename P2>
148 void operator()( const P1& src, P2& dst) const
149 {
150 using namespace hsv_color_space;
151
152 bits32f red, green, blue;
153
154 //If saturation is 0, the color is a shade of gray
155 if( std::abs( get_color( src, saturation_t() )) < 0.0001f )
156 {
157 // If saturation is 0, the color is a shade of gray
158 red = get_color( src, value_t() );
159 green = get_color( src, value_t() );
160 blue = get_color( src, value_t() );
161 }
162 else
163 {
164 bits32f frac, p, q, t, h;
165 bits32 i;
166
167 //to bring hue to a number between 0 and 6, better for the calculations
168 h = get_color( src, hue_t() );
169 h *= 6.f;
170
171 i = static_cast<bits32>( floor( h ));
172
173 frac = h - i;
174
175 // p = value * (1 - saturation)
176 p = get_color( src, value_t() )
177 * ( 1.f - get_color( src, saturation_t() ));
178
179 // q = value * (1 - saturation * hue_frac)
180 // it drops with increasing distance from floor(hue)
181 q = get_color( src, value_t() )
182 * ( 1.f - ( get_color( src, saturation_t() ) * frac ));
183
184 // t = value * (1 - (saturation * (1 - hue_frac))
185 // it grows with increasing distance from floor(hue)
186 t = get_color( src, value_t() )
187 * ( 1.f - ( get_color( src, saturation_t() ) * ( 1.f - frac )));
188
189 switch( i % 6 )
190 {
191 case 0: // red to yellow
192 {
193 red = get_color( src, value_t() );
194 green = t;
195 blue = p;
196
197 break;
198 }
199
200 case 1: // yellow to green
201 {
202 red = q;
203 green = get_color( src, value_t() );
204 blue = p;
205
206 break;
207 }
208
209 case 2: // green to cyan
210 {
211 red = p;
212 green = get_color( src, value_t() );
213 blue = t;
214
215 break;
216 }
217
218 case 3: // cyan to blue
219 {
220 red = p;
221 green = q;
222 blue = get_color( src, value_t() );
223
224 break;
225 }
226
227 case 4: // blue to magenta
228 {
229 red = t;
230 green = p;
231 blue = get_color( src, value_t() );
232
233 break;
234 }
235
236 case 5: // magenta to red
237 {
238 red = get_color( src, value_t() );
239 green = p;
240 blue = q;
241
242 break;
243 }
244
245 }
246 }
247
248 get_color(dst,red_t()) =
249 channel_convert<typename color_element_type< P2, red_t >::type>( red );
250 get_color(dst,green_t())=
251 channel_convert<typename color_element_type< P2, green_t >::type>( green );
252 get_color(dst,blue_t()) =
253 channel_convert<typename color_element_type< P2, blue_t >::type>( blue );
254 }
255 };
256
257 } } // namespace boost::gil
258
259 #endif // GIL_HSV_H