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 #include <boost/utility/enable_if.hpp>
23 
24 #include <functional>
25 
26 namespace boost { namespace gil {
27 
28 // Forward-declare gray_t
29 struct gray_color_t;
30 typedef mpl::vector1<gray_color_t> gray_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  typedef ChannelValue channel_t;
96  typedef detail::homogeneous_color_base<ChannelValue,Layout,mpl::size<typename Layout::color_space_t>::value> parent_t;
97 public:
98  typedef pixel value_type;
99  typedef value_type& reference;
100  typedef const value_type& const_reference;
101  BOOST_STATIC_CONSTANT(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> pixel(const Pixel& p, typename enable_if_c<is_pixel<Pixel>::value>::type* dummy = 0) : parent_t(p) {
116  check_compatible<Pixel>();
117  boost::ignore_unused(dummy);
118  }
119 
120  template <typename P> pixel& operator=(const P& p) { assign(p, mpl::bool_<is_pixel<P>::value>()); return *this; }
121  template <typename P> bool operator==(const P& p) const { return equal(p, mpl::bool_<is_pixel<P>::value>()); }
122 
123  template <typename P> bool operator!=(const P& p) const { return !(*this==p); }
124 
125  // homogeneous pixels have operator[]
126  typename channel_traits<channel_t>::reference operator[](std::size_t i) { return dynamic_at_c(*this,i); }
127  typename channel_traits<channel_t>::const_reference operator[](std::size_t i) const { return dynamic_at_c(*this,i); }
128 private:
129  template <typename Pixel> void assign(const Pixel& p, mpl::true_) { check_compatible<Pixel>(); static_copy(p,*this); }
130  template <typename Pixel> bool equal(const Pixel& p, mpl::true_) const { check_compatible<Pixel>(); return static_equal(*this,p); }
131 
132  template <typename Pixel> void check_compatible() const { gil_function_requires<PixelsCompatibleConcept<Pixel,pixel> >(); }
133 
134 // Support for assignment/equality comparison of a channel with a grayscale pixel
135 
136 private:
137  static void check_gray() { BOOST_STATIC_ASSERT((is_same<typename Layout::color_space_t, gray_t>::value)); }
138  template <typename Channel> void assign(const Channel& chan, mpl::false_) { check_gray(); gil::at_c<0>(*this)=chan; }
139  template <typename Channel> bool equal (const Channel& chan, mpl::false_) const { check_gray(); return gil::at_c<0>(*this)==chan; }
140 public:
141  pixel& operator= (channel_t chan) { check_gray(); gil::at_c<0>(*this)=chan; return *this; }
142  bool operator==(channel_t chan) const { check_gray(); return gil::at_c<0>(*this)==chan; }
143 };
144 
146 // ColorBasedConcept
148 
149 template <typename ChannelValue, typename Layout, int K>
150 struct kth_element_type<pixel<ChannelValue,Layout>, K> {
151  typedef ChannelValue type;
152 };
153 
154 template <typename ChannelValue, typename Layout, int K>
155 struct kth_element_reference_type<pixel<ChannelValue,Layout>, K> {
156  typedef typename channel_traits<ChannelValue>::reference type;
157 };
158 
159 template <typename ChannelValue, typename Layout, int K>
160 struct kth_element_reference_type<const pixel<ChannelValue,Layout>, K> {
161  typedef typename channel_traits<ChannelValue>::const_reference type;
162 };
163 
164 template <typename ChannelValue, typename Layout, int K>
165 struct kth_element_const_reference_type<pixel<ChannelValue,Layout>, K> {
166  typedef typename channel_traits<ChannelValue>::const_reference type;
167 };
168 
170 // PixelConcept
172 
173 template <typename ChannelValue, typename Layout>
174 struct is_pixel<pixel<ChannelValue,Layout> > : public mpl::true_{};
175 
177 // HomogeneousPixelBasedConcept
179 
180 template <typename ChannelValue, typename Layout>
181 struct color_space_type<pixel<ChannelValue,Layout> > {
182  typedef typename Layout::color_space_t type;
183 };
184 
185 template <typename ChannelValue, typename Layout>
186 struct channel_mapping_type<pixel<ChannelValue,Layout> > {
187  typedef typename Layout::channel_mapping_t type;
188 };
189 
190 template <typename ChannelValue, typename Layout>
191 struct is_planar<pixel<ChannelValue,Layout> > : public mpl::false_ {};
192 
193 template <typename ChannelValue, typename Layout>
194 struct channel_type<pixel<ChannelValue,Layout> > {
195  typedef ChannelValue type;
196 };
197 
198 }} // namespace boost::gil
199 
200 namespace boost {
201  template <typename ChannelValue, typename Layout>
202  struct has_trivial_constructor<gil::pixel<ChannelValue,Layout> > : public has_trivial_constructor<ChannelValue> {};
203 }
204 
205 #endif