29 #ifndef PT_GFX_PIXEL_VIEW_H
30 #define PT_GFX_PIXEL_VIEW_H
32 #include <Pt/Gfx/Api.h>
33 #include <Pt/Gfx/View.h>
43 template <
typename FormatT,
typename TraitsT>
46 template <
typename FormatT,
typename TraitsT>
47 class ConstPixelIterator;
50 template <
typename FormatT,
typename TraitsT>
53 template <
typename F,
typename T>
54 friend class ConstPixelIterator;
57 typedef FormatT Format;
58 typedef TraitsT Traits;
60 typedef typename Traits::Pixel Pixel;
61 typedef typename Traits::ConstPixel ConstPixel;
63 typedef ConstPixelIterator<Format, Traits> ConstIterator;
66 using iterator_category = std::forward_iterator_tag;
67 using value_type = Pixel;
68 using difference_type = std::ptrdiff_t;
69 using pointer = Pixel*;
70 using reference = Pixel&;
74 PixelIterator(T& view, Pt::ssize_t x, Pt::ssize_t y)
80 PixelIterator(
const PixelIterator& it)
86 PixelIterator& operator=(
const PixelIterator& it)
88 _pixel.reset(it._pixel);
94 bool operator!=(
const PixelIterator& it)
const
95 {
return ! _pixel.equals(it._pixel); }
97 bool operator!=(
const ConstIterator& it)
const
98 {
return ! _pixel.equals(it._pixel); }
100 bool operator==(
const PixelIterator& it)
const
101 {
return _pixel.equals(it._pixel); }
103 bool operator==(
const ConstIterator& it)
const
104 {
return _pixel.equals(it._pixel); }
112 PixelIterator& operator++()
116 if( ++_x >= _pixel.view().width() )
121 _pixel.skipPadding();
127 PixelIterator& operator+=(Pt::ssize_t n)
129 Pt::ssize_t off = _x + n;
130 std::size_t dy = off / _pixel.view().width();
131 std::size_t dx = off % _pixel.view().width() - _x;
135 _pixel.advanceLines(dy);
152 template <
typename FormatT,
typename TraitsT>
153 class ConstPixelIterator
155 template <
typename F,
typename T>
156 friend class PixelIterator;
159 typedef FormatT Format;
160 typedef TraitsT Traits;
162 typedef typename Traits::Pixel Pixel;
163 typedef typename Traits::ConstPixel ConstPixel;
165 typedef PixelIterator<Format, Traits> Iterator;
168 using iterator_category = std::forward_iterator_tag;
169 using value_type = ConstPixel;
170 using difference_type = std::ptrdiff_t;
171 using pointer = ConstPixel*;
172 using reference = ConstPixel&;
175 template <
typename T>
176 ConstPixelIterator(
const T& view,
177 Pt::ssize_t x, Pt::ssize_t y)
183 template <
typename T>
184 ConstPixelIterator(T& view,
185 Pt::ssize_t x, Pt::ssize_t y)
191 ConstPixelIterator(
const ConstPixelIterator& it)
197 ConstPixelIterator& operator=(
const ConstPixelIterator& it)
199 _pixel.reset(it._pixel);
205 bool operator!=(
const ConstPixelIterator& it)
const
206 {
return ! _pixel.equals(it._pixel); }
208 bool operator!=(
const Iterator& it)
const
209 {
return ! _pixel.equals(it._pixel); }
211 bool operator==(
const ConstPixelIterator& it)
const
212 {
return _pixel.equals(it._pixel); }
214 bool operator==(
const Iterator& it)
const
215 {
return _pixel.equals(it._pixel); }
217 const ConstPixel& operator*()
const
220 const ConstPixel* operator->()
const
223 ConstPixelIterator& operator++()
227 if( ++_x >= _pixel.view().width() )
232 _pixel.skipPadding();
238 ConstPixelIterator& operator+=(Pt::ssize_t n)
240 Pt::ssize_t off = _x + n;
241 std::size_t dy = off / _pixel.view().width();
242 std::size_t dx = off % _pixel.view().width() - _x;
246 _pixel.advanceLines(dy);
263 template <
typename FormatT,
typename TraitsT>
267 typedef FormatT Format;
268 typedef TraitsT Traits;
270 typedef typename Traits::Pixel Pixel;
271 typedef typename Traits::ConstPixel ConstPixel;
273 typedef PixelIterator<Format, Traits> Iterator;
276 explicit BasicPixelView(
const Format& format = FormatT::get() );
278 BasicPixelView(
Pt::uint8_t* data, Pt::ssize_t width, Pt::ssize_t height,
279 Pt::ssize_t padding = 0,
const Format& format = FormatT::get());
281 template <
typename T>
282 explicit BasicPixelView(T& source);
284 template <
typename T>
285 BasicPixelView(T& source, Int x, Int y, Int w, Int h);
287 BasicPixelView& operator=(
const BasicPixelView&) =
default;
290 {
return _view.data(); }
293 {
return _view.data(); }
295 Pt::ssize_t width()
const
296 {
return _view.width(); }
298 Pt::ssize_t height()
const
299 {
return _view.height(); }
301 Pt::ssize_t stride()
const
302 {
return _view.stride(); }
304 const Format& format()
const
305 {
return _view.format(); }
307 Pt::ssize_t pixelStride()
const
308 {
return _view.pixelStride(); }
310 Pt::ssize_t padding()
const
311 {
return _view.padding(); }
313 Iterator pixel(Pt::ssize_t x, Pt::ssize_t y)
314 {
return Iterator(_view, x, y); }
317 {
return Iterator(_view, 0, 0); }
320 {
return Iterator(_view, 0, _view.height()); }
323 BasicView<Format, Traits> _view;
327 template <
typename FormatT,
typename TraitsT>
328 class BasicConstPixelView
331 typedef FormatT Format;
332 typedef TraitsT Traits;
334 typedef typename Traits::Pixel Pixel;
335 typedef typename Traits::ConstPixel ConstPixel;
337 typedef ConstPixelIterator<Format, Traits> Iterator;
340 explicit BasicConstPixelView(
const Format& format = FormatT::get() );
342 BasicConstPixelView(
const Pt::uint8_t* data, Pt::ssize_t width, Pt::ssize_t height,
343 Pt::ssize_t padding,
const Format& format = FormatT::get());
345 template <
typename T>
346 explicit BasicConstPixelView(
const T& source);
348 template <
typename T>
349 BasicConstPixelView(
const T& source, Int x, Int y, Int w, Int h);
351 BasicConstPixelView& operator=(
const BasicConstPixelView&) =
default;
354 {
return _view.data(); }
356 Pt::ssize_t width()
const
357 {
return _view.width(); }
359 Pt::ssize_t height()
const
360 {
return _view.height(); }
362 Pt::ssize_t stride()
const
363 {
return _view.stride(); }
365 const Format& format()
const
366 {
return _view.format(); }
368 Pt::ssize_t pixelStride()
const
369 {
return _view.pixelStride(); }
371 Pt::ssize_t padding()
const
372 {
return _view.padding(); }
374 Iterator pixel(Pt::ssize_t x, Pt::ssize_t y)
const
375 {
return Iterator(_view, x, y); }
377 Iterator begin()
const
378 {
return Iterator(_view, 0, 0); }
381 {
return Iterator(_view, 0, _view.height()); }
384 BasicConstView<Format, Traits> _view;
388 template <
typename T>
389 BasicPixelView<
typename T::Format,
390 typename T::Traits> pixelView(T& source)
392 return BasicPixelView<typename T::Format, typename T::Traits>(source);
395 template <
typename T>
396 BasicConstPixelView<
typename T::Format,
397 typename T::Traits> pixelView(
const T& source)
399 return BasicConstPixelView<typename T::Format, typename T::Traits>(source);
403 template <
typename T>
404 BasicPixelView<
typename T::Format,
405 typename T::Traits> pixelView(T& source, Int x, Int y, Int w, Int h)
407 return BasicPixelView<typename T::Format, typename T::Traits>(source, x, y, w, h);
411 template <
typename T>
412 BasicConstPixelView<
typename T::Format,
413 typename T::Traits> pixelView(
const T& source, Int x, Int y, Int w, Int h)
415 return BasicConstPixelView<typename T::Format, typename T::Traits>(source, x, y, w, h);
419 template <
typename FormatT,
typename TraitsT = ImageTraits<FormatT> >
420 BasicPixelView<FormatT,
421 TraitsT> pixelView(
Pt::uint8_t* data, Pt::ssize_t width,
422 Pt::ssize_t height, Pt::ssize_t padding = 0)
424 return BasicPixelView<FormatT, TraitsT>(data, width, height, padding, FormatT::get());
428 template <
typename FormatT,
typename TraitsT = ImageTraits<FormatT> >
429 BasicConstPixelView<FormatT,
430 TraitsT> pixelView(
const Pt::uint8_t* data, Pt::ssize_t width,
431 Pt::ssize_t height, Pt::ssize_t padding = 0)
433 return BasicConstPixelView<FormatT, TraitsT>(data, width, height, padding, FormatT::get());
437 template <
typename T>
438 BasicPixelView<
typename T::Format,
439 ImageTraitsF> pixelViewF(T& source)
441 return BasicPixelView<typename T::Format, ImageTraitsF>(source);
445 template <
typename T>
446 BasicConstPixelView<
typename T::Format,
447 ImageTraitsF> pixelViewF(
const T& source)
449 return BasicConstPixelView<typename T::Format, ImageTraitsF>(source);
453 template <
typename T>
454 BasicPixelView<
typename T::Format,
455 ImageTraitsF> pixelViewF(T& source, Int x, Int y, Int w, Int h)
457 return BasicPixelView<typename T::Format, ImageTraitsF>(source, x, y, w, h);
461 template <
typename T>
462 BasicConstPixelView<
typename T::Format,
463 ImageTraitsF> pixelViewF(
const T& source, Int x, Int y, Int w, Int h)
465 return BasicConstPixelView<typename T::Format, ImageTraitsF>(source, x, y, w, h);
469 inline PixelViewF pixelViewF(
Pt::uint8_t* data, Pt::ssize_t width,
470 Pt::ssize_t height, Pt::ssize_t padding,
471 const ImageFormat& format)
473 return PixelViewF(data, width, height, padding, format);
477 inline ConstPixelViewF pixelViewF(
const Pt::uint8_t* data, Pt::ssize_t width,
478 Pt::ssize_t height, Pt::ssize_t padding,
479 const ImageFormat& format)
481 return ConstPixelViewF(data, width, height, padding, format);
490 #include <Pt/Gfx/PixelView.hpp>
uint_type uint8_t
Unsigned 8-bit integer type.
Definition: Types.h:18