Rgb32.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_RGB32_H
30 #define PT_GFX_RGB32_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  Rgb32Color()
48  : _value(0)
49  { }
50 
51  Rgb32Color(const Rgb32Color& color) = default;
52 
53  explicit Rgb32Color(uint32_t val)
54  : _value(val)
55  { }
56 
57  explicit Rgb32Color(const uint8_t* base)
58  : _value()
59  {
60  std::memcpy( &_value, base, sizeof(uint32_t) );
61  }
62 
64  : _value( (uint32_t(a) << 24) | (uint32_t(pr) << 16) | (uint32_t(pg) << 8) | uint32_t(pb) )
65  {
66  }
67 
68  Rgb32Color& operator=(const Rgb32Color& color) = default;
69 
70  Pt::uint8_t alpha() const
71  {
72  return _value >> 24;
73  }
74 
75  Pt::uint8_t red() const
76  {
77  return (_value & 0x00FF0000) >> 16;
78  }
79 
80  Pt::uint8_t green() const
81  {
82  return (_value & 0x0000FF00) >> 8;
83  }
84 
85  Pt::uint8_t blue() const
86  {
87  return _value & 0x000000FF;
88  }
89 
90  void setAlpha(Pt::uint8_t a)
91  {
92  _value = (_value & 0x00FFFFFF) | (uint32_t(a) << 24);
93  }
94 
95  void setRed(Pt::uint8_t pr)
96  {
97  _value = (_value & 0xFF00FFFF) | (uint32_t(pr) << 16);
98  }
99 
100  void setGreen(Pt::uint8_t pg)
101  {
102  _value = (_value & 0xFFFF00FF) | (uint32_t(pg) << 8);
103  }
104 
105  void setBlue(Pt::uint8_t pb)
106  {
107  _value = (_value & 0xFFFFFF00) | uint32_t(pb);
108  }
109 
110  const uint32_t& value() const
111  {
112  return _value;
113  }
114 
115  private:
116  Pt::uint32_t _value;
117 };
118 
125 {
126  friend class Rgb32ConstPixel;
127 
128  public:
129  typedef Rgb32 FormatType;
130  typedef Color ColorType;
131 
132  public:
133  template <typename T>
134  Rgb32Pixel(T& view, Pt::ssize_t x, Pt::ssize_t y);
135 
136  Rgb32Pixel(const Rgb32Pixel& p) = default;
137 
138  ~Rgb32Pixel() = default;
139 
140  Rgb32Pixel& operator=(const Rgb32Pixel&) = delete;
141 
142  Rgb32Pixel& operator=(const Color& color);
143 
144  Rgb32Pixel& operator=(const Rgb32Color& color);
145 
146  template <typename T>
147  void reset(T& view, Pt::ssize_t x, Pt::ssize_t y);
148 
149  void reset(const Rgb32Pixel& p);
150 
151  const ViewBase& view() const
152  { return *_view; }
153 
154  Pt::uint8_t* base()
155  { return _base; }
156 
157  const Pt::uint8_t* base() const
158  { return _base; }
159 
160  Pt::uint8_t alpha() const;
161 
162  void setAlpha(Pt::uint8_t a);
163 
164  Pt::uint8_t red() const;
165 
166  void setRed(Pt::uint8_t r);
167 
168  Pt::uint8_t green() const;
169 
170  void setGreen(Pt::uint8_t g);
171 
172  Pt::uint8_t blue() const;
173 
174  void setBlue(Pt::uint8_t b);
175 
178  Rgb32Color color() const;
179 
182  Color getColor() const;
183 
184  void advance();
185 
186  void skipPadding();
187 
188  void advance(Pt::ssize_t n);
189 
190  void advanceLines(Pt::ssize_t n);
191 
192  void assign(const Rgb32Pixel& p);
193 
194  void assign(const Rgb32ConstPixel& p);
195 
196  void assign(const Rgb32Pixel& p, std::size_t length);
197 
198  void assign(const Rgb32ConstPixel& p, std::size_t length);
199 
200  void assign(const Color* colors, std::size_t length);
201 
202  void assign(const Rgb32Color* colors, std::size_t length);
203 
204  void getColors(Color* colors, std::size_t length) const;
205 
208  void getColors(Rgb32Color* colors, std::size_t length) const;
209 
210  void fill(std::size_t n, const Color& color);
211 
212  void fill(std::size_t n, const Rgb32Color& color);
213 
214  bool equals(const Rgb32Pixel& p) const;
215 
216  bool equals(const Rgb32ConstPixel& p) const;
217 
218  private:
219  const ViewBase* _view;
220  Pt::uint8_t* _base;
221 };
222 
226 {
227  friend class Rgb32Pixel;
228 
229  public:
230  typedef Rgb32 FormatType;
231  typedef Color ColorType;
232 
233  protected:
234  Rgb32ConstPixel(const Pt::uint8_t* data, const ViewBase& view,
235  Pt::ssize_t x, Pt::ssize_t y);
236 
237  public:
238  template <typename T>
239  Rgb32ConstPixel(const T& view, Pt::ssize_t x, Pt::ssize_t y);
240 
241  template <typename T>
242  Rgb32ConstPixel(T& view, Pt::ssize_t x, Pt::ssize_t y);
243 
244  Rgb32ConstPixel(const Rgb32ConstPixel& p) = default;
245 
246  Rgb32ConstPixel(const Rgb32Pixel& p);
247 
248  ~Rgb32ConstPixel() = default;
249 
250  Rgb32ConstPixel& operator=(const Rgb32ConstPixel&) = delete;
251 
252  template <typename T>
253  void reset(const T& view, Pt::ssize_t x, Pt::ssize_t y);
254 
255  template <typename T>
256  void reset(T& view, Pt::ssize_t x, Pt::ssize_t y);
257 
258  void reset(const Rgb32ConstPixel& p);
259 
260  void reset(const Rgb32Pixel& p);
261 
262  const ViewBase& view() const
263  { return *_view; }
264 
265  const Pt::uint8_t* base() const
266  { return _base; }
267 
268  Pt::uint8_t alpha() const;
269 
270  Pt::uint8_t red() const;
271 
272  Pt::uint8_t green() const;
273 
274  Pt::uint8_t blue() const;
275 
278  Rgb32Color color() const;
279 
282  Color getColor() const;
283 
284  void advance();
285 
286  void skipPadding();
287 
288  void advance(Pt::ssize_t n);
289 
290  void advanceLines(Pt::ssize_t n);
291 
292  void getColors(Color* colors, std::size_t length) const;
293 
296  void getColors(Rgb32Color* colors, std::size_t length) const;
297 
298  bool equals(const Rgb32ConstPixel& p) const;
299 
300  bool equals(const Rgb32Pixel& p) const;
301 
302  private:
303  const ViewBase* _view;
304  const Pt::uint8_t* _base;
305 };
306 
314 class PT_GFX_API Rgb32 final : public ImageFormat
315 {
316  static const unsigned PixelWidth = 4;
317 
318  public:
319  typedef Rgb32Pixel Pixel;
320  typedef Rgb32ConstPixel ConstPixel;
321 
322  public:
323  static const Rgb32& get()
324  {
325  static Rgb32 _rgb32;
326  return _rgb32;
327  }
328 
329  public:
330  Rgb32()
331  : ImageFormat(PixelWidth)
332  { }
333 
336  std::size_t pixelStride() const
337  {
338  return PixelWidth;
339  }
340 
341  std::size_t imageSize(std::size_t width, std::size_t height,
342  std::size_t padding) const
343  {
344  std::size_t stride = (width * 4) + padding;
345  std::size_t n = stride * height;
346  return n;
347  }
348 
349  protected:
350  virtual std::unique_ptr<ImageFormat> onClone() const override
351  {
352  return std::unique_ptr<ImageFormat>(new Rgb32);
353  }
354 
355  virtual const std::type_info& onGetType() const override
356  {
357  return typeid(*this);
358  }
359 
360  virtual std::size_t onImageSize(Pt::ssize_t width, Pt::ssize_t height,
361  std::size_t padding) const override;
362 
363  virtual PixelBase* onCreatePixel(Pt::uint8_t* data, const ViewBase& view,
364  Pt::ssize_t x, Pt::ssize_t y,
365  PixelStorage& store) const override;
366 
367  public:
368  template <typename BasePtr>
369  static BasePtr getPixel(const ViewBase& view, BasePtr base,
370  Pt::ssize_t xpos, Pt::ssize_t ypos)
371  {
372  base += ypos * view.stride();
373  base += xpos * PixelWidth;
374  return base;
375  }
376 
377  template <typename BasePtr>
378  static BasePtr advance(const ViewBase& view, BasePtr base)
379  {
380  return base + PixelWidth;
381  }
382 
383  template <typename BasePtr>
384  static BasePtr advance(const ViewBase& view, BasePtr base, Pt::ssize_t n)
385  {
386  return base + n * PixelWidth;
387  }
388 
389  template <typename BasePtr>
390  static BasePtr skipPadding(const ViewBase& view, BasePtr base)
391  {
392  Pt::ssize_t w = view.width() * PixelWidth;
393  Pt::ssize_t off = view.stride() - w;
394  return base + off;
395  }
396 
397  template <typename BasePtr>
398  static BasePtr advanceLines(const ViewBase& view, BasePtr base, Pt::ssize_t n)
399  {
400  return base + n * view.stride();
401  }
402 
403  static Rgb32Color getRgb32Color(const Pt::uint8_t* p);
404 
405  static ColorF getColorF(const Pt::uint8_t* p);
406 
407  static Color getColor(const Pt::uint8_t* p);
408 
409  static void getColors(const Pt::uint8_t* p, Rgb32Color* colors, std::size_t n);
410 
411  static void getColors(const Pt::uint8_t* p, Color* colors, std::size_t n);
412 
413  static void getColors(const Pt::uint8_t* p, Gfx::ColorF* colors, std::size_t n);
414 
415  static void assign(Pt::uint8_t* to, const Color& from);
416 
417  static void assign(Pt::uint8_t* to, const ColorF& c);
418 
419  static void fill(Pt::uint8_t* to, std::size_t length, const Rgb32Color& c);
420 
421  static void fill(Pt::uint8_t* to, std::size_t length, const Color& c);
422 
423  static void fill(Pt::uint8_t* to, std::size_t length, const ColorF& c);
424 
425  static void assign(Pt::uint8_t* to, const Rgb32Color* colors, std::size_t length);
426 
427  static void assign(Pt::uint8_t* to, const Color* colors, std::size_t length);
428 
429  static void assign(Pt::uint8_t* to, const ColorF* colors, std::size_t length);
430 
431  static void copy(Pt::uint8_t* to, const Pt::uint8_t* from);
432 
433  static void copy(Pt::uint8_t* to, const Pt::uint8_t* from, std::size_t length);
434 
435  private:
438  static Pt::uint32_t premultiply(Pt::uint8_t a, Pt::uint8_t r,
440  {
441  const Pt::uint32_t pr = (r * a + 127) / 255;
442  const Pt::uint32_t pg = (g * a + 127) / 255;
443  const Pt::uint32_t pb = (b * a + 127) / 255;
444 
445  return (Pt::uint32_t(a) << 24) |
446  (Pt::uint32_t(pr) << 16) |
447  (Pt::uint32_t(pg) << 8) |
448  Pt::uint32_t(pb);
449  }
450 
453  static Pt::uint32_t premultiply(const ColorF& c)
454  {
455  const Pt::uint32_t a = c.alpha() >> 8;
456  const Pt::uint32_t r = (Pt::uint32_t)(c.red () >> 8) * a / 255;
457  const Pt::uint32_t g = (Pt::uint32_t)(c.green() >> 8) * a / 255;
458  const Pt::uint32_t b = (Pt::uint32_t)(c.blue () >> 8) * a / 255;
459 
460  return (a << 24) | (r << 16) | (g << 8) | b;
461  }
462 };
463 
466 inline void sourceCopy(Rgb32Pixel& to, std::size_t length, const Rgb32Color& from);
467 
470 inline void sourceOver(Rgb32Pixel& to, std::size_t length, const Rgb32Color& from);
471 
474 inline void sourceCopy(Rgb32Pixel& to, const Rgb32ConstPixel& from, std::size_t length);
475 
478 inline void sourceOver(Rgb32Pixel& to, const Rgb32ConstPixel& from, std::size_t length);
479 
480 } // namespace
481 
482 } // namespace
483 
484 #include <Pt/Gfx/Rgb32.hpp>
485 
486 #endif
Core module.
Definition: Allocator.h:33
RGB-32 pixel as premultiplied ARGB-32.
Definition: Rgb32.h:125
void getColors(Rgb32Color *colors, std::size_t length) const
Get as premultiplied RGB-32 colors.
void sourceOver(Argb32Pixel &to, std::size_t length, const Color &from)
Blends one color over N destination pixels.
std::size_t pixelStride() const
Returns the distance between two pixel base pointers in bytes.
Definition: Rgb32.h:336
Premultiplied standard color type.
Definition: Rgb32.h:45
Color getColor() const
Returns ARGB-32 color.
Premultiplied ARGB-32 image format.
Definition: Rgb32.h:315
Image format.
Definition: ImageFormat.h:53
void sourceCopy(Argb32Pixel &to, std::size_t length, const Color &from)
Copies one color to N destination pixels.
Rgb32Color color() const
Returns a premultiplied RGB-32 color.
uint_type uint8_t
Unsigned 8-bit integer type.
Definition: Api-Types.h:20
void getColors(Rgb32Color *colors, std::size_t length) const
Get as premultiplied RGB-32 colors.
Color getColor() const
Returns a ARGB-32 color.
uint_type uint32_t
Unsigned 32-bit integer type.
Definition: Api-Types.h:48
Standard color type.
Definition: Color.h:47
Premultiplied ARGB-32 const pixel.
Definition: Rgb32.h:226
Rgb32Color color() const
Returns the premultiplied RGB-32 color.