step_iterator.hpp 14 KB

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