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 <iterator>
17 
18 namespace boost { namespace gil {
19 //forward declaration (as this file is included in planar_pixel_reference.hpp)
20 template <typename ChannelReference, typename ColorSpace>
21 struct planar_pixel_reference;
22 
27 
31 
39 template <typename ChannelPtr, typename ColorSpace>
40 struct planar_pixel_iterator : public iterator_facade<planar_pixel_iterator<ChannelPtr,ColorSpace>,
41  pixel<typename std::iterator_traits<ChannelPtr>::value_type,layout<ColorSpace> >,
42  std::random_access_iterator_tag,
43  const planar_pixel_reference<typename std::iterator_traits<ChannelPtr>::reference,ColorSpace> >,
44  public detail::homogeneous_color_base<ChannelPtr,layout<ColorSpace>,mpl::size<ColorSpace>::value > {
45 private:
46  using parent_t = iterator_facade<planar_pixel_iterator<ChannelPtr,ColorSpace>,
47  pixel<typename std::iterator_traits<ChannelPtr>::value_type,layout<ColorSpace>>,
48  std::random_access_iterator_tag,
49  const planar_pixel_reference<typename std::iterator_traits<ChannelPtr>::reference,ColorSpace>>;
50  using color_base_parent_t = detail::homogeneous_color_base<ChannelPtr,layout<ColorSpace>,mpl::size<ColorSpace>::value>;
51  using channel_t = typename std::iterator_traits<ChannelPtr>::value_type;
52 public:
53  using value_type = typename parent_t::value_type;
54  using reference = typename parent_t::reference;
55  using difference_type = typename parent_t::difference_type;
56 
57  planar_pixel_iterator() : color_base_parent_t(0) {}
58  planar_pixel_iterator(bool) {} // constructor that does not fill with zero (for performance)
59 
60  planar_pixel_iterator(const ChannelPtr& v0, const ChannelPtr& v1) : color_base_parent_t(v0,v1) {}
61  planar_pixel_iterator(const ChannelPtr& v0, const ChannelPtr& v1, const ChannelPtr& v2) : color_base_parent_t(v0,v1,v2) {}
62  planar_pixel_iterator(const ChannelPtr& v0, const ChannelPtr& v1, const ChannelPtr& v2, const ChannelPtr& v3) : color_base_parent_t(v0,v1,v2,v3) {}
63  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) {}
64 
65  template <typename IC1,typename C1>
66  planar_pixel_iterator(const planar_pixel_iterator<IC1,C1>& ptr) : color_base_parent_t(ptr) {}
67 
71  template <typename P>
72  planar_pixel_iterator(P* pix) : color_base_parent_t(pix, true) {
73  function_requires<PixelsCompatibleConcept<P,value_type> >();
74  }
75 
76  struct address_of { template <typename T> T* operator()(T& t) { return &t; } };
77  template <typename P>
78  planar_pixel_iterator& operator=(P* pix) {
79  function_requires<PixelsCompatibleConcept<P,value_type> >();
80  static_transform(*pix,*this, address_of());
81 
82  // PERFORMANCE_CHECK: Compare to this:
83  //this->template semantic_at_c<0>()=&pix->template semantic_at_c<0>();
84  //this->template semantic_at_c<1>()=&pix->template semantic_at_c<1>();
85  //this->template semantic_at_c<2>()=&pix->template semantic_at_c<2>();
86  return *this;
87  }
88 
91  reference operator[](difference_type d) const { return memunit_advanced_ref(*this,d*sizeof(channel_t));}
92 
93  reference operator->() const { return **this; }
94 
95  // PERFORMANCE_CHECK: Remove?
96  bool operator< (const planar_pixel_iterator& ptr) const { return gil::at_c<0>(*this)< gil::at_c<0>(ptr); }
97  bool operator!=(const planar_pixel_iterator& ptr) const { return gil::at_c<0>(*this)!=gil::at_c<0>(ptr); }
98 private:
99  friend class boost::iterator_core_access;
100 
101  void increment() { static_transform(*this,*this,detail::inc<ChannelPtr>()); }
102  void decrement() { static_transform(*this,*this,detail::dec<ChannelPtr>()); }
103  void advance(std::ptrdiff_t d){ static_transform(*this,*this,std::bind(detail::plus_asymmetric<ChannelPtr,std::ptrdiff_t>(),std::placeholders::_1,d)); }
104  reference dereference() const { return this->template deref<reference>(); }
105 
106  std::ptrdiff_t distance_to(const planar_pixel_iterator& it) const { return gil::at_c<0>(it)-gil::at_c<0>(*this); }
107  bool equal(const planar_pixel_iterator& it) const { return gil::at_c<0>(*this)==gil::at_c<0>(it); }
108 };
109 
110 namespace detail {
111  template <typename IC> struct channel_iterator_is_mutable : public mpl::true_ {};
112  template <typename T> struct channel_iterator_is_mutable<const T*> : public mpl::false_ {};
113 }
114 
115 template <typename IC, typename C>
117 private:
118  using channel_t = typename std::iterator_traits<IC>::value_type;
119 public:
121 };
122 
123 // The default implementation when the iterator is a C pointer is to use the standard constness semantics
124 template <typename IC, typename C>
125 struct iterator_is_mutable<planar_pixel_iterator<IC,C> > : public detail::channel_iterator_is_mutable<IC> {};
126 
128 // ColorBasedConcept
130 
131 template <typename IC, typename C, int K>
132 struct kth_element_type<planar_pixel_iterator<IC,C>, K> {
133  using type = IC;
134 };
135 
136 template <typename IC, typename C, int K>
137 struct kth_element_reference_type<planar_pixel_iterator<IC,C>, K> : public add_reference<IC> {};
138 
139 template <typename IC, typename C, int K>
140 struct kth_element_const_reference_type<planar_pixel_iterator<IC,C>, K> : public add_reference<typename add_const<IC>::type> {};
141 
143 // HomogeneousPixelBasedConcept
145 
146 template <typename IC, typename C>
147 struct color_space_type<planar_pixel_iterator<IC,C> > {
148  using type = C;
149 };
150 
151 template <typename IC, typename C>
152 struct channel_mapping_type<planar_pixel_iterator<IC,C> > : public channel_mapping_type<typename planar_pixel_iterator<IC,C>::value_type> {};
153 
154 template <typename IC, typename C>
155 struct is_planar<planar_pixel_iterator<IC,C> > : public mpl::true_ {};
156 
157 template <typename IC, typename C>
158 struct channel_type<planar_pixel_iterator<IC,C> > {
159  using type = typename std::iterator_traits<IC>::value_type;
160 };
161 
163 // MemoryBasedIteratorConcept
165 
166 template <typename IC, typename C>
167 inline std::ptrdiff_t memunit_step(const planar_pixel_iterator<IC,C>&) { return sizeof(typename std::iterator_traits<IC>::value_type); }
168 
169 template <typename IC, typename C>
170 inline std::ptrdiff_t memunit_distance(const planar_pixel_iterator<IC,C>& p1, const planar_pixel_iterator<IC,C>& p2) {
171  return memunit_distance(gil::at_c<0>(p1),gil::at_c<0>(p2));
172 }
173 
174 template <typename IC>
175 struct memunit_advance_fn {
176  memunit_advance_fn(std::ptrdiff_t diff) : _diff(diff) {}
177  IC operator()(const IC& p) const { return memunit_advanced(p,_diff); }
178 
179  std::ptrdiff_t _diff;
180 };
181 
182 template <typename IC, typename C>
183 inline void memunit_advance(planar_pixel_iterator<IC,C>& p, std::ptrdiff_t diff) {
184  static_transform(p, p, memunit_advance_fn<IC>(diff));
185 }
186 
187 template <typename IC, typename C>
188 inline planar_pixel_iterator<IC,C> memunit_advanced(const planar_pixel_iterator<IC,C>& p, std::ptrdiff_t diff) {
190  memunit_advance(ret, diff);
191  return ret;
192 }
193 
194 template <typename ChannelPtr, typename ColorSpace>
196  memunit_advanced_ref(const planar_pixel_iterator<ChannelPtr,ColorSpace>& ptr, std::ptrdiff_t diff) {
198 }
199 
201 // HasDynamicXStepTypeConcept
203 
204 template <typename IC, typename C>
205 struct dynamic_x_step_type<planar_pixel_iterator<IC,C> > {
207 };
208 } } // namespace boost::gil
209 
210 #endif
A reference proxy to a planar pixel. Models: HomogeneousColorBaseConcept, HomogeneousPixelConcept.
Definition: metafunctions.hpp:33
plus function object whose arguments may be of different type.
Definition: utilities.hpp:202
Definition: algorithm.hpp:30
An iterator over planar pixels. Models HomogeneousColorBaseConcept, PixelIteratorConcept, HomogeneousPixelBasedConcept, MemoryBasedIteratorConcept, HasDynamicXStepTypeConcept.
Definition: algorithm.hpp:34
operator– wrapped in a function object
Definition: utilities.hpp:223
operator++ wrapped in a function object
Definition: utilities.hpp:214
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:929
Definition: color_convert.hpp:30
planar_pixel_iterator(P *pix)
Definition: planar_pixel_iterator.hpp:72
Metafunction predicate returning whether the given iterator allows for changing its values...
Definition: pixel_iterator.hpp:48
Returns the type of an iterator just like the input iterator, except operating over immutable values...
Definition: pixel_iterator.hpp:39
reference operator[](difference_type d) const
Definition: planar_pixel_iterator.hpp:91
Base template for types that model HasDynamicXStepTypeConcept.
Definition: dynamic_step.hpp:17
MEMORY-BASED STEP ITERATOR.
Definition: algorithm.hpp:36