2 // Copyright 2019 Miral Shah <miralshah2211@gmail.com>
4 // Use, modification and distribution are subject to the Boost Software License,
5 // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
6 // http://www.boost.org/LICENSE_1_0.txt)
8 #include <boost/gil/image_processing/threshold.hpp>
9 #include <boost/gil/image_view.hpp>
10 #include <boost/gil/algorithm.hpp>
11 #include <boost/gil/gray.hpp>
12 #include <boost/core/lightweight_test.hpp>
15 namespace gil = boost::gil;
20 gil::gray8_image_t original_gray(width, height), threshold_gray(width, height),
21 expected_gray(width, height);
23 gil::rgb8_image_t original_rgb(width, height), threshold_rgb(width, height),
24 expected_rgb(width, height);
26 void fill_original_gray()
28 //filling original view's upper half part with gray pixels of value 50
29 //filling original view's lower half part with gray pixels of value 150
30 gil::fill_pixels(gil::subimage_view(gil::view(original_gray), 0, 0, original_gray.width(),
31 original_gray.height() / 2), gil::gray8_pixel_t(50));
32 gil::fill_pixels(gil::subimage_view(gil::view(original_gray), 0, original_gray.height() / 2,
33 original_gray.width(), original_gray.height() / 2), gil::gray8_pixel_t(150));
36 void fill_original_rgb()
38 //filling original_rgb view's upper half part with rgb pixels of value 50, 85, 135
39 //filling original_rgb view's lower half part with rgb pixels of value 150, 205, 106
40 gil::fill_pixels(gil::subimage_view(gil::view(original_rgb), 0, 0, original_rgb.width(),
41 original_rgb.height() / 2), gil::rgb8_pixel_t(50, 85, 135));
42 gil::fill_pixels(gil::subimage_view(gil::view(original_rgb), 0, original_rgb.height() / 2,
43 original_rgb.width(), original_rgb.height() / 2), gil::rgb8_pixel_t(150, 205, 106));
46 void threshold_gray_to_gray()
48 //expected view after thresholding of the original view with threshold value of 100
49 //filling expected view's upper half part with gray pixels of value 50
50 //filling expected view's lower half part with gray pixels of value 100
51 gil::fill_pixels(gil::subimage_view(gil::view(expected_gray), 0, 0, original_gray.width(),
52 original_gray.height() / 2), gil::gray8_pixel_t(50));
53 gil::fill_pixels(gil::subimage_view(gil::view(expected_gray), 0, original_gray.height() / 2,
54 original_gray.width(), original_gray.height() / 2), gil::gray8_pixel_t(100));
56 gil::threshold_truncate(gil::view(original_gray), gil::view(threshold_gray), 100);
58 //comparing threshold view generated by the function with the expected view
59 BOOST_TEST(gil::equal_pixels(gil::view(threshold_gray), gil::view(expected_gray)));
62 void threshold_inverse_gray_to_gray()
64 //expected view after thresholding of the original view with threshold value of 100
65 //filling expected view's upper half part with gray pixels of value 100
66 //filling expected view's lower half part with gray pixels of value 150
67 gil::fill_pixels(gil::subimage_view(gil::view(expected_gray), 0, 0, original_gray.width(),
68 original_gray.height() / 2), gil::gray8_pixel_t(100));
69 gil::fill_pixels(gil::subimage_view(gil::view(expected_gray), 0, original_gray.height() / 2,
70 original_gray.width(), original_gray.height() / 2), gil::gray8_pixel_t(150));
72 gil::threshold_truncate
74 gil::view(original_gray),
75 gil::view(threshold_gray),
77 gil::threshold_truncate_mode::threshold,
78 gil::threshold_direction::inverse
81 //comparing threshold view generated by the function with the expected view
82 BOOST_TEST(gil::equal_pixels(gil::view(threshold_gray), gil::view(expected_gray)));
85 void zero_gray_to_gray()
87 //expected view after thresholding of the original view with threshold value of 100
88 //filling expected view's upper half part with gray pixels of value 0
89 //filling expected view's lower half part with gray pixels of value 150
90 gil::fill_pixels(gil::subimage_view(gil::view(expected_gray), 0, 0, original_gray.width(),
91 original_gray.height() / 2), gil::gray8_pixel_t(0));
92 gil::fill_pixels(gil::subimage_view(gil::view(expected_gray), 0, original_gray.height() / 2,
93 original_gray.width(), original_gray.height() / 2), gil::gray8_pixel_t(150));
95 gil::threshold_truncate
97 gil::view(original_gray),
98 gil::view(threshold_gray),
100 gil::threshold_truncate_mode::zero,
101 gil::threshold_direction::regular
104 //comparing threshold view generated by the function with the expected view
105 BOOST_TEST(gil::equal_pixels(gil::view(threshold_gray), gil::view(expected_gray)));
108 void zero_inverse_gray_to_gray()
110 //expected view after thresholding of the original view with threshold value of 100
111 //filling expected view's upper half part with gray pixels of value 50
112 //filling expected view's lower half part with gray pixels of value 0
113 gil::fill_pixels(gil::subimage_view(gil::view(expected_gray), 0, 0, original_gray.width(),
114 original_gray.height() / 2), gil::gray8_pixel_t(50));
115 gil::fill_pixels(gil::subimage_view(gil::view(expected_gray), 0, original_gray.height() / 2,
116 original_gray.width(), original_gray.height() / 2), gil::gray8_pixel_t(0));
118 gil::threshold_truncate
120 gil::view(original_gray),
121 gil::view(threshold_gray),
123 gil::threshold_truncate_mode::zero,
124 gil::threshold_direction::inverse
127 //comparing threshold view generated by the function with the expected view
128 BOOST_TEST(gil::equal_pixels(gil::view(threshold_gray), gil::view(expected_gray)));
131 void threshold_rgb_to_rgb()
133 //expected view after thresholding of the original view with threshold value of 100
134 //filling expected_rgb view's upper half part with rgb pixels of value 50
135 //filling expected_rgb view's lower half part with rgb pixels of value 100
136 gil::fill_pixels(gil::subimage_view(gil::view(expected_rgb), 0, 0, original_rgb.width(),
137 original_rgb.height() / 2), gil::rgb8_pixel_t(50, 85, 100));
138 gil::fill_pixels(gil::subimage_view(gil::view(expected_rgb), 0, original_rgb.height() / 2,
139 original_rgb.width(), original_rgb.height() / 2), gil::rgb8_pixel_t(100, 100, 100));
141 gil::threshold_truncate(gil::view(original_rgb), gil::view(threshold_rgb), 100);
143 //comparing threshold_rgb view generated by the function with the expected_rgb view
144 BOOST_TEST(gil::equal_pixels(gil::view(threshold_rgb), gil::view(expected_rgb)));
147 void threshold_inverse_rgb_to_rgb()
149 //expected view after thresholding of the original view with threshold value of 100
150 //filling expected_rgb view's upper half part with rgb pixels of value 103, 59, 246
151 //filling expected_rgb view's lower half part with rgb pixels of value 150
152 gil::fill_pixels(gil::subimage_view(gil::view(expected_rgb), 0, 0, original_rgb.width(),
153 original_rgb.height() / 2), gil::rgb8_pixel_t(100, 100, 135));
154 gil::fill_pixels(gil::subimage_view(gil::view(expected_rgb), 0, original_rgb.height() / 2,
155 original_rgb.width(), original_rgb.height() / 2), gil::rgb8_pixel_t(150, 205, 106));
157 gil::threshold_truncate
159 gil::view(original_rgb),
160 gil::view(threshold_rgb),
162 gil::threshold_truncate_mode::threshold,
163 gil::threshold_direction::inverse
166 //comparing threshold_rgb view generated by the function with the expected_rgb view
167 BOOST_TEST(gil::equal_pixels(gil::view(threshold_rgb), gil::view(expected_rgb)));
170 void zero_rgb_to_rgb()
172 //expected view after thresholding of the original view with threshold value of 100
173 //filling expected_rgb view's upper half part with rgb pixels of value 0
174 //filling expected_rgb view's lower half part with rgb pixels of value 150
175 gil::fill_pixels(gil::subimage_view(gil::view(expected_rgb), 0, 0, original_rgb.width(),
176 original_rgb.height() / 2), gil::rgb8_pixel_t(0, 0, 135));
177 gil::fill_pixels(gil::subimage_view(gil::view(expected_rgb), 0, original_rgb.height() / 2,
178 original_rgb.width(), original_rgb.height() / 2), gil::rgb8_pixel_t(150, 205, 106));
180 gil::threshold_truncate
182 gil::view(original_rgb),
183 gil::view(threshold_rgb),
185 gil::threshold_truncate_mode::zero,
186 gil::threshold_direction::regular
189 //comparing threshold_rgb view generated by the function with the expected_rgb view
190 BOOST_TEST(gil::equal_pixels(gil::view(threshold_rgb), gil::view(expected_rgb)));
193 void zero_inverse_rgb_to_rgb()
195 //expected view after thresholding of the original view with threshold value of 100
196 //filling expected_rgb view's upper half part with rgb pixels of value 50
197 //filling expected_rgb view's lower half part with rgb pixels of value 0
198 gil::fill_pixels(gil::subimage_view(gil::view(expected_rgb), 0, 0, original_rgb.width(),
199 original_rgb.height() / 2), gil::rgb8_pixel_t(50, 85, 0));
200 gil::fill_pixels(gil::subimage_view(gil::view(expected_rgb), 0, original_rgb.height() / 2,
201 original_rgb.width(), original_rgb.height() / 2), gil::rgb8_pixel_t(0, 0, 0));
203 gil::threshold_truncate
205 gil::view(original_rgb),
206 gil::view(threshold_rgb),
208 gil::threshold_truncate_mode::zero,
209 gil::threshold_direction::inverse
212 //comparing threshold_rgb view generated by the function with the expected_rgb view
213 BOOST_TEST(gil::equal_pixels(gil::view(threshold_rgb), gil::view(expected_rgb)));
219 fill_original_gray();
222 threshold_gray_to_gray();
223 threshold_inverse_gray_to_gray();
225 zero_inverse_gray_to_gray();
227 threshold_rgb_to_rgb();
228 threshold_inverse_rgb_to_rgb();
230 zero_inverse_rgb_to_rgb();
232 return boost::report_errors();