Boost GIL


planar_pixel_iterator.hpp
Go to the documentation of this file.
1 /*
2  Copyright 2005-2007 Adobe Systems Incorporated
3 
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).
7 
8  See http://opensource.adobe.com/gil for most recent version including documentation.
9 */
10 
11 /*************************************************************************************************/
12 
13 #ifndef GIL_PLANAR_PTR_H
14 #define GIL_PLANAR_PTR_H
15 
24 
25 #include <cassert>
26 #include <iterator>
27 #include <boost/iterator/iterator_facade.hpp>
28 #include "gil_config.hpp"
29 #include "pixel.hpp"
30 #include "step_iterator.hpp"
31 
32 namespace boost { namespace gil {
33 //forward declaration (as this file is included in planar_pixel_reference.hpp)
34 template <typename ChannelReference, typename ColorSpace>
35 struct planar_pixel_reference;
36 
41 
45 
53 template <typename ChannelPtr, typename ColorSpace>
54 struct planar_pixel_iterator : public iterator_facade<planar_pixel_iterator<ChannelPtr,ColorSpace>,
55  pixel<typename std::iterator_traits<ChannelPtr>::value_type,layout<ColorSpace> >,
56  std::random_access_iterator_tag,
57  const planar_pixel_reference<typename std::iterator_traits<ChannelPtr>::reference,ColorSpace> >,
58  public detail::homogeneous_color_base<ChannelPtr,layout<ColorSpace>,mpl::size<ColorSpace>::value > {
59 private:
60  typedef iterator_facade<planar_pixel_iterator<ChannelPtr,ColorSpace>,
61  pixel<typename std::iterator_traits<ChannelPtr>::value_type,layout<ColorSpace> >,
62  std::random_access_iterator_tag,
63  const planar_pixel_reference<typename std::iterator_traits<ChannelPtr>::reference,ColorSpace> > parent_t;
64  typedef detail::homogeneous_color_base<ChannelPtr,layout<ColorSpace>,mpl::size<ColorSpace>::value> color_base_parent_t;
65  typedef typename std::iterator_traits<ChannelPtr>::value_type channel_t;
66 public:
67  typedef typename parent_t::value_type value_type;
68  typedef typename parent_t::reference reference;
69  typedef typename parent_t::difference_type difference_type;
70 
71  planar_pixel_iterator() : color_base_parent_t(0) {}
72  planar_pixel_iterator(bool) {} // constructor that does not fill with zero (for performance)
73 
74  planar_pixel_iterator(const ChannelPtr& v0, const ChannelPtr& v1) : color_base_parent_t(v0,v1) {}
75  planar_pixel_iterator(const ChannelPtr& v0, const ChannelPtr& v1, const ChannelPtr& v2) : color_base_parent_t(v0,v1,v2) {}
76  planar_pixel_iterator(const ChannelPtr& v0, const ChannelPtr& v1, const ChannelPtr& v2, const ChannelPtr& v3) : color_base_parent_t(v0,v1,v2,v3) {}
77  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) {}
78 
79  template <typename IC1,typename C1>
80  planar_pixel_iterator(const planar_pixel_iterator<IC1,C1>& ptr) : color_base_parent_t(ptr) {}
81 
85  template <typename P>
86  planar_pixel_iterator(P* pix) : color_base_parent_t(pix, true) {
87  function_requires<PixelsCompatibleConcept<P,value_type> >();
88  }
89 
90  struct address_of { template <typename T> T* operator()(T& t) { return &t; } };
91  template <typename P>
92  planar_pixel_iterator& operator=(P* pix) {
93  function_requires<PixelsCompatibleConcept<P,value_type> >();
94  static_transform(*pix,*this, address_of());
95 
96  // PERFORMANCE_CHECK: Compare to this:
97  //this->template semantic_at_c<0>()=&pix->template semantic_at_c<0>();
98  //this->template semantic_at_c<1>()=&pix->template semantic_at_c<1>();
99  //this->template semantic_at_c<2>()=&pix->template semantic_at_c<2>();
100  return *this;
101  }
102 
105  reference operator[](difference_type d) const { return memunit_advanced_ref(*this,d*sizeof(channel_t));}
106 
107  reference operator->() const { return **this; }
108 
109  // PERFORMANCE_CHECK: Remove?
110  bool operator< (const planar_pixel_iterator& ptr) const { return gil::at_c<0>(*this)< gil::at_c<0>(ptr); }
111  bool operator!=(const planar_pixel_iterator& ptr) const { return gil::at_c<0>(*this)!=gil::at_c<0>(ptr); }
112 private:
113  friend class boost::iterator_core_access;
114 
115  void increment() { static_transform(*this,*this,detail::inc<ChannelPtr>()); }
116  void decrement() { static_transform(*this,*this,detail::dec<ChannelPtr>()); }
117 #ifdef BOOST_NO_CXX98_BINDERS
118  void advance(std::ptrdiff_t d){ static_transform(*this,*this,std::bind(detail::plus_asymmetric<ChannelPtr,std::ptrdiff_t>(),std::placeholders::_1,d)); }
119 #else
120  void advance(std::ptrdiff_t d){ static_transform(*this,*this,std::bind2nd(detail::plus_asymmetric<ChannelPtr,std::ptrdiff_t>(),d)); }
121 #endif
122  reference dereference() const { return this->template deref<reference>(); }
123 
124  std::ptrdiff_t distance_to(const planar_pixel_iterator& it) const { return gil::at_c<0>(it)-gil::at_c<0>(*this); }
125  bool equal(const planar_pixel_iterator& it) const { return gil::at_c<0>(*this)==gil::at_c<0>(it); }
126 };
127 
128 namespace detail {
129  template <typename IC> struct channel_iterator_is_mutable : public mpl::true_ {};
130  template <typename T> struct channel_iterator_is_mutable<const T*> : public mpl::false_ {};
131 }
132 
133 template <typename IC, typename C>
134 struct const_iterator_type<planar_pixel_iterator<IC,C> > {
135 private:
136  typedef typename std::iterator_traits<IC>::value_type channel_t;
137 public:
138  typedef planar_pixel_iterator<typename channel_traits<channel_t>::const_pointer,C> type;
139 };
140 
141 // The default implementation when the iterator is a C pointer is to use the standard constness semantics
142 template <typename IC, typename C>
143 struct iterator_is_mutable<planar_pixel_iterator<IC,C> > : public detail::channel_iterator_is_mutable<IC> {};
144 
146 // ColorBasedConcept
148 
149 template <typename IC, typename C, int K>
150 struct kth_element_type<planar_pixel_iterator<IC,C>, K> {
151  typedef IC type;
152 };
153 
154 template <typename IC, typename C, int K>
155 struct kth_element_reference_type<planar_pixel_iterator<IC,C>, K> : public add_reference<IC> {};
156 
157 template <typename IC, typename C, int K>
158 struct kth_element_const_reference_type<planar_pixel_iterator<IC,C>, K> : public add_reference<typename add_const<IC>::type> {};
159 
161 // HomogeneousPixelBasedConcept
163 
164 template <typename IC, typename C>
165 struct color_space_type<planar_pixel_iterator<IC,C> > {
166  typedef C type;
167 };
168 
169 template <typename IC, typename C>
170 struct channel_mapping_type<planar_pixel_iterator<IC,C> > : public channel_mapping_type<typename planar_pixel_iterator<IC,C>::value_type> {};
171 
172 template <typename IC, typename C>
173 struct is_planar<planar_pixel_iterator<IC,C> > : public mpl::true_ {};
174 
175 template <typename IC, typename C>
176 struct channel_type<planar_pixel_iterator<IC,C> > {
177  typedef typename std::iterator_traits<IC>::value_type type;
178 };
179 
181 // MemoryBasedIteratorConcept
183 
184 template <typename IC, typename C>
185 inline std::ptrdiff_t memunit_step(const planar_pixel_iterator<IC,C>&) { return sizeof(typename std::iterator_traits<IC>::value_type); }
186 
187 template <typename IC, typename C>
188 inline std::ptrdiff_t memunit_distance(const planar_pixel_iterator<IC,C>& p1, const planar_pixel_iterator<IC,C>& p2) {
189  return memunit_distance(gil::at_c<0>(p1),gil::at_c<0>(p2));
190 }
191 
192 template <typename IC>
193 struct memunit_advance_fn {
194  memunit_advance_fn(std::ptrdiff_t diff) : _diff(diff) {}
195  IC operator()(const IC& p) const { return memunit_advanced(p,_diff); }
196 
197  std::ptrdiff_t _diff;
198 };
199 
200 template <typename IC, typename C>
201 inline void memunit_advance(planar_pixel_iterator<IC,C>& p, std::ptrdiff_t diff) {
202  static_transform(p, p, memunit_advance_fn<IC>(diff));
203 }
204 
205 template <typename IC, typename C>
206 inline planar_pixel_iterator<IC,C> memunit_advanced(const planar_pixel_iterator<IC,C>& p, std::ptrdiff_t diff) {
207  planar_pixel_iterator<IC,C> ret=p;
208  memunit_advance(ret, diff);
209  return ret;
210 }
211 
212 template <typename ChannelPtr, typename ColorSpace>
213 inline planar_pixel_reference<typename std::iterator_traits<ChannelPtr>::reference,ColorSpace>
214  memunit_advanced_ref(const planar_pixel_iterator<ChannelPtr,ColorSpace>& ptr, std::ptrdiff_t diff) {
215  return planar_pixel_reference<typename std::iterator_traits<ChannelPtr>::reference,ColorSpace>(ptr, diff);
216 }
217 
219 // HasDynamicXStepTypeConcept
221 
222 template <typename IC, typename C>
223 struct dynamic_x_step_type<planar_pixel_iterator<IC,C> > {
224  typedef memory_based_step_iterator<planar_pixel_iterator<IC,C> > type;
225 };
226 } } // namespace boost::gil
227 
228 #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:934
pixel step iterator
planar_pixel_iterator(P *pix)
Definition: planar_pixel_iterator.hpp:86
GIL configuration file.
pixel class and related utilities
reference operator[](difference_type d) const
Definition: planar_pixel_iterator.hpp:105