Argb32.h
1 /* Copyright (C) 2016-2016 Marc Boris Duerner
2  Copyright (C) 2017-2017 Aloysius Indrayanto
3 
4  This library is free software; you can redistribute it and/or
5  modify it under the terms of the GNU Lesser General Public
6  License as published by the Free Software Foundation; either
7  version 2.1 of the License, or (at your option) any later version.
8 
9  As a special exception, you may use this file as part of a free
10  software library without restriction. Specifically, if other files
11  instantiate templates or use macros or inline functions from this
12  file, or you compile this file and link it with other files to
13  produce an executable, this file does not by itself cause the
14  resulting executable to be covered by the GNU General Public
15  License. This exception does not however invalidate any other
16  reasons why the executable file might be covered by the GNU Library
17  General Public License.
18 
19  This library is distributed in the hope that it will be useful,
20  but WITHOUT ANY WARRANTY; without even the implied warranty of
21  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22  Lesser General Public License for more details.
23 
24  You should have received a copy of the GNU Lesser General Public
25  License along with this library; if not, write to the Free Software
26  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
27  MA 02110-1301 USA
28 */
29 
30 #ifndef PT_GFX_ARGB32_H
31 #define PT_GFX_ARGB32_H
32 
33 #include <Pt/Gfx/Api.h>
34 #include <Pt/Gfx/ImageFormat.h>
35 #include <Pt/Gfx/Color.h>
36 #include <Pt/Types.h>
37 #include <cstring>
38 
39 namespace Pt {
40 
41 namespace Gfx {
42 
46 {
47  friend class Argb32ConstPixel;
48 
49  public:
50  typedef Argb32 FormatType;
51  typedef Color ColorType;
52 
53  public:
54  template <typename T>
55  Argb32Pixel(T& view, Pt::ssize_t x, Pt::ssize_t y);
56 
57  Argb32Pixel(const Argb32Pixel& p) = default;
58 
59  ~Argb32Pixel() = default;
60 
61  Argb32Pixel& operator=(const Argb32Pixel&) = delete;
62 
63  Argb32Pixel& operator=(const Color& color);
64 
65  template <typename T>
66  void reset(T& view, Pt::ssize_t x, Pt::ssize_t y);
67 
68  void reset(const Argb32Pixel& p);
69 
70  const ViewBase& view() const
71  { return *_view; }
72 
73  Pt::uint8_t* base()
74  { return _base; }
75 
76  const Pt::uint8_t* base() const
77  { return _base; }
78 
79  Pt::uint8_t alpha() const;
80 
81  void setAlpha(Pt::uint8_t a);
82 
83  Pt::uint8_t red() const;
84 
85  void setRed(Pt::uint8_t r);
86 
87  Pt::uint8_t green() const;
88 
89  void setGreen(Pt::uint8_t g);
90 
91  Pt::uint8_t blue() const;
92 
93  void setBlue(Pt::uint8_t b);
94 
95  Color getColor() const;
96 
97  void advance();
98 
99  void skipPadding();
100 
101  void advance(Pt::ssize_t n);
102 
103  void advanceLines(Pt::ssize_t n);
104 
105  void assign(const Argb32Pixel& p);
106 
107  void assign(const Argb32ConstPixel& p);
108 
109  void assign(const Argb32Pixel& p, std::size_t length);
110 
111  void assign(const Argb32ConstPixel& p, std::size_t length);
112 
113  void getColors(Color* colors, std::size_t length) const;
114 
115  void assign(const Color* colors, std::size_t length);
116 
117  void fill(std::size_t n, const Color& color);
118 
119  bool equals(const Argb32Pixel& p) const;
120 
121  bool equals(const Argb32ConstPixel& p) const;
122 
123  private:
124  const ViewBase* _view;
125  Pt::uint8_t* _base;
126 };
127 
131 {
132  friend class Argb32Pixel;
133 
134  public:
135  typedef Argb32 FormatType;
136  typedef Color ColorType;
137 
138  protected:
139  Argb32ConstPixel(const Pt::uint8_t* data, const ViewBase& view,
140  Pt::ssize_t x, Pt::ssize_t y);
141 
142  public:
143  template <typename T>
144  Argb32ConstPixel(const T& view, Pt::ssize_t x, Pt::ssize_t y);
145 
146  template <typename T>
147  Argb32ConstPixel(T& view, Pt::ssize_t x, Pt::ssize_t y);
148 
149  Argb32ConstPixel(const Argb32ConstPixel& p) = default;
150 
151  Argb32ConstPixel(const Argb32Pixel& p);
152 
153  ~Argb32ConstPixel() = default;
154 
155  Argb32ConstPixel& operator=(const Argb32ConstPixel&) = delete;
156 
157  template <typename T>
158  void reset(const T& view, Pt::ssize_t x, Pt::ssize_t y);
159 
160  template <typename T>
161  void reset(T& view, Pt::ssize_t x, Pt::ssize_t y);
162 
163  void reset(const Argb32ConstPixel& p);
164 
165  void reset(const Argb32Pixel& p);
166 
167  const ViewBase& view() const
168  { return *_view; }
169 
170  const Pt::uint8_t* base() const
171  { return _base; }
172 
173  Pt::uint8_t alpha() const;
174 
175  Pt::uint8_t red() const;
176 
177  Pt::uint8_t green() const;
178 
179  Pt::uint8_t blue() const;
180 
181  Color getColor() const;
182 
183  void advance();
184 
185  void skipPadding();
186 
187  void advance(Pt::ssize_t n);
188 
189  void advanceLines(Pt::ssize_t n);
190 
191  void getColors(Color* colors, std::size_t length) const;
192 
193  bool equals(const Argb32ConstPixel& p) const;
194 
195  bool equals(const Argb32Pixel& p) const;
196 
197  private:
198  const ViewBase* _view;
199  const Pt::uint8_t* _base;
200 };
201 
204 class PT_GFX_API Argb32 final : public ImageFormat
205 {
206  static const unsigned PixelWidth = 4;
207 
208  public:
209  typedef Argb32Pixel Pixel;
211 
212  public:
213  static const Argb32& get()
214  {
215  static Argb32 _argb32;
216  return _argb32;
217  }
218 
219  public:
220  Argb32()
221  : ImageFormat(PixelWidth)
222  { }
223 
226  std::size_t pixelStride() const
227  {
228  return PixelWidth;
229  }
230 
231  std::size_t imageSize(std::size_t width, std::size_t height,
232  std::size_t padding) const
233  {
234  std::size_t stride = (width * 4) + padding;
235  std::size_t n = stride * height;
236  return n;
237  }
238 
239  protected:
240  virtual std::unique_ptr<ImageFormat> onClone() const override
241  {
242  return std::unique_ptr<ImageFormat>(new Argb32);
243  }
244 
245  virtual const std::type_info& onGetType() const override
246  {
247  return typeid(*this);
248  }
249 
250  virtual std::size_t onImageSize(Pt::ssize_t width, Pt::ssize_t height,
251  std::size_t padding) const override;
252 
253  virtual PixelBase* onCreatePixel(Pt::uint8_t* data, const ViewBase& view,
254  Pt::ssize_t x, Pt::ssize_t y,
255  PixelStorage& store) const override;
256 
257  public:
258  template <typename BasePtr>
259  static BasePtr getPixel(const ViewBase& view, BasePtr base,
260  Pt::ssize_t xpos, Pt::ssize_t ypos)
261  {
262  base += ypos * view.stride();
263  base += xpos * PixelWidth;
264  return base;
265  }
266 
267  template <typename BasePtr>
268  static BasePtr advance(const ViewBase& view, BasePtr base)
269  {
270  return base + PixelWidth;
271  }
272 
273  template <typename BasePtr>
274  static BasePtr advance(const ViewBase& view, BasePtr base, Pt::ssize_t n)
275  {
276  return base + n * PixelWidth;
277  }
278 
279  template <typename BasePtr>
280  static BasePtr skipPadding(const ViewBase& view, BasePtr base)
281  {
282  Pt::ssize_t w = view.width() * PixelWidth;
283  Pt::ssize_t off = view.stride() - w;
284  return base + off;
285  }
286 
287  template <typename BasePtr>
288  static BasePtr advanceLines(const ViewBase& view, BasePtr base, Pt::ssize_t n)
289  {
290  return base + n * view.stride();
291  }
292 
293  static ColorF getColorF(const Pt::uint8_t* p);
294 
295  static Color getColor(const Pt::uint8_t* p);
296 
297  static void getColors(const Pt::uint8_t* p, Gfx::ColorF* colors, std::size_t n);
298 
299  static void getColors(const Pt::uint8_t* p, Color* colors, std::size_t n);
300 
301  static void assign(Pt::uint8_t* to, const Color& from);
302 
303  static void assign(Pt::uint8_t* to, const ColorF& c);
304 
305  static void fill(Pt::uint8_t* to, std::size_t length, const Color& c);
306 
307  static void fill(Pt::uint8_t* to, std::size_t length, const ColorF& c);
308 
309  static void assign(Pt::uint8_t* to, const Color* colors, std::size_t length);
310 
311  static void assign(Pt::uint8_t* to, const ColorF* colors, std::size_t length);
312 
313  static void copy(Pt::uint8_t* to, const Pt::uint8_t* from);
314 
315  static void copy(Pt::uint8_t* to, const Pt::uint8_t* from, std::size_t length);
316 };
317 
320 inline void sourceCopy(Argb32Pixel& to, std::size_t length, const Color& from);
321 
324 inline void sourceOver(Argb32Pixel& to, std::size_t length, const Color& from);
325 
328 inline void sourceCopy(Argb32Pixel& to, const Argb32ConstPixel& from, std::size_t length);
329 
332 inline void sourceOver(Argb32Pixel& to, const Argb32ConstPixel& from, std::size_t length);
333 
334 } // namespace
335 
336 } // namespace
337 
338 #include <Pt/Gfx/Argb32.hpp>
339 
340 #endif
Core module.
Definition: Allocator.h:33
ARGB-32 const pixel.
Definition: Argb32.h:131
void sourceOver(Argb32Pixel &to, std::size_t length, const Color &from)
Blends one color over N destination pixels.
ARGB-32 image format.
Definition: Argb32.h:205
Image format.
Definition: ImageFormat.h:53
std::size_t pixelStride() const
Returns the distance between two pixel base pointers in bytes.
Definition: Argb32.h:226
ARGB-32 pixel.
Definition: Argb32.h:46
void sourceCopy(Argb32Pixel &to, std::size_t length, const Color &from)
Copies one color to N destination pixels.
uint_type uint8_t
Unsigned 8-bit integer type.
Definition: Api-Types.h:20
Standard color type.
Definition: Color.h:47