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