Boost GIL


position_iterator.hpp
Go to the documentation of this file.
1 /*
2  Copyright 2005-2007 Adobe Systems Incorporated
3 
4  Use, modification and distribution are subject to the Boost Software License,
5  Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
6  http://www.boost.org/LICENSE_1_0.txt).
7 
8  See http://opensource.adobe.com/gil for most recent version including documentation.
9 */
10 
11 /*************************************************************************************************/
12 
13 #ifndef BOOST_GIL_POSITION_ITERATOR_HPP
14 #define BOOST_GIL_POSITION_ITERATOR_HPP
15 
24 
25 #include <boost/gil/locator.hpp>
26 
27 #include <boost/iterator/iterator_facade.hpp>
28 
29 namespace boost { namespace gil {
30 
34 
35 
39 template <typename Deref, // A function object that given a point returns a pixel reference. Models PixelDereferenceAdaptorConcept
40  int Dim> // the dimension to advance along
41 struct position_iterator : public iterator_facade<position_iterator<Deref,Dim>,
42  typename Deref::value_type,
43  std::random_access_iterator_tag,
44  typename Deref::reference,
45  typename Deref::argument_type::template axis<Dim>::coord_t> {
46  typedef iterator_facade<position_iterator<Deref,Dim>,
47  typename Deref::value_type,
48  std::random_access_iterator_tag,
49  typename Deref::reference,
50  typename Deref::argument_type::template axis<Dim>::coord_t> parent_t;
51  typedef typename parent_t::difference_type difference_type;
52  typedef typename parent_t::reference reference;
53  typedef typename Deref::argument_type point_t;
54 
56  position_iterator(const point_t& p, const point_t& step, const Deref& d) : _p(p), _step(step), _d(d) {}
57 
58  position_iterator(const position_iterator& p) : _p(p._p), _step(p._step), _d(p._d) {}
59  template <typename D> position_iterator(const position_iterator<D,Dim>& p) : _p(p._p), _step(p._step), _d(p._d) {}
60  position_iterator& operator=(const position_iterator& p) { _p=p._p; _d=p._d; _step=p._step; return *this; }
61 
62  const point_t& pos() const { return _p; }
63  const point_t& step() const { return _step; }
64  const Deref& deref_fn() const { return _d; }
65 
66  void set_step(difference_type s) { _step[Dim]=s; }
69  reference operator[](difference_type d) const { point_t p=_p; p[Dim]+=d*_step[Dim]; return _d(p); }
70 
71 private:
72  point_t _p, _step;
73  Deref _d;
74 
75  template <typename DE, int DI> friend struct position_iterator;
76  friend class boost::iterator_core_access;
77  reference dereference() const { return _d(_p); }
78  void increment() { _p[Dim]+=_step[Dim]; }
79  void decrement() { _p[Dim]-=_step[Dim]; }
80  void advance(difference_type d) { _p[Dim]+=d*_step[Dim]; }
81 
82  difference_type distance_to(const position_iterator& it) const { return (it._p[Dim]-_p[Dim])/_step[Dim]; }
83  bool equal(const position_iterator& it) const { return _p==it._p; }
84 };
85 
86 template <typename Deref,int Dim>
87 struct const_iterator_type<position_iterator<Deref,Dim> > {
88  typedef position_iterator<typename Deref::const_t,Dim> type;
89 };
90 
91 template <typename Deref,int Dim>
92 struct iterator_is_mutable<position_iterator<Deref,Dim> > : public mpl::bool_<Deref::is_mutable> {
93 };
94 
96 // PixelBasedConcept
98 
99 template <typename Deref,int Dim>
100 struct color_space_type<position_iterator<Deref,Dim> > : public color_space_type<typename Deref::value_type> {};
101 
102 template <typename Deref,int Dim>
103 struct channel_mapping_type<position_iterator<Deref,Dim> > : public channel_mapping_type<typename Deref::value_type> {};
104 
105 template <typename Deref,int Dim>
106 struct is_planar<position_iterator<Deref,Dim> > : public mpl::false_ {};
107 
108 template <typename Deref,int Dim>
109 struct channel_type<position_iterator<Deref,Dim> > : public channel_type<typename Deref::value_type> {};
110 
112 // HasDynamicXStepTypeConcept
114 
115 template <typename Deref,int Dim>
116 struct dynamic_x_step_type<position_iterator<Deref,Dim> > {
117  typedef position_iterator<Deref,Dim> type;
118 };
119 
120 } } // namespace boost::gil
121 
122 #endif
reference operator[](difference_type d) const
Definition: position_iterator.hpp:69
pixel 2D locator
An iterator that remembers its current X,Y position and invokes a function object with it upon derefe...
Definition: position_iterator.hpp:41
BOOST_FORCEINLINE bool equal(boost::gil::iterator_from_2d< Loc1 > first, boost::gil::iterator_from_2d< Loc1 > last, boost::gil::iterator_from_2d< Loc2 > first2)
std::equal(I1,I1,I2) with I1 and I2 being a iterator_from_2d
Definition: algorithm.hpp:933