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_REFERENCE_HPP
9 #define BOOST_GIL_PLANAR_PIXEL_REFERENCE_HPP
10
11 #include <boost/gil/channel.hpp>
12 #include <boost/gil/color_base.hpp>
13 #include <boost/gil/concepts.hpp>
14 #include <boost/gil/pixel.hpp>
15 #include <boost/gil/planar_pixel_iterator.hpp>
16 #include <boost/gil/detail/mp11.hpp>
17
18 #include <type_traits>
19
20 namespace boost { namespace gil {
21
22 /// \defgroup ColorBaseModelPlanarRef planar_pixel_reference
23 /// \ingroup ColorBaseModel
24 /// \brief A homogeneous color base whose element is a channel reference. Models HomogeneousColorBaseConcept, HomogeneousPixelConcept.
25 /// This class is used as a reference proxy to a planar pixel.
26
27 /// \defgroup PixelModelPlanarRef planar_pixel_reference
28 /// \ingroup PixelModel
29 /// \brief A reference proxy to a planar pixel. Models HomogeneousColorBaseConcept, HomogeneousPixelConcept.
30
31 /// \ingroup PixelModelPlanarRef ColorBaseModelPlanarRef PixelBasedModel
32 /// \brief A reference proxy to a planar pixel.
33 ///
34 /// A reference to a planar pixel is a proxy class containing references to each of the corresponding channels.
35 /// Models: HomogeneousColorBaseConcept, HomogeneousPixelConcept
36 ///
37 /// \tparam ChannelReference A channel reference, either const or mutable
38 /// \tparam ColorSpace
39 template <typename ChannelReference, typename ColorSpace>
40 struct planar_pixel_reference : detail::homogeneous_color_base
41 <
42 ChannelReference,
43 layout<ColorSpace>,
44 mp11::mp_size<ColorSpace>::value
45 >
46 {
47 using parent_t =detail::homogeneous_color_base
48 <
49 ChannelReference,
50 layout<ColorSpace>,
51 mp11::mp_size<ColorSpace>::value
52 >;
53
54 private:
55 // These three are only defined for homogeneous pixels
56 using channel_t = typename channel_traits<ChannelReference>::value_type;
57 using channel_const_reference = typename channel_traits<ChannelReference>::const_reference;
58
59 public:
60 static constexpr bool is_mutable = channel_traits<ChannelReference>::is_mutable;
61 using value_type = pixel<channel_t,layout<ColorSpace>>;
62 using reference = planar_pixel_reference<ChannelReference, ColorSpace>;
63 using const_reference = planar_pixel_reference<channel_const_reference,ColorSpace>;
64
planar_pixel_referenceboost::gil::planar_pixel_reference65 planar_pixel_reference(ChannelReference v0, ChannelReference v1)
66 : parent_t(v0, v1)
67 {}
68
planar_pixel_referenceboost::gil::planar_pixel_reference69 planar_pixel_reference(ChannelReference v0, ChannelReference v1, ChannelReference v2)
70 : parent_t(v0, v1, v2)
71 {}
72
planar_pixel_referenceboost::gil::planar_pixel_reference73 planar_pixel_reference(ChannelReference v0, ChannelReference v1, ChannelReference v2, ChannelReference v3)
74 : parent_t(v0, v1, v2, v3)
75 {}
76
planar_pixel_referenceboost::gil::planar_pixel_reference77 planar_pixel_reference(ChannelReference v0, ChannelReference v1, ChannelReference v2, ChannelReference v3, ChannelReference v4)
78 : parent_t(v0, v1, v2, v3, v4)
79 {}
80
planar_pixel_referenceboost::gil::planar_pixel_reference81 planar_pixel_reference(ChannelReference v0, ChannelReference v1, ChannelReference v2, ChannelReference v3, ChannelReference v4, ChannelReference v5)
82 : parent_t(v0, v1, v2, v3, v4, v5)
83 {}
84
planar_pixel_referenceboost::gil::planar_pixel_reference85 planar_pixel_reference(planar_pixel_reference const& p) : parent_t(p) {}
86
87 // TODO: What is the purpose of returning via const reference?
operator =boost::gil::planar_pixel_reference88 auto operator=(planar_pixel_reference const& p) const -> planar_pixel_reference const&
89 {
90 static_copy(p, *this);
91 return *this;
92 }
93
94 template <typename Pixel>
planar_pixel_referenceboost::gil::planar_pixel_reference95 planar_pixel_reference(Pixel const& p) : parent_t(p)
96 {
97 check_compatible<Pixel>();
98 }
99
100 // TODO: What is the purpose of returning via const reference?
101 template <typename Pixel>
operator =boost::gil::planar_pixel_reference102 auto operator=(Pixel const& p) const -> planar_pixel_reference const&
103 {
104 check_compatible<Pixel>();
105 static_copy(p, *this);
106 return *this;
107 }
108
109 // PERFORMANCE_CHECK: Is this constructor necessary?
110 template <typename ChannelV, typename Mapping>
planar_pixel_referenceboost::gil::planar_pixel_reference111 planar_pixel_reference(pixel<ChannelV, layout<ColorSpace, Mapping>>& p)
112 : parent_t(p)
113 {
114 check_compatible<pixel<ChannelV, layout<ColorSpace, Mapping>>>();
115 }
116
117 // Construct at offset from a given location
118 template <typename ChannelPtr>
planar_pixel_referenceboost::gil::planar_pixel_reference119 planar_pixel_reference(planar_pixel_iterator<ChannelPtr, ColorSpace> const& p, std::ptrdiff_t diff)
120 : parent_t(p, diff)
121 {}
122
123 // This overload is necessary for a compiler implementing Core Issue 574
124 // to prevent generation of an implicit copy assignment operator (the reason
125 // for generating implicit copy assignment operator is that according to
126 // Core Issue 574, a cv-qualified assignment operator is not considered
127 // "copy assignment operator").
128 // EDG implemented Core Issue 574 starting with EDG Version 3.8. I'm not
129 // sure why they did it for a template member function as well.
130 #if BOOST_WORKAROUND(__HP_aCC, >= 61700) || BOOST_WORKAROUND(__INTEL_COMPILER, >= 1000)
operator =boost::gil::planar_pixel_reference131 const planar_pixel_reference& operator=(const planar_pixel_reference& p) { static_copy(p,*this); return *this; }
operator =boost::gil::planar_pixel_reference132 template <typename P> const planar_pixel_reference& operator=(const P& p) { check_compatible<P>(); static_copy(p,*this); return *this; }
133 #endif
134
135 template <typename Pixel>
operator ==boost::gil::planar_pixel_reference136 bool operator==(Pixel const& p) const
137 {
138 check_compatible<Pixel>();
139 return static_equal(*this, p);
140 }
141
142 template <typename Pixel>
operator !=boost::gil::planar_pixel_reference143 bool operator!=(Pixel const &p) const { return !(*this == p); }
144
operator []boost::gil::planar_pixel_reference145 auto operator[](std::size_t i) const -> ChannelReference { return this->at_c_dynamic(i); }
operator ->boost::gil::planar_pixel_reference146 auto operator->() const -> planar_pixel_reference const* { return this; }
147
148 private:
149 template <typename Pixel>
check_compatibleboost::gil::planar_pixel_reference150 static void check_compatible()
151 {
152 gil_function_requires<PixelsCompatibleConcept<Pixel, planar_pixel_reference>>();
153 }
154 };
155
156 /////////////////////////////
157 // ColorBasedConcept
158 /////////////////////////////
159
160 template <typename ChannelReference, typename ColorSpace, int K>
161 struct kth_element_type<planar_pixel_reference<ChannelReference, ColorSpace>, K>
162 {
163 using type = ChannelReference;
164 };
165
166 template <typename ChannelReference, typename ColorSpace, int K>
167 struct kth_element_reference_type
168 <
169 planar_pixel_reference<ChannelReference, ColorSpace>,
170 K
171 >
172 {
173 using type = ChannelReference;
174 };
175
176 template <typename ChannelReference, typename ColorSpace, int K>
177 struct kth_element_const_reference_type
178 <
179 planar_pixel_reference<ChannelReference, ColorSpace>,
180 K
181 >
182 : std::add_lvalue_reference<typename std::add_const<ChannelReference>::type>
183 {
184 // using type = typename channel_traits<ChannelReference>::const_reference;
185 };
186
187 /////////////////////////////
188 // PixelConcept
189 /////////////////////////////
190
191 /// \brief Metafunction predicate that flags planar_pixel_reference as a model of PixelConcept. Required by PixelConcept
192 /// \ingroup PixelModelPlanarRef
193 template <typename ChannelReference, typename ColorSpace>
194 struct is_pixel< planar_pixel_reference<ChannelReference, ColorSpace>>
195 : std::true_type
196 {};
197
198 /////////////////////////////
199 // HomogeneousPixelBasedConcept
200 /////////////////////////////
201
202 /// \brief Specifies the color space type of a planar pixel reference. Required by PixelBasedConcept
203 /// \ingroup PixelModelPlanarRef
204 template <typename ChannelReference, typename ColorSpace>
205 struct color_space_type<planar_pixel_reference<ChannelReference,ColorSpace> > {
206 using type = ColorSpace;
207 };
208
209 /// \brief Specifies the color space type of a planar pixel reference. Required by PixelBasedConcept
210 /// \ingroup PixelModelPlanarRef
211 template <typename ChannelReference, typename ColorSpace>
212 struct channel_mapping_type<planar_pixel_reference<ChannelReference,ColorSpace> > {
213 using type = typename layout<ColorSpace>::channel_mapping_t;
214 };
215
216 /// \brief Specifies that planar_pixel_reference represents a planar construct. Required by PixelBasedConcept
217 /// \ingroup PixelModelPlanarRef
218 template <typename ChannelReference, typename ColorSpace>
219 struct is_planar<planar_pixel_reference<ChannelReference, ColorSpace>>
220 : std::true_type
221 {};
222
223 /// \brief Specifies the color space type of a planar pixel reference. Required by HomogeneousPixelBasedConcept
224 /// \ingroup PixelModelPlanarRef
225 template <typename ChannelReference, typename ColorSpace>
226 struct channel_type<planar_pixel_reference<ChannelReference,ColorSpace> > {
227 using type = typename channel_traits<ChannelReference>::value_type;
228 };
229
230 }} // namespace boost::gil
231
232 namespace std {
233 // We are forced to define swap inside std namespace because on some platforms (Visual Studio 8) STL calls swap qualified.
234 // swap with 'left bias':
235 // - swap between proxy and anything
236 // - swap between value type and proxy
237 // - swap between proxy and proxy
238 // Having three overloads allows us to swap between different (but compatible) models of PixelConcept
239
240 /// \brief swap for planar_pixel_reference
241 /// \ingroup PixelModelPlanarRef
242 template <typename CR, typename CS, typename R> inline
swap(const boost::gil::planar_pixel_reference<CR,CS> x,R & y)243 void swap(const boost::gil::planar_pixel_reference<CR,CS> x, R& y) {
244 boost::gil::swap_proxy<typename boost::gil::planar_pixel_reference<CR,CS>::value_type>(x,y);
245 }
246
247 /// \brief swap for planar_pixel_reference
248 /// \ingroup PixelModelPlanarRef
249 template <typename CR, typename CS> inline
swap(typename boost::gil::planar_pixel_reference<CR,CS>::value_type & x,const boost::gil::planar_pixel_reference<CR,CS> y)250 void swap(typename boost::gil::planar_pixel_reference<CR,CS>::value_type& x, const boost::gil::planar_pixel_reference<CR,CS> y) {
251 boost::gil::swap_proxy<typename boost::gil::planar_pixel_reference<CR,CS>::value_type>(x,y);
252 }
253
254 /// \brief swap for planar_pixel_reference
255 /// \ingroup PixelModelPlanarRef
256 template <typename CR, typename CS> inline
swap(const boost::gil::planar_pixel_reference<CR,CS> x,const boost::gil::planar_pixel_reference<CR,CS> y)257 void swap(const boost::gil::planar_pixel_reference<CR,CS> x, const boost::gil::planar_pixel_reference<CR,CS> y) {
258 boost::gil::swap_proxy<typename boost::gil::planar_pixel_reference<CR,CS>::value_type>(x,y);
259 }
260
261 } // namespace std
262
263 #endif
264