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 
42 namespace Pt {
43 
44 namespace Gfx {
45 
47 // ImageFormat
49 
53 {
54  friend struct ImageTraits<ImageFormat>;
55  friend struct ImageTraitsF;
56 
57  public:
58  explicit ImageFormat(size_t pixelStride)
59  : _pixelStride(pixelStride)
60  { }
61 
62  virtual ~ImageFormat()
63  { }
64 
65  PixelBase* createPixel(Pt::uint8_t* data, const ViewBase& view,
66  Pt::ssize_t x, Pt::ssize_t y,
67  PixelStorage& store) const
68  {
69  return onCreatePixel(data, view, x, y, store);
70  }
71 
72  bool operator==(const ImageFormat& a) const
73  {
74  return onGetType() == a.onGetType();
75  }
76 
77  bool operator!=(const ImageFormat& a) const
78  {
79  return ! (*this == a);
80  }
81 
82  const std::type_info& type() const
83  {
84  return onGetType();
85  }
86 
87  public:
88  PT_GFX_API static const ImageFormat& get(int type = 0);
89 
90  PT_GFX_API static const ImageFormat& rgb16();
91 
92  PT_GFX_API static const ImageFormat& rgb32();
93 
94  PT_GFX_API static const ImageFormat& argb32();
95 
96  protected:
97  virtual std::unique_ptr<ImageFormat> onClone() const
98  {
99  return 0;
100  }
101 
102  virtual const std::type_info& onGetType() const = 0;
103 
104  virtual std::size_t onImageSize(Pt::ssize_t width, Pt::ssize_t height,
105  std::size_t padding) const = 0;
106 
107  virtual PixelBase* onCreatePixel(Pt::uint8_t* data, const ViewBase& view,
108  Pt::ssize_t x, Pt::ssize_t y,
109  PixelStorage& store) const = 0;
110 
111  inline const void* r0() const
112  { return _r0.ptr; }
113 
114  inline const void* r1() const
115  { return _r1.ptr; }
116 
117  inline const void* r2() const
118  { return _r2.ptr; }
119 
120  private:
121  const std::size_t _pixelStride;
122  const varint_t _r0;
123  const varint_t _r1;
124  const varint_t _r2;
125 };
126 
128 // ImageTraits
130 
131 template <>
132 struct ImageTraits<ImageFormat>
133 {
134  typedef Gfx::Pixel<Color> Pixel;
135  typedef Gfx::ConstPixel<Color> ConstPixel;
136 
137  static std::size_t pixelStride(const ImageFormat& format)
138  {
139  return format._pixelStride;
140  }
141 
142  static std::size_t imageSize(const ImageFormat& format, Pt::ssize_t width, Pt::ssize_t height,
143  std::size_t padding)
144  {
145  return format.onImageSize(width, height, padding);
146  }
147 
148  static std::unique_ptr<ImageFormat> clone(const ImageFormat& format)
149  {
150  return format.onClone();
151  }
152 };
153 
154 
155 struct ImageTraitsF
156 {
157  typedef Gfx::Pixel<ColorF> Pixel;
158  typedef Gfx::ConstPixel<ColorF> ConstPixel;
159 
160  static std::size_t pixelStride(const ImageFormat& format)
161  {
162  return format._pixelStride;
163  }
164 
165  static std::size_t imageSize(const ImageFormat& format, Pt::ssize_t width, Pt::ssize_t height,
166  std::size_t padding)
167  {
168  return format.onImageSize(width, height, padding);
169  }
170 
171  static std::unique_ptr<ImageFormat> clone(const ImageFormat& format)
172  {
173  return format.onClone();
174  }
175 };
176 
178 // Pixel
180 
181 template <typename ColorT>
182 class Pixel
183 {
184  template <typename C>
185  friend class ConstPixel;
186 
187  public:
188  typedef ImageFormat FormatType;
189  typedef ColorT ColorType;
190 
191  public:
192  template <typename T>
193  Pixel(T& view, Pt::ssize_t x, Pt::ssize_t y);
194 
195  Pixel(const Pixel& p);
196 
197  ~Pixel();
198 
199  Pixel& operator=(const Pixel&) = delete;
200 
201  Pixel& operator=(const ColorT& color)
202  {
203  _pixel->assign(color);
204  return *this;
205  }
206 
207  template <typename T>
208  void reset(T& view, Pt::ssize_t x, Pt::ssize_t y);
209 
210  void reset(const Pixel& p);
211 
212  const PixelBase* pixelBase() const
213  { return _pixel; }
214 
215  PixelBase* pixelBase()
216  { return _pixel; }
217 
218  const ViewBase& view() const
219  { return _pixel->view(); }
220 
221  const ImageFormat& format() const
222  { return *_format; }
223 
224  Pt::uint8_t* base()
225  { return _pixel->base(); }
226 
227  const Pt::uint8_t* base() const
228  { return _pixel->base(); }
229 
230  void advance()
231  {
232  _pixel->advance();
233  }
234 
235  void skipPadding()
236  {
237  _pixel->skipPadding();
238  }
239 
240  void advance(Pt::ssize_t n)
241  {
242  _pixel->advance(n);
243  }
244 
245  void advanceLines(Pt::ssize_t n)
246  {
247  _pixel->advanceLines(n);
248  }
249 
250  ColorT getColor() const
251  {
252  return _pixel->getColor<ColorT>();
253  }
254 
255  void getColors(ColorT* colors, std::size_t length) const
256  {
257  _pixel->getColors(colors, length);
258  }
259 
260  void assign(const Pixel& p);
261 
262  void assign(const ConstPixel<ColorT>& p);
263 
264  void assign(const Pixel& p, std::size_t length);
265 
266  void assign(const ConstPixel<ColorT>& p, std::size_t length);
267 
268  void assign(const ColorT* colors, std::size_t length)
269  {
270  _pixel->assign(colors, length);
271  }
272 
273  void fill(std::size_t n, const ColorT& color)
274  {
275  _pixel->fill(n, color);
276  }
277 
278  bool equals(const Pixel& p) const
279  {
280  return _pixel->base() == p.base();
281  }
282 
283  bool equals(const ConstPixel<ColorT>& p) const;
284 
285  private:
286  template <typename PixelT>
287  void assignPixels(const PixelT& p, std::size_t length);
288 
289  private:
290  PixelBase* _pixel;
291  PixelStorage _storage;
292  const ImageFormat* _format;
293 };
294 
296 // ConstPixel
298 
299 template <typename ColorT>
300 class ConstPixel
301 {
302  template <typename C>
303  friend class Pixel;
304 
305  public:
306  typedef ImageFormat FormatType;
307  typedef ColorT ColorType;
308 
309  public:
310  template <typename T>
311  ConstPixel(const T& view, Pt::ssize_t x, Pt::ssize_t y);
312 
313  template <typename T>
314  ConstPixel(T& view, Pt::ssize_t x, Pt::ssize_t y);
315 
316  ConstPixel(const ConstPixel& p);
317 
318  ConstPixel(const Pixel<ColorT>& p);
319 
320  ~ConstPixel();
321 
322  ConstPixel& operator=(const ConstPixel&) = delete;
323 
324  template <typename T>
325  void reset(const T& view, Pt::ssize_t x, Pt::ssize_t y);
326 
327  template <typename T>
328  void reset(T& view, Pt::ssize_t x, Pt::ssize_t y);
329 
330  void reset(const ConstPixel& p);
331 
332  void reset(const Pixel<ColorT>& p);
333 
334  const PixelBase* pixelBase() const
335  { return _pixel; }
336 
337  const ImageFormat& format() const
338  { return *_format; }
339 
340  const ViewBase& view() const
341  { return _pixel->view(); }
342 
343  const Pt::uint8_t* base() const
344  { return _pixel->base(); }
345 
346  void advance()
347  {
348  _pixel->advance();
349  }
350 
351  void skipPadding()
352  {
353  _pixel->skipPadding();
354  }
355 
356  void advance(Pt::ssize_t n)
357  {
358  _pixel->advance(n);
359  }
360 
361  void advanceLines(Pt::ssize_t n)
362  {
363  _pixel->advanceLines(n);
364  }
365 
366  ColorT getColor() const
367  {
368  return _pixel->getColor<ColorT>();
369  }
370 
371  void getColors(ColorT* colors, std::size_t length) const
372  {
373  _pixel->getColors(colors, length);
374  }
375 
376  bool equals(const ConstPixel& p) const
377  {
378  return _pixel->base() == p.base();
379  }
380 
381  bool equals(const Pixel<ColorT>& p) const
382  {
383  return _pixel->base() == p.base();
384  }
385 
386  private:
387  PixelBase* _pixel;
388  PixelStorage _storage;
389  const ImageFormat* _format;
390 };
391 
392 } // namespace
393 
394 } // namespace
395 
396 #include <Pt/Gfx/ImageFormat.hpp>
397 
398 #endif
Core module.
Definition: pt-gfx-images.dox:14
Image format.
Definition: ImageFormat.h:53
Pixel base class.
Definition: PixelBase.h:52
Image traits.
Definition: ImageTraits.h:44
uint_type uint8_t
Unsigned 8-bit integer type.
Definition: Types.h:18