1 //
2 // Copyright 2019-2020 Mateusz Loskot <mateusz at loskot dot net>
3 //
4 // Distribtted 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_TEST_TEST_UTILITY_HPP
9 #define BOOST_GIL_TEST_TEST_UTILITY_HPP
10
11 #include <boost/gil/color_base_algorithm.hpp> // static_for_each
12 #include <boost/gil/packed_pixel.hpp>
13 #include <boost/gil/pixel.hpp>
14 #include <boost/gil/planar_pixel_reference.hpp>
15 #include <boost/gil/promote_integral.hpp>
16
17 #include <boost/core/demangle.hpp>
18 #include <boost/core/typeinfo.hpp>
19
20 #include <cstdint>
21 #include <ostream>
22 #include <type_traits>
23
24 // Utilities to make GIL primitives printable for BOOST_TEST_EQ and other macros
25
26 namespace boost { namespace gil {
27
28 namespace test { namespace utility {
29
30 template <typename T>
31 struct printable_numeric
32 {
33 using type = typename std::conditional
34 <
35 std::is_integral<T>::value,
36 typename ::boost::gil::promote_integral<T>::type,
37 typename std::common_type<T, double>::type
38 >::type;
39
40 static_assert(std::is_arithmetic<T>::value, "T must be numeric type");
41 static_assert(sizeof(T) <= sizeof(type), "bit-size narrowing conversion");
42 };
43
44 template <typename T>
45 using printable_numeric_t = typename printable_numeric<T>::type;
46
47 struct print_color_base
48 {
49 std::ostream& os_;
50 std::size_t element_index_{0};
print_color_baseboost::gil::test::utility::print_color_base51 print_color_base(std::ostream& os) : os_(os) {}
52
53 template <typename Element>
operator ()boost::gil::test::utility::print_color_base54 void operator()(Element const& c)
55 {
56 printable_numeric_t<Element> n{c};
57 if (element_index_ > 0) os_ << ", ";
58 os_ << "v" << element_index_ << "=" << n;
59 ++element_index_;
60 }
61
62 template <typename BitField, int FirstBit, int NumBits, bool IsMutable>
operator ()boost::gil::test::utility::print_color_base63 void operator()(gil::packed_channel_reference<BitField, FirstBit, NumBits, IsMutable> const& c)
64 {
65 printable_numeric_t<BitField> n{c.get()};
66 if (element_index_ > 0) os_ << ", ";
67 os_ << "v" << element_index_ << "=" << n;
68 ++element_index_;
69 }
70
71 template <typename BaseChannelValue, typename MinVal, typename MaxVal>
operator ()boost::gil::test::utility::print_color_base72 void operator()(gil::scoped_channel_value<BaseChannelValue, MinVal, MaxVal> const& c)
73 {
74 printable_numeric_t<BaseChannelValue> n{c};
75 if (element_index_ > 0) os_ << ", ";
76 os_ << "v" << element_index_ << "=" << n;
77 ++element_index_;
78 }
79 };
80
81 }} // namespace test::utility
82
83 template <typename T>
operator <<(std::ostream & os,point<T> const & p)84 std::ostream& operator<<(std::ostream& os, point<T> const& p)
85 {
86 os << "point<" << boost::core::demangled_name(typeid(T)) << ">";
87 os << "(" << p.x << ", " << p.y << ")" << std::endl;
88 return os;
89 }
90
91 template <typename ChannelValue, typename Layout>
operator <<(std::ostream & os,pixel<ChannelValue,Layout> const & p)92 std::ostream& operator<<(std::ostream& os, pixel<ChannelValue, Layout> const& p)
93 {
94 os << "pixel<"
95 << "\n\tChannel=" << boost::core::demangled_name(typeid(ChannelValue))
96 << ",\n\tLayout=" << boost::core::demangled_name(typeid(Layout))
97 << "\n>(";
98
99 static_for_each(p, test::utility::print_color_base{os});
100 os << ")" << std::endl;
101 return os;
102 }
103
104 template <typename BitField, typename ChannelRefs, typename Layout>
operator <<(std::ostream & os,packed_pixel<BitField,ChannelRefs,Layout> const & p)105 std::ostream& operator<<(std::ostream& os, packed_pixel<BitField, ChannelRefs, Layout> const& p)
106 {
107 os << "packed_pixel<"
108 << "\n\tBitField=" << boost::core::demangled_name(typeid(BitField))
109 << ",\n\tChannelRefs=" << boost::core::demangled_name(typeid(ChannelRefs))
110 << ",\n\tLayout=" << boost::core::demangled_name(typeid(Layout))
111 << ">(";
112
113 static_for_each(p, test::utility::print_color_base{os});
114 os << ")" << std::endl;
115 return os;
116 }
117
118 template <typename ChannelReference, typename ColorSpace>
operator <<(std::ostream & os,planar_pixel_reference<ChannelReference,ColorSpace> const & p)119 std::ostream& operator<<(std::ostream& os, planar_pixel_reference<ChannelReference, ColorSpace> const& p)
120 {
121 os << "planar_pixel_reference<"
122 << "\nChannelReference=" << boost::core::demangled_name(typeid(ChannelReference))
123 << ",\nColorSpace=" << boost::core::demangled_name(typeid(ColorSpace))
124 << ">(";
125
126 static_for_each(p, test::utility::print_color_base{os});
127 os << ")" << std::endl;
128 return os;
129 }
130
131 }} // namespace boost::gil
132
133 #endif
134