Imported Upstream version 1.71.0
[platform/upstream/boost.git] / libs / gil / test / pixel / packed_pixel.cpp
1 //
2 // Copyright 2019 Mateusz Loskot <mateusz at loskot dot net>
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 #include <boost/gil/channel.hpp>
9 #include <boost/gil/packed_pixel.hpp>
10 #include <boost/gil/gray.hpp>
11 #include <boost/gil/rgb.hpp>
12
13 #include <boost/core/typeinfo.hpp>
14 #include <boost/mpl/at.hpp>
15 #include <boost/mpl/int.hpp>
16 #include <boost/mpl/size.hpp>
17 #include <boost/mpl/vector_c.hpp>
18
19 #define BOOST_TEST_MODULE test_channel_traits
20 #include "unit_test.hpp"
21
22 namespace gil = boost::gil;
23 namespace mpl = boost::mpl;
24
25 namespace boost { namespace gil {
26
27 template <typename BitField, typename ChannelRefs, typename Layout>
28 std::ostream& operator<<(std::ostream& os, gil::packed_pixel<BitField, ChannelRefs, Layout> const& p)
29 {
30     os << "packed_pixel<"
31        << "BitField=" << boost::core::demangled_name(typeid(BitField))
32        << ", ChannelRefs="  << boost::core::demangled_name(typeid(ChannelRefs))
33        << ", Layout=" << boost::core::demangled_name(typeid(Layout))
34         << ">(" << (std::uint64_t)p._bitfield << ")";
35     return os;
36 }
37
38 }} // namespace boost::gil
39
40 using packed_channel_references_3 = typename gil::detail::packed_channel_references_vector_type
41 <
42     std::uint8_t,
43     mpl::vector1_c<int, 3>
44 >::type;
45
46 using packed_pixel_gray3 = gil::packed_pixel
47 <
48     std::uint8_t,
49     packed_channel_references_3,
50     gil::gray_layout_t
51 >;
52
53 BOOST_AUTO_TEST_CASE(packed_pixel_gray3_definition)
54 {
55     // Verify packed_pixel members
56
57     static_assert(std::is_same<packed_pixel_gray3::layout_t, gil::gray_layout_t>::value,
58         "layout should be bgr");
59
60     static_assert(std::is_same<packed_pixel_gray3::value_type, packed_pixel_gray3>::value,
61         "value_type member should be of the same type as the packed_pixel specialization");
62
63     static_assert(std::is_reference<packed_pixel_gray3::reference>::value,
64         "reference member should be a reference");
65
66     static_assert(std::is_reference<packed_pixel_gray3::const_reference>::value,
67         "const_reference member should be a reference");
68
69     static_assert(std::is_same<decltype(packed_pixel_gray3::is_mutable), bool const>::value &&
70         packed_pixel_gray3::is_mutable,
71         "is_mutable should be boolean");
72
73     // Verify metafunctions
74
75     using channel_references_t = packed_channel_references_3::type;
76
77     static_assert(mpl::size<channel_references_t>::value == 1,
78         "packed_channel_references_vector_type should define one reference to channel start bits");
79
80     using channel1_ref_t = mpl::at_c<channel_references_t, 0>::type;
81     static_assert(channel1_ref_t::num_bits == 3,
82         "1st channel of gray3 pixel should be of 3-bit size");
83
84     static_assert(std::is_same
85         <
86             channel1_ref_t,
87             gil::packed_channel_reference<std::uint8_t, 0, 3, true> const
88         >::value,
89         "1st element of packed_channel_references_vector should be packed_channel_reference of 1st channel");
90
91     // double check intermediate metafunction packed_channel_reference_type
92     static_assert(std::is_same
93         <
94             gil::detail::packed_channel_reference_type<std::uint8_t, mpl::int_<0>, mpl::int_<3>>::type,
95             channel1_ref_t
96         >::value,
97         "packed_channel_reference_type should return packed_channel_reference");
98     static_assert(std::is_same
99         <
100             gil::detail::packed_channel_reference_type<std::uint8_t, mpl::int_<0>, mpl::int_<3>>::type,
101             gil::packed_channel_reference<std::uint8_t, 0, 3, true> const
102         >::value,
103         "packed_channel_reference_type should return packed_channel_reference");
104 }
105
106 BOOST_AUTO_TEST_CASE(packed_pixel_gray3_assignment)
107 {
108     packed_pixel_gray3 p1{int{5}};
109     packed_pixel_gray3 p2;
110     p2 = p1;
111     BOOST_TEST(p1._bitfield == p2._bitfield);
112 }
113
114 BOOST_AUTO_TEST_CASE(packed_pixel_gray_equality)
115 {
116     packed_pixel_gray3 p1{int{5}};
117     packed_pixel_gray3 p2{int{5}};
118     BOOST_TEST(p1 == p2);
119
120     packed_pixel_gray3 p3{int{3}};
121     BOOST_TEST(p2 != p3);
122 }
123
124 BOOST_AUTO_TEST_CASE(packed_pixel_gray3_assignment_gray_channel)
125 {
126     packed_pixel_gray3 p1{0};
127     p1 = int{5};
128     BOOST_TEST(p1._bitfield == int{5});
129 }
130
131 BOOST_AUTO_TEST_CASE(packed_pixel_gray_equality_gray_channel)
132 {
133     packed_pixel_gray3 p1{int{3}};
134     BOOST_TEST(p1 == int{3});
135 }
136
137 using packed_channel_references_121 = typename gil::detail::packed_channel_references_vector_type
138 <
139     std::uint8_t,
140     mpl::vector3_c<int, 1, 2, 1>
141 >::type;
142
143 using packed_pixel_bgr121 = gil::packed_pixel
144 <
145     std::uint8_t,
146     packed_channel_references_121,
147     gil::bgr_layout_t
148 >;
149
150 BOOST_AUTO_TEST_CASE(packed_pixel_bgr121_definition)
151 {
152     // Verify packed_pixel members
153
154     static_assert(std::is_same<packed_pixel_bgr121::layout_t, gil::bgr_layout_t>::value,
155         "layout should be bgr");
156
157     static_assert(std::is_same<packed_pixel_bgr121::value_type, packed_pixel_bgr121>::value,
158         "value_type member should be of the same type as the packed_pixel specialization");
159
160     static_assert(std::is_reference<packed_pixel_bgr121::reference>::value,
161         "reference member should be a reference");
162
163     static_assert(std::is_reference<packed_pixel_bgr121::const_reference>::value,
164         "const_reference member should be a reference");
165
166     static_assert(std::is_same<decltype(packed_pixel_bgr121::is_mutable), bool const>::value &&
167         packed_pixel_bgr121::is_mutable,
168         "is_mutable should be boolean");
169
170     // Verify metafunctions
171
172     using channel_references_t = packed_channel_references_121::type;
173
174     static_assert(mpl::size<channel_references_t>::value == 3,
175         "packed_channel_references_vector_type should define three references to channel start bits");
176
177     using channel1_ref_t = mpl::at_c<channel_references_t, 0>::type;
178     static_assert(channel1_ref_t::num_bits == 1,
179         "1st channel of bgr121 pixel should be of 1-bit size");
180
181     using channel2_ref_t = mpl::at_c<channel_references_t, 1>::type;
182     static_assert(channel2_ref_t::num_bits == 2,
183         "2nd channel of bgr121 pixel should be of 2-bit size");
184
185     using channel3_ref_t = mpl::at_c<channel_references_t, 2>::type;
186     static_assert(channel3_ref_t::num_bits == 1,
187         "3rd channel of bgr121 pixel should be of 1-bit size");
188
189     static_assert(std::is_same
190         <
191             channel1_ref_t,
192             gil::packed_channel_reference<std::uint8_t, 0, 1, true> const
193         >::value,
194         "1st element of packed_channel_references_vector should be packed_channel_reference of 1st channel");
195
196     static_assert(std::is_same
197         <
198             channel2_ref_t,
199             gil::packed_channel_reference<std::uint8_t, 1, 2, true> const
200         >::value,
201         "2nd element of packed_channel_references_vector should be packed_channel_reference of 2nd channel");
202
203     static_assert(std::is_same
204         <
205             channel3_ref_t,
206             gil::packed_channel_reference<std::uint8_t, 3, 1, true> const
207         >::value,
208         "3rd element of packed_channel_references_vector should be packed_channel_reference of 3rd channel");
209
210     // double check intermediate metafunction packed_channel_reference_type
211     static_assert(std::is_same
212         <
213             gil::detail::packed_channel_reference_type<std::uint8_t, mpl::int_<0>, mpl::int_<1>>::type,
214             channel1_ref_t
215         >::value,
216         "packed_channel_reference_type should return packed_channel_reference");
217     static_assert(std::is_same
218         <
219             gil::detail::packed_channel_reference_type<std::uint8_t, mpl::int_<0>, mpl::int_<1>>::type,
220             gil::packed_channel_reference<std::uint8_t, 0, 1, true> const
221         >::value,
222         "packed_channel_reference_type should return packed_channel_reference");
223 }
224
225 BOOST_AUTO_TEST_CASE(packed_pixel_bgr121_assignment)
226 {
227     packed_pixel_bgr121 p1{0, 3, 1};
228     packed_pixel_bgr121 p2;
229     p2 = p1;
230     BOOST_TEST(p1._bitfield == p2._bitfield);
231 }
232
233 BOOST_AUTO_TEST_CASE(packed_pixel_bgr121_equality)
234 {
235     packed_pixel_bgr121 p1{1, 3, 0};
236     packed_pixel_bgr121 p2{1, 3, 0};
237     BOOST_TEST(p1 == p2);
238
239     packed_pixel_bgr121 p3{0, 3, 1};
240     BOOST_TEST(p2 != p3);
241 }
242
243 using packed_channel_references_535 = typename gil::detail::packed_channel_references_vector_type
244 <
245     std::uint16_t,
246     mpl::vector3_c<int, 5, 3, 5>
247 >::type;
248
249 using packed_pixel_rgb535 = gil::packed_pixel
250 <
251     std::uint16_t,
252     packed_channel_references_535,
253     gil::rgb_layout_t
254 >;
255
256 BOOST_AUTO_TEST_CASE(packed_pixel_rgb535_definition)
257 {
258     // Verify packed_pixel members
259
260     static_assert(std::is_same<packed_pixel_rgb535::layout_t, gil::rgb_layout_t>::value,
261         "layout should be bgr");
262
263     static_assert(std::is_same<packed_pixel_rgb535::value_type, packed_pixel_rgb535>::value,
264         "value_type member should be of the same type as the packed_pixel specialization");
265
266     static_assert(std::is_reference<packed_pixel_rgb535::reference>::value,
267         "reference member should be a reference");
268
269     static_assert(std::is_reference<packed_pixel_rgb535::const_reference>::value,
270         "const_reference member should be a reference");
271
272     static_assert(std::is_same<decltype(packed_pixel_rgb535::is_mutable), bool const>::value &&
273         packed_pixel_rgb535::is_mutable,
274         "is_mutable should be boolean");
275
276     // Verify metafunctions
277
278     using channel_references_t = packed_channel_references_535::type;
279
280     static_assert(mpl::size<channel_references_t>::value == 3,
281         "packed_channel_references_vector_type should define three references to channel start bits");
282
283     using channel1_ref_t = mpl::at_c<channel_references_t, 0>::type;
284     static_assert(channel1_ref_t::num_bits == 5,
285         "1st channel of rgb535 pixel should be of 5-bit size");
286
287     using channel2_ref_t = mpl::at_c<channel_references_t, 1>::type;
288     static_assert(channel2_ref_t::num_bits == 3,
289         "2nd channel of rgb535 pixel should be of 3-bit size");
290
291     using channel3_ref_t = mpl::at_c<channel_references_t, 2>::type;
292     static_assert(channel3_ref_t::num_bits == 5,
293         "3rd channel of rgb535 pixel should be of 5-bit size");
294
295     static_assert(std::is_same
296         <
297             channel1_ref_t,
298             gil::packed_channel_reference<std::uint16_t, 0, 5, true> const
299         >::value,
300         "1st element of packed_channel_references_vector should be packed_channel_reference of 1st channel");
301
302     static_assert(std::is_same
303         <
304             channel2_ref_t,
305             gil::packed_channel_reference<std::uint16_t, 5, 3, true> const
306         >::value,
307         "2nd element of packed_channel_references_vector should be packed_channel_reference of 2nd channel");
308
309     static_assert(std::is_same
310         <
311             channel3_ref_t,
312             gil::packed_channel_reference<std::uint16_t, 8, 5, true> const
313         >::value,
314         "3rd element of packed_channel_references_vector should be packed_channel_reference of 3rd channel");
315
316     // double check intermediate metafunction packed_channel_reference_type
317     static_assert(std::is_same
318         <
319             gil::detail::packed_channel_reference_type<std::uint16_t, mpl::int_<0>, mpl::int_<5>>::type,
320             channel1_ref_t
321         >::value,
322         "packed_channel_reference_type should return packed_channel_reference");
323     static_assert(std::is_same
324         <
325             gil::detail::packed_channel_reference_type<std::uint16_t, mpl::int_<0>, mpl::int_<5>>::type,
326             gil::packed_channel_reference<std::uint16_t, 0, 5, true> const
327         >::value,
328         "packed_channel_reference_type should return packed_channel_reference");
329 }
330
331 BOOST_AUTO_TEST_CASE(packed_pixel_rgb535_assignment)
332 {
333     packed_pixel_rgb535 p1{31, 7, 31};
334     packed_pixel_rgb535 p2;
335     p2 = p1;
336     BOOST_TEST(p1._bitfield == p2._bitfield);
337 }
338
339 BOOST_AUTO_TEST_CASE(packed_pixel_rgb535_equality)
340 {
341     packed_pixel_rgb535 p1{7, 3, 7};
342     packed_pixel_rgb535 p2{7, 3, 7};
343     BOOST_TEST(p1 == p2);
344
345     packed_pixel_rgb535 p3{7, 7, 7};
346     BOOST_TEST(p2 != p3);
347 }