Imported Upstream version 1.71.0
[platform/upstream/boost.git] / libs / gil / test / pixel / test_fixture.hpp
1 //
2 // Copyright 2019 Mateusz Loskot <mateusz at loskot dot net>
3 // Copyright 2005-2007 Adobe Systems Incorporated
4 //
5 // Distributed under the Boost Software License, Version 1.0
6 // See accompanying file LICENSE_1_0.txt or copy at
7 // http://www.boost.org/LICENSE_1_0.txt
8 //
9 #include <boost/gil/channel.hpp>
10 #include <boost/gil/color_base_algorithm.hpp>
11 #include <boost/gil/concepts/pixel.hpp>
12 #include <boost/gil/pixel.hpp>
13 #include <boost/gil/planar_pixel_reference.hpp>
14 #include <boost/gil/promote_integral.hpp>
15 #include <boost/gil/typedefs.hpp>
16
17 #include <boost/core/typeinfo.hpp>
18 #include <boost/mp11.hpp>
19 #include <boost/mp11/mpl.hpp> // for compatibility with Boost.Test
20
21 #include <cstdint>
22 #include <tuple>
23 #include <type_traits>
24
25 namespace boost { namespace gil {
26
27 namespace detail {
28
29 struct print_color_base
30 {
31     std::ostream& os_;
32     std::size_t element_index_{0};
33     print_color_base(std::ostream& os) : os_(os) {}
34
35     template <typename Element>
36     void operator()(Element& c)
37     {
38         typename gil::promote_integral<Element>::type const v(c);
39         if (element_index_ > 0) os_ << ", ";
40         os_ << "v" << element_index_ << "=" << v;
41         ++element_index_;
42     }
43 };
44
45 } // namespace detail
46
47 // Pixel has to implement operator<< to be printable for BOOST_TEST()
48 template <typename ChannelValue, typename Layout>
49 std::ostream& operator<<(std::ostream& os, pixel<ChannelValue, Layout> const& p)
50 {
51     os << "pixel<"
52        << "Channel=" << boost::core::demangled_name(typeid(ChannelValue))
53        << ", Layout=" << boost::core::demangled_name(typeid(Layout))
54        << ">(";
55
56     gil::static_for_each(p, detail::print_color_base{os});
57     os << ")" << std::endl;
58     return os;
59 }
60
61 namespace test { namespace fixture {
62
63 template <typename Pixel, int Tag = 0>
64 class pixel_value
65 {
66 public:
67     using type = Pixel;
68     using pixel_t = type;
69     type pixel_{};
70
71     pixel_value() = default;
72     explicit pixel_value(pixel_t const& pixel)
73         : pixel_(pixel) // test copy constructor
74     {
75         type temp_pixel; // test default constructor
76         boost::function_requires<PixelValueConcept<pixel_t> >();
77     }
78 };
79
80 // Alias compatible with naming of equivalent class in the test/legacy/pixel.cpp
81 // The core suffix indicates `Pixel` is GIL core pixel type.
82 template <typename Pixel, int Tag = 0>
83 using value_core = pixel_value<Pixel, Tag>;
84
85 template <typename PixelRef, int Tag = 0>
86 struct pixel_reference
87     : pixel_value
88     <
89         typename std::remove_reference<PixelRef>::type,
90         Tag
91     >
92 {
93     static_assert(
94         std::is_reference<PixelRef>::value ||
95         gil::is_planar<PixelRef>::value, // poor-man test for specialization of planar_pixel_reference
96         "PixelRef must be reference or gil::planar_pixel_reference");
97
98     using type = PixelRef;
99     using pixel_t = typename std::remove_reference<PixelRef>::type;
100     using parent_t = pixel_value<typename pixel_t::value_type, Tag>;
101     using value_t = typename pixel_t::value_type;
102     type pixel_{}; // reference
103
104     pixel_reference() : parent_t{}, pixel_(parent_t::pixel_) {}
105     explicit pixel_reference(value_t const& pixel) : parent_t(pixel), pixel_(parent_t::pixel_)
106     {
107         boost::function_requires<PixelConcept<pixel_t>>();
108     }
109 };
110
111 // Alias compatible with naming of equivalent class in the test/legacy/pixel.cpp
112 // The core suffix indicates `Pixel` is GIL core pixel type.
113 template <typename Pixel, int Tag = 0>
114 using reference_core = pixel_reference<Pixel, Tag>;
115
116 // Metafunction to yield nested type of a representative pixel type
117 template <typename PixelValueOrReference>
118 using nested_type = typename PixelValueOrReference::type;
119
120 // Metafunction to yield nested type of a representative pixel_t type
121 template <typename PixelValueOrReference>
122 using nested_pixel_type = typename PixelValueOrReference::pixel_t;
123
124 // Subset of pixel models that covers all color spaces, channel depths,
125 // reference/value, planar/interleaved, const/mutable.
126 // Operations like color conversion will be invoked on pairs of those.
127 using representative_pixel_types= ::boost::mp11::mp_list
128 <
129     value_core<gil::gray8_pixel_t>,
130     reference_core<gil::gray16_pixel_t&>,
131     value_core<gil::bgr8_pixel_t>,
132     reference_core<gil::rgb8_planar_ref_t>,
133     value_core<gil::argb32_pixel_t>,
134     reference_core<gil::cmyk32f_pixel_t&>,
135     reference_core<gil::abgr16c_ref_t>, // immutable reference
136     reference_core<gil::rgb32fc_planar_ref_t>
137 >;
138
139 // List of all core pixel types (i.e. without cv-qualifiers)
140 using pixel_types = ::boost::mp11::mp_list
141 <
142     gil::gray8_pixel_t,
143     gil::gray8s_pixel_t,
144     gil::gray16_pixel_t,
145     gil::gray16s_pixel_t,
146     gil::gray32_pixel_t,
147     gil::gray32s_pixel_t,
148     gil::gray32f_pixel_t,
149     gil::bgr8_pixel_t,
150     gil::bgr8s_pixel_t,
151     gil::bgr16_pixel_t,
152     gil::bgr16s_pixel_t,
153     gil::bgr32_pixel_t,
154     gil::bgr32f_pixel_t,
155     gil::bgr32s_pixel_t,
156     gil::rgb8_pixel_t,
157     gil::rgb8s_pixel_t,
158     gil::rgb16_pixel_t,
159     gil::rgb16s_pixel_t,
160     gil::rgb32_pixel_t,
161     gil::rgb32f_pixel_t,
162     gil::rgb32s_pixel_t,
163     gil::abgr8_pixel_t,
164     gil::abgr8s_pixel_t,
165     gil::abgr16_pixel_t,
166     gil::abgr16s_pixel_t,
167     gil::abgr32_pixel_t,
168     gil::abgr32f_pixel_t,
169     gil::abgr32s_pixel_t,
170     gil::bgra8_pixel_t,
171     gil::bgra8s_pixel_t,
172     gil::bgra16_pixel_t,
173     gil::bgra16s_pixel_t,
174     gil::bgra32_pixel_t,
175     gil::bgra32f_pixel_t,
176     gil::bgra32s_pixel_t,
177     gil::cmyk8_pixel_t,
178     gil::cmyk8s_pixel_t,
179     gil::cmyk16_pixel_t,
180     gil::cmyk16s_pixel_t,
181     gil::cmyk32_pixel_t,
182     gil::cmyk32f_pixel_t,
183     gil::cmyk32s_pixel_t,
184     gil::rgba8_pixel_t,
185     gil::rgba8s_pixel_t,
186     gil::rgba16_pixel_t,
187     gil::rgba16s_pixel_t,
188     gil::rgba32_pixel_t,
189     gil::rgba32f_pixel_t,
190     gil::rgba32s_pixel_t
191 >;
192
193 // List of all core pixel typedefs (i.e. with cv-qualifiers)
194 using pixel_typedefs = ::boost::mp11::mp_list
195 <
196     gil::gray8_pixel_t,
197     gil::gray8c_pixel_t,
198     gil::gray8s_pixel_t,
199     gil::gray8sc_pixel_t,
200     gil::gray16_pixel_t,
201     gil::gray16c_pixel_t,
202     gil::gray16s_pixel_t,
203     gil::gray16sc_pixel_t,
204     gil::gray32_pixel_t,
205     gil::gray32c_pixel_t,
206     gil::gray32f_pixel_t,
207     gil::gray32fc_pixel_t,
208     gil::gray32s_pixel_t,
209     gil::gray32sc_pixel_t,
210     gil::bgr8_pixel_t,
211     gil::bgr8c_pixel_t,
212     gil::bgr8s_pixel_t,
213     gil::bgr8sc_pixel_t,
214     gil::bgr16_pixel_t,
215     gil::bgr16c_pixel_t,
216     gil::bgr16s_pixel_t,
217     gil::bgr16sc_pixel_t,
218     gil::bgr32_pixel_t,
219     gil::bgr32c_pixel_t,
220     gil::bgr32f_pixel_t,
221     gil::bgr32fc_pixel_t,
222     gil::bgr32s_pixel_t,
223     gil::bgr32sc_pixel_t,
224     gil::rgb8_pixel_t,
225     gil::rgb8c_pixel_t,
226     gil::rgb8s_pixel_t,
227     gil::rgb8sc_pixel_t,
228     gil::rgb16_pixel_t,
229     gil::rgb16c_pixel_t,
230     gil::rgb16s_pixel_t,
231     gil::rgb16sc_pixel_t,
232     gil::rgb32_pixel_t,
233     gil::rgb32c_pixel_t,
234     gil::rgb32f_pixel_t,
235     gil::rgb32fc_pixel_t,
236     gil::rgb32s_pixel_t,
237     gil::rgb32sc_pixel_t,
238     gil::abgr8_pixel_t,
239     gil::abgr8c_pixel_t,
240     gil::abgr8s_pixel_t,
241     gil::abgr8sc_pixel_t,
242     gil::abgr16_pixel_t,
243     gil::abgr16c_pixel_t,
244     gil::abgr16s_pixel_t,
245     gil::abgr16sc_pixel_t,
246     gil::abgr32_pixel_t,
247     gil::abgr32c_pixel_t,
248     gil::abgr32f_pixel_t,
249     gil::abgr32fc_pixel_t,
250     gil::abgr32s_pixel_t,
251     gil::abgr32sc_pixel_t,
252     gil::bgra8_pixel_t,
253     gil::bgra8c_pixel_t,
254     gil::bgra8s_pixel_t,
255     gil::bgra8sc_pixel_t,
256     gil::bgra16_pixel_t,
257     gil::bgra16c_pixel_t,
258     gil::bgra16s_pixel_t,
259     gil::bgra16sc_pixel_t,
260     gil::bgra32_pixel_t,
261     gil::bgra32c_pixel_t,
262     gil::bgra32f_pixel_t,
263     gil::bgra32fc_pixel_t,
264     gil::bgra32s_pixel_t,
265     gil::bgra32sc_pixel_t,
266     gil::cmyk8_pixel_t,
267     gil::cmyk8c_pixel_t,
268     gil::cmyk8s_pixel_t,
269     gil::cmyk8sc_pixel_t,
270     gil::cmyk16_pixel_t,
271     gil::cmyk16c_pixel_t,
272     gil::cmyk16s_pixel_t,
273     gil::cmyk16sc_pixel_t,
274     gil::cmyk32_pixel_t,
275     gil::cmyk32c_pixel_t,
276     gil::cmyk32f_pixel_t,
277     gil::cmyk32fc_pixel_t,
278     gil::cmyk32s_pixel_t,
279     gil::cmyk32sc_pixel_t,
280     gil::rgba8_pixel_t,
281     gil::rgba8c_pixel_t,
282     gil::rgba8s_pixel_t,
283     gil::rgba8sc_pixel_t,
284     gil::rgba16_pixel_t,
285     gil::rgba16c_pixel_t,
286     gil::rgba16s_pixel_t,
287     gil::rgba16sc_pixel_t,
288     gil::rgba32_pixel_t,
289     gil::rgba32c_pixel_t,
290     gil::rgba32f_pixel_t,
291     gil::rgba32fc_pixel_t,
292     gil::rgba32s_pixel_t,
293     gil::rgba32sc_pixel_t
294 >;
295
296 struct not_a_pixel_type {};
297
298 using non_pixels = ::boost::mp11::mp_list
299 <
300     not_a_pixel_type,
301     char,
302     short, int, long,
303     double, float,
304     std::size_t,
305     std::true_type,
306     std::false_type
307 >;
308
309
310 }}}} // namespace boost::gil::test::fixture