Imported Upstream version 1.71.0
[platform/upstream/boost.git] / libs / gil / test / core / channel / algorithm_channel_convert.cpp
1 //
2 // Copyright 2005-2007 Adobe Systems Incorporated
3 // Copyright 2018 Mateusz Loskot <mateusz at loskot dot net>
4 //
5 // Distributed 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_algorithm.hpp>
10 #include <cstdint>
11
12 #define BOOST_TEST_MODULE test_algorithm_channel_convert
13 #include "unit_test.hpp"
14 #include "test_fixture.hpp"
15
16 namespace gil = boost::gil;
17 namespace fixture = boost::gil::test::fixture;
18
19 template <typename ChannelFixtureBase>
20 struct test_convert_to
21 {
22     using channel_t = typename fixture::channel<ChannelFixtureBase>::channel_t;
23     using channel_value_t = typename fixture::channel<ChannelFixtureBase>::channel_value_t;
24
25     template <typename Channel>
26     static void from(Channel src_min_v, Channel src_max_v)
27     {
28         channel_value_t min_v = gil::channel_convert<channel_t>(src_min_v);
29         channel_value_t max_v = gil::channel_convert<channel_t>(src_max_v);
30         fixture::channel_minmax_value<channel_value_t> expect;
31         BOOST_TEST(min_v == expect.min_v_);
32         BOOST_TEST(max_v == expect.max_v_);
33     }
34 };
35
36 //--- Test gil::channel_convert from integral channels to all byte channels -------------
37 #define GIL_TEST_CHANNEL_CONVERT_FROM(source_channel_type) \
38     BOOST_FIXTURE_TEST_SUITE( \
39         channel_convert_from_##source_channel_type, \
40         fixture::channel_minmax_value<std::source_channel_type>) \
41     BOOST_AUTO_TEST_CASE_TEMPLATE(channel_value, Channel, fixture::channel_byte_types) \
42     {   test_convert_to<fixture::channel_value<Channel>>::from(min_v_, max_v_); } \
43     BOOST_AUTO_TEST_CASE_TEMPLATE(channel_reference, Channel, fixture::channel_byte_types) \
44     {   test_convert_to<fixture::channel_reference<Channel&>>::from(min_v_, max_v_); } \
45     BOOST_AUTO_TEST_CASE_TEMPLATE(channel_reference_const, Channel, fixture::channel_byte_types) \
46     {   test_convert_to<fixture::channel_reference<Channel const&>>::from(min_v_, max_v_); } \
47     BOOST_AUTO_TEST_CASE(packed_channel_reference) \
48     { \
49         using channels565_t = fixture::packed_channels565<std::uint16_t>; \
50         test_convert_to<typename channels565_t::fixture_0_5_t>::from(min_v_, max_v_); \
51         test_convert_to<typename channels565_t::fixture_5_6_t>::from(min_v_, max_v_); \
52         test_convert_to<typename channels565_t::fixture_11_5_t>::from(min_v_, max_v_); \
53     } \
54     BOOST_AUTO_TEST_CASE(packed_dynamic_channel_reference) \
55     { \
56         using channels565_t = fixture::packed_dynamic_channels565<std::uint16_t>; \
57         test_convert_to<typename channels565_t::fixture_5_t>::from(min_v_, max_v_); \
58         test_convert_to<typename channels565_t::fixture_6_t>::from(min_v_, max_v_); \
59     } \
60     BOOST_AUTO_TEST_SUITE_END()
61
62 GIL_TEST_CHANNEL_CONVERT_FROM(uint8_t)
63 GIL_TEST_CHANNEL_CONVERT_FROM(int8_t)
64 GIL_TEST_CHANNEL_CONVERT_FROM(uint16_t)
65 GIL_TEST_CHANNEL_CONVERT_FROM(int16_t)
66 GIL_TEST_CHANNEL_CONVERT_FROM(uint32_t)
67 GIL_TEST_CHANNEL_CONVERT_FROM(int32_t)
68
69 #undef GIL_TEST_CHANNEL_CONVERT_FROM
70
71 // FIXME: gil::float32_t <-> gil::float64_t seems not supported
72
73 //--- Test gil::channel_convert from gil::float32_t to all integer channels -------------
74 BOOST_FIXTURE_TEST_SUITE(channel_convert_from_float32_t,
75                          fixture::channel_minmax_value<gil::float32_t>)
76
77 BOOST_AUTO_TEST_CASE_TEMPLATE(channel_value, Channel, fixture::channel_integer_types)
78 {
79     test_convert_to<fixture::channel_value<Channel>>::from(min_v_, max_v_);
80 }
81
82 BOOST_AUTO_TEST_CASE_TEMPLATE(channel_reference, Channel, fixture::channel_integer_types)
83 {
84     test_convert_to<fixture::channel_reference<Channel&>>::from(min_v_, max_v_);
85 }
86
87 BOOST_AUTO_TEST_CASE_TEMPLATE(
88     channel_reference_const, Channel, fixture::channel_integer_types)
89 {
90     test_convert_to<fixture::channel_reference<Channel const&>>::from(min_v_, max_v_);
91 }
92
93 BOOST_AUTO_TEST_SUITE_END()
94
95 //--- Test gil::channel_convert from gil::float64_t to all integer channels -------------
96 BOOST_FIXTURE_TEST_SUITE(channel_convert_from_float64_t,
97                          fixture::channel_minmax_value<gil::float64_t>)
98
99 BOOST_AUTO_TEST_CASE_TEMPLATE(channel_value, Channel, fixture::channel_integer_types)
100 {
101     test_convert_to<fixture::channel_value<Channel>>::from(min_v_, max_v_);
102 }
103
104 BOOST_AUTO_TEST_CASE_TEMPLATE(channel_reference, Channel, fixture::channel_integer_types)
105 {
106     test_convert_to<fixture::channel_reference<Channel&>>::from(min_v_, max_v_);
107 }
108
109 BOOST_AUTO_TEST_CASE_TEMPLATE(
110     channel_reference_const, Channel, fixture::channel_integer_types)
111 {
112     test_convert_to<fixture::channel_reference<Channel const&>>::from(min_v_, max_v_);
113 }
114
115 BOOST_AUTO_TEST_SUITE_END()