• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include <boost/gil/detail/mp11.hpp>
13 
14 #include <functional>
15 #include <type_traits>
16 
17 namespace boost { namespace gil {
18 
19 /// A model of a heterogeneous pixel whose channels are bit ranges.
20 /// For example 16-bit RGB in '565' format.
21 
22 /// \defgroup ColorBaseModelPackedPixel packed_pixel
23 /// \ingroup ColorBaseModel
24 /// \brief A heterogeneous color base whose elements are reference proxies to channels in a pixel. Models ColorBaseValueConcept. This class is used to model packed pixels, such as 16-bit packed RGB.
25 
26 /// \defgroup PixelModelPackedPixel packed_pixel
27 /// \ingroup PixelModel
28 /// \brief A heterogeneous pixel used to represent packed pixels with non-byte-aligned channels. Models PixelValueConcept
29 ///
30 /// Example:
31 /// \code
32 /// using rgb565_pixel_t = packed_pixel_type<uint16_t, mp11::mp_list-c<unsigned,5,6,5>, rgb_layout_t>::type;
33 /// static_assert(sizeof(rgb565_pixel_t) == 2, "");
34 ///
35 /// rgb565_pixel_t r565;
36 /// get_color(r565,red_t())   = 31;
37 /// get_color(r565,green_t()) = 63;
38 /// get_color(r565,blue_t())  = 31;
39 /// assert(r565 == rgb565_pixel_t((uint16_t)0xFFFF));
40 /// \endcode
41 
42 /// \ingroup ColorBaseModelPackedPixel PixelModelPackedPixel PixelBasedModel
43 /// \brief Heterogeneous pixel value whose channel references can be constructed from the pixel bitfield and their index. Models ColorBaseValueConcept, PixelValueConcept, PixelBasedConcept
44 /// Typical use for this is a model of a packed pixel (like 565 RGB)
45 /// \tparam BitField Type that holds the bits of the pixel. Typically an integral type, like std::uint16_t.
46 /// \tparam ChannelRefs MP11 list whose elements are packed channels. They must be constructible from BitField. GIL uses packed_channel_reference
47 /// \tparam Layout defining the color space and ordering of the channels. Example value: rgb_layout_t
48 template <typename BitField, typename ChannelRefs, typename Layout>
49 struct packed_pixel
50 {
51     BitField _bitfield{0}; // TODO: Make private
52 
53     using layout_t = Layout;
54     using value_type = packed_pixel<BitField, ChannelRefs, Layout>;
55     using reference = value_type&;
56     using const_reference = value_type const&;
57 
58     static constexpr bool is_mutable =
59         channel_traits<mp11::mp_front<ChannelRefs>>::is_mutable;
60 
61     packed_pixel() = default;
packed_pixelboost::gil::packed_pixel62     explicit packed_pixel(const BitField& bitfield) : _bitfield(bitfield) {}
63 
64     // Construct from another compatible pixel type
packed_pixelboost::gil::packed_pixel65     packed_pixel(const packed_pixel& p) : _bitfield(p._bitfield) {}
66 
67     template <typename Pixel>
packed_pixelboost::gil::packed_pixel68     packed_pixel(Pixel const& p,
69         typename std::enable_if<is_pixel<Pixel>::value>::type* /*dummy*/ = nullptr)
70     {
71         check_compatible<Pixel>();
72         static_copy(p, *this);
73     }
74 
packed_pixelboost::gil::packed_pixel75     packed_pixel(int chan0, int chan1)
76         : _bitfield(0)
77     {
78         static_assert(num_channels<packed_pixel>::value == 2, "");
79         gil::at_c<0>(*this) = chan0;
80         gil::at_c<1>(*this) = chan1;
81     }
82 
packed_pixelboost::gil::packed_pixel83     packed_pixel(int chan0, int chan1, int chan2)
84         : _bitfield(0)
85     {
86         static_assert(num_channels<packed_pixel>::value == 3, "");
87         gil::at_c<0>(*this) = chan0;
88         gil::at_c<1>(*this) = chan1;
89         gil::at_c<2>(*this) = chan2;
90     }
91 
packed_pixelboost::gil::packed_pixel92     packed_pixel(int chan0, int chan1, int chan2, int chan3)
93         : _bitfield(0)
94     {
95         static_assert(num_channels<packed_pixel>::value == 4, "");
96         gil::at_c<0>(*this) = chan0;
97         gil::at_c<1>(*this) = chan1;
98         gil::at_c<2>(*this) = chan2;
99         gil::at_c<3>(*this) = chan3;
100     }
101 
packed_pixelboost::gil::packed_pixel102     packed_pixel(int chan0, int chan1, int chan2, int chan3, int chan4)
103         : _bitfield(0)
104     {
105         static_assert(num_channels<packed_pixel>::value == 5, "");
106         gil::at_c<0>(*this) = chan0;
107         gil::at_c<1>(*this) = chan1;
108         gil::at_c<2>(*this) = chan2;
109         gil::at_c<3>(*this) = chan3;
110         gil::at_c<4>(*this) = chan4;
111     }
112 
operator =boost::gil::packed_pixel113     auto operator=(packed_pixel const& p) -> packed_pixel&
114     {
115         _bitfield = p._bitfield;
116         return *this;
117     }
118 
119     template <typename Pixel>
operator =boost::gil::packed_pixel120     auto operator=(Pixel const& p) -> packed_pixel&
121     {
122         assign(p, is_pixel<Pixel>());
123         return *this;
124     }
125 
126     template <typename Pixel>
operator ==boost::gil::packed_pixel127     bool operator==(Pixel const& p) const
128     {
129         return equal(p, is_pixel<Pixel>());
130     }
131 
132     template <typename Pixel>
operator !=boost::gil::packed_pixel133     bool operator!=(Pixel const& p) const { return !(*this==p); }
134 
135 private:
136     template <typename Pixel>
check_compatibleboost::gil::packed_pixel137     static void check_compatible()
138     {
139         gil_function_requires<PixelsCompatibleConcept<Pixel, packed_pixel>>();
140     }
141 
142     template <typename Pixel>
assignboost::gil::packed_pixel143     void assign(Pixel const& p, std::true_type)
144     {
145         check_compatible<Pixel>();
146         static_copy(p, *this);
147     }
148 
149     template <typename Pixel>
equalboost::gil::packed_pixel150     bool  equal(Pixel const& p, std::true_type) const
151     {
152         check_compatible<Pixel>();
153         return static_equal(*this, p);
154     }
155 
156     // Support for assignment/equality comparison of a channel with a grayscale pixel
check_grayboost::gil::packed_pixel157     static void check_gray()
158     {
159         static_assert(std::is_same<typename Layout::color_space_t, gray_t>::value, "");
160     }
161 
162     template <typename Channel>
assignboost::gil::packed_pixel163     void assign(Channel const& channel, std::false_type)
164     {
165         check_gray();
166         gil::at_c<0>(*this) = channel;
167     }
168 
169     template <typename Channel>
equalboost::gil::packed_pixel170     bool equal (Channel const& channel, std::false_type) const
171     {
172         check_gray();
173         return gil::at_c<0>(*this) == channel;
174     }
175 
176 public:
operator =boost::gil::packed_pixel177     auto operator=(int channel) -> packed_pixel&
178     {
179         check_gray();
180         gil::at_c<0>(*this) = channel;
181         return *this;
182     }
183 
operator ==boost::gil::packed_pixel184     bool operator==(int channel) const
185     {
186         check_gray();
187         return gil::at_c<0>(*this) == channel;
188     }
189 };
190 
191 /////////////////////////////
192 //  ColorBasedConcept
193 /////////////////////////////
194 
195 template <typename BitField, typename ChannelRefs, typename Layout, int K>
196 struct kth_element_type<packed_pixel<BitField, ChannelRefs, Layout>, K>
197 {
198     using type = typename channel_traits<mp11::mp_at_c<ChannelRefs, K>>::value_type;
199 };
200 
201 template <typename BitField, typename ChannelRefs, typename Layout, int K>
202 struct kth_element_reference_type<packed_pixel<BitField, ChannelRefs, Layout>, K>
203 {
204     using type = typename channel_traits<mp11::mp_at_c<ChannelRefs, K>>::reference;
205 };
206 
207 template <typename BitField, typename ChannelRefs, typename Layout, int K>
208 struct kth_element_const_reference_type<packed_pixel<BitField, ChannelRefs, Layout>, K>
209 {
210     using type = typename channel_traits<mp11::mp_at_c<ChannelRefs, K>>::const_reference;
211 };
212 
213 template <int K, typename P, typename C, typename L>
214 inline
at_c(packed_pixel<P,C,L> & p)215 auto at_c(packed_pixel<P, C, L>& p)
216     -> typename kth_element_reference_type<packed_pixel<P, C, L>, K>::type
217 {
218     return typename kth_element_reference_type
219         <
220             packed_pixel<P, C, L>,
221             K
222         >::type{&p._bitfield};
223 }
224 
225 template <int K, typename P, typename C, typename L>
226 inline
at_c(const packed_pixel<P,C,L> & p)227 auto at_c(const packed_pixel<P, C, L>& p)
228     -> typename kth_element_const_reference_type<packed_pixel<P, C, L>, K>::type
229 {
230     return typename kth_element_const_reference_type
231         <
232             packed_pixel<P, C, L>,
233         K>::type{&p._bitfield};
234 }
235 
236 /////////////////////////////
237 //  PixelConcept
238 /////////////////////////////
239 
240 // Metafunction predicate that flags packed_pixel as a model of PixelConcept.
241 // Required by PixelConcept
242 template <typename BitField, typename ChannelRefs, typename Layout>
243 struct is_pixel<packed_pixel<BitField, ChannelRefs, Layout>> : std::true_type {};
244 
245 /////////////////////////////
246 //  PixelBasedConcept
247 /////////////////////////////
248 
249 template <typename P, typename C, typename Layout>
250 struct color_space_type<packed_pixel<P, C, Layout>>
251 {
252     using type = typename Layout::color_space_t;
253 };
254 
255 template <typename P, typename C, typename Layout>
256 struct channel_mapping_type<packed_pixel<P, C, Layout>>
257 {
258     using type = typename Layout::channel_mapping_t;
259 };
260 
261 template <typename P, typename C, typename Layout>
262 struct is_planar<packed_pixel<P, C, Layout>> : std::false_type {};
263 
264 ////////////////////////////////////////////////////////////////////////////////
265 /// Support for interleaved iterators over packed pixel
266 ////////////////////////////////////////////////////////////////////////////////
267 
268 /// \defgroup PixelIteratorModelPackedInterleavedPtr Pointer to packed_pixel<P,CR,Layout>
269 /// \ingroup PixelIteratorModel
270 /// \brief Iterators over interleaved pixels.
271 /// The pointer packed_pixel<P,CR,Layout>* is used as an iterator over interleaved
272 /// pixels of packed format.
273 /// Models PixelIteratorConcept, HasDynamicXStepTypeConcept, MemoryBasedIteratorConcept
274 
275 template <typename P, typename C, typename L>
276 struct iterator_is_mutable<packed_pixel<P, C, L>*>
277     : std::integral_constant<bool, packed_pixel<P, C, L>::is_mutable>
278 {};
279 
280 template <typename P, typename C, typename L>
281 struct iterator_is_mutable<const packed_pixel<P, C, L>*> : std::false_type {};
282 
283 }}  // namespace boost::gil
284 
285 #endif
286