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 #include <boost/gil/detail/mp11.hpp>
14 
15 #include <boost/iterator/iterator_facade.hpp>
16 
17 #include <iterator>
18 #include <type_traits>
19 
20 namespace boost { namespace gil {
21 
22 //forward declaration (as this file is included in planar_pixel_reference.hpp)
23 template <typename ChannelReference, typename ColorSpace>
24 struct planar_pixel_reference;
25 
30 
34 
42 template <typename ChannelPtr, typename ColorSpace>
43 struct planar_pixel_iterator
44  :
45  iterator_facade
46  <
47  planar_pixel_iterator<ChannelPtr, ColorSpace>,
48  pixel<typename std::iterator_traits<ChannelPtr>::value_type,layout<ColorSpace>>,
49  std::random_access_iterator_tag,
50  planar_pixel_reference<typename std::iterator_traits<ChannelPtr>::reference, ColorSpace> const
51  >,
52  detail::homogeneous_color_base
53  <
54  ChannelPtr,
55  layout<ColorSpace>,
56  mp11::mp_size<ColorSpace>::value
57  >
58 {
59 private:
60  using parent_t = iterator_facade
61  <
62  planar_pixel_iterator<ChannelPtr, ColorSpace>,
63  pixel<typename std::iterator_traits<ChannelPtr>::value_type,layout<ColorSpace>>,
64  std::random_access_iterator_tag,
65  planar_pixel_reference<typename std::iterator_traits<ChannelPtr>::reference, ColorSpace> const
66  >;
67 
68  using color_base_parent_t = detail::homogeneous_color_base
69  <
70  ChannelPtr,
71  layout<ColorSpace>,
72  mp11::mp_size<ColorSpace>::value
73  >;
74 
75  using channel_t = typename std::iterator_traits<ChannelPtr>::value_type;
76 
77 public:
78  using value_type = typename parent_t::value_type;
79  using reference = typename parent_t::reference;
80  using difference_type = typename parent_t::difference_type;
81 
82  planar_pixel_iterator() : color_base_parent_t(0) {}
83  planar_pixel_iterator(bool) {} // constructor that does not fill with zero (for performance)
84 
85  planar_pixel_iterator(const ChannelPtr& v0, const ChannelPtr& v1) : color_base_parent_t(v0,v1) {}
86  planar_pixel_iterator(const ChannelPtr& v0, const ChannelPtr& v1, const ChannelPtr& v2) : color_base_parent_t(v0,v1,v2) {}
87  planar_pixel_iterator(const ChannelPtr& v0, const ChannelPtr& v1, const ChannelPtr& v2, const ChannelPtr& v3) : color_base_parent_t(v0,v1,v2,v3) {}
88  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) {}
89 
90  template <typename IC1,typename C1>
91  planar_pixel_iterator(const planar_pixel_iterator<IC1,C1>& ptr) : color_base_parent_t(ptr) {}
92 
96  template <typename P>
97  planar_pixel_iterator(P* pix) : color_base_parent_t(pix, true) {
98  function_requires<PixelsCompatibleConcept<P,value_type> >();
99  }
100 
101  struct address_of { template <typename T> T* operator()(T& t) { return &t; } };
102  template <typename P>
103  planar_pixel_iterator& operator=(P* pix) {
104  function_requires<PixelsCompatibleConcept<P,value_type> >();
105  static_transform(*pix,*this, address_of());
106 
107  // PERFORMANCE_CHECK: Compare to this:
108  //this->template semantic_at_c<0>()=&pix->template semantic_at_c<0>();
109  //this->template semantic_at_c<1>()=&pix->template semantic_at_c<1>();
110  //this->template semantic_at_c<2>()=&pix->template semantic_at_c<2>();
111  return *this;
112  }
113 
116  reference operator[](difference_type d) const { return memunit_advanced_ref(*this,d*sizeof(channel_t));}
117 
118  reference operator->() const { return **this; }
119 
120  // PERFORMANCE_CHECK: Remove?
121  bool operator< (const planar_pixel_iterator& ptr) const { return gil::at_c<0>(*this)< gil::at_c<0>(ptr); }
122  bool operator!=(const planar_pixel_iterator& ptr) const { return gil::at_c<0>(*this)!=gil::at_c<0>(ptr); }
123 private:
124  friend class boost::iterator_core_access;
125 
126  void increment() { static_transform(*this,*this,detail::inc<ChannelPtr>()); }
127  void decrement() { static_transform(*this,*this,detail::dec<ChannelPtr>()); }
128  void advance(std::ptrdiff_t d){ static_transform(*this,*this,std::bind(detail::plus_asymmetric<ChannelPtr,std::ptrdiff_t>(),std::placeholders::_1,d)); }
129  reference dereference() const { return this->template deref<reference>(); }
130 
131  std::ptrdiff_t distance_to(const planar_pixel_iterator& it) const { return gil::at_c<0>(it)-gil::at_c<0>(*this); }
132  bool equal(const planar_pixel_iterator& it) const { return gil::at_c<0>(*this)==gil::at_c<0>(it); }
133 };
134 
135 namespace detail {
136 template <typename I>
137 struct channel_iterator_is_mutable : std::true_type {};
138 
139 template <typename I>
140 struct channel_iterator_is_mutable<I const*> : std::false_type {};
141 
142 } // namespace detail
143 
144 template <typename IC, typename C>
145 struct const_iterator_type<planar_pixel_iterator<IC,C> > {
146 private:
147  using channel_t = typename std::iterator_traits<IC>::value_type;
148 public:
149  using type = planar_pixel_iterator<typename channel_traits<channel_t>::const_pointer,C>;
150 };
151 
152 // The default implementation when the iterator is a C pointer is to use the standard constness semantics
153 template <typename IC, typename C>
154 struct iterator_is_mutable<planar_pixel_iterator<IC,C> > : public detail::channel_iterator_is_mutable<IC> {};
155 
157 // ColorBasedConcept
159 
160 template <typename IC, typename C, int K>
161 struct kth_element_type<planar_pixel_iterator<IC, C>, K>
162 {
163  using type = IC;
164 };
165 
166 template <typename IC, typename C, int K>
167 struct kth_element_reference_type<planar_pixel_iterator<IC, C>, K>
168  : std::add_lvalue_reference<IC> {};
169 
170 template <typename IC, typename C, int K>
171 struct kth_element_const_reference_type<planar_pixel_iterator<IC, C>, K>
172  : std::add_lvalue_reference<typename std::add_const<IC>::type>
173 {};
174 
176 // HomogeneousPixelBasedConcept
178 
179 template <typename IC, typename C>
180 struct color_space_type<planar_pixel_iterator<IC,C>>
181 {
182  using type = C;
183 };
184 
185 template <typename IC, typename C>
186 struct channel_mapping_type<planar_pixel_iterator<IC, C>>
187  : channel_mapping_type<typename planar_pixel_iterator<IC,C>::value_type>
188 {};
189 
190 template <typename IC, typename C>
191 struct is_planar<planar_pixel_iterator<IC, C>> : std::true_type {};
192 
193 template <typename IC, typename C>
194 struct channel_type<planar_pixel_iterator<IC, C>>
195 {
196  using type = typename std::iterator_traits<IC>::value_type;
197 };
198 
200 // MemoryBasedIteratorConcept
202 
203 template <typename IC, typename C>
204 inline std::ptrdiff_t memunit_step(const planar_pixel_iterator<IC,C>&) { return sizeof(typename std::iterator_traits<IC>::value_type); }
205 
206 template <typename IC, typename C>
207 inline std::ptrdiff_t memunit_distance(const planar_pixel_iterator<IC,C>& p1, const planar_pixel_iterator<IC,C>& p2) {
208  return memunit_distance(gil::at_c<0>(p1),gil::at_c<0>(p2));
209 }
210 
211 template <typename IC>
212 struct memunit_advance_fn {
213  memunit_advance_fn(std::ptrdiff_t diff) : _diff(diff) {}
214  IC operator()(const IC& p) const { return memunit_advanced(p,_diff); }
215 
216  std::ptrdiff_t _diff;
217 };
218 
219 template <typename IC, typename C>
220 inline void memunit_advance(planar_pixel_iterator<IC,C>& p, std::ptrdiff_t diff) {
221  static_transform(p, p, memunit_advance_fn<IC>(diff));
222 }
223 
224 template <typename IC, typename C>
225 inline planar_pixel_iterator<IC,C> memunit_advanced(const planar_pixel_iterator<IC,C>& p, std::ptrdiff_t diff) {
226  planar_pixel_iterator<IC,C> ret=p;
227  memunit_advance(ret, diff);
228  return ret;
229 }
230 
231 template <typename ChannelPtr, typename ColorSpace>
232 inline planar_pixel_reference<typename std::iterator_traits<ChannelPtr>::reference,ColorSpace>
233  memunit_advanced_ref(const planar_pixel_iterator<ChannelPtr,ColorSpace>& ptr, std::ptrdiff_t diff) {
234  return planar_pixel_reference<typename std::iterator_traits<ChannelPtr>::reference,ColorSpace>(ptr, diff);
235 }
236 
238 // HasDynamicXStepTypeConcept
240 
241 template <typename IC, typename C>
242 struct dynamic_x_step_type<planar_pixel_iterator<IC,C> > {
243  using type = memory_based_step_iterator<planar_pixel_iterator<IC,C>>;
244 };
245 } } // namespace boost::gil
246 
247 #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:1029
planar_pixel_iterator(P *pix)
Definition: planar_pixel_iterator.hpp:97
reference operator[](difference_type d) const
Definition: planar_pixel_iterator.hpp:116