Boost GIL


packed_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 
13 #ifndef GIL_PACKED_PIXEL_H
14 #define GIL_PACKED_PIXEL_H
15 
24 
25 #include <functional>
26 #include <boost/utility/enable_if.hpp>
27 #include <boost/mpl/bool.hpp>
28 #include <boost/mpl/front.hpp>
29 #include "gil_config.hpp"
30 #include "pixel.hpp"
31 
32 namespace boost { namespace gil {
33 
37 
56 template <typename BitField, // A type that holds the bits of the pixel. Typically an integral type, like boost::uint16_t
60  typename ChannelRefVec, // An MPL vector whose elements are packed channels. They must be constructible from BitField. GIL uses packed_channel_reference
61  typename Layout> // Layout defining the color space and ordering of the channels. Example value: rgb_layout_t
62 struct packed_pixel {
63  BitField _bitfield;
64 
65  typedef Layout layout_t;
66  typedef packed_pixel value_type;
67  typedef value_type& reference;
68  typedef const value_type& const_reference;
69 
70  BOOST_STATIC_CONSTANT(bool, is_mutable = channel_traits<typename mpl::front<ChannelRefVec>::type>::is_mutable);
71 
72  packed_pixel(){}
73  explicit packed_pixel(const BitField& bitfield) : _bitfield(bitfield) {}
74 
75  // Construct from another compatible pixel type
76  packed_pixel(const packed_pixel& p) : _bitfield(p._bitfield) {}
77  template <typename P> packed_pixel(const P& p, typename enable_if_c<is_pixel<P>::value>::type* d=0) { check_compatible<P>(); static_copy(p,*this); }
78  packed_pixel(int chan0, int chan1) : _bitfield(0) {
79  BOOST_STATIC_ASSERT((num_channels<packed_pixel>::value==2));
80  gil::at_c<0>(*this)=chan0; gil::at_c<1>(*this)=chan1;
81  }
82  packed_pixel(int chan0, int chan1, int chan2) : _bitfield(0) {
83  BOOST_STATIC_ASSERT((num_channels<packed_pixel>::value==3));
84  gil::at_c<0>(*this) = chan0;
85  gil::at_c<1>(*this) = chan1;
86  gil::at_c<2>(*this) = chan2;
87  }
88  packed_pixel(int chan0, int chan1, int chan2, int chan3) : _bitfield(0) {
89  BOOST_STATIC_ASSERT((num_channels<packed_pixel>::value==4));
90  gil::at_c<0>(*this)=chan0; gil::at_c<1>(*this)=chan1; gil::at_c<2>(*this)=chan2; gil::at_c<3>(*this)=chan3;
91  }
92  packed_pixel(int chan0, int chan1, int chan2, int chan3, int chan4) : _bitfield(0) {
93  BOOST_STATIC_ASSERT((num_channels<packed_pixel>::value==5));
94  gil::at_c<0>(*this)=chan0; gil::at_c<1>(*this)=chan1; gil::at_c<2>(*this)=chan2; gil::at_c<3>(*this)=chan3; gil::at_c<4>(*this)=chan4;
95  }
96 
97  packed_pixel& operator=(const packed_pixel& p) { _bitfield=p._bitfield; return *this; }
98 
99  template <typename P> packed_pixel& operator=(const P& p) { assign(p, mpl::bool_<is_pixel<P>::value>()); return *this; }
100  template <typename P> bool operator==(const P& p) const { return equal(p, mpl::bool_<is_pixel<P>::value>()); }
101 
102  template <typename P> bool operator!=(const P& p) const { return !(*this==p); }
103 
104 private:
105  template <typename Pixel> static void check_compatible() { gil_function_requires<PixelsCompatibleConcept<Pixel,packed_pixel> >(); }
106  template <typename Pixel> void assign(const Pixel& p, mpl::true_) { check_compatible<Pixel>(); static_copy(p,*this); }
107  template <typename Pixel> bool equal(const Pixel& p, mpl::true_) const { check_compatible<Pixel>(); return static_equal(*this,p); }
108 
109 // Support for assignment/equality comparison of a channel with a grayscale pixel
110  static void check_gray() { BOOST_STATIC_ASSERT((is_same<typename Layout::color_space_t, gray_t>::value)); }
111  template <typename Channel> void assign(const Channel& chan, mpl::false_) { check_gray(); gil::at_c<0>(*this)=chan; }
112  template <typename Channel> bool equal (const Channel& chan, mpl::false_) const { check_gray(); return gil::at_c<0>(*this)==chan; }
113 public:
114  packed_pixel& operator= (int chan) { check_gray(); gil::at_c<0>(*this)=chan; return *this; }
115  bool operator==(int chan) const { check_gray(); return gil::at_c<0>(*this)==chan; }
116 };
117 
119 // ColorBasedConcept
121 
122 template <typename BitField, typename ChannelRefVec, typename Layout, int K>
123 struct kth_element_type<packed_pixel<BitField,ChannelRefVec,Layout>,K> : public mpl::at_c<ChannelRefVec,K> {};
124 
125 template <typename BitField, typename ChannelRefVec, typename Layout, int K>
126 struct kth_element_reference_type<packed_pixel<BitField,ChannelRefVec,Layout>,K> : public mpl::at_c<ChannelRefVec,K> {};
127 
128 template <typename BitField, typename ChannelRefVec, typename Layout, int K>
129 struct kth_element_const_reference_type<packed_pixel<BitField,ChannelRefVec,Layout>,K> {
130  typedef typename channel_traits<typename mpl::at_c<ChannelRefVec,K>::type>::const_reference type;
131 };
132 
133 template <int K, typename P, typename C, typename L> inline
134 typename kth_element_reference_type<packed_pixel<P,C,L>, K>::type
135 at_c(packed_pixel<P,C,L>& p) {
136  return typename kth_element_reference_type<packed_pixel<P,C,L>, K>::type(&p._bitfield);
137 }
138 
139 template <int K, typename P, typename C, typename L> inline
140 typename kth_element_const_reference_type<packed_pixel<P,C,L>, K>::type
141 at_c(const packed_pixel<P,C,L>& p) {
142  return typename kth_element_const_reference_type<packed_pixel<P,C,L>, K>::type(&p._bitfield);
143 }
144 
146 // PixelConcept
148 
149 // Metafunction predicate that flags packed_pixel as a model of PixelConcept. Required by PixelConcept
150 template <typename BitField, typename ChannelRefVec, typename Layout>
151 struct is_pixel<packed_pixel<BitField,ChannelRefVec,Layout> > : public mpl::true_{};
152 
154 // PixelBasedConcept
156 
157 template <typename P, typename C, typename Layout>
158 struct color_space_type<packed_pixel<P,C,Layout> > {
159  typedef typename Layout::color_space_t type;
160 };
161 
162 template <typename P, typename C, typename Layout>
163 struct channel_mapping_type<packed_pixel<P,C,Layout> > {
164  typedef typename Layout::channel_mapping_t type;
165 };
166 
167 template <typename P, typename C, typename Layout>
168 struct is_planar<packed_pixel<P,C,Layout> > : mpl::false_ {};
169 
170 
176 
181 
182 template <typename P, typename C, typename L>
183 struct iterator_is_mutable<packed_pixel<P,C,L>*> : public mpl::bool_<packed_pixel<P,C,L>::is_mutable> {};
184 template <typename P, typename C, typename L>
185 struct iterator_is_mutable<const packed_pixel<P,C,L>*> : public mpl::false_ {};
186 
187 
188 
189 } } // namespace boost::gil
190 
191 namespace boost {
192  template <typename P, typename C, typename L>
193  struct has_trivial_constructor<gil::packed_pixel<P,C,L> > : public has_trivial_constructor<P> {};
194 }
195 #endif
GIL configuration file.
pixel class and related utilities