2 Copyright 2005-2007 Adobe Systems Incorporated
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).
8 See http://opensource.adobe.com/gil for most recent version including documentation.
11 /*************************************************************************************************/
13 #ifndef GIL_POSITION_ITERATOR_HPP
14 #define GIL_POSITION_ITERATOR_HPP
16 ////////////////////////////////////////////////////////////////////////////////////////
18 /// \brief Locator for virtual image views
19 /// \author Lubomir Bourdev and Hailin Jin \n
20 /// Adobe Systems Incorporated
21 /// \date 2005-2007 \n Last updated on February 12, 2007
23 ////////////////////////////////////////////////////////////////////////////////////////
25 #include <boost/iterator/iterator_facade.hpp>
26 #include "locator.hpp"
28 namespace boost
{ namespace gil
{
30 /// \defgroup PixelIteratorModelVirtual position_iterator
31 /// \ingroup PixelIteratorModel
32 /// \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.
35 /// \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.
36 /// Models: StepIteratorConcept, PixelIteratorConcept, PixelBasedConcept, HasDynamicXStepTypeConcept
37 /// \ingroup PixelIteratorModelVirtual PixelBasedModel
38 template <typename Deref
, // A function object that given a point returns a pixel reference. Models PixelDereferenceAdaptorConcept
39 int Dim
> // the dimension to advance along
40 struct position_iterator
: public iterator_facade
<position_iterator
<Deref
,Dim
>,
41 typename
Deref::value_type
,
42 random_access_traversal_tag
,
43 typename
Deref::reference
,
44 typename
Deref::argument_type::template axis
<Dim
>::coord_t
> {
45 typedef iterator_facade
<position_iterator
<Deref
,Dim
>,
46 typename
Deref::value_type
,
47 random_access_traversal_tag
,
48 typename
Deref::reference
,
49 typename
Deref::argument_type::template axis
<Dim
>::coord_t
> parent_t
;
50 typedef typename
parent_t::difference_type difference_type
;
51 typedef typename
parent_t::reference reference
;
52 typedef typename
Deref::argument_type point_t
;
54 position_iterator() {}
55 position_iterator(const point_t
& p
, const point_t
& step
, const Deref
& d
) : _p(p
), _step(step
), _d(d
) {}
57 position_iterator(const position_iterator
& p
) : _p(p
._p
), _step(p
._step
), _d(p
._d
) {}
58 template <typename D
> position_iterator(const position_iterator
<D
,Dim
>& p
) : _p(p
._p
), _step(p
._step
), _d(p
._d
) {}
59 position_iterator
& operator=(const position_iterator
& p
) { _p
=p
._p
; _d
=p
._d
; _step
=p
._step
; return *this; }
61 const point_t
& pos() const { return _p
; }
62 const point_t
& step() const { return _step
; }
63 const Deref
& deref_fn() const { return _d
; }
65 void set_step(difference_type s
) { _step
[Dim
]=s
; }
66 /// For some reason operator[] provided by iterator_adaptor returns a custom class that is convertible to reference
67 /// We require our own reference because it is registered in iterator_traits
68 reference
operator[](difference_type d
) const { point_t p
=_p
; p
[Dim
]+=d
*_step
[Dim
]; return _d(p
); }
74 template <typename DE
, int DI
> friend struct position_iterator
;
75 friend class boost::iterator_core_access
;
76 reference
dereference() const { return _d(_p
); }
77 void increment() { _p
[Dim
]+=_step
[Dim
]; }
78 void decrement() { _p
[Dim
]-=_step
[Dim
]; }
79 void advance(difference_type d
) { _p
[Dim
]+=d
*_step
[Dim
]; }
81 difference_type
distance_to(const position_iterator
& it
) const { return (it
._p
[Dim
]-_p
[Dim
])/_step
[Dim
]; }
82 bool equal(const position_iterator
& it
) const { return _p
==it
._p
; }
85 template <typename Deref
,int Dim
>
86 struct const_iterator_type
<position_iterator
<Deref
,Dim
> > {
87 typedef position_iterator
<typename
Deref::const_t
,Dim
> type
;
90 template <typename Deref
,int Dim
>
91 struct iterator_is_mutable
<position_iterator
<Deref
,Dim
> > : public mpl::bool_
<Deref::is_mutable
> {
94 /////////////////////////////
96 /////////////////////////////
98 template <typename Deref
,int Dim
>
99 struct color_space_type
<position_iterator
<Deref
,Dim
> > : public color_space_type
<typename
Deref::value_type
> {};
101 template <typename Deref
,int Dim
>
102 struct channel_mapping_type
<position_iterator
<Deref
,Dim
> > : public channel_mapping_type
<typename
Deref::value_type
> {};
104 template <typename Deref
,int Dim
>
105 struct is_planar
<position_iterator
<Deref
,Dim
> > : public mpl::false_
{};
107 template <typename Deref
,int Dim
>
108 struct channel_type
<position_iterator
<Deref
,Dim
> > : public channel_type
<typename
Deref::value_type
> {};
110 /////////////////////////////
111 // HasDynamicXStepTypeConcept
112 /////////////////////////////
114 template <typename Deref
,int Dim
>
115 struct dynamic_x_step_type
<position_iterator
<Deref
,Dim
> > {
116 typedef position_iterator
<Deref
,Dim
> type
;
119 } } // namespace boost::gil