Boost GIL


packed_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_PACKED_PIXEL_HPP
9 #define BOOST_GIL_PACKED_PIXEL_HPP
10 
11 #include <boost/gil/pixel.hpp>
12 
13 #include <boost/core/ignore_unused.hpp>
14 #include <boost/utility/enable_if.hpp>
15 #include <boost/mpl/bool.hpp>
16 #include <boost/mpl/front.hpp>
17 
18 #include <functional>
19 
20 namespace boost { namespace gil {
21 
24 
28 
47 template <typename BitField, // A type that holds the bits of the pixel. Typically an integral type, like std::uint16_t
51  typename ChannelRefVec, // An MPL vector whose elements are packed channels. They must be constructible from BitField. GIL uses packed_channel_reference
52  typename Layout> // Layout defining the color space and ordering of the channels. Example value: rgb_layout_t
53 struct packed_pixel {
54  BitField _bitfield;
55 
56  typedef Layout layout_t;
57  typedef packed_pixel value_type;
58  typedef value_type& reference;
59  typedef const value_type& const_reference;
60 
61  BOOST_STATIC_CONSTANT(bool, is_mutable = channel_traits<typename mpl::front<ChannelRefVec>::type>::is_mutable);
62 
63  packed_pixel(){}
64  explicit packed_pixel(const BitField& bitfield) : _bitfield(bitfield) {}
65 
66  // Construct from another compatible pixel type
67  packed_pixel(const packed_pixel& p) : _bitfield(p._bitfield) {}
68  template <typename P> packed_pixel(const P& p, typename enable_if_c<is_pixel<P>::value>::type* d=0) {
69  check_compatible<P>(); static_copy(p,*this);
70  boost::ignore_unused(d);
71  }
72  packed_pixel(int chan0, int chan1) : _bitfield(0) {
73  BOOST_STATIC_ASSERT((num_channels<packed_pixel>::value==2));
74  gil::at_c<0>(*this)=chan0; gil::at_c<1>(*this)=chan1;
75  }
76  packed_pixel(int chan0, int chan1, int chan2) : _bitfield(0) {
77  BOOST_STATIC_ASSERT((num_channels<packed_pixel>::value==3));
78  gil::at_c<0>(*this) = chan0;
79  gil::at_c<1>(*this) = chan1;
80  gil::at_c<2>(*this) = chan2;
81  }
82  packed_pixel(int chan0, int chan1, int chan2, int chan3) : _bitfield(0) {
83  BOOST_STATIC_ASSERT((num_channels<packed_pixel>::value==4));
84  gil::at_c<0>(*this)=chan0; gil::at_c<1>(*this)=chan1; gil::at_c<2>(*this)=chan2; gil::at_c<3>(*this)=chan3;
85  }
86  packed_pixel(int chan0, int chan1, int chan2, int chan3, int chan4) : _bitfield(0) {
87  BOOST_STATIC_ASSERT((num_channels<packed_pixel>::value==5));
88  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;
89  }
90 
91  packed_pixel& operator=(const packed_pixel& p) { _bitfield=p._bitfield; return *this; }
92 
93  template <typename P> packed_pixel& operator=(const P& p) { assign(p, mpl::bool_<is_pixel<P>::value>()); return *this; }
94  template <typename P> bool operator==(const P& p) const { return equal(p, mpl::bool_<is_pixel<P>::value>()); }
95 
96  template <typename P> bool operator!=(const P& p) const { return !(*this==p); }
97 
98 private:
99  template <typename Pixel> static void check_compatible() { gil_function_requires<PixelsCompatibleConcept<Pixel,packed_pixel> >(); }
100  template <typename Pixel> void assign(const Pixel& p, mpl::true_) { check_compatible<Pixel>(); static_copy(p,*this); }
101  template <typename Pixel> bool equal(const Pixel& p, mpl::true_) const { check_compatible<Pixel>(); return static_equal(*this,p); }
102 
103 // Support for assignment/equality comparison of a channel with a grayscale pixel
104  static void check_gray() { BOOST_STATIC_ASSERT((is_same<typename Layout::color_space_t, gray_t>::value)); }
105  template <typename Channel> void assign(const Channel& chan, mpl::false_) { check_gray(); gil::at_c<0>(*this)=chan; }
106  template <typename Channel> bool equal (const Channel& chan, mpl::false_) const { check_gray(); return gil::at_c<0>(*this)==chan; }
107 public:
108  packed_pixel& operator= (int chan) { check_gray(); gil::at_c<0>(*this)=chan; return *this; }
109  bool operator==(int chan) const { check_gray(); return gil::at_c<0>(*this)==chan; }
110 };
111 
113 // ColorBasedConcept
115 
116 template <typename BitField, typename ChannelRefVec, typename Layout, int K>
117 struct kth_element_type<packed_pixel<BitField,ChannelRefVec,Layout>,K> : public mpl::at_c<ChannelRefVec,K> {};
118 
119 template <typename BitField, typename ChannelRefVec, typename Layout, int K>
120 struct kth_element_reference_type<packed_pixel<BitField,ChannelRefVec,Layout>,K> : public mpl::at_c<ChannelRefVec,K> {};
121 
122 template <typename BitField, typename ChannelRefVec, typename Layout, int K>
123 struct kth_element_const_reference_type<packed_pixel<BitField,ChannelRefVec,Layout>,K> {
124  typedef typename channel_traits<typename mpl::at_c<ChannelRefVec,K>::type>::const_reference type;
125 };
126 
127 template <int K, typename P, typename C, typename L> inline
128 typename kth_element_reference_type<packed_pixel<P,C,L>, K>::type
129 at_c(packed_pixel<P,C,L>& p) {
130  return typename kth_element_reference_type<packed_pixel<P,C,L>, K>::type(&p._bitfield);
131 }
132 
133 template <int K, typename P, typename C, typename L> inline
134 typename kth_element_const_reference_type<packed_pixel<P,C,L>, K>::type
135 at_c(const packed_pixel<P,C,L>& p) {
136  return typename kth_element_const_reference_type<packed_pixel<P,C,L>, K>::type(&p._bitfield);
137 }
138 
140 // PixelConcept
142 
143 // Metafunction predicate that flags packed_pixel as a model of PixelConcept. Required by PixelConcept
144 template <typename BitField, typename ChannelRefVec, typename Layout>
145 struct is_pixel<packed_pixel<BitField,ChannelRefVec,Layout> > : public mpl::true_{};
146 
148 // PixelBasedConcept
150 
151 template <typename P, typename C, typename Layout>
152 struct color_space_type<packed_pixel<P,C,Layout> > {
153  typedef typename Layout::color_space_t type;
154 };
155 
156 template <typename P, typename C, typename Layout>
157 struct channel_mapping_type<packed_pixel<P,C,Layout> > {
158  typedef typename Layout::channel_mapping_t type;
159 };
160 
161 template <typename P, typename C, typename Layout>
162 struct is_planar<packed_pixel<P,C,Layout> > : mpl::false_ {};
163 
164 
170 
175 
176 template <typename P, typename C, typename L>
177 struct iterator_is_mutable<packed_pixel<P,C,L>*> : public mpl::bool_<packed_pixel<P,C,L>::is_mutable> {};
178 template <typename P, typename C, typename L>
179 struct iterator_is_mutable<const packed_pixel<P,C,L>*> : public mpl::false_ {};
180 
181 
182 
183 } } // namespace boost::gil
184 
185 namespace boost {
186  template <typename P, typename C, typename L>
187  struct has_trivial_constructor<gil::packed_pixel<P,C,L> > : public has_trivial_constructor<P> {};
188 }
189 #endif
add_reference< E >::type at_c(detail::homogeneous_color_base< E, L, N > &p)
Provides mutable access to the K-th element, in physical order.
Definition: color_base.hpp:381