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)
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.
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.
23 #include <l4/sys/compiler.h>
25 namespace L4Re { namespace Video {
28 * \brief A color component.
29 * \ingroup api_l4re_goos
31 class L4_EXPORT Color_component
34 unsigned char _bits; ///< Number of bits used by the component
35 unsigned char _shift; ///< Position in bits of the component in the pixel
39 Color_component() : _bits(0), _shift(0) {}
43 * \param bits Number of bits used by the component
44 * \param shift Position in bits of the component in the pixel
46 Color_component(unsigned char bits, unsigned char shift)
47 : _bits(bits), _shift(shift) {}
50 * \brief Return the number of bits used by the component.
51 * \return Number of bits used by the component
53 unsigned char size() const { return _bits; }
56 * \brief Return the position of the component in the pixel.
57 * \return Position in bits of the component in the pixel
59 unsigned char shift() const { return _shift; }
62 * \brief Compare for equality.
63 * \return True if the same components are described, false if not.
65 bool operator == (Color_component const &o) const
66 { return _shift == o._shift && _bits == o._bits; }
69 * \brief Get component from value (normalized to 16bits).
71 * \return Converted value
73 int get(unsigned long v) const
75 return ((v >> (unsigned long)_shift)
76 & ~(~0UL << (unsigned long)_bits)) << (unsigned long)(16 - _bits);
80 * \brief Transform 16bit normalized value to the component in the color space.
82 * return Converted value.
84 long unsigned set(int v) const
85 { return (v >> (unsigned long)(16 - _bits)) << (unsigned long)_shift; }
88 * \brief Dump information on the view information to a stream
92 template< typename OUT >
93 void dump(OUT &s) const
95 s.printf("%d(%d)", (int)size(), (int)shift());
97 } __attribute__((packed));
100 * \brief Pixel information.
101 * \ingroup api_l4re_goos
103 * This class wraps the information on a pixel, such as the size and
104 * position of each color component in the pixel.
106 class L4_EXPORT Pixel_info
109 Color_component _r, _g, _b, _a; ///< Red, green, blue and alpha color components
110 unsigned char _bpp; ///< Size of the pixel in bytes.
114 * \brief Return the red color compoment of the pixel.
115 * \return Red color component.
117 Color_component const &r() const { return _r; }
120 * \brief Return the green color compoment of the pixel.
121 * \return Green color component.
123 Color_component const &g() const { return _g; }
126 * \brief Return the blue color compoment of the pixel.
127 * \return Blue color component.
129 Color_component const &b() const { return _b; }
132 * \brief Return the alpha color compoment of the pixel.
133 * \return Alpha color component.
135 Color_component const &a() const { return _a; }
138 * \brief Query size of pixel in bytes.
139 * \return Size of pixel in bytes.
141 unsigned char bytes_per_pixel() const { return _bpp; }
144 * \brief Number of bits of the pixel.
145 * \return Number of bits used by the pixel.
147 unsigned char bits_per_pixel() const
148 { return _r.size() + _g.size() + _b.size() +_a.size(); }
151 * \brief Return whether the pixel has an alpha channel.
152 * \return True if the pixel has an alpha channel, false if not.
154 bool has_alpha() const { return _a.size() > 0; }
157 * \brief Set the red color component of the pixel.
158 * \param c Red color component.
160 void r(Color_component const &c) { _r = c; }
163 * \brief Set the green color component of the pixel.
164 * \param c Green color component.
166 void g(Color_component const &c) { _g = c; }
169 * \brief Set the blue color component of the pixel.
170 * \param c Blue color component.
172 void b(Color_component const &c) { _b = c; }
175 * \brief Set the alpha color component of the pixel.
176 * \param c Alpha color component.
178 void a(Color_component const &c) { _a = c; }
181 * \brief Set the size of the pixel in bytes.
182 * \param bpp Size of pixel in bytes.
184 void bytes_per_pixel(unsigned char bpp) { _bpp = bpp; }
187 * \brief Constructor.
189 Pixel_info() : _r(), _g(), _b(), _a() {}
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.
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)
209 * \brief Convenience constructor.
210 * \param vbi Suitable information structure.
211 * Convenience constructor to create the pixel info from
212 * a VESA Framebuffer Info.
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)
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.
227 bool operator == (Pixel_info const &o) const
229 return _r == o._r && _g == o._g && _b == o._b && _a == o._a && _bpp == o._bpp;
233 * \brief Dump information on the pixel to a stream
237 template< typename OUT >
238 void dump(OUT &s) const
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());