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
39namespace Pt {
40
41namespace Gfx {
42
52class Argb32Pixel
53{
54 friend class Argb32ConstPixel;
55
56 public:
57 typedef Argb32 FormatType;
58 typedef Color ColorType;
59
60 public:
61 template <typename T>
62 Argb32Pixel(T& view, Pt::ssize_t x, Pt::ssize_t y);
63
64 Argb32Pixel(const Argb32Pixel& p) = default;
65
66 ~Argb32Pixel() = default;
67
68 Argb32Pixel& operator=(const Argb32Pixel&) = delete;
69
70 Argb32Pixel& operator=(const Color& color);
71
72 template <typename T>
73 void reset(T& view, Pt::ssize_t x, Pt::ssize_t y);
74
75 void reset(const Argb32Pixel& p);
76
77 const ViewBase& view() const
78 { return *_view; }
79
80 Pt::uint8_t* base()
81 { return _base; }
82
83 const Pt::uint8_t* base() const
84 { return _base; }
85
86 Pt::uint8_t alpha() const;
87
88 void setAlpha(Pt::uint8_t a);
89
90 Pt::uint8_t red() const;
91
92 void setRed(Pt::uint8_t r);
93
94 Pt::uint8_t green() const;
95
96 void setGreen(Pt::uint8_t g);
97
98 Pt::uint8_t blue() const;
99
100 void setBlue(Pt::uint8_t b);
101
102 Color getColor() const;
103
104 void advance();
105
106 void skipPadding();
107
108 void advance(Pt::ssize_t n);
109
110 void advanceLines(Pt::ssize_t n);
111
112 void assign(const Argb32Pixel& p);
113
114 void assign(const Argb32ConstPixel& p);
115
116 void assign(const Argb32Pixel& p, std::size_t length);
117
118 void assign(const Argb32ConstPixel& p, std::size_t length);
119
120 void getColors(Color* colors, std::size_t length) const;
121
122 void assign(const Color* colors, std::size_t length);
123
124 void fill(std::size_t n, const Color& color);
125
126 bool equals(const Argb32Pixel& p) const;
127
128 bool equals(const Argb32ConstPixel& p) const;
129
130 private:
131 const ViewBase* _view;
132 Pt::uint8_t* _base;
133};
134
139class Argb32ConstPixel
140{
141 friend class Argb32Pixel;
142
143 public:
144 typedef Argb32 FormatType;
145 typedef Color ColorType;
146
147 protected:
148 Argb32ConstPixel(const Pt::uint8_t* data, const ViewBase& view,
149 Pt::ssize_t x, Pt::ssize_t y);
150
151 public:
152 template <typename T>
153 Argb32ConstPixel(const T& view, Pt::ssize_t x, Pt::ssize_t y);
154
155 template <typename T>
156 Argb32ConstPixel(T& view, Pt::ssize_t x, Pt::ssize_t y);
157
158 Argb32ConstPixel(const Argb32ConstPixel& p) = default;
159
160 Argb32ConstPixel(const Argb32Pixel& p);
161
162 ~Argb32ConstPixel() = default;
163
164 Argb32ConstPixel& operator=(const Argb32ConstPixel&) = delete;
165
166 template <typename T>
167 void reset(const T& view, Pt::ssize_t x, Pt::ssize_t y);
168
169 template <typename T>
170 void reset(T& view, Pt::ssize_t x, Pt::ssize_t y);
171
172 void reset(const Argb32ConstPixel& p);
173
174 void reset(const Argb32Pixel& p);
175
176 const ViewBase& view() const
177 { return *_view; }
178
179 const Pt::uint8_t* base() const
180 { return _base; }
181
182 Pt::uint8_t alpha() const;
183
184 Pt::uint8_t red() const;
185
186 Pt::uint8_t green() const;
187
188 Pt::uint8_t blue() const;
189
190 Color getColor() const;
191
192 void advance();
193
194 void skipPadding();
195
196 void advance(Pt::ssize_t n);
197
198 void advanceLines(Pt::ssize_t n);
199
200 void getColors(Color* colors, std::size_t length) const;
201
202 bool equals(const Argb32ConstPixel& p) const;
203
204 bool equals(const Argb32Pixel& p) const;
205
206 private:
207 const ViewBase* _view;
208 const Pt::uint8_t* _base;
209};
210
219class PT_GFX_API Argb32 final : public ImageFormat
220{
221 static const unsigned PixelWidth = 4;
222
223 public:
224 typedef Argb32Pixel Pixel;
225 typedef Argb32ConstPixel ConstPixel;
226
227 public:
228 static const Argb32& get()
229 {
230 static Argb32 _argb32;
231 return _argb32;
232 }
233
234 public:
235 Argb32()
236 : ImageFormat(PixelWidth)
237 { }
238
241 std::size_t pixelStride() const
242 {
243 return PixelWidth;
244 }
245
246 std::size_t imageSize(std::size_t width, std::size_t height,
247 std::size_t padding) const
248 {
249 std::size_t stride = (width * 4) + padding;
250 std::size_t n = stride * height;
251 return n;
252 }
253
254 protected:
255 virtual std::unique_ptr<ImageFormat> onClone() const override
256 {
257 return std::unique_ptr<ImageFormat>(new Argb32);
258 }
259
260 virtual const std::type_info& onGetType() const override
261 {
262 return typeid(*this);
263 }
264
265 virtual std::size_t onImageSize(Pt::ssize_t width, Pt::ssize_t height,
266 std::size_t padding) const override;
267
268 virtual PixelBase* onCreatePixel(Pt::uint8_t* data, const ViewBase& view,
269 Pt::ssize_t x, Pt::ssize_t y,
270 PixelStorage& store) const override;
271
272 public:
273 template <typename BasePtr>
274 static BasePtr getPixel(const ViewBase& view, BasePtr base,
275 Pt::ssize_t xpos, Pt::ssize_t ypos)
276 {
277 base += ypos * view.stride();
278 base += xpos * PixelWidth;
279 return base;
280 }
281
282 template <typename BasePtr>
283 static BasePtr advance(const ViewBase& view, BasePtr base)
284 {
285 return base + PixelWidth;
286 }
287
288 template <typename BasePtr>
289 static BasePtr advance(const ViewBase& view, BasePtr base, Pt::ssize_t n)
290 {
291 return base + n * PixelWidth;
292 }
293
294 template <typename BasePtr>
295 static BasePtr skipPadding(const ViewBase& view, BasePtr base)
296 {
297 Pt::ssize_t w = view.width() * PixelWidth;
298 Pt::ssize_t off = view.stride() - w;
299 return base + off;
300 }
301
302 template <typename BasePtr>
303 static BasePtr advanceLines(const ViewBase& view, BasePtr base, Pt::ssize_t n)
304 {
305 return base + n * view.stride();
306 }
307
308 static ColorF getColorF(const Pt::uint8_t* p);
309
310 static Color getColor(const Pt::uint8_t* p);
311
312 static void getColors(const Pt::uint8_t* p, Gfx::ColorF* colors, std::size_t n);
313
314 static void getColors(const Pt::uint8_t* p, Color* colors, std::size_t n);
315
316 static void assign(Pt::uint8_t* to, const Color& from);
317
318 static void assign(Pt::uint8_t* to, const ColorF& c);
319
320 static void fill(Pt::uint8_t* to, std::size_t length, const Color& c);
321
322 static void fill(Pt::uint8_t* to, std::size_t length, const ColorF& c);
323
324 static void assign(Pt::uint8_t* to, const Color* colors, std::size_t length);
325
326 static void assign(Pt::uint8_t* to, const ColorF* colors, std::size_t length);
327
328 static void copy(Pt::uint8_t* to, const Pt::uint8_t* from);
329
330 static void copy(Pt::uint8_t* to, const Pt::uint8_t* from, std::size_t length);
331};
332
335inline void sourceCopy(Argb32Pixel& to, std::size_t length, const Color& from);
336
339inline void sourceOver(Argb32Pixel& to, std::size_t length, const Color& from);
340
343inline void sourceCopy(Argb32Pixel& to, const Argb32ConstPixel& from, std::size_t length);
344
347inline void sourceOver(Argb32Pixel& to, const Argb32ConstPixel& from, std::size_t length);
348
349} // namespace
350
351} // namespace
352
353#include <Pt/Gfx/Argb32.hpp>
354
355#endif
Read-only cursor to one ARGB-32 pixel.
Definition Argb32.h:140
Cursor to one ARGB-32 pixel.
Definition Argb32.h:53
ARGB-32 image format.
Definition Argb32.h:220
std::size_t pixelStride() const
Returns the distance between two pixel base pointers in bytes.
Definition Argb32.h:241
8-bit ARGB color.
Definition Color.h:65
Width, height and stride of an image or view.
Definition ViewBase.h:50
uint_type uint8_t
Unsigned 8-bit integer type.
Definition Api-Types.h:24
Graphics and imaging services.
Definition Api-Argb32Image.h:12
void sourceOver(Argb32Pixel &to, std::size_t length, const Color &from)
Blends one color over N destination pixels.
void sourceCopy(Argb32Pixel &to, std::size_t length, const Color &from)
Copies one color to N destination pixels.
Core module.
Definition Allocator.h:33