2 // Copyright 2019 Mateusz Loskot <mateusz at loskot dot net>
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
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>
13 #include <boost/core/typeinfo.hpp>
14 #include <boost/mp11.hpp>
16 #define BOOST_TEST_MODULE test_channel_traits
17 #include "unit_test.hpp"
19 namespace gil = boost::gil;
20 namespace mp11 = boost::mp11;
22 namespace boost { namespace gil {
24 template <typename BitField, typename ChannelRefs, typename Layout>
25 std::ostream& operator<<(std::ostream& os, gil::packed_pixel<BitField, ChannelRefs, Layout> const& p)
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 << ")";
35 }} // namespace boost::gil
37 using packed_channel_references_3 = typename gil::detail::packed_channel_references_vector_type
40 mp11::mp_list_c<int, 3>
43 using packed_pixel_gray3 = gil::packed_pixel
46 packed_channel_references_3,
50 BOOST_AUTO_TEST_CASE(packed_pixel_gray3_definition)
52 // Verify packed_pixel members
54 static_assert(std::is_same<packed_pixel_gray3::layout_t, gil::gray_layout_t>::value,
55 "layout should be bgr");
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");
60 static_assert(std::is_reference<packed_pixel_gray3::reference>::value,
61 "reference member should be a reference");
63 static_assert(std::is_reference<packed_pixel_gray3::const_reference>::value,
64 "const_reference member should be a reference");
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");
70 // Verify metafunctions
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");
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");
79 static_assert(std::is_same
82 gil::packed_channel_reference<std::uint8_t, 0, 3, true> const
84 "1st element of packed_channel_references_vector should be packed_channel_reference of 1st channel");
86 // double check intermediate metafunction packed_channel_reference_type
87 static_assert(std::is_same
89 gil::detail::packed_channel_reference_type
92 std::integral_constant<int, 0>,
93 std::integral_constant<int, 3>
97 "packed_channel_reference_type should return packed_channel_reference");
98 static_assert(std::is_same
100 gil::detail::packed_channel_reference_type
103 std::integral_constant<int, 0>,
104 std::integral_constant<int, 3>
106 gil::packed_channel_reference<std::uint8_t, 0, 3, true> const
108 "packed_channel_reference_type should return packed_channel_reference");
111 BOOST_AUTO_TEST_CASE(packed_pixel_gray3_assignment)
113 packed_pixel_gray3 p1{int{5}};
114 packed_pixel_gray3 p2;
116 BOOST_TEST(p1._bitfield == p2._bitfield);
119 BOOST_AUTO_TEST_CASE(packed_pixel_gray_equality)
121 packed_pixel_gray3 p1{int{5}};
122 packed_pixel_gray3 p2{int{5}};
123 BOOST_TEST(p1 == p2);
125 packed_pixel_gray3 p3{int{3}};
126 BOOST_TEST(p2 != p3);
129 BOOST_AUTO_TEST_CASE(packed_pixel_gray3_assignment_gray_channel)
132 packed_pixel_gray3 p1; // default-initialized
134 BOOST_TEST(p1._bitfield == int{5});
138 packed_pixel_gray3 p1{0}; // value-initialized
140 BOOST_TEST(p1._bitfield == int{5});
144 BOOST_AUTO_TEST_CASE(packed_pixel_gray_equality_gray_channel)
146 packed_pixel_gray3 p1{int{3}};
147 BOOST_TEST(p1 == int{3});
150 using packed_channel_references_121 = typename gil::detail::packed_channel_references_vector_type
153 mp11::mp_list_c<int, 1, 2, 1>
156 using packed_pixel_bgr121 = gil::packed_pixel
159 packed_channel_references_121,
163 BOOST_AUTO_TEST_CASE(packed_pixel_bgr121_definition)
165 // Verify packed_pixel members
167 static_assert(std::is_same<packed_pixel_bgr121::layout_t, gil::bgr_layout_t>::value,
168 "layout should be bgr");
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");
173 static_assert(std::is_reference<packed_pixel_bgr121::reference>::value,
174 "reference member should be a reference");
176 static_assert(std::is_reference<packed_pixel_bgr121::const_reference>::value,
177 "const_reference member should be a reference");
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");
183 // Verify metafunctions
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");
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");
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");
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");
200 static_assert(std::is_same
203 gil::packed_channel_reference<std::uint8_t, 0, 1, true> const
205 "1st element of packed_channel_references_vector should be packed_channel_reference of 1st channel");
207 static_assert(std::is_same
210 gil::packed_channel_reference<std::uint8_t, 1, 2, true> const
212 "2nd element of packed_channel_references_vector should be packed_channel_reference of 2nd channel");
214 static_assert(std::is_same
217 gil::packed_channel_reference<std::uint8_t, 3, 1, true> const
219 "3rd element of packed_channel_references_vector should be packed_channel_reference of 3rd channel");
221 // double check intermediate metafunction packed_channel_reference_type
222 static_assert(std::is_same
224 gil::detail::packed_channel_reference_type
226 std::uint8_t, mp11::mp_int<0>, mp11::mp_int<1>
230 "packed_channel_reference_type should return packed_channel_reference");
231 static_assert(std::is_same
233 gil::detail::packed_channel_reference_type
235 std::uint8_t, mp11::mp_int<0>, mp11::mp_int<1>
237 gil::packed_channel_reference<std::uint8_t, 0, 1, true> const
239 "packed_channel_reference_type should return packed_channel_reference");
242 BOOST_AUTO_TEST_CASE(packed_pixel_bgr121_assignment)
244 packed_pixel_bgr121 p1{0, 3, 1};
245 packed_pixel_bgr121 p2;
247 BOOST_TEST(p1._bitfield == p2._bitfield);
250 BOOST_AUTO_TEST_CASE(packed_pixel_bgr121_equality)
252 packed_pixel_bgr121 p1{1, 3, 0};
253 packed_pixel_bgr121 p2{1, 3, 0};
254 BOOST_TEST(p1 == p2);
256 packed_pixel_bgr121 p3{0, 3, 1};
257 BOOST_TEST(p2 != p3);
260 using packed_channel_references_535 = typename gil::detail::packed_channel_references_vector_type
263 mp11::mp_list_c<int, 5, 3, 5>
266 using packed_pixel_rgb535 = gil::packed_pixel
269 packed_channel_references_535,
273 BOOST_AUTO_TEST_CASE(packed_pixel_rgb535_definition)
275 // Verify packed_pixel members
277 static_assert(std::is_same<packed_pixel_rgb535::layout_t, gil::rgb_layout_t>::value,
278 "layout should be bgr");
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");
283 static_assert(std::is_reference<packed_pixel_rgb535::reference>::value,
284 "reference member should be a reference");
286 static_assert(std::is_reference<packed_pixel_rgb535::const_reference>::value,
287 "const_reference member should be a reference");
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");
293 // Verify metafunctions
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");
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");
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");
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");
310 static_assert(std::is_same
313 gil::packed_channel_reference<std::uint16_t, 0, 5, true> const
315 "1st element of packed_channel_references_vector should be packed_channel_reference of 1st channel");
317 static_assert(std::is_same
320 gil::packed_channel_reference<std::uint16_t, 5, 3, true> const
322 "2nd element of packed_channel_references_vector should be packed_channel_reference of 2nd channel");
324 static_assert(std::is_same
327 gil::packed_channel_reference<std::uint16_t, 8, 5, true> const
329 "3rd element of packed_channel_references_vector should be packed_channel_reference of 3rd channel");
331 // double check intermediate metafunction packed_channel_reference_type
332 static_assert(std::is_same
334 gil::detail::packed_channel_reference_type
337 std::integral_constant<int, 0>,
338 std::integral_constant<int, 5>
342 "packed_channel_reference_type should return packed_channel_reference");
343 static_assert(std::is_same
345 gil::detail::packed_channel_reference_type
348 std::integral_constant<int, 0>,
349 std::integral_constant<int, 5>
351 gil::packed_channel_reference<std::uint16_t, 0, 5, true> const
353 "packed_channel_reference_type should return packed_channel_reference");
356 BOOST_AUTO_TEST_CASE(packed_pixel_rgb535_assignment)
358 packed_pixel_rgb535 p1{31, 7, 31};
359 packed_pixel_rgb535 p2;
361 BOOST_TEST(p1._bitfield == p2._bitfield);
364 BOOST_AUTO_TEST_CASE(packed_pixel_rgb535_equality)
366 packed_pixel_rgb535 p1{7, 3, 7};
367 packed_pixel_rgb535 p2{7, 3, 7};
368 BOOST_TEST(p1 == p2);
370 packed_pixel_rgb535 p3{7, 7, 7};
371 BOOST_TEST(p2 != p3);