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 | |
10 | #include <boost/mp11.hpp> |
11 | #include <boost/core/lightweight_test.hpp> |
12 | |
13 | #include <memory> |
14 | #include <random> |
15 | #include <type_traits> |
16 | #include <vector> |
17 | |
18 | #include "test_utility_output_stream.hpp" |
19 | #include "core/channel/test_fixture.hpp" |
20 | #include "core/image/test_fixture.hpp" |
21 | #include "core/pixel/test_fixture.hpp" |
22 | |
23 | namespace boost { namespace gil { namespace test { namespace fixture { |
24 | |
25 | template <typename Pixel> |
26 | struct pixel_array |
27 | { |
28 | using iterator = Pixel*; |
29 | #ifdef NDEBUG |
30 | constexpr static std::size_t default_x_size = 256; |
31 | constexpr static std::size_t default_y_size = 128; |
32 | #else |
33 | constexpr static std::size_t default_x_size = 16; |
34 | constexpr static std::size_t default_y_size = 8; |
35 | #endif |
36 | |
37 | pixel_array(std::size_t x_size = default_x_size, std::size_t y_size = default_y_size) |
38 | : pixels_(new Pixel[x_size * y_size]) |
39 | , x_size_(x_size) |
40 | , y_size_(y_size) |
41 | {} |
42 | |
43 | auto begin() -> iterator { return pixels_.get(); } |
44 | auto end() -> iterator { return pixels_.get() + x_size_ * y_size_; } |
45 | |
46 | private: |
47 | std::unique_ptr<Pixel[]> pixels_; |
48 | std::size_t x_size_; |
49 | std::size_t y_size_; |
50 | }; |
51 | |
52 | }}}} |
53 | |
54 | namespace gil = boost::gil; |
55 | namespace fixture = boost::gil::test::fixture; |
56 | |
57 | struct fill_with_pixel_integer_types |
58 | { |
59 | template <typename Pixel> |
60 | void operator()(Pixel const &) |
61 | { |
62 | using pixel_t = Pixel; |
63 | auto min_pixel = fixture::pixel_generator<pixel_t>::min(); |
64 | auto max_pixel = fixture::pixel_generator<pixel_t>::max(); |
65 | auto rnd_pixel = fixture::pixel_generator<pixel_t>::random(); |
66 | |
67 | for (auto const &fill_pixel : {min_pixel, max_pixel, rnd_pixel}) |
68 | { |
69 | fixture::pixel_array<pixel_t> pixels; |
70 | std::uninitialized_fill(pixels.begin(), pixels.end(), fill_pixel); |
71 | |
72 | for (pixel_t const &p : pixels) |
73 | BOOST_TEST_EQ(p, fill_pixel); |
74 | } |
75 | } |
76 | static void run() |
77 | { |
78 | boost::mp11::mp_for_each<fixture::pixel_integer_types>(f: fill_with_pixel_integer_types{}); |
79 | } |
80 | }; |
81 | |
82 | struct fill_with_pixel_float_types |
83 | { |
84 | template <typename Pixel> |
85 | void operator()(Pixel const &) |
86 | { |
87 | using pixel_t = Pixel; |
88 | auto min_pixel = fixture::pixel_generator<pixel_t>::min(); |
89 | auto max_pixel = fixture::pixel_generator<pixel_t>::max(); |
90 | |
91 | for (auto const &fill_pixel : {min_pixel, max_pixel}) |
92 | { |
93 | fixture::pixel_array<Pixel> pixels; |
94 | std::uninitialized_fill(pixels.begin(), pixels.end(), fill_pixel); |
95 | |
96 | for (Pixel const &p : pixels) |
97 | BOOST_TEST_EQ(p, fill_pixel); |
98 | } |
99 | } |
100 | static void run() |
101 | { |
102 | boost::mp11::mp_for_each<fixture::pixel_float_types>(f: fill_with_pixel_float_types{}); |
103 | } |
104 | }; |
105 | |
106 | void |
107 | test_fill_with_packed_pixel_gray3() |
108 | { |
109 | auto min_pixel = fixture::packed_pixel_gray3{0}; |
110 | auto mid_pixel = fixture::packed_pixel_gray3{3}; |
111 | auto max_pixel = fixture::packed_pixel_gray3{7}; |
112 | |
113 | for (auto const& fill_pixel : {min_pixel, max_pixel, mid_pixel} ) |
114 | { |
115 | fixture::pixel_array<fixture::packed_pixel_gray3> pixels; |
116 | std::uninitialized_fill(first: pixels.begin(), last: pixels.end(), x: fill_pixel); |
117 | |
118 | for (fixture::packed_pixel_gray3 const& p : pixels) |
119 | { |
120 | BOOST_TEST_EQ(p, fill_pixel); |
121 | BOOST_TEST_EQ((int)get_color(p, gil::gray_color_t()), (int)get_color(fill_pixel, gil::gray_color_t())); |
122 | } |
123 | } |
124 | } |
125 | |
126 | void test_fill_with_packed_pixel_bgr121() |
127 | { |
128 | auto min_pixel = fixture::packed_pixel_bgr121{0}; |
129 | auto mid_pixel = fixture::packed_pixel_bgr121{8}; |
130 | auto max_pixel = fixture::packed_pixel_bgr121{17}; |
131 | |
132 | for (auto const& fill_pixel : {min_pixel, max_pixel, mid_pixel} ) |
133 | { |
134 | fixture::pixel_array<fixture::packed_pixel_bgr121> pixels; |
135 | std::uninitialized_fill(first: pixels.begin(), last: pixels.end(), x: fill_pixel); |
136 | |
137 | for (fixture::packed_pixel_bgr121 const& p : pixels) |
138 | { |
139 | BOOST_TEST_EQ(p, fill_pixel); |
140 | BOOST_TEST_EQ((int)get_color(p, gil::red_t()), (int)get_color(fill_pixel, gil::red_t())); |
141 | BOOST_TEST_EQ((int)get_color(p, gil::green_t()), (int)get_color(fill_pixel, gil::green_t())); |
142 | BOOST_TEST_EQ((int)get_color(p, gil::blue_t()), (int)get_color(fill_pixel, gil::blue_t())); |
143 | } |
144 | } |
145 | } |
146 | |
147 | void test_fill_with_packed_pixel_rgb535() |
148 | { |
149 | fixture::packed_pixel_rgb535 min_pixel(0, 0, 0); |
150 | fixture::packed_pixel_rgb535 mid_pixel(15, 3, 15); |
151 | fixture::packed_pixel_rgb535 max_pixel(31, 7, 31); |
152 | |
153 | for (auto const& fill_pixel : {min_pixel, max_pixel, mid_pixel} ) |
154 | { |
155 | fixture::pixel_array<fixture::packed_pixel_rgb535> pixels; |
156 | std::uninitialized_fill(first: pixels.begin(), last: pixels.end(), x: fill_pixel); |
157 | |
158 | for (fixture::packed_pixel_rgb535 const& p : pixels) |
159 | { |
160 | BOOST_TEST_EQ(p, fill_pixel); |
161 | BOOST_TEST_EQ((int)get_color(p, gil::red_t()), (int)get_color(fill_pixel, gil::red_t())); |
162 | BOOST_TEST_EQ((int)get_color(p, gil::green_t()), (int)get_color(fill_pixel, gil::green_t())); |
163 | BOOST_TEST_EQ((int)get_color(p, gil::blue_t()), (int)get_color(fill_pixel, gil::blue_t())); |
164 | } |
165 | } |
166 | } |
167 | |
168 | void test_bit_aligned_pixel_bgr232() |
169 | { |
170 | fixture::bit_aligned_pixel_bgr232 min_pixel(0, 0, 0); |
171 | fixture::bit_aligned_pixel_bgr232 mid_pixel(1, 4, 2); |
172 | fixture::bit_aligned_pixel_bgr232 max_pixel(3, 7, 3); |
173 | |
174 | for (auto const& fill_pixel : {min_pixel, max_pixel, mid_pixel} ) |
175 | { |
176 | fixture::pixel_array<fixture::bit_aligned_pixel_bgr232> pixels; |
177 | std::uninitialized_fill(first: pixels.begin(), last: pixels.end(), x: fill_pixel); |
178 | |
179 | for (fixture::bit_aligned_pixel_bgr232 const& p : pixels) |
180 | { |
181 | BOOST_TEST_EQ(p, fill_pixel); |
182 | BOOST_TEST_EQ((int)get_color(p, gil::red_t()), (int)get_color(fill_pixel, gil::red_t())); |
183 | BOOST_TEST_EQ((int)get_color(p, gil::green_t()), (int)get_color(fill_pixel, gil::green_t())); |
184 | BOOST_TEST_EQ((int)get_color(p, gil::blue_t()), (int)get_color(fill_pixel, gil::blue_t())); |
185 | } |
186 | } |
187 | } |
188 | |
189 | void test_bit_aligned_pixel_rgb567() |
190 | { |
191 | fixture::bit_aligned_pixel_rgb567 min_pixel(0, 0, 0); |
192 | fixture::bit_aligned_pixel_rgb567 mid_pixel(15, 31, 63); |
193 | fixture::bit_aligned_pixel_rgb567 max_pixel(31, 63, 127); |
194 | |
195 | for (auto const& fill_pixel : {min_pixel, max_pixel, mid_pixel} ) |
196 | { |
197 | fixture::pixel_array<fixture::bit_aligned_pixel_rgb567> pixels; |
198 | std::uninitialized_fill(first: pixels.begin(), last: pixels.end(), x: fill_pixel); |
199 | |
200 | for (fixture::bit_aligned_pixel_rgb567 const& p : pixels) |
201 | { |
202 | BOOST_TEST_EQ(p, fill_pixel); |
203 | BOOST_TEST_EQ((int)get_color(p, gil::red_t()), (int)get_color(fill_pixel, gil::red_t())); |
204 | BOOST_TEST_EQ((int)get_color(p, gil::green_t()), (int)get_color(fill_pixel, gil::green_t())); |
205 | BOOST_TEST_EQ((int)get_color(p, gil::blue_t()), (int)get_color(fill_pixel, gil::blue_t())); |
206 | } |
207 | } |
208 | } |
209 | |
210 | int main() |
211 | { |
212 | fill_with_pixel_integer_types::run(); |
213 | fill_with_pixel_float_types::run(); |
214 | |
215 | test_fill_with_packed_pixel_gray3(); |
216 | test_fill_with_packed_pixel_bgr121(); |
217 | test_fill_with_packed_pixel_rgb535(); |
218 | |
219 | test_bit_aligned_pixel_bgr232(); |
220 | test_bit_aligned_pixel_rgb567(); |
221 | |
222 | return ::boost::report_errors(); |
223 | } |
224 | |