pixel_iterator_adaptor.hpp 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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_PIXEL_ITERATOR_ADAPTOR_HPP
  9. #define BOOST_GIL_PIXEL_ITERATOR_ADAPTOR_HPP
  10. #include <boost/gil/concepts.hpp>
  11. #include <boost/gil/pixel_iterator.hpp>
  12. #include <boost/iterator/iterator_facade.hpp>
  13. #include <iterator>
  14. namespace boost { namespace gil {
  15. /// \defgroup PixelIteratorModelDerefPtr dereference_iterator_adaptor
  16. /// \ingroup PixelIteratorModel
  17. /// \brief An iterator that invokes a provided function object upon dereference. Models: IteratorAdaptorConcept, PixelIteratorConcept
  18. /// \ingroup PixelIteratorModelDerefPtr PixelBasedModel
  19. /// \brief An adaptor over an existing iterator that provides for custom filter on dereferencing the object. Models: IteratorAdaptorConcept, PixelIteratorConcept
  20. template <typename Iterator, // Models Iterator
  21. typename DFn> // Models Returns the result of dereferencing a given iterator of type Iterator
  22. class dereference_iterator_adaptor : public iterator_adaptor<dereference_iterator_adaptor<Iterator,DFn>,
  23. Iterator,
  24. typename DFn::value_type,
  25. typename std::iterator_traits<Iterator>::iterator_category,
  26. typename DFn::reference,
  27. use_default> {
  28. DFn _deref_fn;
  29. public:
  30. using parent_t = iterator_adaptor<dereference_iterator_adaptor<Iterator,DFn>,
  31. Iterator,
  32. typename DFn::value_type,
  33. typename std::iterator_traits<Iterator>::iterator_category,
  34. typename DFn::reference,
  35. use_default>;
  36. using reference = typename DFn::result_type;
  37. using difference_type = typename std::iterator_traits<Iterator>::difference_type;
  38. using dereference_fn = DFn;
  39. dereference_iterator_adaptor() {}
  40. template <typename Iterator1>
  41. dereference_iterator_adaptor(const dereference_iterator_adaptor<Iterator1,DFn>& dit) : parent_t(dit.base()), _deref_fn(dit._deref_fn) {}
  42. dereference_iterator_adaptor(Iterator it, DFn deref_fn=DFn()) : parent_t(it), _deref_fn(deref_fn) {}
  43. template <typename Iterator1, typename DFn1>
  44. dereference_iterator_adaptor(const dereference_iterator_adaptor<Iterator1,DFn1>& it) : parent_t(it.base()), _deref_fn(it._deref_fn) {}
  45. /// For some reason operator[] provided by iterator_facade returns a custom class that is convertible to reference
  46. /// We require our own reference because it is registered in iterator_traits
  47. reference operator[](difference_type d) const { return *(*this+d);}
  48. // although iterator_adaptor defines these, the default implementation computes distance and compares for zero.
  49. // it is often faster to just apply the relation operator to the base
  50. bool operator> (const dereference_iterator_adaptor& p) const { return this->base_reference()> p.base_reference(); }
  51. bool operator< (const dereference_iterator_adaptor& p) const { return this->base_reference()< p.base_reference(); }
  52. bool operator>=(const dereference_iterator_adaptor& p) const { return this->base_reference()>=p.base_reference(); }
  53. bool operator<=(const dereference_iterator_adaptor& p) const { return this->base_reference()<=p.base_reference(); }
  54. bool operator==(const dereference_iterator_adaptor& p) const { return this->base_reference()==p.base_reference(); }
  55. bool operator!=(const dereference_iterator_adaptor& p) const { return this->base_reference()!=p.base_reference(); }
  56. Iterator& base() { return this->base_reference(); }
  57. const Iterator& base() const { return this->base_reference(); }
  58. const DFn& deref_fn() const { return _deref_fn; }
  59. private:
  60. template <typename Iterator1, typename DFn1>
  61. friend class dereference_iterator_adaptor;
  62. friend class boost::iterator_core_access;
  63. reference dereference() const { return _deref_fn(*(this->base_reference())); }
  64. };
  65. template <typename I, typename DFn>
  66. struct const_iterator_type<dereference_iterator_adaptor<I,DFn> > {
  67. using type = dereference_iterator_adaptor<typename const_iterator_type<I>::type,typename DFn::const_t>;
  68. };
  69. template <typename I, typename DFn>
  70. struct iterator_is_mutable<dereference_iterator_adaptor<I, DFn>>
  71. : std::integral_constant<bool, DFn::is_mutable>
  72. {};
  73. template <typename I, typename DFn>
  74. struct is_iterator_adaptor<dereference_iterator_adaptor<I, DFn>> : std::true_type {};
  75. template <typename I, typename DFn>
  76. struct iterator_adaptor_get_base<dereference_iterator_adaptor<I, DFn>>
  77. {
  78. using type = I;
  79. };
  80. template <typename I, typename DFn, typename NewBaseIterator>
  81. struct iterator_adaptor_rebind<dereference_iterator_adaptor<I,DFn>,NewBaseIterator> {
  82. using type = dereference_iterator_adaptor<NewBaseIterator,DFn>;
  83. };
  84. /////////////////////////////
  85. // PixelBasedConcept
  86. /////////////////////////////
  87. template <typename I, typename DFn>
  88. struct color_space_type<dereference_iterator_adaptor<I,DFn> > : public color_space_type<typename DFn::value_type> {};
  89. template <typename I, typename DFn>
  90. struct channel_mapping_type<dereference_iterator_adaptor<I,DFn> > : public channel_mapping_type<typename DFn::value_type> {};
  91. template <typename I, typename DFn>
  92. struct is_planar<dereference_iterator_adaptor<I,DFn> > : public is_planar<typename DFn::value_type> {};
  93. template <typename I, typename DFn>
  94. struct channel_type<dereference_iterator_adaptor<I,DFn> > : public channel_type<typename DFn::value_type> {};
  95. /////////////////////////////
  96. // MemoryBasedIteratorConcept
  97. /////////////////////////////
  98. template <typename Iterator, typename DFn>
  99. struct byte_to_memunit<dereference_iterator_adaptor<Iterator,DFn>> : public byte_to_memunit<Iterator> {};
  100. template <typename Iterator, typename DFn>
  101. inline auto memunit_step(dereference_iterator_adaptor<Iterator,DFn> const& p)
  102. -> typename std::iterator_traits<Iterator>::difference_type
  103. {
  104. return memunit_step(p.base());
  105. }
  106. template <typename Iterator, typename DFn>
  107. inline auto memunit_distance(dereference_iterator_adaptor<Iterator,DFn> const& p1,
  108. dereference_iterator_adaptor<Iterator,DFn> const& p2)
  109. -> typename std::iterator_traits<Iterator>::difference_type
  110. {
  111. return memunit_distance(p1.base(),p2.base());
  112. }
  113. template <typename Iterator, typename DFn>
  114. inline void memunit_advance(dereference_iterator_adaptor<Iterator,DFn>& p,
  115. typename std::iterator_traits<Iterator>::difference_type diff) {
  116. memunit_advance(p.base(), diff);
  117. }
  118. template <typename Iterator, typename DFn>
  119. inline auto memunit_advanced(dereference_iterator_adaptor<Iterator,DFn> const& p,
  120. typename std::iterator_traits<Iterator>::difference_type diff)
  121. -> dereference_iterator_adaptor<Iterator,DFn>
  122. {
  123. return dereference_iterator_adaptor<Iterator,DFn>(memunit_advanced(p.base(), diff), p.deref_fn());
  124. }
  125. template <typename Iterator, typename DFn>
  126. inline auto memunit_advanced_ref(dereference_iterator_adaptor<Iterator,DFn> const& p,
  127. typename std::iterator_traits<Iterator>::difference_type diff)
  128. -> typename std::iterator_traits<dereference_iterator_adaptor<Iterator,DFn> >::reference
  129. {
  130. return *memunit_advanced(p, diff);
  131. }
  132. /////////////////////////////
  133. // HasDynamicXStepTypeConcept
  134. /////////////////////////////
  135. template <typename Iterator, typename DFn>
  136. struct dynamic_x_step_type<dereference_iterator_adaptor<Iterator,DFn> > {
  137. using type = dereference_iterator_adaptor<typename dynamic_x_step_type<Iterator>::type,DFn>;
  138. };
  139. /// \brief Returns the type (and creates an instance) of an iterator that invokes the given dereference adaptor upon dereferencing
  140. /// \ingroup PixelIteratorModelDerefPtr
  141. template <typename Iterator, typename Deref>
  142. struct iterator_add_deref {
  143. BOOST_GIL_CLASS_REQUIRE(Deref, boost::gil, PixelDereferenceAdaptorConcept)
  144. using type = dereference_iterator_adaptor<Iterator, Deref>;
  145. static type make(const Iterator& it, const Deref& d) { return type(it,d); }
  146. };
  147. /// \ingroup PixelIteratorModelDerefPtr
  148. /// \brief For dereference iterator adaptors, compose the new function object after the old one
  149. template <typename Iterator, typename PREV_DEREF, typename Deref>
  150. struct iterator_add_deref<dereference_iterator_adaptor<Iterator, PREV_DEREF>,Deref> {
  151. // BOOST_GIL_CLASS_REQUIRE(Deref, boost::gil, PixelDereferenceAdaptorConcept)
  152. using type = dereference_iterator_adaptor<Iterator, deref_compose<Deref,PREV_DEREF>>;
  153. static type make(const dereference_iterator_adaptor<Iterator, PREV_DEREF>& it, const Deref& d) {
  154. return type(it.base(),deref_compose<Deref,PREV_DEREF>(d,it.deref_fn()));
  155. }
  156. };
  157. }} // namespace boost::gil
  158. #endif