step_iterator.hpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. /*
  2. Copyright 2005-2007 Adobe Systems Incorporated
  3. Use, modification and distribution are subject to the Boost Software License,
  4. Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  5. http://www.boost.org/LICENSE_1_0.txt).
  6. See http://opensource.adobe.com/gil for most recent version including documentation.
  7. */
  8. /*************************************************************************************************/
  9. #ifndef GIL_STEP_ITERATOR_H
  10. #define GIL_STEP_ITERATOR_H
  11. ////////////////////////////////////////////////////////////////////////////////////////
  12. /// \file
  13. /// \brief pixel step iterator
  14. /// \author Lubomir Bourdev and Hailin Jin \n
  15. /// Adobe Systems Incorporated
  16. /// \date 2005-2007 \n Last updated on September 18, 2007
  17. ///
  18. ////////////////////////////////////////////////////////////////////////////////////////
  19. #include <cstddef>
  20. #include <iterator>
  21. #include <boost/iterator/iterator_facade.hpp>
  22. #include "gil_config.hpp"
  23. #include "utilities.hpp"
  24. #include "pixel_iterator.hpp"
  25. #include "pixel_iterator_adaptor.hpp"
  26. namespace boost { namespace gil {
  27. /// \defgroup PixelIteratorModelStepPtr step iterators
  28. /// \ingroup PixelIteratorModel
  29. /// \brief Iterators that allow for specifying the step between two adjacent values
  30. namespace detail {
  31. /// \ingroup PixelIteratorModelStepPtr
  32. /// \brief An adaptor over an existing iterator that changes the step unit
  33. ///
  34. /// (i.e. distance(it,it+1)) by a given predicate. Instead of calling base's
  35. /// operators ++, --, +=, -=, etc. the adaptor is using the passed policy object SFn
  36. /// for advancing and for computing the distance between iterators.
  37. template <typename Derived, // type of the derived class
  38. typename Iterator, // Models Iterator
  39. typename SFn> // A policy object that can compute the distance between two iterators of type Iterator
  40. // and can advance an iterator of type Iterator a given number of Iterator's units
  41. class step_iterator_adaptor : public iterator_adaptor<Derived, Iterator, use_default, use_default, use_default, typename SFn::difference_type> {
  42. public:
  43. typedef iterator_adaptor<Derived, Iterator, use_default, use_default, use_default, typename SFn::difference_type> parent_t;
  44. typedef typename std::iterator_traits<Iterator>::difference_type base_difference_type;
  45. typedef typename SFn::difference_type difference_type;
  46. typedef typename std::iterator_traits<Iterator>::reference reference;
  47. step_iterator_adaptor() {}
  48. step_iterator_adaptor(const Iterator& it, SFn step_fn=SFn()) : parent_t(it), _step_fn(step_fn) {}
  49. difference_type step() const { return _step_fn.step(); }
  50. protected:
  51. SFn _step_fn;
  52. private:
  53. friend class boost::iterator_core_access;
  54. void increment() { _step_fn.advance(this->base_reference(),1); }
  55. void decrement() { _step_fn.advance(this->base_reference(),-1); }
  56. void advance(base_difference_type d) { _step_fn.advance(this->base_reference(),d); }
  57. difference_type distance_to(const step_iterator_adaptor& it) const { return _step_fn.difference(this->base_reference(),it.base_reference()); }
  58. };
  59. // although iterator_adaptor defines these, the default implementation computes distance and compares for zero.
  60. // it is often faster to just apply the relation operator to the base
  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.step()>0 ? p1.base()<=p2.base() : 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. template <typename D,typename Iterator,typename SFn> inline
  82. bool operator!=(const step_iterator_adaptor<D,Iterator,SFn>& p1, const step_iterator_adaptor<D,Iterator,SFn>& p2) {
  83. return p1.base()!=p2.base();
  84. }
  85. } // namespace detail
  86. ////////////////////////////////////////////////////////////////////////////////////////
  87. /// MEMORY-BASED STEP ITERATOR
  88. ////////////////////////////////////////////////////////////////////////////////////////
  89. /// \class memory_based_step_iterator
  90. /// \ingroup PixelIteratorModelStepPtr PixelBasedModel
  91. /// \brief Iterator with dynamically specified step in memory units (bytes or bits). Models StepIteratorConcept, IteratorAdaptorConcept, MemoryBasedIteratorConcept, PixelIteratorConcept, HasDynamicXStepTypeConcept
  92. ///
  93. /// A refinement of step_iterator_adaptor that uses a dynamic parameter for the step
  94. /// which is specified in memory units, such as bytes or bits
  95. ///
  96. /// Pixel step iterators are used to provide iteration over non-adjacent pixels.
  97. /// Common use is a vertical traversal, where the step is the row stride.
  98. ///
  99. /// Another application is as a sub-channel view. For example, a red intensity image over
  100. /// interleaved RGB data would use a step iterator adaptor with step sizeof(channel_t)*3
  101. /// In the latter example the step size could be fixed at compile time for efficiency.
  102. /// Compile-time fixed step can be implemented by providing a step function object that takes the step as a template
  103. ////////////////////////////////////////////////////////////////////////////////////////
  104. /// \ingroup PixelIteratorModelStepPtr
  105. /// \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)
  106. template <typename Iterator>
  107. struct memunit_step_fn {
  108. typedef std::ptrdiff_t difference_type;
  109. memunit_step_fn(difference_type step=memunit_step(Iterator())) : _step(step) {}
  110. difference_type difference(const Iterator& it1, const Iterator& it2) const { return memunit_distance(it1,it2)/_step; }
  111. void advance(Iterator& it, difference_type d) const { memunit_advance(it,d*_step); }
  112. difference_type step() const { return _step; }
  113. void set_step(std::ptrdiff_t step) { _step=step; }
  114. private:
  115. GIL_CLASS_REQUIRE(Iterator, boost::gil, MemoryBasedIteratorConcept)
  116. difference_type _step;
  117. };
  118. template <typename Iterator>
  119. class memory_based_step_iterator : public detail::step_iterator_adaptor<memory_based_step_iterator<Iterator>,
  120. Iterator,
  121. memunit_step_fn<Iterator> > {
  122. GIL_CLASS_REQUIRE(Iterator, boost::gil, MemoryBasedIteratorConcept)
  123. public:
  124. typedef detail::step_iterator_adaptor<memory_based_step_iterator<Iterator>,
  125. Iterator,
  126. memunit_step_fn<Iterator> > parent_t;
  127. typedef typename parent_t::reference reference;
  128. typedef typename parent_t::difference_type difference_type;
  129. typedef Iterator x_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. reference operator[](difference_type d) const { return *(*this+d); }
  138. void set_step(std::ptrdiff_t memunit_step) { this->_step_fn.set_step(memunit_step); }
  139. x_iterator& base() { return parent_t::base_reference(); }
  140. x_iterator const& base() const { return parent_t::base_reference(); }
  141. };
  142. template <typename Iterator>
  143. struct const_iterator_type<memory_based_step_iterator<Iterator> > {
  144. typedef memory_based_step_iterator<typename const_iterator_type<Iterator>::type> 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> > : public mpl::true_{};
  153. template <typename Iterator>
  154. struct iterator_adaptor_get_base<memory_based_step_iterator<Iterator> > {
  155. typedef Iterator type;
  156. };
  157. template <typename Iterator, typename NewBaseIterator>
  158. struct iterator_adaptor_rebind<memory_based_step_iterator<Iterator>,NewBaseIterator> {
  159. typedef memory_based_step_iterator<NewBaseIterator> type;
  160. };
  161. /////////////////////////////
  162. // PixelBasedConcept
  163. /////////////////////////////
  164. template <typename Iterator>
  165. struct color_space_type<memory_based_step_iterator<Iterator> > : public color_space_type<Iterator> {};
  166. template <typename Iterator>
  167. struct channel_mapping_type<memory_based_step_iterator<Iterator> > : public channel_mapping_type<Iterator> {};
  168. template <typename Iterator>
  169. struct is_planar<memory_based_step_iterator<Iterator> > : public is_planar<Iterator> {};
  170. template <typename Iterator>
  171. struct channel_type<memory_based_step_iterator<Iterator> > : public channel_type<Iterator> {};
  172. /////////////////////////////
  173. // MemoryBasedIteratorConcept
  174. /////////////////////////////
  175. template <typename Iterator>
  176. struct byte_to_memunit<memory_based_step_iterator<Iterator> > : public byte_to_memunit<Iterator> {};
  177. template <typename Iterator>
  178. inline std::ptrdiff_t memunit_step(const memory_based_step_iterator<Iterator>& p) { return p.step(); }
  179. template <typename Iterator>
  180. inline std::ptrdiff_t memunit_distance(const memory_based_step_iterator<Iterator>& p1,
  181. const memory_based_step_iterator<Iterator>& p2) {
  182. return memunit_distance(p1.base(),p2.base());
  183. }
  184. template <typename Iterator>
  185. inline void memunit_advance(memory_based_step_iterator<Iterator>& p,
  186. std::ptrdiff_t diff) {
  187. memunit_advance(p.base(), diff);
  188. }
  189. template <typename Iterator>
  190. inline memory_based_step_iterator<Iterator>
  191. memunit_advanced(const memory_based_step_iterator<Iterator>& p,
  192. std::ptrdiff_t diff) {
  193. return memory_based_step_iterator<Iterator>(memunit_advanced(p.base(), diff),p.step());
  194. }
  195. template <typename Iterator>
  196. inline typename std::iterator_traits<Iterator>::reference
  197. memunit_advanced_ref(const memory_based_step_iterator<Iterator>& p,
  198. std::ptrdiff_t diff) {
  199. return memunit_advanced_ref(p.base(), diff);
  200. }
  201. /////////////////////////////
  202. // HasDynamicXStepTypeConcept
  203. /////////////////////////////
  204. template <typename Iterator>
  205. struct dynamic_x_step_type<memory_based_step_iterator<Iterator> > {
  206. typedef memory_based_step_iterator<Iterator> type;
  207. };
  208. // For step iterators, pass the function object to the base
  209. template <typename Iterator, typename Deref>
  210. struct iterator_add_deref<memory_based_step_iterator<Iterator>,Deref> {
  211. GIL_CLASS_REQUIRE(Deref, boost::gil, PixelDereferenceAdaptorConcept)
  212. typedef memory_based_step_iterator<typename iterator_add_deref<Iterator, Deref>::type> type;
  213. 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()); }
  214. };
  215. ////////////////////////////////////////////////////////////////////////////////////////
  216. /// make_step_iterator
  217. ////////////////////////////////////////////////////////////////////////////////////////
  218. template <typename I> typename dynamic_x_step_type<I>::type make_step_iterator(const I& it, std::ptrdiff_t step);
  219. namespace detail {
  220. // if the iterator is a plain base iterator (non-adaptor), wraps it in memory_based_step_iterator
  221. template <typename I>
  222. typename dynamic_x_step_type<I>::type make_step_iterator_impl(const I& it, std::ptrdiff_t step, mpl::false_) {
  223. return memory_based_step_iterator<I>(it, step);
  224. }
  225. // If the iterator is compound, put the step in its base
  226. template <typename I>
  227. typename dynamic_x_step_type<I>::type make_step_iterator_impl(const I& it, std::ptrdiff_t step, mpl::true_) {
  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. memory_based_step_iterator<BaseIt> make_step_iterator_impl(const memory_based_step_iterator<BaseIt>& it, std::ptrdiff_t step, mpl::true_) {
  233. return memory_based_step_iterator<BaseIt>(it.base(), step);
  234. }
  235. }
  236. /// \brief Constructs a step iterator from a base iterator and a step.
  237. ///
  238. /// To construct a step iterator from a given iterator Iterator and a given step, if Iterator does not
  239. /// already have a dynamic step, we wrap it in a memory_based_step_iterator. Otherwise we
  240. /// do a compile-time traversal of the chain of iterator adaptors to locate the step iterator
  241. /// and then set it step to the new one.
  242. ///
  243. /// The step iterator of Iterator is not always memory_based_step_iterator<Iterator>. For example, Iterator may
  244. /// already be a memory_based_step_iterator, in which case it will be inefficient to stack them;
  245. /// we can obtain the same result by multiplying their steps. Note that for Iterator to be a
  246. /// step iterator it does not necessarily have to have the form memory_based_step_iterator<J>.
  247. /// The step iterator can be wrapped inside another iterator. Also, it may not have the
  248. /// type memory_based_step_iterator, but it could be a user-provided type.
  249. template <typename I> // Models MemoryBasedIteratorConcept, HasDynamicXStepTypeConcept
  250. typename dynamic_x_step_type<I>::type make_step_iterator(const I& it, std::ptrdiff_t step) {
  251. return detail::make_step_iterator_impl(it, step, typename is_iterator_adaptor<I>::type());
  252. }
  253. } } // namespace boost::gil
  254. #endif