Imported Upstream version 1.72.0
[platform/upstream/boost.git] / 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
26 /// \defgroup ColorBaseModelPlanarPtr planar_pixel_iterator
27 /// \ingroup ColorBaseModel
28 /// \brief A homogeneous color base whose element is a channel iterator. Models HomogeneousColorBaseValueConcept
29 /// This class is used as an iterator to a planar pixel.
30
31 /// \defgroup PixelIteratorModelPlanarPtr planar_pixel_iterator
32 /// \ingroup PixelIteratorModel
33 /// \brief An iterator over planar pixels. Models PixelIteratorConcept, HomogeneousPixelBasedConcept, MemoryBasedIteratorConcept, HasDynamicXStepTypeConcept
34
35 ////////////////////////////////////////////////////////////////////////////////////////
36 /// \brief An iterator over planar pixels. Models HomogeneousColorBaseConcept, PixelIteratorConcept, HomogeneousPixelBasedConcept, MemoryBasedIteratorConcept, HasDynamicXStepTypeConcept
37 ///
38 /// Planar pixels have channel data that is not consecutive in memory.
39 /// To abstract this we use classes to represent references and pointers to planar pixels.
40 ///
41 /// \ingroup PixelIteratorModelPlanarPtr ColorBaseModelPlanarPtr PixelBasedModel
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
93     /// Copy constructor and operator= from pointers to compatible planar pixels or planar pixel references.
94     /// That allow constructs like pointer = &value or pointer = &reference
95     /// Since we should not override operator& that's the best we can do.
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
114     /// For some reason operator[] provided by iterator_facade returns a custom class that is convertible to reference
115     /// We require our own reference because it is registered in iterator_traits
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
156 /////////////////////////////
157 //  ColorBasedConcept
158 /////////////////////////////
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
175 /////////////////////////////
176 //  HomogeneousPixelBasedConcept
177 /////////////////////////////
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
199 /////////////////////////////
200 //  MemoryBasedIteratorConcept
201 /////////////////////////////
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
237 /////////////////////////////
238 //  HasDynamicXStepTypeConcept
239 /////////////////////////////
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