position_iterator.hpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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_POSITION_ITERATOR_HPP
  10. #define GIL_POSITION_ITERATOR_HPP
  11. ////////////////////////////////////////////////////////////////////////////////////////
  12. /// \file
  13. /// \brief Locator for virtual image views
  14. /// \author Lubomir Bourdev and Hailin Jin \n
  15. /// Adobe Systems Incorporated
  16. /// \date 2005-2007 \n Last updated on February 12, 2007
  17. ///
  18. ////////////////////////////////////////////////////////////////////////////////////////
  19. #include <boost/iterator/iterator_facade.hpp>
  20. #include "locator.hpp"
  21. namespace boost { namespace gil {
  22. /// \defgroup PixelIteratorModelVirtual position_iterator
  23. /// \ingroup PixelIteratorModel
  24. /// \brief An iterator that remembers its current X,Y position and invokes a function object with it upon dereferencing. Models PixelIteratorConcept, PixelBasedConcept, HasDynamicXStepTypeConcept. Used to create virtual image views.
  25. /// \brief An iterator that remembers its current X,Y position and invokes a function object with it upon dereferencing. Models PixelIteratorConcept. Used to create virtual image views.
  26. /// Models: StepIteratorConcept, PixelIteratorConcept, PixelBasedConcept, HasDynamicXStepTypeConcept
  27. /// \ingroup PixelIteratorModelVirtual PixelBasedModel
  28. template <typename Deref, // A function object that given a point returns a pixel reference. Models PixelDereferenceAdaptorConcept
  29. int Dim> // the dimension to advance along
  30. struct position_iterator : public iterator_facade<position_iterator<Deref,Dim>,
  31. typename Deref::value_type,
  32. std::random_access_iterator_tag,
  33. typename Deref::reference,
  34. typename Deref::argument_type::template axis<Dim>::coord_t> {
  35. typedef iterator_facade<position_iterator<Deref,Dim>,
  36. typename Deref::value_type,
  37. std::random_access_iterator_tag,
  38. typename Deref::reference,
  39. typename Deref::argument_type::template axis<Dim>::coord_t> parent_t;
  40. typedef typename parent_t::difference_type difference_type;
  41. typedef typename parent_t::reference reference;
  42. typedef typename Deref::argument_type point_t;
  43. position_iterator() {}
  44. position_iterator(const point_t& p, const point_t& step, const Deref& d) : _p(p), _step(step), _d(d) {}
  45. position_iterator(const position_iterator& p) : _p(p._p), _step(p._step), _d(p._d) {}
  46. template <typename D> position_iterator(const position_iterator<D,Dim>& p) : _p(p._p), _step(p._step), _d(p._d) {}
  47. position_iterator& operator=(const position_iterator& p) { _p=p._p; _d=p._d; _step=p._step; return *this; }
  48. const point_t& pos() const { return _p; }
  49. const point_t& step() const { return _step; }
  50. const Deref& deref_fn() const { return _d; }
  51. void set_step(difference_type s) { _step[Dim]=s; }
  52. /// For some reason operator[] provided by iterator_adaptor returns a custom class that is convertible to reference
  53. /// We require our own reference because it is registered in iterator_traits
  54. reference operator[](difference_type d) const { point_t p=_p; p[Dim]+=d*_step[Dim]; return _d(p); }
  55. private:
  56. point_t _p, _step;
  57. Deref _d;
  58. template <typename DE, int DI> friend struct position_iterator;
  59. friend class boost::iterator_core_access;
  60. reference dereference() const { return _d(_p); }
  61. void increment() { _p[Dim]+=_step[Dim]; }
  62. void decrement() { _p[Dim]-=_step[Dim]; }
  63. void advance(difference_type d) { _p[Dim]+=d*_step[Dim]; }
  64. difference_type distance_to(const position_iterator& it) const { return (it._p[Dim]-_p[Dim])/_step[Dim]; }
  65. bool equal(const position_iterator& it) const { return _p==it._p; }
  66. };
  67. template <typename Deref,int Dim>
  68. struct const_iterator_type<position_iterator<Deref,Dim> > {
  69. typedef position_iterator<typename Deref::const_t,Dim> type;
  70. };
  71. template <typename Deref,int Dim>
  72. struct iterator_is_mutable<position_iterator<Deref,Dim> > : public mpl::bool_<Deref::is_mutable> {
  73. };
  74. /////////////////////////////
  75. // PixelBasedConcept
  76. /////////////////////////////
  77. template <typename Deref,int Dim>
  78. struct color_space_type<position_iterator<Deref,Dim> > : public color_space_type<typename Deref::value_type> {};
  79. template <typename Deref,int Dim>
  80. struct channel_mapping_type<position_iterator<Deref,Dim> > : public channel_mapping_type<typename Deref::value_type> {};
  81. template <typename Deref,int Dim>
  82. struct is_planar<position_iterator<Deref,Dim> > : public mpl::false_ {};
  83. template <typename Deref,int Dim>
  84. struct channel_type<position_iterator<Deref,Dim> > : public channel_type<typename Deref::value_type> {};
  85. /////////////////////////////
  86. // HasDynamicXStepTypeConcept
  87. /////////////////////////////
  88. template <typename Deref,int Dim>
  89. struct dynamic_x_step_type<position_iterator<Deref,Dim> > {
  90. typedef position_iterator<Deref,Dim> type;
  91. };
  92. } } // namespace boost::gil
  93. #endif