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/mpl/bool.hpp>
15 #include <boost/mpl/front.hpp>
16 
17 #include <functional>
18 #include <type_traits>
19 
20 namespace boost { namespace gil {
21 
24 
28 
44 
48 template <typename BitField, // A type that holds the bits of the pixel. Typically an integral type, like std::uint16_t
49  typename ChannelRefVec, // An MPL vector whose elements are packed channels. They must be constructible from BitField. GIL uses packed_channel_reference
50  typename Layout> // Layout defining the color space and ordering of the channels. Example value: rgb_layout_t
51 struct packed_pixel
52 {
53  BitField _bitfield;
54 
55  using layout_t = Layout;
56  using value_type = packed_pixel<BitField, ChannelRefVec, Layout>;
57  using reference = value_type&;
58  using const_reference = value_type const&;
59 
60  static constexpr bool is_mutable =
61  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 
69  template <typename P>
70  packed_pixel(P const& p,
71  typename std::enable_if<is_pixel<P>::value>::type* /*dummy*/ = nullptr)
72  {
73  check_compatible<P>();
74  static_copy(p, *this);
75  }
76 
77  packed_pixel(int chan0, int chan1) : _bitfield(0) {
78  static_assert(num_channels<packed_pixel>::value == 2, "");
79  gil::at_c<0>(*this)=chan0; gil::at_c<1>(*this)=chan1;
80  }
81  packed_pixel(int chan0, int chan1, int chan2) : _bitfield(0) {
82  static_assert(num_channels<packed_pixel>::value == 3, "");
83  gil::at_c<0>(*this) = chan0;
84  gil::at_c<1>(*this) = chan1;
85  gil::at_c<2>(*this) = chan2;
86  }
87  packed_pixel(int chan0, int chan1, int chan2, int chan3) : _bitfield(0) {
88  static_assert(num_channels<packed_pixel>::value == 4, "");
89  gil::at_c<0>(*this)=chan0; gil::at_c<1>(*this)=chan1; gil::at_c<2>(*this)=chan2; gil::at_c<3>(*this)=chan3;
90  }
91  packed_pixel(int chan0, int chan1, int chan2, int chan3, int chan4) : _bitfield(0) {
92  static_assert(num_channels<packed_pixel>::value == 5, "");
93  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;
94  }
95 
96  packed_pixel& operator=(const packed_pixel& p) { _bitfield=p._bitfield; return *this; }
97 
98  template <typename P> packed_pixel& operator=(const P& p) { assign(p, mpl::bool_<is_pixel<P>::value>()); return *this; }
99  template <typename P> bool operator==(const P& p) const { return equal(p, mpl::bool_<is_pixel<P>::value>()); }
100 
101  template <typename P> bool operator!=(const P& p) const { return !(*this==p); }
102 
103 private:
104  template <typename Pixel> static void check_compatible() { gil_function_requires<PixelsCompatibleConcept<Pixel,packed_pixel> >(); }
105  template <typename Pixel> void assign(const Pixel& p, mpl::true_) { check_compatible<Pixel>(); static_copy(p,*this); }
106  template <typename Pixel> bool equal(const Pixel& p, mpl::true_) const { check_compatible<Pixel>(); return static_equal(*this,p); }
107 
108 // Support for assignment/equality comparison of a channel with a grayscale pixel
109  static void check_gray()
110  {
111  static_assert(is_same<typename Layout::color_space_t, gray_t>::value, "");
112  }
113  template <typename Channel> void assign(const Channel& chan, mpl::false_) { check_gray(); gil::at_c<0>(*this)=chan; }
114  template <typename Channel> bool equal (const Channel& chan, mpl::false_) const { check_gray(); return gil::at_c<0>(*this)==chan; }
115 public:
116  packed_pixel& operator= (int chan) { check_gray(); gil::at_c<0>(*this)=chan; return *this; }
117  bool operator==(int chan) const { check_gray(); return gil::at_c<0>(*this)==chan; }
118 };
119 
121 // ColorBasedConcept
123 
124 template <typename BitField, typename ChannelRefVec, typename Layout, int K>
125 struct kth_element_type<packed_pixel<BitField,ChannelRefVec,Layout>,K> : public mpl::at_c<ChannelRefVec,K> {};
126 
127 template <typename BitField, typename ChannelRefVec, typename Layout, int K>
128 struct kth_element_reference_type<packed_pixel<BitField,ChannelRefVec,Layout>,K> : public mpl::at_c<ChannelRefVec,K> {};
129 
130 template <typename BitField, typename ChannelRefVec, typename Layout, int K>
131 struct kth_element_const_reference_type<packed_pixel<BitField,ChannelRefVec,Layout>,K>
132 {
133  using type = typename channel_traits<typename mpl::at_c<ChannelRefVec,K>::type>::const_reference;
134 };
135 
136 template <int K, typename P, typename C, typename L> inline
137 typename kth_element_reference_type<packed_pixel<P,C,L>, K>::type
138 at_c(packed_pixel<P,C,L>& p) {
139  return typename kth_element_reference_type<packed_pixel<P,C,L>, K>::type(&p._bitfield);
140 }
141 
142 template <int K, typename P, typename C, typename L> inline
143 typename kth_element_const_reference_type<packed_pixel<P,C,L>, K>::type
144 at_c(const packed_pixel<P,C,L>& p) {
145  return typename kth_element_const_reference_type<packed_pixel<P,C,L>, K>::type(&p._bitfield);
146 }
147 
149 // PixelConcept
151 
152 // Metafunction predicate that flags packed_pixel as a model of PixelConcept. Required by PixelConcept
153 template <typename BitField, typename ChannelRefVec, typename Layout>
154 struct is_pixel<packed_pixel<BitField,ChannelRefVec,Layout> > : public mpl::true_{};
155 
157 // PixelBasedConcept
159 
160 template <typename P, typename C, typename Layout>
161 struct color_space_type<packed_pixel<P,C,Layout> > {
162  using type = typename Layout::color_space_t;
163 };
164 
165 template <typename P, typename C, typename Layout>
166 struct channel_mapping_type<packed_pixel<P,C,Layout> > {
167  using type = typename Layout::channel_mapping_t;
168 };
169 
170 template <typename P, typename C, typename Layout>
171 struct is_planar<packed_pixel<P,C,Layout> > : mpl::false_ {};
172 
173 
179 
184 
185 template <typename P, typename C, typename L>
186 struct iterator_is_mutable<packed_pixel<P,C,L>*> : public mpl::bool_<packed_pixel<P,C,L>::is_mutable> {};
187 template <typename P, typename C, typename L>
188 struct iterator_is_mutable<const packed_pixel<P,C,L>*> : public mpl::false_ {};
189 
190 
191 
192 } } // namespace boost::gil
193 
194 namespace boost {
195  template <typename P, typename C, typename L>
196  struct has_trivial_constructor<gil::packed_pixel<P,C,L> > : public has_trivial_constructor<P> {};
197 }
198 #endif
Definition: algorithm.hpp:30
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:387