iterator_from_2d.hpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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_ITERATOR_FROM_2D_HPP
  9. #define BOOST_GIL_ITERATOR_FROM_2D_HPP
  10. #include <boost/gil/concepts.hpp>
  11. #include <boost/gil/locator.hpp>
  12. #include <boost/gil/pixel_iterator.hpp>
  13. #include <boost/gil/point.hpp>
  14. #include <boost/assert.hpp>
  15. #include <boost/iterator/iterator_facade.hpp>
  16. namespace boost { namespace gil {
  17. /// pixel step iterator, pixel image iterator and pixel dereference iterator
  18. ////////////////////////////////////////////////////////////////////////////////////////
  19. ///
  20. /// ITERATOR FROM 2D ADAPTOR
  21. ///
  22. ////////////////////////////////////////////////////////////////////////////////////////
  23. /// \defgroup PixelIteratorModelFromLocator iterator_from_2d
  24. /// \ingroup PixelIteratorModel
  25. /// \brief An iterator over two-dimensional locator. Useful for iterating over the pixels of an image view. Models PixelIteratorConcept, PixelBasedConcept, HasDynamicXStepTypeConcept
  26. /// \ingroup PixelIteratorModelFromLocator PixelBasedModel
  27. /// \brief Provides 1D random-access navigation to the pixels of the image. Models: PixelIteratorConcept, PixelBasedConcept, HasDynamicXStepTypeConcept
  28. ///
  29. /// Pixels are traversed from the top to the bottom row and from the left to the right
  30. /// within each row
  31. template <typename Loc2> // Models PixelLocatorConcept
  32. class iterator_from_2d : public iterator_facade<iterator_from_2d<Loc2>,
  33. typename Loc2::value_type,
  34. std::random_access_iterator_tag,
  35. typename Loc2::reference,
  36. typename Loc2::coord_t> {
  37. GIL_CLASS_REQUIRE(Loc2, boost::gil, PixelLocatorConcept)
  38. public:
  39. using parent_t = iterator_facade<iterator_from_2d<Loc2>,
  40. typename Loc2::value_type,
  41. std::random_access_iterator_tag,
  42. typename Loc2::reference,
  43. typename Loc2::coord_t>;
  44. using reference = typename parent_t::reference;
  45. using difference_type = typename parent_t::difference_type;
  46. using x_iterator = typename Loc2::x_iterator;
  47. using point_t = typename Loc2::point_t;
  48. std::ptrdiff_t width() const { return _width; } // number of pixels per image row
  49. std::ptrdiff_t x_pos() const { return _coords.x; } // current x position
  50. std::ptrdiff_t y_pos() const { return _coords.y; } // current y position
  51. /// For some reason operator[] provided by iterator_adaptor returns a custom class that is convertible to reference
  52. /// We require our own reference because it is registered in iterator_traits
  53. reference operator[](difference_type d) const { return *(*this+d); }
  54. bool is_1d_traversable() const { return _p.is_1d_traversable(width()); } // is there no gap at the end of each row?
  55. x_iterator& x() { return _p.x(); }
  56. iterator_from_2d(){}
  57. iterator_from_2d(const Loc2& p, std::ptrdiff_t width, std::ptrdiff_t x=0, std::ptrdiff_t y=0) : _coords(x,y), _width(width), _p(p) {}
  58. iterator_from_2d(const iterator_from_2d& pit) : _coords(pit._coords), _width(pit._width), _p(pit._p) {}
  59. template <typename Loc> iterator_from_2d(const iterator_from_2d<Loc>& pit) : _coords(pit._coords), _width(pit._width), _p(pit._p) {}
  60. private:
  61. template <typename Loc> friend class iterator_from_2d;
  62. friend class boost::iterator_core_access;
  63. reference dereference() const { return *_p; }
  64. void increment() {
  65. ++_coords.x;
  66. ++_p.x();
  67. if (_coords.x>=_width) {
  68. _coords.x=0;
  69. ++_coords.y;
  70. _p+=point_t(-_width,1);
  71. }
  72. }
  73. void decrement() {
  74. --_coords.x;
  75. --_p.x();
  76. if (_coords.x<0) {
  77. _coords.x=_width-1;
  78. --_coords.y;
  79. _p+=point_t(_width,-1);
  80. }
  81. }
  82. BOOST_FORCEINLINE void advance(difference_type d) {
  83. if (_width==0) return; // unfortunately we need to check for that. Default-constructed images have width of 0 and the code below will throw if executed.
  84. point_t delta;
  85. if (_coords.x+d>=0) { // not going back to a previous row?
  86. delta.x=(_coords.x+(std::ptrdiff_t)d)%_width - _coords.x;
  87. delta.y=(_coords.x+(std::ptrdiff_t)d)/_width;
  88. } else {
  89. delta.x=(_coords.x+(std::ptrdiff_t)d*(1-_width))%_width -_coords.x;
  90. delta.y=-(_width-_coords.x-(std::ptrdiff_t)d-1)/_width;
  91. }
  92. _p+=delta;
  93. _coords.x+=delta.x;
  94. _coords.y+=delta.y;
  95. }
  96. difference_type distance_to(const iterator_from_2d& it) const {
  97. if (_width==0) return 0;
  98. return (it.y_pos()-_coords.y)*_width + (it.x_pos()-_coords.x);
  99. }
  100. bool equal(iterator_from_2d const& it) const
  101. {
  102. BOOST_ASSERT(_width == it.width()); // they must belong to the same image
  103. return _coords == it._coords && _p == it._p;
  104. }
  105. point_t _coords;
  106. std::ptrdiff_t _width;
  107. Loc2 _p;
  108. };
  109. template <typename Loc> // Models PixelLocatorConcept
  110. struct const_iterator_type<iterator_from_2d<Loc> > {
  111. using type = iterator_from_2d<typename Loc::const_t>;
  112. };
  113. template <typename Loc> // Models PixelLocatorConcept
  114. struct iterator_is_mutable<iterator_from_2d<Loc> > : public iterator_is_mutable<typename Loc::x_iterator> {};
  115. /////////////////////////////
  116. // HasDynamicXStepTypeConcept
  117. /////////////////////////////
  118. template <typename Loc>
  119. struct dynamic_x_step_type<iterator_from_2d<Loc> > {
  120. using type = iterator_from_2d<typename dynamic_x_step_type<Loc>::type>;
  121. };
  122. /////////////////////////////
  123. // PixelBasedConcept
  124. /////////////////////////////
  125. template <typename Loc> // Models PixelLocatorConcept
  126. struct color_space_type<iterator_from_2d<Loc> > : public color_space_type<Loc> {};
  127. template <typename Loc> // Models PixelLocatorConcept
  128. struct channel_mapping_type<iterator_from_2d<Loc> > : public channel_mapping_type<Loc> {};
  129. template <typename Loc> // Models PixelLocatorConcept
  130. struct is_planar<iterator_from_2d<Loc> > : public is_planar<Loc> {};
  131. template <typename Loc> // Models PixelLocatorConcept
  132. struct channel_type<iterator_from_2d<Loc> > : public channel_type<Loc> {};
  133. } } // namespace boost::gil
  134. #endif