Boost GIL


pixel.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 #ifndef GIL_PIXEL_H
13 #define GIL_PIXEL_H
14 
23 
24 #include <functional>
25 #include <boost/core/ignore_unused.hpp>
26 #include <boost/utility/enable_if.hpp>
27 #include <boost/mpl/bool.hpp>
28 #include <boost/mpl/front.hpp>
29 #include <boost/type_traits.hpp>
30 #include "gil_config.hpp"
31 #include "color_base.hpp"
32 #include "gil_concept.hpp"
33 #include "channel.hpp"
34 #include "metafunctions.hpp"
35 #include "utilities.hpp"
36 #include "color_base_algorithm.hpp"
37 
38 namespace boost { namespace gil {
39 
40 // Forward-declare gray_t
41 struct gray_color_t;
42 typedef mpl::vector1<gray_color_t> gray_t;
43 template <typename PixelBased> struct color_space_type;
44 template <typename PixelBased> struct channel_mapping_type;
45 template <typename PixelBased> struct channel_type;
46 template <typename PixelBased> struct is_planar;
47 
48 template <typename PixelBased> struct color_space_type<const PixelBased> : public color_space_type<PixelBased> {};
49 template <typename PixelBased> struct channel_mapping_type<const PixelBased> : public channel_mapping_type<PixelBased> {};
50 template <typename PixelBased> struct channel_type<const PixelBased> : public channel_type<PixelBased> {};
51 
52 template <typename PixelBased> struct is_planar : mpl::false_ {};
53 template <typename PixelBased> struct is_planar<const PixelBased> : public is_planar<PixelBased> {};
54 
55 
56 template <typename T> struct is_pixel : public mpl::false_{};
57 template <typename T> struct is_pixel<const T> : public is_pixel<T> {};
58 
61 template <typename PixelBased>
62 struct num_channels : public mpl::size<typename color_space_type<PixelBased>::type> {};
63 
80 
87 
103 
104 template <typename ChannelValue, typename Layout> // = mpl::range_c<int,0,ColorSpace::size> >
105 struct pixel : public detail::homogeneous_color_base<ChannelValue,Layout,mpl::size<typename Layout::color_space_t>::value> {
106 private:
107  typedef ChannelValue channel_t;
108  typedef detail::homogeneous_color_base<ChannelValue,Layout,mpl::size<typename Layout::color_space_t>::value> parent_t;
109 public:
110  typedef pixel value_type;
111  typedef value_type& reference;
112  typedef const value_type& const_reference;
113  BOOST_STATIC_CONSTANT(bool, is_mutable = channel_traits<channel_t>::is_mutable);
114 
115  pixel(){}
116  explicit pixel(channel_t v) : parent_t(v) {} // sets all channels to v
117  pixel(channel_t v0, channel_t v1) : parent_t(v0,v1) {}
118  pixel(channel_t v0, channel_t v1, channel_t v2) : parent_t(v0,v1,v2) {}
119  pixel(channel_t v0, channel_t v1, channel_t v2, channel_t v3) : parent_t(v0,v1,v2,v3) {}
120  pixel(channel_t v0, channel_t v1, channel_t v2, channel_t v3, channel_t v4) : parent_t(v0,v1,v2,v3,v4) {}
121  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) {}
122 
123  pixel(const pixel& p) : parent_t(p) {}
124  pixel& operator=(const pixel& p) { static_copy(p,*this); return *this; }
125 
126  // Construct from another compatible pixel type
127  template <typename Pixel> pixel(const Pixel& p, typename enable_if_c<is_pixel<Pixel>::value>::type* dummy = 0) : parent_t(p) {
128  check_compatible<Pixel>();
129  boost::ignore_unused(dummy);
130  }
131 
132  template <typename P> pixel& operator=(const P& p) { assign(p, mpl::bool_<is_pixel<P>::value>()); return *this; }
133  template <typename P> bool operator==(const P& p) const { return equal(p, mpl::bool_<is_pixel<P>::value>()); }
134 
135  template <typename P> bool operator!=(const P& p) const { return !(*this==p); }
136 
137  // homogeneous pixels have operator[]
138  typename channel_traits<channel_t>::reference operator[](std::size_t i) { return dynamic_at_c(*this,i); }
139  typename channel_traits<channel_t>::const_reference operator[](std::size_t i) const { return dynamic_at_c(*this,i); }
140 private:
141  template <typename Pixel> void assign(const Pixel& p, mpl::true_) { check_compatible<Pixel>(); static_copy(p,*this); }
142  template <typename Pixel> bool equal(const Pixel& p, mpl::true_) const { check_compatible<Pixel>(); return static_equal(*this,p); }
143 
144  template <typename Pixel> void check_compatible() const { gil_function_requires<PixelsCompatibleConcept<Pixel,pixel> >(); }
145 
146 // Support for assignment/equality comparison of a channel with a grayscale pixel
147 
148 private:
149  static void check_gray() { BOOST_STATIC_ASSERT((is_same<typename Layout::color_space_t, gray_t>::value)); }
150  template <typename Channel> void assign(const Channel& chan, mpl::false_) { check_gray(); gil::at_c<0>(*this)=chan; }
151  template <typename Channel> bool equal (const Channel& chan, mpl::false_) const { check_gray(); return gil::at_c<0>(*this)==chan; }
152 public:
153  pixel& operator= (channel_t chan) { check_gray(); gil::at_c<0>(*this)=chan; return *this; }
154  bool operator==(channel_t chan) const { check_gray(); return gil::at_c<0>(*this)==chan; }
155 };
156 
158 // ColorBasedConcept
160 
161 template <typename ChannelValue, typename Layout, int K>
162 struct kth_element_type<pixel<ChannelValue,Layout>, K> {
163  typedef ChannelValue type;
164 };
165 
166 template <typename ChannelValue, typename Layout, int K>
167 struct kth_element_reference_type<pixel<ChannelValue,Layout>, K> {
168  typedef typename channel_traits<ChannelValue>::reference type;
169 };
170 
171 template <typename ChannelValue, typename Layout, int K>
172 struct kth_element_reference_type<const pixel<ChannelValue,Layout>, K> {
173  typedef typename channel_traits<ChannelValue>::const_reference type;
174 };
175 
176 template <typename ChannelValue, typename Layout, int K>
177 struct kth_element_const_reference_type<pixel<ChannelValue,Layout>, K> {
178  typedef typename channel_traits<ChannelValue>::const_reference type;
179 };
180 
182 // PixelConcept
184 
185 template <typename ChannelValue, typename Layout>
186 struct is_pixel<pixel<ChannelValue,Layout> > : public mpl::true_{};
187 
189 // HomogeneousPixelBasedConcept
191 
192 template <typename ChannelValue, typename Layout>
193 struct color_space_type<pixel<ChannelValue,Layout> > {
194  typedef typename Layout::color_space_t type;
195 };
196 
197 template <typename ChannelValue, typename Layout>
198 struct channel_mapping_type<pixel<ChannelValue,Layout> > {
199  typedef typename Layout::channel_mapping_t type;
200 };
201 
202 template <typename ChannelValue, typename Layout>
203 struct is_planar<pixel<ChannelValue,Layout> > : public mpl::false_ {};
204 
205 template <typename ChannelValue, typename Layout>
206 struct channel_type<pixel<ChannelValue,Layout> > {
207  typedef ChannelValue type;
208 };
209 
210 } } // namespace boost::gil
211 
212 namespace boost {
213  template <typename ChannelValue, typename Layout>
214  struct has_trivial_constructor<gil::pixel<ChannelValue,Layout> > : public has_trivial_constructor<ChannelValue> {};
215 }
216 #endif
pixel related algorithms
Channel utilities.
metafunctions that construct types or return type properties
Concept check classes for GIL concepts.
pixel class and related utilities
GIL configuration file.
Various utilities not specific to the image library. Some are non-standard STL extensions or generic ...