Boost GIL


pixel.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_PIXEL_HPP
9 #define BOOST_GIL_PIXEL_HPP
10 
11 #include <boost/gil/channel.hpp>
12 #include <boost/gil/color_base.hpp>
13 #include <boost/gil/color_base_algorithm.hpp>
14 #include <boost/gil/concepts.hpp>
15 #include <boost/gil/metafunctions.hpp>
16 #include <boost/gil/utilities.hpp>
17 
18 #include <boost/core/ignore_unused.hpp>
19 #include <boost/mpl/bool.hpp>
20 #include <boost/mpl/front.hpp>
21 #include <boost/type_traits.hpp>
22 
23 #include <functional>
24 #include <type_traits>
25 
26 namespace boost { namespace gil {
27 
28 // Forward-declare gray_t
29 struct gray_color_t;
30 using gray_t = mpl::vector1<gray_color_t>;
31 template <typename PixelBased> struct color_space_type;
32 template <typename PixelBased> struct channel_mapping_type;
33 template <typename PixelBased> struct channel_type;
34 template <typename PixelBased> struct is_planar;
35 
36 template <typename PixelBased> struct color_space_type<const PixelBased> : public color_space_type<PixelBased> {};
37 template <typename PixelBased> struct channel_mapping_type<const PixelBased> : public channel_mapping_type<PixelBased> {};
38 template <typename PixelBased> struct channel_type<const PixelBased> : public channel_type<PixelBased> {};
39 
40 template <typename PixelBased> struct is_planar : mpl::false_ {};
41 template <typename PixelBased> struct is_planar<const PixelBased> : public is_planar<PixelBased> {};
42 
43 
44 template <typename T> struct is_pixel : public mpl::false_{};
45 template <typename T> struct is_pixel<const T> : public is_pixel<T> {};
46 
49 template <typename PixelBased>
50 struct num_channels : public mpl::size<typename color_space_type<PixelBased>::type> {};
51 
68 
75 
91 
92 template <typename ChannelValue, typename Layout> // = mpl::range_c<int,0,ColorSpace::size> >
93 struct pixel : public detail::homogeneous_color_base<ChannelValue,Layout,mpl::size<typename Layout::color_space_t>::value> {
94 private:
95  using channel_t = ChannelValue;
96  using parent_t = detail::homogeneous_color_base<ChannelValue,Layout,mpl::size<typename Layout::color_space_t>::value>;
97 public:
98  using value_type = pixel<ChannelValue, Layout>;
99  using reference = value_type&;
100  using const_reference = value_type const&;
101  static constexpr bool is_mutable = channel_traits<channel_t>::is_mutable;
102 
103  pixel(){}
104  explicit pixel(channel_t v) : parent_t(v) {} // sets all channels to v
105  pixel(channel_t v0, channel_t v1) : parent_t(v0,v1) {}
106  pixel(channel_t v0, channel_t v1, channel_t v2) : parent_t(v0,v1,v2) {}
107  pixel(channel_t v0, channel_t v1, channel_t v2, channel_t v3) : parent_t(v0,v1,v2,v3) {}
108  pixel(channel_t v0, channel_t v1, channel_t v2, channel_t v3, channel_t v4) : parent_t(v0,v1,v2,v3,v4) {}
109  pixel(channel_t v0, channel_t v1, channel_t v2, channel_t v3, channel_t v4, channel_t v5) : parent_t(v0,v1,v2,v3,v4,v5) {}
110 
111  pixel(const pixel& p) : parent_t(p) {}
112  pixel& operator=(const pixel& p) { static_copy(p,*this); return *this; }
113 
114  // Construct from another compatible pixel type
115  template <typename Pixel>
116  pixel(Pixel const& p,
117  typename std::enable_if<is_pixel<Pixel>::value>::type* /*dummy*/ = nullptr)
118  : parent_t(p)
119  {
120  check_compatible<Pixel>();
121  }
122 
123  template <typename P> pixel& operator=(const P& p) { assign(p, mpl::bool_<is_pixel<P>::value>()); return *this; }
124  template <typename P> bool operator==(const P& p) const { return equal(p, mpl::bool_<is_pixel<P>::value>()); }
125 
126  template <typename P> bool operator!=(const P& p) const { return !(*this==p); }
127 
128  // homogeneous pixels have operator[]
129  typename channel_traits<channel_t>::reference operator[](std::size_t i) { return dynamic_at_c(*this,i); }
130  typename channel_traits<channel_t>::const_reference operator[](std::size_t i) const { return dynamic_at_c(*this,i); }
131 private:
132  template <typename Pixel> void assign(const Pixel& p, mpl::true_) { check_compatible<Pixel>(); static_copy(p,*this); }
133  template <typename Pixel> bool equal(const Pixel& p, mpl::true_) const { check_compatible<Pixel>(); return static_equal(*this,p); }
134 
135  template <typename Pixel> void check_compatible() const { gil_function_requires<PixelsCompatibleConcept<Pixel,pixel> >(); }
136 
137 // Support for assignment/equality comparison of a channel with a grayscale pixel
138 
139 private:
140  static void check_gray()
141  {
142  static_assert(is_same<typename Layout::color_space_t, gray_t>::value, "");
143  }
144  template <typename Channel> void assign(const Channel& chan, mpl::false_) { check_gray(); gil::at_c<0>(*this)=chan; }
145  template <typename Channel> bool equal (const Channel& chan, mpl::false_) const { check_gray(); return gil::at_c<0>(*this)==chan; }
146 public:
147  pixel& operator= (channel_t chan) { check_gray(); gil::at_c<0>(*this)=chan; return *this; }
148  bool operator==(channel_t chan) const { check_gray(); return gil::at_c<0>(*this)==chan; }
149 };
150 
152 // ColorBasedConcept
154 
155 template <typename ChannelValue, typename Layout, int K>
156 struct kth_element_type<pixel<ChannelValue,Layout>, K> {
157  using type = ChannelValue;
158 };
159 
160 template <typename ChannelValue, typename Layout, int K>
161 struct kth_element_reference_type<pixel<ChannelValue,Layout>, K> {
162  using type = typename channel_traits<ChannelValue>::reference;
163 };
164 
165 template <typename ChannelValue, typename Layout, int K>
166 struct kth_element_reference_type<const pixel<ChannelValue,Layout>, K> {
167  using type = typename channel_traits<ChannelValue>::const_reference;
168 };
169 
170 template <typename ChannelValue, typename Layout, int K>
171 struct kth_element_const_reference_type<pixel<ChannelValue,Layout>, K> {
172  using type = typename channel_traits<ChannelValue>::const_reference;
173 };
174 
176 // PixelConcept
178 
179 template <typename ChannelValue, typename Layout>
180 struct is_pixel<pixel<ChannelValue,Layout> > : public mpl::true_{};
181 
183 // HomogeneousPixelBasedConcept
185 
186 template <typename ChannelValue, typename Layout>
187 struct color_space_type<pixel<ChannelValue,Layout> > {
188  using type = typename Layout::color_space_t;
189 };
190 
191 template <typename ChannelValue, typename Layout>
192 struct channel_mapping_type<pixel<ChannelValue,Layout> > {
193  using type = typename Layout::channel_mapping_t;
194 };
195 
196 template <typename ChannelValue, typename Layout>
197 struct is_planar<pixel<ChannelValue,Layout> > : public mpl::false_ {};
198 
199 template <typename ChannelValue, typename Layout>
200 struct channel_type<pixel<ChannelValue,Layout> > {
201  using type = ChannelValue;
202 };
203 
204 }} // namespace boost::gil
205 
206 namespace boost {
207  template <typename ChannelValue, typename Layout>
208  struct has_trivial_constructor<gil::pixel<ChannelValue,Layout> > : public has_trivial_constructor<ChannelValue> {};
209 }
210 
211 #endif
Definition: algorithm.hpp:30
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