Imported Upstream version 1.72.0
[platform/upstream/boost.git] / libs / gil / test / core / image_processing / threshold_binary.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
27 void fill_original_gray()
28 {
29     //filling original_gray view's upper half part with gray pixels of value 50
30     //filling original_gray view's lower half part with gray pixels of value 150
31     gil::fill_pixels(gil::subimage_view(gil::view(original_gray), 0, 0, original_gray.width(),
32         original_gray.height() / 2), gil::gray8_pixel_t(50));
33     gil::fill_pixels(gil::subimage_view(gil::view(original_gray), 0, original_gray.height() / 2,
34         original_gray.width(), original_gray.height() / 2), gil::gray8_pixel_t(150));
35 }
36
37 void fill_original_rgb()
38 {
39     //filling original_rgb view's upper half part with rgb pixels of value 50, 155, 115
40     //filling original_rgb view's lower half part with rgb pixels of value 203, 9, 60
41     gil::fill_pixels(gil::subimage_view(gil::view(original_rgb), 0, 0, original_rgb.width(),
42         original_rgb.height() / 2), gil::rgb8_pixel_t(50, 155, 115));
43     gil::fill_pixels(gil::subimage_view(gil::view(original_rgb), 0, original_rgb.height() / 2,
44         original_rgb.width(), original_rgb.height() / 2), gil::rgb8_pixel_t(203, 9, 60));
45 }
46
47 void binary_gray_to_gray()
48 {
49     //expected_gray view after thresholding of the original_gray view with threshold_gray value of 100
50     //filling expected_gray view's upper half part with gray pixels of value 0
51     //filling expected_gray view's lower half part with gray pixels of value 255
52     gil::fill_pixels(gil::subimage_view(gil::view(expected_gray), 0, 0, original_gray.width(),
53         original_gray.height() / 2), gil::gray8_pixel_t(0));
54     gil::fill_pixels(gil::subimage_view(gil::view(expected_gray), 0, original_gray.height() / 2,
55         original_gray.width(), original_gray.height() / 2), gil::gray8_pixel_t(255));
56
57     gil::threshold_binary(gil::view(original_gray), gil::view(threshold_gray), 100);
58
59     //comparing threshold_gray view generated by the function with the expected_gray view
60     BOOST_TEST(gil::equal_pixels(gil::view(threshold_gray), gil::view(expected_gray)));
61 }
62
63 void binary_inverse_gray_to_gray()
64 {
65     //expected_gray view after thresholding of the original_gray view with threshold_gray value of 100
66     //filling expected_gray view's upper half part with gray pixels of value 200
67     //filling expected_gray view's lower half part with gray pixels of value 0
68     gil::fill_pixels(gil::subimage_view(gil::view(expected_gray), 0, 0, original_gray.width(),
69         original_gray.height() / 2), gil::gray8_pixel_t(200));
70     gil::fill_pixels(gil::subimage_view(gil::view(expected_gray), 0, original_gray.height() / 2,
71         original_gray.width(), original_gray.height() / 2), gil::gray8_pixel_t(0));
72
73     gil::threshold_binary
74     (
75         gil::view(original_gray),
76         gil::view(threshold_gray),
77         100,
78         200,
79         gil::threshold_direction::inverse
80     );
81
82     //comparing threshold_gray view generated by the function with the expected_gray view
83     BOOST_TEST(gil::equal_pixels(gil::view(threshold_gray), gil::view(expected_gray)));
84 }
85
86 void binary_rgb_to_rgb()
87 {
88     //expected_rgb view after thresholding of the original_rgb view with threshold value of 100
89     //filling expected_rgb view's upper half part with rgb pixels of value 0, 165, 165
90     //filling expected_rgb view's lower half part with rgb pixels of value 165, 0, 0 
91     gil::fill_pixels(gil::subimage_view(gil::view(expected_rgb), 0, 0, original_rgb.width(),
92         original_rgb.height() / 2), gil::rgb8_pixel_t(0, 165, 165));
93     gil::fill_pixels(gil::subimage_view(gil::view(expected_rgb), 0, original_rgb.height() / 2,
94         original_rgb.width(), original_rgb.height() / 2), gil::rgb8_pixel_t(165, 0, 0));
95
96     gil::threshold_binary(gil::view(original_rgb), gil::view(threshold_rgb), 100, 165);
97
98     //comparing threshold_rgb view generated by the function with the expected_rgb view
99     BOOST_TEST(gil::equal_pixels(gil::view(threshold_rgb), gil::view(expected_rgb)));
100 }
101
102 void binary_inverse_rgb_to_rgb()
103 {
104     //expected_rgb view after thresholding of the original_rgb view with threshold value of 100
105     //filling expected_rgb view's upper half part with rgb pixels of value 90, 0, 0 
106     //filling expected_rgb view's lower half part with rgb pixels of value 0, 90, 90
107     gil::fill_pixels(gil::subimage_view(gil::view(expected_rgb), 0, 0, original_rgb.width(),
108         original_rgb.height() / 2), gil::rgb8_pixel_t(90, 0, 0));
109     gil::fill_pixels(gil::subimage_view(gil::view(expected_rgb), 0, original_rgb.height() / 2,
110         original_rgb.width(), original_rgb.height() / 2), gil::rgb8_pixel_t(0, 90, 90));
111
112     gil::threshold_binary
113     (
114         gil::view(original_rgb),
115         gil::view(threshold_rgb),
116         100,
117         90,
118         gil::threshold_direction::inverse
119     );
120
121     //comparing threshold_rgb view generated by the function with the expected_rgb view
122     BOOST_TEST(gil::equal_pixels(gil::view(threshold_rgb), gil::view(expected_rgb)));
123 }
124
125
126 int main()
127 {
128     fill_original_gray();
129     fill_original_rgb();
130     
131     binary_gray_to_gray();
132     binary_inverse_gray_to_gray();
133     binary_rgb_to_rgb();
134     binary_inverse_rgb_to_rgb();
135
136     return boost::report_errors();
137 }