L4Re - L4 Runtime Environment
colors
1 // vi:set ft=cpp: -*- Mode: C++ -*-
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 
31 class L4_EXPORT Color_component
32 {
33 private:
34  unsigned char _bits;
35  unsigned char _shift;
36 
37 public:
39  Color_component() : _bits(0), _shift(0) {}
40 
46  Color_component(unsigned char bits, unsigned char shift)
47  : _bits(bits), _shift(shift) {}
48 
53  unsigned char size() const { return _bits; }
54 
59  unsigned char shift() const { return _shift; }
60 
65  bool operator == (Color_component const &o) const
66  { return _shift == o._shift && _bits == o._bits; }
67 
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 
84  long unsigned set(int v) const
85  { return (v >> (unsigned long)(16 - _bits)) << (unsigned long)_shift; }
86 
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 
106 class L4_EXPORT Pixel_info
107 {
108 private:
109  Color_component _r, _g, _b, _a;
110  unsigned char _bpp;
111 
112 public:
117  Color_component const &r() const { return _r; }
118 
123  Color_component const &g() const { return _g; }
124 
129  Color_component const &b() const { return _b; }
130 
135  Color_component const &a() const { return _a; }
136 
141  unsigned char bytes_per_pixel() const { return _bpp; }
142 
147  unsigned char bits_per_pixel() const
148  { return _r.size() + _g.size() + _b.size() +_a.size(); }
149 
154  bool has_alpha() const { return _a.size() > 0; }
155 
160  void r(Color_component const &c) { _r = c; }
161 
166  void g(Color_component const &c) { _g = c; }
167 
172  void b(Color_component const &c) { _b = c; }
173 
178  void a(Color_component const &c) { _a = c; }
179 
184  void bytes_per_pixel(unsigned char bpp) { _bpp = bpp; }
185 
189  Pixel_info() : _r(), _g(), _b(), _a() {}
190 
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 
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 
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 
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 
unsigned char shift() const
Return the position of the component in the pixel.
Definition: colors:59
void dump(OUT &s) const
Dump information on the view information to a stream.
Definition: colors:93
unsigned char size() const
Return the number of bits used by the component.
Definition: colors:53
void r(Color_component const &c)
Set the red color component of the pixel.
Definition: colors:160
Color_component(unsigned char bits, unsigned char shift)
Constructor.
Definition: colors:46
A color component.
Definition: colors:31
Color_component const & r() const
Return the red color compoment of the pixel.
Definition: colors:117
unsigned char bytes_per_pixel() const
Query size of pixel in bytes.
Definition: colors:141
Pixel information.
Definition: colors:106
Color_component const & b() const
Return the blue color compoment of the pixel.
Definition: colors:129
L4Re C++ Interfaces.
Definition: cmd_control:15
Color_component const & g() const
Return the green color compoment of the pixel.
Definition: colors:123
L4 compiler related defines.
void dump(OUT &s) const
Dump information on the pixel to a stream.
Definition: colors:238
Color_component const & a() const
Return the alpha color compoment of the pixel.
Definition: colors:135
void b(Color_component const &c)
Set the blue color component of the pixel.
Definition: colors:172
Pixel_info()
Constructor.
Definition: colors:189
void a(Color_component const &c)
Set the alpha color component of the pixel.
Definition: colors:178
bool has_alpha() const
Return whether the pixel has an alpha channel.
Definition: colors:154
void bytes_per_pixel(unsigned char bpp)
Set the size of the pixel in bytes.
Definition: colors:184
Color_component()
Constructor.
Definition: colors:39
unsigned char bits_per_pixel() const
Number of bits of the pixel.
Definition: colors:147
Pixel_info(unsigned char bpp, char r, char rs, char g, char gs, char b, char bs, char a=0, char as=0)
Constructor.
Definition: colors:203
void g(Color_component const &c)
Set the green color component of the pixel.
Definition: colors:166
Pixel_info(VBI const *vbi)
Convenience constructor.
Definition: colors:215