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/mpl/at.hpp>
15 #include <boost/mpl/int.hpp>
16 #include <boost/mpl/size.hpp>
17 #include <boost/mpl/vector_c.hpp>
19 #define BOOST_TEST_MODULE test_channel_traits
20 #include "unit_test.hpp"
22 namespace gil = boost::gil;
23 namespace mpl = boost::mpl;
25 namespace boost { namespace gil {
27 template <typename BitField, typename ChannelRefs, typename Layout>
28 std::ostream& operator<<(std::ostream& os, gil::packed_pixel<BitField, ChannelRefs, Layout> const& p)
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 << ")";
38 }} // namespace boost::gil
40 using packed_channel_references_3 = typename gil::detail::packed_channel_references_vector_type
43 mpl::vector1_c<int, 3>
46 using packed_pixel_gray3 = gil::packed_pixel
49 packed_channel_references_3,
53 BOOST_AUTO_TEST_CASE(packed_pixel_gray3_definition)
55 // Verify packed_pixel members
57 static_assert(std::is_same<packed_pixel_gray3::layout_t, gil::gray_layout_t>::value,
58 "layout should be bgr");
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");
63 static_assert(std::is_reference<packed_pixel_gray3::reference>::value,
64 "reference member should be a reference");
66 static_assert(std::is_reference<packed_pixel_gray3::const_reference>::value,
67 "const_reference member should be a reference");
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");
73 // Verify metafunctions
75 using channel_references_t = packed_channel_references_3::type;
77 static_assert(mpl::size<channel_references_t>::value == 1,
78 "packed_channel_references_vector_type should define one reference to channel start bits");
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");
84 static_assert(std::is_same
87 gil::packed_channel_reference<std::uint8_t, 0, 3, true> const
89 "1st element of packed_channel_references_vector should be packed_channel_reference of 1st channel");
91 // double check intermediate metafunction packed_channel_reference_type
92 static_assert(std::is_same
94 gil::detail::packed_channel_reference_type<std::uint8_t, mpl::int_<0>, mpl::int_<3>>::type,
97 "packed_channel_reference_type should return packed_channel_reference");
98 static_assert(std::is_same
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
103 "packed_channel_reference_type should return packed_channel_reference");
106 BOOST_AUTO_TEST_CASE(packed_pixel_gray3_assignment)
108 packed_pixel_gray3 p1{int{5}};
109 packed_pixel_gray3 p2;
111 BOOST_TEST(p1._bitfield == p2._bitfield);
114 BOOST_AUTO_TEST_CASE(packed_pixel_gray_equality)
116 packed_pixel_gray3 p1{int{5}};
117 packed_pixel_gray3 p2{int{5}};
118 BOOST_TEST(p1 == p2);
120 packed_pixel_gray3 p3{int{3}};
121 BOOST_TEST(p2 != p3);
124 BOOST_AUTO_TEST_CASE(packed_pixel_gray3_assignment_gray_channel)
126 packed_pixel_gray3 p1{0};
128 BOOST_TEST(p1._bitfield == int{5});
131 BOOST_AUTO_TEST_CASE(packed_pixel_gray_equality_gray_channel)
133 packed_pixel_gray3 p1{int{3}};
134 BOOST_TEST(p1 == int{3});
137 using packed_channel_references_121 = typename gil::detail::packed_channel_references_vector_type
140 mpl::vector3_c<int, 1, 2, 1>
143 using packed_pixel_bgr121 = gil::packed_pixel
146 packed_channel_references_121,
150 BOOST_AUTO_TEST_CASE(packed_pixel_bgr121_definition)
152 // Verify packed_pixel members
154 static_assert(std::is_same<packed_pixel_bgr121::layout_t, gil::bgr_layout_t>::value,
155 "layout should be bgr");
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");
160 static_assert(std::is_reference<packed_pixel_bgr121::reference>::value,
161 "reference member should be a reference");
163 static_assert(std::is_reference<packed_pixel_bgr121::const_reference>::value,
164 "const_reference member should be a reference");
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");
170 // Verify metafunctions
172 using channel_references_t = packed_channel_references_121::type;
174 static_assert(mpl::size<channel_references_t>::value == 3,
175 "packed_channel_references_vector_type should define three references to channel start bits");
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");
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");
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");
189 static_assert(std::is_same
192 gil::packed_channel_reference<std::uint8_t, 0, 1, true> const
194 "1st element of packed_channel_references_vector should be packed_channel_reference of 1st channel");
196 static_assert(std::is_same
199 gil::packed_channel_reference<std::uint8_t, 1, 2, true> const
201 "2nd element of packed_channel_references_vector should be packed_channel_reference of 2nd channel");
203 static_assert(std::is_same
206 gil::packed_channel_reference<std::uint8_t, 3, 1, true> const
208 "3rd element of packed_channel_references_vector should be packed_channel_reference of 3rd channel");
210 // double check intermediate metafunction packed_channel_reference_type
211 static_assert(std::is_same
213 gil::detail::packed_channel_reference_type<std::uint8_t, mpl::int_<0>, mpl::int_<1>>::type,
216 "packed_channel_reference_type should return packed_channel_reference");
217 static_assert(std::is_same
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
222 "packed_channel_reference_type should return packed_channel_reference");
225 BOOST_AUTO_TEST_CASE(packed_pixel_bgr121_assignment)
227 packed_pixel_bgr121 p1{0, 3, 1};
228 packed_pixel_bgr121 p2;
230 BOOST_TEST(p1._bitfield == p2._bitfield);
233 BOOST_AUTO_TEST_CASE(packed_pixel_bgr121_equality)
235 packed_pixel_bgr121 p1{1, 3, 0};
236 packed_pixel_bgr121 p2{1, 3, 0};
237 BOOST_TEST(p1 == p2);
239 packed_pixel_bgr121 p3{0, 3, 1};
240 BOOST_TEST(p2 != p3);
243 using packed_channel_references_535 = typename gil::detail::packed_channel_references_vector_type
246 mpl::vector3_c<int, 5, 3, 5>
249 using packed_pixel_rgb535 = gil::packed_pixel
252 packed_channel_references_535,
256 BOOST_AUTO_TEST_CASE(packed_pixel_rgb535_definition)
258 // Verify packed_pixel members
260 static_assert(std::is_same<packed_pixel_rgb535::layout_t, gil::rgb_layout_t>::value,
261 "layout should be bgr");
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");
266 static_assert(std::is_reference<packed_pixel_rgb535::reference>::value,
267 "reference member should be a reference");
269 static_assert(std::is_reference<packed_pixel_rgb535::const_reference>::value,
270 "const_reference member should be a reference");
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");
276 // Verify metafunctions
278 using channel_references_t = packed_channel_references_535::type;
280 static_assert(mpl::size<channel_references_t>::value == 3,
281 "packed_channel_references_vector_type should define three references to channel start bits");
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");
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");
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");
295 static_assert(std::is_same
298 gil::packed_channel_reference<std::uint16_t, 0, 5, true> const
300 "1st element of packed_channel_references_vector should be packed_channel_reference of 1st channel");
302 static_assert(std::is_same
305 gil::packed_channel_reference<std::uint16_t, 5, 3, true> const
307 "2nd element of packed_channel_references_vector should be packed_channel_reference of 2nd channel");
309 static_assert(std::is_same
312 gil::packed_channel_reference<std::uint16_t, 8, 5, true> const
314 "3rd element of packed_channel_references_vector should be packed_channel_reference of 3rd channel");
316 // double check intermediate metafunction packed_channel_reference_type
317 static_assert(std::is_same
319 gil::detail::packed_channel_reference_type<std::uint16_t, mpl::int_<0>, mpl::int_<5>>::type,
322 "packed_channel_reference_type should return packed_channel_reference");
323 static_assert(std::is_same
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
328 "packed_channel_reference_type should return packed_channel_reference");
331 BOOST_AUTO_TEST_CASE(packed_pixel_rgb535_assignment)
333 packed_pixel_rgb535 p1{31, 7, 31};
334 packed_pixel_rgb535 p2;
336 BOOST_TEST(p1._bitfield == p2._bitfield);
339 BOOST_AUTO_TEST_CASE(packed_pixel_rgb535_equality)
341 packed_pixel_rgb535 p1{7, 3, 7};
342 packed_pixel_rgb535 p2{7, 3, 7};
343 BOOST_TEST(p1 == p2);
345 packed_pixel_rgb535 p3{7, 7, 7};
346 BOOST_TEST(p2 != p3);