Voxel
 All Classes Namespaces Files Functions Typedefs Enumerations Enumerator Macros Pages
color.h
Go to the documentation of this file.
1 #pragma once
2 
3 #ifndef COLOR_H
4 #define COLOR_H
5 
6 
7 /**
8  \file
9  \brief Contains Color and OpenCL::ImageFormat classes.
10 */
11 
12 
13 
14 
15 #include "settings.h"
16 
17 #include <stdint.h>
18 
19 
20 
21 
22 /*==============================================================================
23  ColorFormat enumeration
24 ==============================================================================*/
25 
26 
27 /// \brief Enumeration over color formats.
29 
30  COLOR_FORMAT_RGBA_8888, ///< 32-bit RGBA. From high to low: AAAAAAAA BBBBBBBB GGGGGGGG RRRRRRRR.
31  COLOR_FORMAT_RGB_565 ///< 16-bit RGB. From high to low: RRRRR GGGGGG BBBBB.
32 
33 };
34 
35 
36 
37 
38 /*==============================================================================
39  Color class
40 ==============================================================================*/
41 
42 
43 /**
44  \brief Class storing a color in the format given by its parameter.
45 
46  \param t_ColorFormat Format in which to store the color.
47 
48  All of the Color specializations have (roughly) the same interface. The
49  constructor takes 8-bit color channel values in the range 0-255, regardless
50  of the number of bits used to represent the channels in the underlying
51  representation. If some channels (such as alpha) are not included in the
52  color format, then they are not included as constructor parameters. The
53  default constructor initializes all channels to zero.
54 
55  Copy constructors and assignment operators are overloaded to enable
56  conversion between colors, but *only* if the destination color contains a
57  superset of the channels supported by the source color. In other words, you
58  can convert RGBA to RGB, but not the reverse.
59 
60  Additionally, there are accessor functions \c Red, \c Green, \c Blue and \c
61  Alpha, again only for those channels supported by the format, which return
62  channel values in the range 0-255.
63 
64  Finally, the \c PackedType typedef is an unsigned integer type representing
65  the underlying packed format, and the \c Packed accessor returns the color,
66  packed into this format.
67 
68  \see \ref ColorFormat
69 */
70 template< int t_ColorFormat >
71 struct Color;
72 
73 
74 
75 
76 /*==============================================================================
77  Color< COLOR_FORMAT_RGBA_8888 > specialization
78 ==============================================================================*/
79 
80 
81 /// \brief Color specialization storing 32-bit RGBA colors.
82 template<>
83 struct Color< COLOR_FORMAT_RGBA_8888 > {
84 
85  typedef uint32_t PackedType; ///< 32-bit RGBA. From high to low: AAAAAAAA BBBBBBBB GGGGGGGG RRRRRRRR.
86 
87 
88  /// \brief Initializes all channels to zero.
89  inline Color();
90 
91  /// \brief Constructs a color from the given channel values (all 0-255).
92  inline Color( uint8_t const red, uint8_t const green, uint8_t const blue, uint8_t const alpha = 0 );
93 
94  inline Color( Color const& other );
95 
96  template< int t_OtherColorFormat >
97  inline Color( Color< t_OtherColorFormat > const& other );
98 
99 
100  inline Color const& operator=( Color const& other );
101 
102  template< int t_OtherColorFormat >
103  inline Color const& operator=( Color< t_OtherColorFormat > const& other );
104 
105 
106  inline PackedType Packed() const; ///< \brief Returns packed 32-bit RGBA. From high to low: AAAAAAAA BBBBBBBB GGGGGGGG RRRRRRRR.
107 
108  inline uint8_t Red() const; ///< \brief Returns red channel (0-255).
109  inline uint8_t Green() const; ///< \brief Returns green channel (0-255).
110  inline uint8_t Blue() const; ///< \brief Returns blue channel (0-255).
111  inline uint8_t Alpha() const; ///< \brief Returns alpha channel (0-255).
112 
113 
114 private:
115 
116  inline void Set( uint8_t const red, uint8_t const green, uint8_t const blue, uint8_t const alpha );
117 
118 
119  uint32_t m_color;
120 };
121 
122 
123 
124 
125 /*==============================================================================
126  Color< COLOR_FORMAT_RGB_565 > specialization
127 ==============================================================================*/
128 
129 
130 /// \brief Color specialization storing 16-bit RGB colors.
131 template<>
132 struct Color< COLOR_FORMAT_RGB_565 > {
133 
134  typedef uint16_t PackedType; ///< 16-bit RGB. From high to low: RRRRR GGGGGG BBBBB.
135 
136 
137  /// \brief Initializes all channels to zero.
138  inline Color();
139 
140  /// \brief Constructs a color from the given channel values (all 0-255).
141  inline Color( uint8_t const red, uint8_t const green, uint8_t const blue );
142 
143  inline Color( Color const& other );
144 
145  template< int t_OtherColorFormat >
146  inline Color( Color< t_OtherColorFormat > const& other );
147 
148 
149  inline Color const& operator=( Color const& other );
150 
151  template< int t_OtherColorFormat >
152  inline Color const& operator=( Color< t_OtherColorFormat > const& other );
153 
154 
155  inline PackedType Packed() const; ///< \brief Returns packed 16-bit RGB. From high to low: RRRRR GGGGGG BBBBB.
156 
157  inline uint8_t Red() const; ///< \brief Returns red channel (0-255).
158  inline uint8_t Green() const; ///< \brief Returns green channel (0-255).
159  inline uint8_t Blue() const; ///< \brief Returns blue channel (0-255).
160  inline uint8_t Alpha() const; ///< \brief Returns alpha channel (0-255).
161 
162 
163 private:
164 
165  inline void Set( uint8_t const red, uint8_t const green, uint8_t const blue );
166 
167  uint16_t m_color;
168 };
169 
170 
171 
172 
173 /*==============================================================================
174  Color< COLOR_FORMAT_RGBA_8888 > methods
175 ==============================================================================*/
176 
177 
179 }
180 
181 
183  uint8_t const red,
184  uint8_t const green,
185  uint8_t const blue,
186  uint8_t const alpha
187 )
188 {
189  Set( red, green, blue, alpha );
190 }
191 
192 
193 Color< COLOR_FORMAT_RGBA_8888 >::Color( Color const& other ) : m_color( other.m_color ) {
194 }
195 
196 
197 template< int t_OtherColorFormat >
198 Color< COLOR_FORMAT_RGBA_8888 >::Color( Color< t_OtherColorFormat > const& other ) {
199 
200  Set( other.Red(), other.Green(), other.Blue(), other.Alpha() );
201 }
202 
203 
204 Color< COLOR_FORMAT_RGBA_8888 > const& Color< COLOR_FORMAT_RGBA_8888 >::operator=( Color const& other ) {
205 
206  m_color = other.m_color;
207  return *this;
208 }
209 
210 
211 template< int t_OtherColorFormat >
212 Color< COLOR_FORMAT_RGBA_8888 > const& Color< COLOR_FORMAT_RGBA_8888 >::operator=( Color< t_OtherColorFormat > const& other ) {
213 
214  Set( other.Red(), other.Green(), other.Blue(), other.Alpha() );
215  return *this;
216 }
217 
218 
220 
221  return m_color;
222 }
223 
224 
226 
227  return( m_color & 0xff );
228 }
229 
230 
232 
233  return( ( m_color >> 8 ) & 0xff );
234 }
235 
236 
238 
239  return( ( m_color >> 16 ) & 0xff );
240 }
241 
242 
244 
245  return( m_color >> 24 );
246 }
247 
248 
249 void Color< COLOR_FORMAT_RGBA_8888 >::Set(
250  uint8_t const red,
251  uint8_t const green,
252  uint8_t const blue,
253  uint8_t const alpha
254 )
255 {
256  m_color = (
257  ( static_cast< uint32_t >( alpha ) << 24 ) |
258  ( static_cast< uint32_t >( blue ) << 16 ) |
259  ( static_cast< uint32_t >( green ) << 8 ) |
260  ( static_cast< uint32_t >( red ) )
261  );
262 }
263 
264 
265 
266 
267 /*==============================================================================
268  Color< COLOR_FORMAT_RGB_565 > methods
269 ==============================================================================*/
270 
271 
273 }
274 
275 
277  uint8_t const red,
278  uint8_t const green,
279  uint8_t const blue
280 )
281 {
282  Set( red, green, blue );
283 }
284 
285 
286 Color< COLOR_FORMAT_RGB_565 >::Color( Color const& other ) : m_color( other.m_color ) {
287 }
288 
289 
290 template< int t_OtherColorFormat >
291 Color< COLOR_FORMAT_RGB_565 >::Color( Color< t_OtherColorFormat > const& other ) {
292 
293  Set( other.Red(), other.Green(), other.Blue() );
294 }
295 
296 
297 Color< COLOR_FORMAT_RGB_565 > const& Color< COLOR_FORMAT_RGB_565 >::operator=( Color const& other ) {
298 
299  m_color = other.m_color;
300  return *this;
301 }
302 
303 
304 template< int t_OtherColorFormat >
305 Color< COLOR_FORMAT_RGB_565 > const& Color< COLOR_FORMAT_RGB_565 >::operator=( Color< t_OtherColorFormat > const& other ) {
306 
307  Set( other.Red(), other.Green(), other.Blue() );
308  return *this;
309 }
310 
311 
313 
314  return m_color;
315 }
316 
317 
319 
320  uint8_t const channel = ( ( m_color >> 8 ) & 0xf8 );
321  return( channel | ( channel >> 5 ) );
322 }
323 
324 
326 
327  uint8_t const channel = ( ( m_color >> 3 ) & 0xfc );
328  return( channel | ( channel >> 6 ) );
329 }
330 
331 
333 
334  uint8_t const channel = ( ( m_color << 3 ) & 0xf8 );
335  return( channel | ( channel >> 5 ) );
336 }
337 
338 
339 void Color< COLOR_FORMAT_RGB_565 >::Set(
340  uint8_t const red,
341  uint8_t const green,
342  uint8_t const blue
343 )
344 {
345  m_color = (
346  ( static_cast< uint16_t >( red & 0xf8 ) << 8 ) |
347  ( static_cast< uint16_t >( green & 0xfc ) << 3 ) |
348  ( static_cast< uint16_t >( blue & 0xf8 ) >> 3 )
349  );
350 }
351 
352 
353 
354 
355 /*==============================================================================
356  OpenCL::Converter specialization, OpenCL::ImageFormat class
357 ==============================================================================*/
358 
359 
360 #if ( SETTING_OPENCL != 0 )
361 
362 
363 #include "opencl_converter.h"
364 
365 #include <CL/cl.h>
366 
367 
368 namespace OpenCL {
369 
370 
371 /// \brief Converter specialization for Color classes.
372 template< int t_ColorFormat >
373 struct Converter< Color< t_ColorFormat > > {
374 
375  typedef typename Converter< typename Color< t_ColorFormat >::PackedType >::Type Type;
376 
377  static Type Convert( Color< t_ColorFormat > const& source ) {
378 
379  return Converter< typename Color< t_ColorFormat >::PackedType >::Convert( source.Packed() );
380  }
381 };
382 
383 
384 /**
385  \brief Maps color formats to OpenCL image format descriptors.
386 
387  \param t_ColorFormat Format in which colors are stored.
388 
389  This class contains a single static member: format, which is an OpenCL
390  image format descriptor (cl_image_format) for images represented with the
391  given color format. We use *normalized* color channels, so, when accessing
392  the image from OpenCL, it will be as floating-point channel values in the
393  range \f$[0,1]\f$.
394 */
395 template< int t_ColorFormat >
396 struct ImageFormat {
397 
398  static cl_image_format const format;
399 };
400 
401 
402 } // namespace OpenCL
403 
404 
405 #endif // SETTING_OPENCL
406 
407 
408 
409 
410 #endif // COLOR_H