Imported Upstream version 1.72.0
[platform/upstream/boost.git] / libs / gil / test / core / image / test_fixture.hpp
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.hpp>
9 #include <boost/assert.hpp>
10
11 #include <cstdint>
12 #include <initializer_list>
13 #include <limits>
14 #include <random>
15 #include <tuple>
16 #include <type_traits>
17
18 namespace boost { namespace gil {
19
20 namespace test { namespace fixture {
21
22 using image_types = std::tuple
23 <
24     gil::gray8_image_t,
25     gil::gray16_image_t,
26     gil::gray32_image_t,
27     gil::bgr8_image_t,
28     gil::bgr16_image_t,
29     gil::bgr32_image_t,
30     gil::rgb8_image_t,
31     gil::rgb16_image_t,
32     gil::rgb32_image_t,
33     gil::rgba8_image_t,
34     gil::rgba16_image_t,
35     gil::rgba32_image_t
36 >;
37
38 template <typename T>
39 struct consecutive_value
40 {
41     consecutive_value(T start) : current_(start)
42     {
43         BOOST_TEST(static_cast<int>(current_) >= 0);
44     }
45
46     T operator()()
47     {
48         BOOST_ASSERT(static_cast<int>(current_) + 1 > 0);
49         return current_++;
50     }
51
52     T current_;
53 };
54
55 template <typename T>
56 struct reverse_consecutive_value
57 {
58     reverse_consecutive_value(T start) : current_(start)
59     {
60         BOOST_ASSERT(static_cast<int>(current_) > 0);
61     }
62
63     T operator()()
64     {
65         BOOST_ASSERT(static_cast<int>(current_) + 1 >= 0);
66         return current_--;
67     }
68
69     T current_;
70 };
71
72 template <typename T>
73 struct random_value
74 {
75     static_assert(std::is_integral<T>::value, "T must be integral type");
76     static constexpr auto range_min = std::numeric_limits<T>::min();
77     static constexpr auto range_max = std::numeric_limits<T>::max();
78
79     random_value() : rng_(rd_()), uid_(range_min, range_max) {}
80
81     T operator()()
82     {
83         auto value = uid_(rng_);
84         BOOST_ASSERT(range_min <= value && value <= range_max);
85         return static_cast<T>(value);
86     }
87
88     std::random_device rd_;
89     std::mt19937 rng_;
90     std::uniform_int_distribution<typename gil::promote_integral<T>::type> uid_;
91 };
92
93 template <typename Image, typename Generator>
94 auto generate_image(std::ptrdiff_t size_x, std::ptrdiff_t size_y, Generator&& generate) -> Image
95 {
96     using pixel_t = typename Image::value_type;
97
98     Image out(size_x, size_y);
99     gil::for_each_pixel(view(out), [&generate](pixel_t& p) {
100         gil::static_generate(p, [&generate]() { return generate(); });
101     });
102
103     return out;
104 }
105
106 template <typename Image>
107 auto create_image(std::ptrdiff_t size_x, std::ptrdiff_t size_y, int channel_value) -> Image
108 {
109     using pixel_t = typename Image::value_type;
110     using channel_t = typename gil::channel_type<pixel_t>::type;
111     static_assert(std::is_integral<channel_t>::value, "channel must be integral type");
112
113     Image out(size_x, size_y);
114     gil::for_each_pixel(view(out), [&channel_value](pixel_t& p) {
115         gil::static_fill(p, static_cast<channel_t>(channel_value));
116     });
117
118     return out;
119 }
120
121 }}}} // namespace boost::gil::test::fixture