Rgb16.h
1 /* Copyright (C) 2015 Marc Boris Duerner
2 
3  This library is free software; you can redistribute it and/or
4  modify it under the terms of the GNU Lesser General Public
5  License as published by the Free Software Foundation; either
6  version 2.1 of the License, or (at your option) any later version.
7 
8  As a special exception, you may use this file as part of a free
9  software library without restriction. Specifically, if other files
10  instantiate templates or use macros or inline functions from this
11  file, or you compile this file and link it with other files to
12  produce an executable, this file does not by itself cause the
13  resulting executable to be covered by the GNU General Public
14  License. This exception does not however invalidate any other
15  reasons why the executable file might be covered by the GNU Library
16  General Public License.
17 
18  This library is distributed in the hope that it will be useful,
19  but WITHOUT ANY WARRANTY; without even the implied warranty of
20  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21  Lesser General Public License for more details.
22 
23  You should have received a copy of the GNU Lesser General Public
24  License along with this library; if not, write to the Free Software
25  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
26  MA 02110-1301 USA
27 */
28 
29 #ifndef PT_GFX_RGB16_H
30 #define PT_GFX_RGB16_H
31 
32 #include <Pt/Gfx/Api.h>
33 #include <Pt/Gfx/ImageFormat.h>
34 #include <Pt/Gfx/Color.h>
35 #include <Pt/Types.h>
36 #include <cstring>
37 
38 namespace Pt {
39 
40 namespace Gfx {
41 
45 {
46  public:
47  Rgb16Color()
48  : _value(0)
49  { }
50 
51  Rgb16Color(const Rgb16Color& color) = default;
52 
53  explicit Rgb16Color(Pt::uint16_t value)
54  : _value(value)
55  { }
56 
57  explicit Rgb16Color(const Pt::uint8_t* base)
58  : _value()
59  {
60  std::memcpy(&_value, base, sizeof(Pt::uint16_t));
61  }
62 
64  : _value( (Pt::uint16_t(r & 0xF8) << 8) |
65  (Pt::uint16_t(g & 0xFC) << 3) |
66  (Pt::uint16_t(b) >> 3) )
67  { }
68 
69  Rgb16Color& operator=(const Rgb16Color& color) = default;
70 
71  Pt::uint8_t red() const
72  {
73  Pt::uint8_t r5 = (_value & 0xF800) >> 11;
74  return (r5 << 3) | (r5 >> 2);
75  }
76 
77  Pt::uint8_t green() const
78  {
79  Pt::uint8_t g6 = (_value & 0x07E0) >> 5;
80  return (g6 << 2) | (g6 >> 4);
81  }
82 
83  Pt::uint8_t blue() const
84  {
85  Pt::uint8_t b5 = _value & 0x001F;
86  return (b5 << 3) | (b5 >> 2);
87  }
88 
89  Pt::uint16_t value() const
90  {
91  return _value;
92  }
93 
94  private:
95  Pt::uint16_t _value;
96 };
97 
101 {
102  friend class Rgb16ConstPixel;
103 
104  public:
105  typedef Rgb16 FormatType;
106  typedef Color ColorType;
107 
108  public:
109  template <typename T>
110  Rgb16Pixel(T& view, Pt::ssize_t x, Pt::ssize_t y);
111 
112  Rgb16Pixel(const Rgb16Pixel& p) = default;
113 
114  ~Rgb16Pixel() = default;
115 
116  Rgb16Pixel& operator=(const Rgb16Pixel&) = delete;
117 
118  Rgb16Pixel& operator=(const Color& color);
119 
120  Rgb16Pixel& operator=(const Rgb16Color& color);
121 
122  template <typename T>
123  void reset(T& view, Pt::ssize_t x, Pt::ssize_t y);
124 
125  void reset(const Rgb16Pixel& p);
126 
127  const ViewBase& view() const
128  { return *_view; }
129 
130  Pt::uint8_t* base()
131  { return _base; }
132 
133  const Pt::uint8_t* base() const
134  { return _base; }
135 
136  Pt::uint8_t red() const;
137 
138  Pt::uint8_t green() const;
139 
140  Pt::uint8_t blue() const;
141 
142  Color getColor() const;
143 
146  Rgb16Color color() const;
147 
148  void advance();
149 
150  void skipPadding();
151 
152  void advance(Pt::ssize_t n);
153 
154  void advanceLines(Pt::ssize_t n);
155 
156  void assign(const Rgb16Pixel& p);
157 
158  void assign(const Rgb16ConstPixel& p);
159 
160  void assign(const Rgb16Pixel& p, std::size_t length);
161 
162  void assign(const Rgb16ConstPixel& p, std::size_t length);
163 
164  void getColors(Color* colors, std::size_t length) const;
165 
168  void getColors(Rgb16Color* colors, std::size_t length) const;
169 
170  void assign(const Color* colors, std::size_t length);
171 
172  void assign(const Rgb16Color* colors, std::size_t length);
173 
174  void fill(std::size_t n, const Color& color);
175 
176  void fill(std::size_t n, const Rgb16Color& color);
177 
178  bool equals(const Rgb16Pixel& p) const;
179 
180  bool equals(const Rgb16ConstPixel& p) const;
181 
182  private:
183  const ViewBase* _view;
184  Pt::uint8_t* _base;
185 };
186 
190 {
191  friend class Rgb16Pixel;
192 
193  public:
194  typedef Rgb16 FormatType;
195  typedef Color ColorType;
196 
197  protected:
198  Rgb16ConstPixel(const Pt::uint8_t* data, const ViewBase& view,
199  Pt::ssize_t x, Pt::ssize_t y);
200 
201  public:
202  template <typename T>
203  Rgb16ConstPixel(const T& view, Pt::ssize_t x, Pt::ssize_t y);
204 
205  template <typename T>
206  Rgb16ConstPixel(T& view, Pt::ssize_t x, Pt::ssize_t y);
207 
208  Rgb16ConstPixel(const Rgb16ConstPixel& p) = default;
209 
210  Rgb16ConstPixel(const Rgb16Pixel& p);
211 
212  ~Rgb16ConstPixel() = default;
213 
214  Rgb16ConstPixel& operator=(const Rgb16ConstPixel&) = delete;
215 
216  template <typename T>
217  void reset(const T& view, Pt::ssize_t x, Pt::ssize_t y);
218 
219  template <typename T>
220  void reset(T& view, Pt::ssize_t x, Pt::ssize_t y);
221 
222  void reset(const Rgb16ConstPixel& p);
223 
224  void reset(const Rgb16Pixel& p);
225 
226  const ViewBase& view() const
227  { return *_view; }
228 
229  const Pt::uint8_t* base() const
230  { return _base; }
231 
232  Pt::uint8_t red() const;
233 
234  Pt::uint8_t green() const;
235 
236  Pt::uint8_t blue() const;
237 
238  Color getColor() const;
239 
242  Rgb16Color color() const;
243 
244  void advance();
245 
246  void skipPadding();
247 
248  void advance(Pt::ssize_t n);
249 
250  void advanceLines(Pt::ssize_t n);
251 
252  void getColors(Color* colors, std::size_t length) const;
253 
256  void getColors(Rgb16Color* colors, std::size_t length) const;
257 
258  bool equals(const Rgb16ConstPixel& p) const;
259 
260  bool equals(const Rgb16Pixel& p) const;
261 
262  private:
263  const ViewBase* _view;
264  const Pt::uint8_t* _base;
265 };
266 
269 class PT_GFX_API Rgb16 final : public ImageFormat
270 {
271  static const unsigned PixelWidth = 2;
272 
273  public:
274  typedef Rgb16Pixel Pixel;
275  typedef Rgb16ConstPixel ConstPixel;
276 
277  public:
278  static const Rgb16& get()
279  {
280  static Rgb16 _rgb16;
281  return _rgb16;
282  }
283 
284  public:
285  Rgb16()
286  : ImageFormat(PixelWidth)
287  { }
288 
291  std::size_t pixelStride() const
292  {
293  return PixelWidth;
294  }
295 
296  std::size_t imageSize(std::size_t width, std::size_t height,
297  std::size_t padding) const
298  {
299  std::size_t stride = (width * PixelWidth) + padding;
300  std::size_t n = stride * height;
301  return n;
302  }
303 
304  protected:
305  virtual std::unique_ptr<ImageFormat> onClone() const override
306  {
307  return std::unique_ptr<ImageFormat>(new Rgb16);
308  }
309 
310  virtual const std::type_info& onGetType() const override
311  {
312  return typeid(*this);
313  }
314 
315  virtual std::size_t onImageSize(Pt::ssize_t width, Pt::ssize_t height,
316  std::size_t padding) const override;
317 
318  virtual PixelBase* onCreatePixel(Pt::uint8_t* data, const ViewBase& view,
319  Pt::ssize_t x, Pt::ssize_t y,
320  PixelStorage& store) const override;
321 
322  public:
323  template <typename BasePtr>
324  static BasePtr getPixel(const ViewBase& view, BasePtr base,
325  Pt::ssize_t xpos, Pt::ssize_t ypos)
326  {
327  base += ypos * view.stride();
328  base += xpos * PixelWidth;
329  return base;
330  }
331 
332  template <typename BasePtr>
333  static BasePtr advance(const ViewBase& view, BasePtr base)
334  {
335  return base + PixelWidth;
336  }
337 
338  template <typename BasePtr>
339  static BasePtr advance(const ViewBase& view, BasePtr base, Pt::ssize_t n)
340  {
341  return base + n * PixelWidth;
342  }
343 
344  template <typename BasePtr>
345  static BasePtr skipPadding(const ViewBase& view, BasePtr base)
346  {
347  Pt::ssize_t w = view.width() * PixelWidth;
348  Pt::ssize_t off = view.stride() - w;
349  return base + off;
350  }
351 
352  template <typename BasePtr>
353  static BasePtr advanceLines(const ViewBase& view, BasePtr base, Pt::ssize_t n)
354  {
355  return base + n * view.stride();
356  }
357 
358  static ColorF getColorF(const Pt::uint8_t* p);
359 
360  static Color getColor(const Pt::uint8_t* p);
361 
362  static Rgb16Color getRgb16Color(const Pt::uint8_t* p);
363 
364  static void getColors(const Pt::uint8_t* p, Gfx::ColorF* colors, std::size_t n);
365 
366  static void getColors(const Pt::uint8_t* p, Color* colors, std::size_t n);
367 
368  static void getColors(const Pt::uint8_t* p, Rgb16Color* colors, std::size_t n);
369 
370  static void assign(Pt::uint8_t* to, const Color& from);
371 
372  static void assign(Pt::uint8_t* to, const ColorF& c);
373 
374  static void assign(Pt::uint8_t* to, const Rgb16Color& c);
375 
376  static void fill(Pt::uint8_t* to, std::size_t length, const Color& c);
377 
378  static void fill(Pt::uint8_t* to, std::size_t length, const ColorF& c);
379 
380  static void fill(Pt::uint8_t* to, std::size_t length, const Rgb16Color& c);
381 
382  static void assign(Pt::uint8_t* to, const Color* colors, std::size_t length);
383 
384  static void assign(Pt::uint8_t* to, const ColorF* colors, std::size_t length);
385 
386  static void assign(Pt::uint8_t* to, const Rgb16Color* colors, std::size_t length);
387 
388  static void copy(Pt::uint8_t* to, const Pt::uint8_t* from);
389 
390  static void copy(Pt::uint8_t* to, const Pt::uint8_t* from, std::size_t length);
391 
392  private:
395  static Pt::uint16_t encode(Pt::uint8_t r, Pt::uint8_t g, Pt::uint8_t b)
396  {
397  return (Pt::uint16_t(r & 0xF8) << 8) |
398  (Pt::uint16_t(g & 0xFC) << 3) |
399  (Pt::uint16_t(b) >> 3);
400  }
401 
404  static Pt::uint16_t encode(const ColorF& c)
405  {
406  return Pt::uint16_t(c.red () & 0xF800) |
407  (Pt::uint16_t(c.green() & 0xFC00) >> 5) |
408  (Pt::uint16_t(c.blue () ) >> 11);
409  }
410 };
411 
412 } // namespace
413 
414 } // namespace
415 
416 #include <Pt/Gfx/Rgb16.hpp>
417 
418 #endif
Core module.
Definition: Allocator.h:33
RGB-565 image format.
Definition: Rgb16.h:270
void getColors(Rgb16Color *colors, std::size_t length) const
Get as native RGB-565 colors.
RGB-565 pixel.
Definition: Rgb16.h:101
Image format.
Definition: ImageFormat.h:53
void getColors(Rgb16Color *colors, std::size_t length) const
Get as native RGB-565 colors.
Native RGB-565 color type.
Definition: Rgb16.h:45
Rgb16Color color() const
Returns the native RGB-565 color.
RGB-565 const pixel.
Definition: Rgb16.h:190
Rgb16Color color() const
Returns the native RGB-565 color.
uint_type uint8_t
Unsigned 8-bit integer type.
Definition: Api-Types.h:20
Standard color type.
Definition: Color.h:47
uint_type uint16_t
Unsigned 16-bit integer type.
Definition: Api-Types.h:34
std::size_t pixelStride() const
Returns the distance between two pixel base pointers in bytes.
Definition: Rgb16.h:291