Boost GIL


planar_pixel_iterator.hpp
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_PLANAR_PIXEL_ITERATOR_HPP
9 #define BOOST_GIL_PLANAR_PIXEL_ITERATOR_HPP
10 
11 #include <boost/gil/pixel.hpp>
12 #include <boost/gil/step_iterator.hpp>
13 
14 #include <boost/iterator/iterator_facade.hpp>
15 
16 #include <cassert>
17 #include <iterator>
18 
19 namespace boost { namespace gil {
20 //forward declaration (as this file is included in planar_pixel_reference.hpp)
21 template <typename ChannelReference, typename ColorSpace>
22 struct planar_pixel_reference;
23 
28 
32 
40 template <typename ChannelPtr, typename ColorSpace>
41 struct planar_pixel_iterator : public iterator_facade<planar_pixel_iterator<ChannelPtr,ColorSpace>,
42  pixel<typename std::iterator_traits<ChannelPtr>::value_type,layout<ColorSpace> >,
43  std::random_access_iterator_tag,
44  const planar_pixel_reference<typename std::iterator_traits<ChannelPtr>::reference,ColorSpace> >,
45  public detail::homogeneous_color_base<ChannelPtr,layout<ColorSpace>,mpl::size<ColorSpace>::value > {
46 private:
47  typedef iterator_facade<planar_pixel_iterator<ChannelPtr,ColorSpace>,
48  pixel<typename std::iterator_traits<ChannelPtr>::value_type,layout<ColorSpace> >,
49  std::random_access_iterator_tag,
50  const planar_pixel_reference<typename std::iterator_traits<ChannelPtr>::reference,ColorSpace> > parent_t;
51  typedef detail::homogeneous_color_base<ChannelPtr,layout<ColorSpace>,mpl::size<ColorSpace>::value> color_base_parent_t;
52  typedef typename std::iterator_traits<ChannelPtr>::value_type channel_t;
53 public:
54  typedef typename parent_t::value_type value_type;
55  typedef typename parent_t::reference reference;
56  typedef typename parent_t::difference_type difference_type;
57 
58  planar_pixel_iterator() : color_base_parent_t(0) {}
59  planar_pixel_iterator(bool) {} // constructor that does not fill with zero (for performance)
60 
61  planar_pixel_iterator(const ChannelPtr& v0, const ChannelPtr& v1) : color_base_parent_t(v0,v1) {}
62  planar_pixel_iterator(const ChannelPtr& v0, const ChannelPtr& v1, const ChannelPtr& v2) : color_base_parent_t(v0,v1,v2) {}
63  planar_pixel_iterator(const ChannelPtr& v0, const ChannelPtr& v1, const ChannelPtr& v2, const ChannelPtr& v3) : color_base_parent_t(v0,v1,v2,v3) {}
64  planar_pixel_iterator(const ChannelPtr& v0, const ChannelPtr& v1, const ChannelPtr& v2, const ChannelPtr& v3, const ChannelPtr& v4) : color_base_parent_t(v0,v1,v2,v3,v4) {}
65 
66  template <typename IC1,typename C1>
67  planar_pixel_iterator(const planar_pixel_iterator<IC1,C1>& ptr) : color_base_parent_t(ptr) {}
68 
72  template <typename P>
73  planar_pixel_iterator(P* pix) : color_base_parent_t(pix, true) {
74  function_requires<PixelsCompatibleConcept<P,value_type> >();
75  }
76 
77  struct address_of { template <typename T> T* operator()(T& t) { return &t; } };
78  template <typename P>
79  planar_pixel_iterator& operator=(P* pix) {
80  function_requires<PixelsCompatibleConcept<P,value_type> >();
81  static_transform(*pix,*this, address_of());
82 
83  // PERFORMANCE_CHECK: Compare to this:
84  //this->template semantic_at_c<0>()=&pix->template semantic_at_c<0>();
85  //this->template semantic_at_c<1>()=&pix->template semantic_at_c<1>();
86  //this->template semantic_at_c<2>()=&pix->template semantic_at_c<2>();
87  return *this;
88  }
89 
92  reference operator[](difference_type d) const { return memunit_advanced_ref(*this,d*sizeof(channel_t));}
93 
94  reference operator->() const { return **this; }
95 
96  // PERFORMANCE_CHECK: Remove?
97  bool operator< (const planar_pixel_iterator& ptr) const { return gil::at_c<0>(*this)< gil::at_c<0>(ptr); }
98  bool operator!=(const planar_pixel_iterator& ptr) const { return gil::at_c<0>(*this)!=gil::at_c<0>(ptr); }
99 private:
100  friend class boost::iterator_core_access;
101 
102  void increment() { static_transform(*this,*this,detail::inc<ChannelPtr>()); }
103  void decrement() { static_transform(*this,*this,detail::dec<ChannelPtr>()); }
104  void advance(std::ptrdiff_t d){ static_transform(*this,*this,std::bind(detail::plus_asymmetric<ChannelPtr,std::ptrdiff_t>(),std::placeholders::_1,d)); }
105  reference dereference() const { return this->template deref<reference>(); }
106 
107  std::ptrdiff_t distance_to(const planar_pixel_iterator& it) const { return gil::at_c<0>(it)-gil::at_c<0>(*this); }
108  bool equal(const planar_pixel_iterator& it) const { return gil::at_c<0>(*this)==gil::at_c<0>(it); }
109 };
110 
111 namespace detail {
112  template <typename IC> struct channel_iterator_is_mutable : public mpl::true_ {};
113  template <typename T> struct channel_iterator_is_mutable<const T*> : public mpl::false_ {};
114 }
115 
116 template <typename IC, typename C>
117 struct const_iterator_type<planar_pixel_iterator<IC,C> > {
118 private:
119  typedef typename std::iterator_traits<IC>::value_type channel_t;
120 public:
121  typedef planar_pixel_iterator<typename channel_traits<channel_t>::const_pointer,C> type;
122 };
123 
124 // The default implementation when the iterator is a C pointer is to use the standard constness semantics
125 template <typename IC, typename C>
126 struct iterator_is_mutable<planar_pixel_iterator<IC,C> > : public detail::channel_iterator_is_mutable<IC> {};
127 
129 // ColorBasedConcept
131 
132 template <typename IC, typename C, int K>
133 struct kth_element_type<planar_pixel_iterator<IC,C>, K> {
134  typedef IC type;
135 };
136 
137 template <typename IC, typename C, int K>
138 struct kth_element_reference_type<planar_pixel_iterator<IC,C>, K> : public add_reference<IC> {};
139 
140 template <typename IC, typename C, int K>
141 struct kth_element_const_reference_type<planar_pixel_iterator<IC,C>, K> : public add_reference<typename add_const<IC>::type> {};
142 
144 // HomogeneousPixelBasedConcept
146 
147 template <typename IC, typename C>
148 struct color_space_type<planar_pixel_iterator<IC,C> > {
149  typedef C type;
150 };
151 
152 template <typename IC, typename C>
153 struct channel_mapping_type<planar_pixel_iterator<IC,C> > : public channel_mapping_type<typename planar_pixel_iterator<IC,C>::value_type> {};
154 
155 template <typename IC, typename C>
156 struct is_planar<planar_pixel_iterator<IC,C> > : public mpl::true_ {};
157 
158 template <typename IC, typename C>
159 struct channel_type<planar_pixel_iterator<IC,C> > {
160  typedef typename std::iterator_traits<IC>::value_type type;
161 };
162 
164 // MemoryBasedIteratorConcept
166 
167 template <typename IC, typename C>
168 inline std::ptrdiff_t memunit_step(const planar_pixel_iterator<IC,C>&) { return sizeof(typename std::iterator_traits<IC>::value_type); }
169 
170 template <typename IC, typename C>
171 inline std::ptrdiff_t memunit_distance(const planar_pixel_iterator<IC,C>& p1, const planar_pixel_iterator<IC,C>& p2) {
172  return memunit_distance(gil::at_c<0>(p1),gil::at_c<0>(p2));
173 }
174 
175 template <typename IC>
176 struct memunit_advance_fn {
177  memunit_advance_fn(std::ptrdiff_t diff) : _diff(diff) {}
178  IC operator()(const IC& p) const { return memunit_advanced(p,_diff); }
179 
180  std::ptrdiff_t _diff;
181 };
182 
183 template <typename IC, typename C>
184 inline void memunit_advance(planar_pixel_iterator<IC,C>& p, std::ptrdiff_t diff) {
185  static_transform(p, p, memunit_advance_fn<IC>(diff));
186 }
187 
188 template <typename IC, typename C>
189 inline planar_pixel_iterator<IC,C> memunit_advanced(const planar_pixel_iterator<IC,C>& p, std::ptrdiff_t diff) {
190  planar_pixel_iterator<IC,C> ret=p;
191  memunit_advance(ret, diff);
192  return ret;
193 }
194 
195 template <typename ChannelPtr, typename ColorSpace>
196 inline planar_pixel_reference<typename std::iterator_traits<ChannelPtr>::reference,ColorSpace>
197  memunit_advanced_ref(const planar_pixel_iterator<ChannelPtr,ColorSpace>& ptr, std::ptrdiff_t diff) {
198  return planar_pixel_reference<typename std::iterator_traits<ChannelPtr>::reference,ColorSpace>(ptr, diff);
199 }
200 
202 // HasDynamicXStepTypeConcept
204 
205 template <typename IC, typename C>
206 struct dynamic_x_step_type<planar_pixel_iterator<IC,C> > {
207  typedef memory_based_step_iterator<planar_pixel_iterator<IC,C> > type;
208 };
209 } } // namespace boost::gil
210 
211 #endif
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:915
planar_pixel_iterator(P *pix)
Definition: planar_pixel_iterator.hpp:73
reference operator[](difference_type d) const
Definition: planar_pixel_iterator.hpp:92