]> rtime.felk.cvut.cz Git - l4.git/blob - l4/pkg/l4re-core/l4re/include/video/colors
Update
[l4.git] / l4 / pkg / l4re-core / l4re / include / video / colors
1 // vi:ft=cpp
2 /*
3  * (c) 2008-2009 Adam Lackorzynski <adam@os.inf.tu-dresden.de>,
4  *               Alexander Warg <warg@os.inf.tu-dresden.de>
5  *     economic rights: Technische Universität Dresden (Germany)
6  *
7  * This file is part of TUD:OS and distributed under the terms of the
8  * GNU General Public License 2.
9  * Please see the COPYING-GPL-2 file for details.
10  *
11  * As a special exception, you may use this file as part of a free software
12  * library without restriction.  Specifically, if other files instantiate
13  * templates or use macros or inline functions from this file, or you compile
14  * this file and link it with other files to produce an executable, this
15  * file does not by itself cause the resulting executable to be covered by
16  * the GNU General Public License.  This exception does not however
17  * invalidate any other reasons why the executable file might be covered by
18  * the GNU General Public License.
19  */
20
21 #pragma once
22
23 #include <l4/sys/compiler.h>
24
25 namespace L4Re { namespace Video {
26
27 /**
28  * \brief A color component.
29  * \ingroup api_l4re_goos
30  */
31 class L4_EXPORT Color_component
32 {
33 private:
34   unsigned char _bits;    ///< Number of bits used by the component
35   unsigned char _shift;   ///< Position in bits of the component in the pixel
36
37 public:
38   /** Constructor */
39   Color_component() : _bits(0), _shift(0) {}
40
41   /**
42    * \brief Constructor
43    * \param bits   Number of bits used by the component
44    * \param shift  Position in bits of the component in the pixel
45    */
46   Color_component(unsigned char bits, unsigned char shift)
47   : _bits(bits), _shift(shift) {}
48
49   /**
50    * \brief  Return the number of bits used by the component.
51    * \return Number of bits used by the component
52    */
53   unsigned char size() const { return _bits; }
54
55   /**
56    * \brief  Return the position of the component in the pixel.
57    * \return Position in bits of the component in the pixel
58    */
59   unsigned char shift() const { return _shift; }
60
61   /**
62    * \brief Compare for equality.
63    * \return True if the same components are described, false if not.
64    */
65   bool operator == (Color_component const &o) const
66   { return _shift == o._shift && _bits == o._bits; }
67
68   /**
69    * \brief Get component from value (normalized to 16bits).
70    * \param v   Value
71    * \return Converted value
72    */
73   int get(unsigned long v) const
74   {
75     return ((v >> (unsigned long)_shift)
76       & ~(~0UL << (unsigned long)_bits)) << (unsigned long)(16 - _bits);
77   }
78
79   /**
80    * \brief Transform 16bit normalized value to the component in the color space.
81    * \param v  Value
82    * return Converted value.
83    */
84   long unsigned set(int v) const
85   { return (v >> (unsigned long)(16 - _bits)) << (unsigned long)_shift; }
86
87   /**
88    * \brief Dump information on the view information to a stream
89    * \param s  Stream
90    * \return The stream
91    */
92   template< typename OUT >
93   void dump(OUT &s) const
94   {
95     s.printf("%d(%d)", (int)size(), (int)shift());
96   }
97 } __attribute__((packed));
98
99 /**
100  * \brief Pixel information.
101  * \ingroup api_l4re_goos
102  *
103  * This class wraps the information on a pixel, such as the size and
104  * position of each color component in the pixel.
105  */
106 class L4_EXPORT Pixel_info
107 {
108 private:
109   Color_component _r, _g, _b, _a;  ///< Red, green, blue and alpha color components
110   unsigned char _bpp;              ///< Size of the pixel in bytes.
111
112 public:
113   /**
114    * \brief Return the red color compoment of the pixel.
115    * \return Red color component.
116    */
117   Color_component const &r() const { return _r; }
118
119   /**
120    * \brief Return the green color compoment of the pixel.
121    * \return Green color component.
122    */
123   Color_component const &g() const { return _g; }
124
125   /**
126    * \brief Return the blue color compoment of the pixel.
127    * \return Blue color component.
128    */
129   Color_component const &b() const { return _b; }
130
131   /**
132    * \brief Return the alpha color compoment of the pixel.
133    * \return Alpha color component.
134    */
135   Color_component const &a() const { return _a; }
136
137   /**
138    * \brief Query size of pixel in bytes.
139    * \return Size of pixel in bytes.
140    */
141   unsigned char bytes_per_pixel() const { return _bpp; }
142
143   /**
144    * \brief Number of bits of the pixel.
145    * \return Number of bits used by the pixel.
146    */
147   unsigned char bits_per_pixel() const
148   { return _r.size() + _g.size() + _b.size() +_a.size(); }
149
150   /**
151    * \brief Return whether the pixel has an alpha channel.
152    * \return True if the pixel has an alpha channel, false if not.
153    */
154   bool has_alpha() const { return _a.size() > 0; }
155
156   /**
157    * \brief Set the red color component of the pixel.
158    * \param c  Red color component.
159    */
160   void r(Color_component const &c) { _r = c; }
161
162   /**
163    * \brief Set the green color component of the pixel.
164    * \param c  Green color component.
165    */
166   void g(Color_component const &c) { _g = c; }
167
168   /**
169    * \brief Set the blue color component of the pixel.
170    * \param c  Blue color component.
171    */
172   void b(Color_component const &c) { _b = c; }
173
174   /**
175    * \brief Set the alpha color component of the pixel.
176    * \param c  Alpha color component.
177    */
178   void a(Color_component const &c) { _a = c; }
179
180   /**
181    * \brief Set the size of the pixel in bytes.
182    * \param bpp   Size of pixel in bytes.
183    */
184   void bytes_per_pixel(unsigned char bpp) { _bpp = bpp; }
185
186   /**
187    * \brief Constructor.
188    */
189   Pixel_info() : _r(), _g(), _b(), _a() {}
190
191   /**
192    * \brief Constructor.
193    * \param bpp   Size of pixel in bytes.
194    * \param r     Red component size.
195    * \param rs    Red component shift.
196    * \param g     Green component size.
197    * \param gs    Green component shift.
198    * \param b     Blue component size.
199    * \param bs    Blue component shift.
200    * \param a     Alpha component size, defaults to 0.
201    * \param as    Alpha component shift, defaults to 0.
202    */
203   Pixel_info(unsigned char bpp, char r, char rs, char g, char gs,
204              char b, char bs, char a = 0, char as = 0)
205   : _r(r, rs), _g(g, gs), _b(b, bs), _a(a, as), _bpp(bpp)
206   {}
207
208   /**
209    * \brief Convenience constructor.
210    * \param vbi   Suitable information structure.
211    * Convenience constructor to create the pixel info from
212    * a VESA Framebuffer Info.
213    */
214   template<typename VBI>
215   explicit Pixel_info(VBI const *vbi)
216   : _r(vbi->red_mask_size, vbi->red_field_position),
217     _g(vbi->green_mask_size, vbi->green_field_position),
218     _b(vbi->blue_mask_size, vbi->blue_field_position),
219     _bpp((vbi->bits_per_pixel + 7) / 8)
220   {}
221
222   /**
223    * \brief Compare for complete equzality of the color sapce.
224    * \param o    A Pixel_info to compare to.
225    * \return true if the both Pixel_info's are equal, false if not.
226    */
227   bool operator == (Pixel_info const &o) const
228   {
229     return _r == o._r && _g == o._g && _b == o._b && _a == o._a && _bpp == o._bpp;
230   }
231
232   /**
233    * \brief Dump information on the pixel to a stream
234    * \param s  Stream
235    * \return The stream
236    */
237   template< typename OUT >
238   void dump(OUT &s) const
239   {
240     s.printf("RGBA(%d):%d(%d):%d(%d):%d(%d):%d(%d)",
241              (int)bytes_per_pixel(),
242              (int)r().size(), (int)r().shift(),
243              (int)g().size(), (int)g().shift(),
244              (int)b().size(), (int)b().shift(),
245              (int)a().size(), (int)a().shift());
246   }
247 };
248
249
250 }}
251
252