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