Imported Upstream version 1.72.0
[platform/upstream/boost.git] / libs / gil / test / core / image_processing / threshold_truncate.cpp
1 //
2 // Copyright 2019 Miral Shah <miralshah2211@gmail.com>
3 //
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)
7 //
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>
13
14
15 namespace gil = boost::gil;
16
17 int height = 4;
18 int width = 4;
19
20 gil::gray8_image_t original_gray(width, height), threshold_gray(width, height),
21 expected_gray(width, height);
22
23 gil::rgb8_image_t original_rgb(width, height), threshold_rgb(width, height),
24 expected_rgb(width, height);
25
26 void fill_original_gray()
27 {
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));
34 }
35
36 void fill_original_rgb()
37 {
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));
44 }
45
46 void threshold_gray_to_gray()
47 {
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));
55
56     gil::threshold_truncate(gil::view(original_gray), gil::view(threshold_gray), 100);
57
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)));
60 }
61
62 void threshold_inverse_gray_to_gray()
63 {
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));
71
72     gil::threshold_truncate
73     (
74         gil::view(original_gray),
75         gil::view(threshold_gray),
76         100,
77         gil::threshold_truncate_mode::threshold,
78         gil::threshold_direction::inverse
79     );
80
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)));
83 }
84
85 void zero_gray_to_gray()
86 {
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));
94
95     gil::threshold_truncate
96     (
97         gil::view(original_gray),
98         gil::view(threshold_gray),
99         100,
100         gil::threshold_truncate_mode::zero,
101         gil::threshold_direction::regular
102     );
103
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)));
106 }
107
108 void zero_inverse_gray_to_gray()
109 {
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));
117
118     gil::threshold_truncate
119     (
120         gil::view(original_gray),
121         gil::view(threshold_gray),
122         100,
123         gil::threshold_truncate_mode::zero,
124         gil::threshold_direction::inverse
125     );
126
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)));
129 }
130
131 void threshold_rgb_to_rgb()
132 {
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));
140
141     gil::threshold_truncate(gil::view(original_rgb), gil::view(threshold_rgb), 100);
142
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)));
145 }
146
147 void threshold_inverse_rgb_to_rgb()
148 {
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));
156
157     gil::threshold_truncate
158     (
159         gil::view(original_rgb),
160         gil::view(threshold_rgb),
161         100,
162         gil::threshold_truncate_mode::threshold,
163         gil::threshold_direction::inverse
164     );
165
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)));
168 }
169
170 void zero_rgb_to_rgb()
171 {
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));
179
180     gil::threshold_truncate
181     (
182         gil::view(original_rgb),
183         gil::view(threshold_rgb),
184         100,
185         gil::threshold_truncate_mode::zero,
186         gil::threshold_direction::regular
187     );
188
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)));
191 }
192
193 void zero_inverse_rgb_to_rgb()
194 {
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));
202
203     gil::threshold_truncate
204     (
205         gil::view(original_rgb),
206         gil::view(threshold_rgb),
207         100,
208         gil::threshold_truncate_mode::zero,
209         gil::threshold_direction::inverse
210     );
211
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)));
214 }
215
216
217 int main()
218 {
219     fill_original_gray();
220     fill_original_rgb();
221
222     threshold_gray_to_gray();
223     threshold_inverse_gray_to_gray();
224     zero_gray_to_gray();
225     zero_inverse_gray_to_gray();
226
227     threshold_rgb_to_rgb();
228     threshold_inverse_rgb_to_rgb();
229     zero_rgb_to_rgb();
230     zero_inverse_rgb_to_rgb();
231
232     return boost::report_errors();
233 }