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>
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  <
64  std::random_access_iterator_tag,
66  >;
67 
68  using color_base_parent_t = detail::homogeneous_color_base
69  <
70  ChannelPtr,
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 auto memunit_step(planar_pixel_iterator<IC,C> const&)
205  -> std::ptrdiff_t
206 {
207  return sizeof(typename std::iterator_traits<IC>::value_type);
208 }
209 
210 template <typename IC, typename C>
211 inline auto memunit_distance(planar_pixel_iterator<IC,C> const& p1, planar_pixel_iterator<IC,C> const& p2)
212  -> std::ptrdiff_t
213 {
214  return memunit_distance(gil::at_c<0>(p1),gil::at_c<0>(p2));
215 }
216 
217 template <typename IC>
218 struct memunit_advance_fn {
219  memunit_advance_fn(std::ptrdiff_t diff) : _diff(diff) {}
220  IC operator()(const IC& p) const { return memunit_advanced(p,_diff); }
221 
222  std::ptrdiff_t _diff;
223 };
224 
225 template <typename IC, typename C>
226 inline void memunit_advance(planar_pixel_iterator<IC,C>& p, std::ptrdiff_t diff) {
227  static_transform(p, p, memunit_advance_fn<IC>(diff));
228 }
229 
230 template <typename IC, typename C>
231 inline auto memunit_advanced(planar_pixel_iterator<IC,C> const& p, std::ptrdiff_t diff)
232  -> planar_pixel_iterator<IC,C>
233 {
234  planar_pixel_iterator<IC,C> ret=p;
235  memunit_advance(ret, diff);
236  return ret;
237 }
238 
239 template <typename ChannelPtr, typename ColorSpace>
240 inline auto memunit_advanced_ref(planar_pixel_iterator<ChannelPtr,ColorSpace> const& ptr, std::ptrdiff_t diff)
241  -> planar_pixel_reference<typename std::iterator_traits<ChannelPtr>::reference,ColorSpace>
242 {
243  return planar_pixel_reference<typename std::iterator_traits<ChannelPtr>::reference,ColorSpace>(ptr, diff);
244 }
245 
247 // HasDynamicXStepTypeConcept
249 
250 template <typename IC, typename C>
251 struct dynamic_x_step_type<planar_pixel_iterator<IC,C> > {
252  using type = memory_based_step_iterator<planar_pixel_iterator<IC,C>>;
253 };
254 } } // namespace boost::gil
255 
256 #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:1087
defined(BOOST_NO_CXX17_HDR_MEMORY_RESOURCE)
Definition: algorithm.hpp:36
Represents a color space and ordering of channels in memory.
Definition: utilities.hpp:268
Represents a pixel value (a container of channels). Models: HomogeneousColorBaseValueConcept,...
Definition: pixel.hpp:106
An iterator over planar pixels. Models HomogeneousColorBaseConcept, PixelIteratorConcept,...
Definition: planar_pixel_iterator.hpp:58
reference operator[](difference_type d) const
Definition: planar_pixel_iterator.hpp:116
planar_pixel_iterator(P *pix)
Definition: planar_pixel_iterator.hpp:97
A reference proxy to a planar pixel.
Definition: planar_pixel_reference.hpp:46