Imported Upstream version 1.72.0
[platform/upstream/boost.git] / libs / gil / test / core / image_processing / simple_kernels.cpp
1 //
2 // Copyright 2019 Olzhas Zhumabek <anonymous.from.applecity@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/numeric.hpp>
9 #include <boost/gil/image.hpp>
10 #include <boost/gil/image_view.hpp>
11 #include <boost/gil/typedefs.hpp>
12 #include <boost/core/lightweight_test.hpp>
13
14 namespace gil = boost::gil;
15
16 void test_normalized_mean_generation()
17 {
18     auto kernel = gil::generate_normalized_mean(5);
19     for (const auto& cell: kernel)
20     {
21         const auto expected_value = static_cast<float>(1 / 25.f);
22         BOOST_TEST(cell == expected_value);
23     }
24 }
25
26 void test_unnormalized_mean_generation()
27 {
28     auto kernel = gil::generate_unnormalized_mean(5);
29     for (const auto& cell: kernel)
30     {
31         BOOST_TEST(cell == 1.0f);
32     }
33 }
34
35 void test_gaussian_kernel_generation()
36 {
37     auto kernel = boost::gil::generate_gaussian_kernel(7, 0.84089642);
38     const float expected_values[7][7] =
39     {
40         {0.00000067f, 0.00002292f, 0.00019117f, 0.00038771f, 0.00019117f, 0.00002292f, 0.00000067f},
41         {0.00002292f, 0.00078633f, 0.00655965f, 0.01330373f, 0.00655965f, 0.00078633f, 0.00002292f},
42         {0.00019117f, 0.00655965f, 0.05472157f, 0.11098164f, 0.05472157f, 0.00655965f, 0.00019117f},
43         {0.00038771f, 0.01330373f, 0.11098164f, 0.25508352f, 0.11098164f, 0.01330373f, 0.00038711f},
44         {0.00019117f, 0.00655965f, 0.05472157f, 0.11098164f, 0.05472157f, 0.00655965f, 0.00019117f},
45         {0.00002292f, 0.00078633f, 0.00655965f, 0.01330373f, 0.00655965f, 0.00078633f, 0.00002292f},
46         {0.00000067f, 0.00002292f, 0.00019117f, 0.00038771f, 0.00019117f, 0.00002292f, 0.00000067f}
47     };
48
49     for (gil::gray32f_view_t::coord_t y = 0; static_cast<std::size_t>(y) < kernel.size(); ++y)
50     {
51         for (gil::gray32f_view_t::coord_t x = 0; static_cast<std::size_t>(x) < kernel.size(); ++x)
52         {
53             auto output = kernel.at(static_cast<std::size_t>(x), static_cast<std::size_t>(y));
54             auto expected = expected_values[y][x];
55             auto percent_difference = std::ceil(std::abs(expected - output) / expected);
56             BOOST_TEST(percent_difference < 5);
57         }
58     }
59 }
60
61 int main()
62 {
63     test_normalized_mean_generation();
64     test_unnormalized_mean_generation();
65     test_gaussian_kernel_generation();
66     return boost::report_errors();
67 }