step_iterator.hpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  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_STEP_ITERATOR_HPP
  9. #define BOOST_GIL_STEP_ITERATOR_HPP
  10. #include <boost/gil/dynamic_step.hpp>
  11. #include <boost/gil/pixel_iterator.hpp>
  12. #include <boost/gil/pixel_iterator_adaptor.hpp>
  13. #include <boost/gil/utilities.hpp>
  14. #include <boost/iterator/iterator_facade.hpp>
  15. #include <cstddef>
  16. #include <iterator>
  17. #include <type_traits>
  18. namespace boost { namespace gil {
  19. /// \defgroup PixelIteratorModelStepPtr step iterators
  20. /// \ingroup PixelIteratorModel
  21. /// \brief Iterators that allow for specifying the step between two adjacent values
  22. namespace detail {
  23. /// \ingroup PixelIteratorModelStepPtr
  24. /// \brief An adaptor over an existing iterator that changes the step unit
  25. ///
  26. /// (i.e. distance(it,it+1)) by a given predicate. Instead of calling base's
  27. /// operators ++, --, +=, -=, etc. the adaptor is using the passed policy object SFn
  28. /// for advancing and for computing the distance between iterators.
  29. template <typename Derived, // type of the derived class
  30. typename Iterator, // Models Iterator
  31. typename SFn> // A policy object that can compute the distance between two iterators of type Iterator
  32. // and can advance an iterator of type Iterator a given number of Iterator's units
  33. class step_iterator_adaptor : public iterator_adaptor<Derived, Iterator, use_default, use_default, use_default, typename SFn::difference_type>
  34. {
  35. public:
  36. using parent_t = iterator_adaptor<Derived, Iterator, use_default, use_default, use_default, typename SFn::difference_type>;
  37. using base_difference_type = typename std::iterator_traits<Iterator>::difference_type;
  38. using difference_type = typename SFn::difference_type;
  39. using reference = typename std::iterator_traits<Iterator>::reference;
  40. step_iterator_adaptor() {}
  41. step_iterator_adaptor(const Iterator& it, SFn step_fn=SFn()) : parent_t(it), _step_fn(step_fn) {}
  42. difference_type step() const { return _step_fn.step(); }
  43. protected:
  44. SFn _step_fn;
  45. private:
  46. friend class boost::iterator_core_access;
  47. void increment() { _step_fn.advance(this->base_reference(),1); }
  48. void decrement() { _step_fn.advance(this->base_reference(),-1); }
  49. void advance(base_difference_type d) { _step_fn.advance(this->base_reference(),d); }
  50. difference_type distance_to(const step_iterator_adaptor& it) const { return _step_fn.difference(this->base_reference(),it.base_reference()); }
  51. };
  52. // although iterator_adaptor defines these, the default implementation computes distance and compares for zero.
  53. // it is often faster to just apply the relation operator to the base
  54. template <typename D,typename Iterator,typename SFn> inline
  55. bool operator>(const step_iterator_adaptor<D,Iterator,SFn>& p1, const step_iterator_adaptor<D,Iterator,SFn>& p2) {
  56. return p1.step()>0 ? p1.base()> p2.base() : p1.base()< p2.base();
  57. }
  58. template <typename D,typename Iterator,typename SFn> inline
  59. bool operator<(const step_iterator_adaptor<D,Iterator,SFn>& p1, const step_iterator_adaptor<D,Iterator,SFn>& p2) {
  60. return p1.step()>0 ? p1.base()< p2.base() : p1.base()> p2.base();
  61. }
  62. template <typename D,typename Iterator,typename SFn> inline
  63. bool operator>=(const step_iterator_adaptor<D,Iterator,SFn>& p1, const step_iterator_adaptor<D,Iterator,SFn>& p2) {
  64. return p1.step()>0 ? p1.base()>=p2.base() : p1.base()<=p2.base();
  65. }
  66. template <typename D,typename Iterator,typename SFn> inline
  67. bool operator<=(const step_iterator_adaptor<D,Iterator,SFn>& p1, const step_iterator_adaptor<D,Iterator,SFn>& p2) {
  68. return p1.step()>0 ? p1.base()<=p2.base() : p1.base()>=p2.base();
  69. }
  70. template <typename D,typename Iterator,typename SFn> inline
  71. bool operator==(const step_iterator_adaptor<D,Iterator,SFn>& p1, const step_iterator_adaptor<D,Iterator,SFn>& p2) {
  72. return p1.base()==p2.base();
  73. }
  74. template <typename D,typename Iterator,typename SFn> inline
  75. bool operator!=(const step_iterator_adaptor<D,Iterator,SFn>& p1, const step_iterator_adaptor<D,Iterator,SFn>& p2) {
  76. return p1.base()!=p2.base();
  77. }
  78. } // namespace detail
  79. ////////////////////////////////////////////////////////////////////////////////////////
  80. /// MEMORY-BASED STEP ITERATOR
  81. ////////////////////////////////////////////////////////////////////////////////////////
  82. /// \class memory_based_step_iterator
  83. /// \ingroup PixelIteratorModelStepPtr PixelBasedModel
  84. /// \brief Iterator with dynamically specified step in memory units (bytes or bits). Models StepIteratorConcept, IteratorAdaptorConcept, MemoryBasedIteratorConcept, PixelIteratorConcept, HasDynamicXStepTypeConcept
  85. ///
  86. /// A refinement of step_iterator_adaptor that uses a dynamic parameter for the step
  87. /// which is specified in memory units, such as bytes or bits
  88. ///
  89. /// Pixel step iterators are used to provide iteration over non-adjacent pixels.
  90. /// Common use is a vertical traversal, where the step is the row stride.
  91. ///
  92. /// Another application is as a sub-channel view. For example, a red intensity image over
  93. /// interleaved RGB data would use a step iterator adaptor with step sizeof(channel_t)*3
  94. /// In the latter example the step size could be fixed at compile time for efficiency.
  95. /// Compile-time fixed step can be implemented by providing a step function object that takes the step as a template
  96. ////////////////////////////////////////////////////////////////////////////////////////
  97. /// \ingroup PixelIteratorModelStepPtr
  98. /// \brief function object that returns the memory unit distance between two iterators and advances a given iterator a given number of mem units (bytes or bits)
  99. template <typename Iterator>
  100. struct memunit_step_fn {
  101. using difference_type = std::ptrdiff_t;
  102. memunit_step_fn(difference_type step=memunit_step(Iterator())) : _step(step) {}
  103. difference_type difference(const Iterator& it1, const Iterator& it2) const { return memunit_distance(it1,it2)/_step; }
  104. void advance(Iterator& it, difference_type d) const { memunit_advance(it,d*_step); }
  105. difference_type step() const { return _step; }
  106. void set_step(std::ptrdiff_t step) { _step=step; }
  107. private:
  108. BOOST_GIL_CLASS_REQUIRE(Iterator, boost::gil, MemoryBasedIteratorConcept)
  109. difference_type _step;
  110. };
  111. template <typename Iterator>
  112. class memory_based_step_iterator : public detail::step_iterator_adaptor<memory_based_step_iterator<Iterator>,
  113. Iterator,
  114. memunit_step_fn<Iterator>>
  115. {
  116. BOOST_GIL_CLASS_REQUIRE(Iterator, boost::gil, MemoryBasedIteratorConcept)
  117. public:
  118. using parent_t = detail::step_iterator_adaptor<memory_based_step_iterator<Iterator>,
  119. Iterator,
  120. memunit_step_fn<Iterator>>;
  121. using reference = typename parent_t::reference;
  122. using difference_type = typename parent_t::difference_type;
  123. using x_iterator = Iterator;
  124. memory_based_step_iterator() : parent_t(Iterator()) {}
  125. memory_based_step_iterator(Iterator it, std::ptrdiff_t memunit_step) : parent_t(it, memunit_step_fn<Iterator>(memunit_step)) {}
  126. template <typename I2>
  127. memory_based_step_iterator(const memory_based_step_iterator<I2>& it)
  128. : parent_t(it.base(), memunit_step_fn<Iterator>(it.step())) {}
  129. /// For some reason operator[] provided by iterator_adaptor returns a custom class that is convertible to reference
  130. /// We require our own reference because it is registered in iterator_traits
  131. reference operator[](difference_type d) const { return *(*this+d); }
  132. void set_step(std::ptrdiff_t memunit_step) { this->_step_fn.set_step(memunit_step); }
  133. x_iterator& base() { return parent_t::base_reference(); }
  134. x_iterator const& base() const { return parent_t::base_reference(); }
  135. };
  136. template <typename Iterator>
  137. struct const_iterator_type<memory_based_step_iterator<Iterator>> {
  138. using type = memory_based_step_iterator<typename const_iterator_type<Iterator>::type>;
  139. };
  140. template <typename Iterator>
  141. struct iterator_is_mutable<memory_based_step_iterator<Iterator>> : public iterator_is_mutable<Iterator> {};
  142. /////////////////////////////
  143. // IteratorAdaptorConcept
  144. /////////////////////////////
  145. template <typename Iterator>
  146. struct is_iterator_adaptor<memory_based_step_iterator<Iterator>> : std::true_type {};
  147. template <typename Iterator>
  148. struct iterator_adaptor_get_base<memory_based_step_iterator<Iterator>>
  149. {
  150. using type = Iterator;
  151. };
  152. template <typename Iterator, typename NewBaseIterator>
  153. struct iterator_adaptor_rebind<memory_based_step_iterator<Iterator>, NewBaseIterator>
  154. {
  155. using type = memory_based_step_iterator<NewBaseIterator>;
  156. };
  157. /////////////////////////////
  158. // PixelBasedConcept
  159. /////////////////////////////
  160. template <typename Iterator>
  161. struct color_space_type<memory_based_step_iterator<Iterator>> : public color_space_type<Iterator> {};
  162. template <typename Iterator>
  163. struct channel_mapping_type<memory_based_step_iterator<Iterator>> : public channel_mapping_type<Iterator> {};
  164. template <typename Iterator>
  165. struct is_planar<memory_based_step_iterator<Iterator>> : public is_planar<Iterator> {};
  166. template <typename Iterator>
  167. struct channel_type<memory_based_step_iterator<Iterator>> : public channel_type<Iterator> {};
  168. /////////////////////////////
  169. // MemoryBasedIteratorConcept
  170. /////////////////////////////
  171. template <typename Iterator>
  172. struct byte_to_memunit<memory_based_step_iterator<Iterator>> : public byte_to_memunit<Iterator> {};
  173. template <typename Iterator>
  174. inline std::ptrdiff_t memunit_step(const memory_based_step_iterator<Iterator>& p) { return p.step(); }
  175. template <typename Iterator>
  176. inline std::ptrdiff_t memunit_distance(const memory_based_step_iterator<Iterator>& p1,
  177. const memory_based_step_iterator<Iterator>& p2) {
  178. return memunit_distance(p1.base(),p2.base());
  179. }
  180. template <typename Iterator>
  181. inline void memunit_advance(memory_based_step_iterator<Iterator>& p,
  182. std::ptrdiff_t diff) {
  183. memunit_advance(p.base(), diff);
  184. }
  185. template <typename Iterator>
  186. inline memory_based_step_iterator<Iterator>
  187. memunit_advanced(const memory_based_step_iterator<Iterator>& p,
  188. std::ptrdiff_t diff) {
  189. return memory_based_step_iterator<Iterator>(memunit_advanced(p.base(), diff),p.step());
  190. }
  191. template <typename Iterator>
  192. inline typename std::iterator_traits<Iterator>::reference
  193. memunit_advanced_ref(const memory_based_step_iterator<Iterator>& p,
  194. std::ptrdiff_t diff) {
  195. return memunit_advanced_ref(p.base(), diff);
  196. }
  197. /////////////////////////////
  198. // HasDynamicXStepTypeConcept
  199. /////////////////////////////
  200. template <typename Iterator>
  201. struct dynamic_x_step_type<memory_based_step_iterator<Iterator>> {
  202. using type = memory_based_step_iterator<Iterator>;
  203. };
  204. // For step iterators, pass the function object to the base
  205. template <typename Iterator, typename Deref>
  206. struct iterator_add_deref<memory_based_step_iterator<Iterator>,Deref> {
  207. BOOST_GIL_CLASS_REQUIRE(Deref, boost::gil, PixelDereferenceAdaptorConcept)
  208. using type = memory_based_step_iterator<typename iterator_add_deref<Iterator, Deref>::type>;
  209. static type make(const memory_based_step_iterator<Iterator>& it, const Deref& d) { return type(iterator_add_deref<Iterator, Deref>::make(it.base(),d),it.step()); }
  210. };
  211. ////////////////////////////////////////////////////////////////////////////////////////
  212. /// make_step_iterator
  213. ////////////////////////////////////////////////////////////////////////////////////////
  214. template <typename I> typename dynamic_x_step_type<I>::type make_step_iterator(const I& it, std::ptrdiff_t step);
  215. namespace detail {
  216. // if the iterator is a plain base iterator (non-adaptor), wraps it in memory_based_step_iterator
  217. template <typename I>
  218. auto make_step_iterator_impl(I const& it, std::ptrdiff_t step, std::false_type)
  219. -> typename dynamic_x_step_type<I>::type
  220. {
  221. return memory_based_step_iterator<I>(it, step);
  222. }
  223. // If the iterator is compound, put the step in its base
  224. template <typename I>
  225. auto make_step_iterator_impl(I const& it, std::ptrdiff_t step, std::true_type)
  226. -> typename dynamic_x_step_type<I>::type
  227. {
  228. return make_step_iterator(it.base(), step);
  229. }
  230. // If the iterator is memory_based_step_iterator, change the step
  231. template <typename BaseIt>
  232. auto make_step_iterator_impl(
  233. memory_based_step_iterator<BaseIt> const& it,
  234. std::ptrdiff_t step,
  235. std::true_type)
  236. -> memory_based_step_iterator<BaseIt>
  237. {
  238. return memory_based_step_iterator<BaseIt>(it.base(), step);
  239. }
  240. } // namespace detail
  241. /// \brief Constructs a step iterator from a base iterator and a step.
  242. ///
  243. /// To construct a step iterator from a given iterator Iterator and a given step, if Iterator does not
  244. /// already have a dynamic step, we wrap it in a memory_based_step_iterator. Otherwise we
  245. /// do a compile-time traversal of the chain of iterator adaptors to locate the step iterator
  246. /// and then set it step to the new one.
  247. ///
  248. /// The step iterator of Iterator is not always memory_based_step_iterator<Iterator>. For example, Iterator may
  249. /// already be a memory_based_step_iterator, in which case it will be inefficient to stack them;
  250. /// we can obtain the same result by multiplying their steps. Note that for Iterator to be a
  251. /// step iterator it does not necessarily have to have the form memory_based_step_iterator<J>.
  252. /// The step iterator can be wrapped inside another iterator. Also, it may not have the
  253. /// type memory_based_step_iterator, but it could be a user-provided type.
  254. template <typename I> // Models MemoryBasedIteratorConcept, HasDynamicXStepTypeConcept
  255. typename dynamic_x_step_type<I>::type make_step_iterator(const I& it, std::ptrdiff_t step) {
  256. return detail::make_step_iterator_impl(it, step, typename is_iterator_adaptor<I>::type());
  257. }
  258. }} // namespace boost::gil
  259. #endif