position_iterator.hpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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(point_t const& p, point_t const& step, Deref const& d) : _p(p), _step(step), _d(d) {}
  40. position_iterator(position_iterator const& p) : _p(p._p), _step(p._step), _d(p._d) {}
  41. template <typename D>
  42. position_iterator(position_iterator<D,Dim> const& p) : _p(p._p), _step(p._step), _d(p._d) {}
  43. auto operator=(position_iterator const& p) -> position_iterator&
  44. {
  45. _p=p._p;
  46. _d=p._d;
  47. _step=p._step;
  48. return *this;
  49. }
  50. auto pos() const -> point_t const& { return _p; }
  51. auto step() const -> point_t const& { return _step; }
  52. auto deref_fn() const -> Deref const& { return _d; }
  53. void set_step(difference_type s) { _step[Dim]=s; }
  54. /// For some reason operator[] provided by iterator_adaptor returns a custom class that is convertible to reference
  55. /// We require our own reference because it is registered in iterator_traits
  56. auto operator[](difference_type d) const -> reference
  57. {
  58. point_t p=_p;
  59. p[Dim]+=d*_step[Dim];
  60. return _d(p);
  61. }
  62. private:
  63. point_t _p, _step;
  64. Deref _d;
  65. template <typename DE, int DI> friend struct position_iterator;
  66. friend class boost::iterator_core_access;
  67. reference dereference() const { return _d(_p); }
  68. void increment() { _p[Dim]+=_step[Dim]; }
  69. void decrement() { _p[Dim]-=_step[Dim]; }
  70. void advance(difference_type d) { _p[Dim]+=d*_step[Dim]; }
  71. difference_type distance_to(const position_iterator& it) const { return (it._p[Dim]-_p[Dim])/_step[Dim]; }
  72. bool equal(const position_iterator& it) const { return _p==it._p; }
  73. };
  74. template <typename Deref,int Dim>
  75. struct const_iterator_type<position_iterator<Deref,Dim> > {
  76. using type = position_iterator<typename Deref::const_t,Dim>;
  77. };
  78. template <typename Deref, int Dim>
  79. struct iterator_is_mutable<position_iterator<Deref, Dim>>
  80. : std::integral_constant<bool, Deref::is_mutable>
  81. {
  82. };
  83. /////////////////////////////
  84. // PixelBasedConcept
  85. /////////////////////////////
  86. template <typename Deref,int Dim>
  87. struct color_space_type<position_iterator<Deref,Dim> > : public color_space_type<typename Deref::value_type> {};
  88. template <typename Deref,int Dim>
  89. struct channel_mapping_type<position_iterator<Deref,Dim> > : public channel_mapping_type<typename Deref::value_type> {};
  90. template <typename Deref,int Dim>
  91. struct is_planar<position_iterator<Deref, Dim>> : std::false_type {};
  92. template <typename Deref,int Dim>
  93. struct channel_type<position_iterator<Deref,Dim> > : public channel_type<typename Deref::value_type> {};
  94. /////////////////////////////
  95. // HasDynamicXStepTypeConcept
  96. /////////////////////////////
  97. template <typename Deref,int Dim>
  98. struct dynamic_x_step_type<position_iterator<Deref,Dim> > {
  99. using type = position_iterator<Deref,Dim>;
  100. };
  101. } } // namespace boost::gil
  102. #endif