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 {
consecutive_valueboost::gil::test::fixture::consecutive_value41 consecutive_value(T start) : current_(start)
42 {
43 BOOST_TEST(static_cast<int>(current_) >= 0);
44 }
45
operator ()boost::gil::test::fixture::consecutive_value46 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 {
reverse_consecutive_valueboost::gil::test::fixture::reverse_consecutive_value58 reverse_consecutive_value(T start) : current_(start)
59 {
60 BOOST_ASSERT(static_cast<int>(current_) > 0);
61 }
62
operator ()boost::gil::test::fixture::reverse_consecutive_value63 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
random_valueboost::gil::test::fixture::random_value79 random_value() : rng_(rd_()), uid_(range_min, range_max) {}
80
operator ()boost::gil::test::fixture::random_value81 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>
generate_image(std::ptrdiff_t size_x,std::ptrdiff_t size_y,Generator && generate)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>
create_image(std::ptrdiff_t size_x,std::ptrdiff_t size_y,int channel_value)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
122