position_iterator.hpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. //
  2. // Copyright 2005-2007 Adobe Systems Incorporated
  3. //
  4. // Distributed under the Boost Software License, Version 1.0
  5. // See accompanying file LICENSE_1_0.txt or copy at
  6. // http://www.boost.org/LICENSE_1_0.txt
  7. //
  8. #ifndef BOOST_GIL_POSITION_ITERATOR_HPP
  9. #define BOOST_GIL_POSITION_ITERATOR_HPP
  10. #include <boost/gil/locator.hpp>
  11. #include <boost/iterator/iterator_facade.hpp>
  12. #include <type_traits>
  13. namespace boost { namespace gil {
  14. /// \defgroup PixelIteratorModelVirtual position_iterator
  15. /// \ingroup PixelIteratorModel
  16. /// \brief An iterator that remembers its current X,Y position and invokes a function object with it upon dereferencing.
  17. /// Models PixelIteratorConcept, PixelBasedConcept, HasDynamicXStepTypeConcept. Used to create virtual image views.
  18. /// \brief An iterator that remembers its current X,Y position and invokes a function object with it upon dereferencing.
  19. /// Used to create virtual image views.
  20. /// Models: StepIteratorConcept, PixelIteratorConcept, PixelBasedConcept, HasDynamicXStepTypeConcept
  21. /// \ingroup PixelIteratorModelVirtual PixelBasedModel
  22. /// \tparam Deref A function object that given a point returns a pixel reference. Models PixelDereferenceAdaptorConcept
  23. /// \tparam Dim Dimension to advance along
  24. template <typename Deref, int Dim>
  25. struct position_iterator : public iterator_facade<position_iterator<Deref,Dim>,
  26. typename Deref::value_type,
  27. std::random_access_iterator_tag,
  28. typename Deref::reference,
  29. typename Deref::argument_type::template axis<Dim>::coord_t> {
  30. using parent_t = iterator_facade<position_iterator<Deref,Dim>,
  31. typename Deref::value_type,
  32. std::random_access_iterator_tag,
  33. typename Deref::reference,
  34. typename Deref::argument_type::template axis<Dim>::coord_t>;
  35. using difference_type = typename parent_t::difference_type;
  36. using reference = typename parent_t::reference;
  37. using point_t = typename Deref::argument_type;
  38. position_iterator() {}
  39. position_iterator(const point_t& p, const point_t& step, const Deref& d) : _p(p), _step(step), _d(d) {}
  40. position_iterator(const position_iterator& p) : _p(p._p), _step(p._step), _d(p._d) {}
  41. template <typename D> position_iterator(const position_iterator<D,Dim>& p) : _p(p._p), _step(p._step), _d(p._d) {}
  42. position_iterator& operator=(const position_iterator& p) { _p=p._p; _d=p._d; _step=p._step; return *this; }
  43. const point_t& pos() const { return _p; }
  44. const point_t& step() const { return _step; }
  45. const Deref& deref_fn() const { return _d; }
  46. void set_step(difference_type s) { _step[Dim]=s; }
  47. /// For some reason operator[] provided by iterator_adaptor returns a custom class that is convertible to reference
  48. /// We require our own reference because it is registered in iterator_traits
  49. reference operator[](difference_type d) const { point_t p=_p; p[Dim]+=d*_step[Dim]; return _d(p); }
  50. private:
  51. point_t _p, _step;
  52. Deref _d;
  53. template <typename DE, int DI> friend struct position_iterator;
  54. friend class boost::iterator_core_access;
  55. reference dereference() const { return _d(_p); }
  56. void increment() { _p[Dim]+=_step[Dim]; }
  57. void decrement() { _p[Dim]-=_step[Dim]; }
  58. void advance(difference_type d) { _p[Dim]+=d*_step[Dim]; }
  59. difference_type distance_to(const position_iterator& it) const { return (it._p[Dim]-_p[Dim])/_step[Dim]; }
  60. bool equal(const position_iterator& it) const { return _p==it._p; }
  61. };
  62. template <typename Deref,int Dim>
  63. struct const_iterator_type<position_iterator<Deref,Dim> > {
  64. using type = position_iterator<typename Deref::const_t,Dim>;
  65. };
  66. template <typename Deref, int Dim>
  67. struct iterator_is_mutable<position_iterator<Deref, Dim>>
  68. : std::integral_constant<bool, Deref::is_mutable>
  69. {
  70. };
  71. /////////////////////////////
  72. // PixelBasedConcept
  73. /////////////////////////////
  74. template <typename Deref,int Dim>
  75. struct color_space_type<position_iterator<Deref,Dim> > : public color_space_type<typename Deref::value_type> {};
  76. template <typename Deref,int Dim>
  77. struct channel_mapping_type<position_iterator<Deref,Dim> > : public channel_mapping_type<typename Deref::value_type> {};
  78. template <typename Deref,int Dim>
  79. struct is_planar<position_iterator<Deref, Dim>> : std::false_type {};
  80. template <typename Deref,int Dim>
  81. struct channel_type<position_iterator<Deref,Dim> > : public channel_type<typename Deref::value_type> {};
  82. /////////////////////////////
  83. // HasDynamicXStepTypeConcept
  84. /////////////////////////////
  85. template <typename Deref,int Dim>
  86. struct dynamic_x_step_type<position_iterator<Deref,Dim> > {
  87. using type = position_iterator<Deref,Dim>;
  88. };
  89. } } // namespace boost::gil
  90. #endif