• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //
2 // Copyright 2005-2007 Adobe Systems Incorporated
3 // Copyright 2018-2020 Mateusz Loskot <mateusz at loskot dot net>
4 //
5 // Distribtted under the Boost Software License, Version 1.0
6 // See accompanying file LICENSE_1_0.txt or copy at
7 // http://www.boost.org/LICENSE_1_0.txt
8 //
9 #include <boost/gil/channel.hpp>
10 #include <boost/gil/channel_algorithm.hpp>
11 #include <boost/gil/typedefs.hpp>
12 
13 #include <boost/core/lightweight_test.hpp>
14 
15 #include <cmath>
16 #include <cstdint>
17 #include <limits>
18 
19 namespace gil = boost::gil;
20 
21 // FIXME: Remove when https://github.com/boostorg/core/issues/38 happens
22 #define BOOST_GIL_TEST_IS_CLOSE(a, b, epsilon) BOOST_TEST_LT(std::fabs((a) - (b)), (epsilon))
23 
applyint_minus_value24 struct int_minus_value  { static std::int8_t apply() { return -64; } };
applyint_plus_value25 struct int_plus_value   { static std::int8_t apply() { return  64; } };
26 using fixture = gil::scoped_channel_value
27     <
28         std::uint8_t, int_minus_value, int_plus_value
29     >;
30 
test_scoped_channel_value_default_constructor()31 void test_scoped_channel_value_default_constructor()
32 {
33     fixture f;
34     std::uint8_t v = f;
35     BOOST_TEST_EQ(v, std::uint8_t{0});
36 }
37 
test_scoped_channel_value_user_defined_constructors()38 void test_scoped_channel_value_user_defined_constructors()
39 {
40     fixture f{1};
41     std::uint8_t v = f;
42     BOOST_TEST_EQ(v, std::uint8_t{1});
43 }
44 
test_scoped_channel_value_copy_constructors()45 void test_scoped_channel_value_copy_constructors()
46 {
47     fixture f1{128};
48     fixture f2{f1};
49 
50     BOOST_TEST_EQ(std::uint8_t{f1}, std::uint8_t{128});
51     BOOST_TEST_EQ(std::uint8_t{f1}, std::uint8_t{f2});
52 }
53 
test_scoped_channel_value_assignment()54 void test_scoped_channel_value_assignment()
55 {
56     fixture f;
57     f = 64;
58     std::uint8_t v = f;
59     BOOST_TEST_EQ(v, std::uint8_t{64});
60 }
61 
test_scoped_channel_value_float32_t()62 void test_scoped_channel_value_float32_t()
63 {
64     auto const epsilon = std::numeric_limits<float>::epsilon();
65     // min
66     BOOST_GIL_TEST_IS_CLOSE(gil::float_point_zero<float>::apply(), 0.0, epsilon);
67     BOOST_TEST_EQ(gil::channel_traits<gil::float32_t>::min_value(), 0.0);
68     // max
69     BOOST_GIL_TEST_IS_CLOSE(gil::float_point_one<float>::apply(), 1.0, epsilon);
70     BOOST_TEST_EQ(gil::channel_traits<gil::float32_t>::max_value(), 1.0);
71 }
72 
test_scoped_channel_value_float64_t()73 void test_scoped_channel_value_float64_t()
74 {
75     auto const epsilon = std::numeric_limits<double>::epsilon();
76     // min
77     BOOST_GIL_TEST_IS_CLOSE(gil::float_point_zero<double>::apply(), 0.0, epsilon);
78     BOOST_GIL_TEST_IS_CLOSE(gil::channel_traits<gil::float64_t>::min_value(), 0.0, epsilon);
79     // max
80     BOOST_GIL_TEST_IS_CLOSE(gil::float_point_one<double>::apply(), 1.0, epsilon);
81     BOOST_GIL_TEST_IS_CLOSE(gil::channel_traits<gil::float64_t>::max_value(), 1.0, epsilon);
82 }
83 
test_scoped_channel_value_halfs()84 void test_scoped_channel_value_halfs()
85 {
86     // Create a double channel with range [-0.5 .. 0.5]
87     struct minus_half { static double apply() { return -0.5; } };
88     struct plus_half { static double apply() { return 0.5; } };
89     using halfs = gil::scoped_channel_value<double, minus_half, plus_half>;
90 
91     auto const epsilon = std::numeric_limits<double>::epsilon();
92     BOOST_GIL_TEST_IS_CLOSE(gil::channel_traits<halfs>::min_value(), minus_half::apply(), epsilon);
93     BOOST_GIL_TEST_IS_CLOSE(gil::channel_traits<halfs>::max_value(), plus_half::apply(), epsilon);
94     // scoped channel maximum should map to the maximum
95     BOOST_GIL_TEST_IS_CLOSE(gil::channel_convert<std::uint16_t>(
96         gil::channel_traits<halfs>::max_value()), 65535, epsilon);
97 }
98 
main()99 int main()
100 {
101     test_scoped_channel_value_default_constructor();
102     test_scoped_channel_value_user_defined_constructors();
103     test_scoped_channel_value_copy_constructors();
104     test_scoped_channel_value_assignment();
105     test_scoped_channel_value_float32_t();
106     test_scoped_channel_value_float64_t();
107     test_scoped_channel_value_halfs();
108 
109     return ::boost::report_errors();
110 }
111