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