ImageFormat.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_IMAGE_FORMAT_H
30#define PT_GFX_IMAGE_FORMAT_H
31
32#include <Pt/Gfx/Api.h>
33#include <Pt/Gfx/ViewBase.h>
34#include <Pt/Gfx/ImageTraits.h>
35#include <Pt/Gfx/PixelBase.h>
36#include <Pt/Gfx/Color.h>
37#include <Pt/TypeTraits.h>
38
39#include <typeinfo>
40#include <memory>
41
42namespace Pt {
43
44namespace Gfx {
45
47// ImageFormat
49
71class ImageFormat
72{
73 friend struct ImageTraits<ImageFormat>;
74 friend struct ImageTraitsF;
75
76 public:
77 explicit ImageFormat(size_t pixelStride)
78 : _pixelStride(pixelStride)
79 { }
80
81 virtual ~ImageFormat()
82 { }
83
84 PixelBase* createPixel(Pt::uint8_t* data, const ViewBase& view,
85 Pt::ssize_t x, Pt::ssize_t y,
86 PixelStorage& store) const
87 {
88 return onCreatePixel(data, view, x, y, store);
89 }
90
91 bool operator==(const ImageFormat& a) const
92 {
93 return onGetType() == a.onGetType();
94 }
95
96 bool operator!=(const ImageFormat& a) const
97 {
98 return ! (*this == a);
99 }
100
101 const std::type_info& type() const
102 {
103 return onGetType();
104 }
105
106 public:
107 PT_GFX_API static const ImageFormat& get(int type = 0);
108
109 PT_GFX_API static const ImageFormat& rgb16();
110
111 PT_GFX_API static const ImageFormat& rgb32();
112
113 PT_GFX_API static const ImageFormat& argb32();
114
115 protected:
116 virtual std::unique_ptr<ImageFormat> onClone() const
117 {
118 return 0;
119 }
120
121 virtual const std::type_info& onGetType() const = 0;
122
123 virtual std::size_t onImageSize(Pt::ssize_t width, Pt::ssize_t height,
124 std::size_t padding) const = 0;
125
126 virtual PixelBase* onCreatePixel(Pt::uint8_t* data, const ViewBase& view,
127 Pt::ssize_t x, Pt::ssize_t y,
128 PixelStorage& store) const = 0;
129
130 inline const void* r0() const
131 { return _r0.ptr; }
132
133 inline const void* r1() const
134 { return _r1.ptr; }
135
136 inline const void* r2() const
137 { return _r2.ptr; }
138
139 private:
140 const std::size_t _pixelStride;
141 const varint_t _r0;
142 const varint_t _r1;
143 const varint_t _r2;
144};
145
147// ImageTraits
149
150template <>
152{
153 typedef Gfx::Pixel<Color> Pixel;
155
156 static std::size_t pixelStride(const ImageFormat& format)
157 {
158 return format._pixelStride;
159 }
160
161 static std::size_t imageSize(const ImageFormat& format, Pt::ssize_t width, Pt::ssize_t height,
162 std::size_t padding)
163 {
164 return format.onImageSize(width, height, padding);
165 }
166
167 static std::unique_ptr<ImageFormat> clone(const ImageFormat& format)
168 {
169 return format.onClone();
170 }
171};
172
173
174struct ImageTraitsF
175{
176 typedef Gfx::Pixel<ColorF> Pixel;
177 typedef Gfx::ConstPixel<ColorF> ConstPixel;
178
179 static std::size_t pixelStride(const ImageFormat& format)
180 {
181 return format._pixelStride;
182 }
183
184 static std::size_t imageSize(const ImageFormat& format, Pt::ssize_t width, Pt::ssize_t height,
185 std::size_t padding)
186 {
187 return format.onImageSize(width, height, padding);
188 }
189
190 static std::unique_ptr<ImageFormat> clone(const ImageFormat& format)
191 {
192 return format.onClone();
193 }
194};
195
197// Pixel
199
217template <typename ColorT>
218class Pixel
219{
220 template <typename C>
221 friend class ConstPixel;
222
223 public:
224 typedef ImageFormat FormatType;
225 typedef ColorT ColorType;
226
227 public:
228 template <typename T>
229 Pixel(T& view, Pt::ssize_t x, Pt::ssize_t y);
230
231 Pixel(const Pixel& p);
232
233 ~Pixel();
234
235 Pixel& operator=(const Pixel&) = delete;
236
237 Pixel& operator=(const ColorT& color)
238 {
239 _pixel->assign(color);
240 return *this;
241 }
242
243 template <typename T>
244 void reset(T& view, Pt::ssize_t x, Pt::ssize_t y);
245
246 void reset(const Pixel& p);
247
248 const PixelBase* pixelBase() const
249 { return _pixel; }
250
251 PixelBase* pixelBase()
252 { return _pixel; }
253
254 const ViewBase& view() const
255 { return _pixel->view(); }
256
257 const ImageFormat& format() const
258 { return *_format; }
259
260 Pt::uint8_t* base()
261 { return _pixel->base(); }
262
263 const Pt::uint8_t* base() const
264 { return _pixel->base(); }
265
266 void advance()
267 {
268 _pixel->advance();
269 }
270
271 void skipPadding()
272 {
273 _pixel->skipPadding();
274 }
275
276 void advance(Pt::ssize_t n)
277 {
278 _pixel->advance(n);
279 }
280
281 void advanceLines(Pt::ssize_t n)
282 {
283 _pixel->advanceLines(n);
284 }
285
286 ColorT getColor() const
287 {
288 return _pixel->getColor<ColorT>();
289 }
290
291 void getColors(ColorT* colors, std::size_t length) const
292 {
293 _pixel->getColors(colors, length);
294 }
295
296 void assign(const Pixel& p);
297
298 void assign(const ConstPixel<ColorT>& p);
299
300 void assign(const Pixel& p, std::size_t length);
301
302 void assign(const ConstPixel<ColorT>& p, std::size_t length);
303
304 void assign(const ColorT* colors, std::size_t length)
305 {
306 _pixel->assign(colors, length);
307 }
308
309 void fill(std::size_t n, const ColorT& color)
310 {
311 _pixel->fill(n, color);
312 }
313
314 bool equals(const Pixel& p) const
315 {
316 return _pixel->base() == p.base();
317 }
318
319 bool equals(const ConstPixel<ColorT>& p) const;
320
321 private:
322 template <typename PixelT>
323 void assignPixels(const PixelT& p, std::size_t length);
324
325 private:
326 PixelBase* _pixel;
327 PixelStorage _storage;
328 const ImageFormat* _format;
329};
330
332// ConstPixel
334
343template <typename ColorT>
344class ConstPixel
345{
346 template <typename C>
347 friend class Pixel;
348
349 public:
350 typedef ImageFormat FormatType;
351 typedef ColorT ColorType;
352
353 public:
354 template <typename T>
355 ConstPixel(const T& view, Pt::ssize_t x, Pt::ssize_t y);
356
357 template <typename T>
358 ConstPixel(T& view, Pt::ssize_t x, Pt::ssize_t y);
359
360 ConstPixel(const ConstPixel& p);
361
362 ConstPixel(const Pixel<ColorT>& p);
363
364 ~ConstPixel();
365
366 ConstPixel& operator=(const ConstPixel&) = delete;
367
368 template <typename T>
369 void reset(const T& view, Pt::ssize_t x, Pt::ssize_t y);
370
371 template <typename T>
372 void reset(T& view, Pt::ssize_t x, Pt::ssize_t y);
373
374 void reset(const ConstPixel& p);
375
376 void reset(const Pixel<ColorT>& p);
377
378 const PixelBase* pixelBase() const
379 { return _pixel; }
380
381 const ImageFormat& format() const
382 { return *_format; }
383
384 const ViewBase& view() const
385 { return _pixel->view(); }
386
387 const Pt::uint8_t* base() const
388 { return _pixel->base(); }
389
390 void advance()
391 {
392 _pixel->advance();
393 }
394
395 void skipPadding()
396 {
397 _pixel->skipPadding();
398 }
399
400 void advance(Pt::ssize_t n)
401 {
402 _pixel->advance(n);
403 }
404
405 void advanceLines(Pt::ssize_t n)
406 {
407 _pixel->advanceLines(n);
408 }
409
410 ColorT getColor() const
411 {
412 return _pixel->getColor<ColorT>();
413 }
414
415 void getColors(ColorT* colors, std::size_t length) const
416 {
417 _pixel->getColors(colors, length);
418 }
419
420 bool equals(const ConstPixel& p) const
421 {
422 return _pixel->base() == p.base();
423 }
424
425 bool equals(const Pixel<ColorT>& p) const
426 {
427 return _pixel->base() == p.base();
428 }
429
430 private:
431 PixelBase* _pixel;
432 PixelStorage _storage;
433 const ImageFormat* _format;
434};
435
436} // namespace
437
438} // namespace
439
440#include <Pt/Gfx/ImageFormat.hpp>
441
442#endif
Read-only cursor to one pixel.
Definition ImageFormat.h:345
Runtime pixel format.
Definition ImageFormat.h:72
Pixel base class.
Definition PixelBase.h:52
Cursor to one pixel.
Definition ImageFormat.h:219
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
Core module.
Definition Allocator.h:33
Image traits.
Definition ImageTraits.h:44