2 // Copyright 2005-2007 Adobe Systems Incorporated
3 // Copyright 2018 Mateusz Loskot <mateusz at loskot dot net>
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
9 #include <boost/gil/channel_algorithm.hpp>
10 #include <type_traits>
13 #define BOOST_TEST_MODULE test_algorithm_channel_arithmetic
14 #include "unit_test.hpp"
15 #include "test_fixture.hpp"
17 namespace gil = boost::gil;
18 namespace fixture = boost::gil::test::fixture;
20 template <typename ChannelFixtureBase>
21 void test_channel_arithmetic_mutable(boost::mpl::false_) {}
23 template <typename ChannelFixtureBase>
24 void test_channel_arithmetic_mutable(boost::mpl::true_)
26 using fixture_t = fixture::channel<ChannelFixtureBase>;
27 using channel_value_t = typename fixture_t::channel_value_t;
29 channel_value_t const v = f.min_v_;
30 channel_value_t const one = 1;
36 BOOST_TEST(v == f.min_v_);
40 BOOST_TEST(v == f.min_v_);
44 BOOST_TEST(v == f.min_v_);
46 f.min_v_ = one; // assignable to scalar
47 BOOST_TEST(f.min_v_ == one);
48 f.min_v_ = v; // and to value type
49 BOOST_TEST(f.min_v_ == v);
52 channel_value_t v1 = f.min_v_;
53 channel_value_t v2 = f.max_v_;
54 std::swap(f.min_v_, f.max_v_);
55 BOOST_TEST(f.min_v_ > f.max_v_);
56 channel_value_t v3 = f.min_v_;
57 channel_value_t v4 = f.max_v_;
62 template <typename ChannelFixtureBase>
63 void test_channel_arithmetic()
65 using fixture_t = fixture::channel<ChannelFixtureBase>;
67 BOOST_TEST(f.min_v_ * 1 == f.min_v_);
68 BOOST_TEST(f.min_v_ / 1 == f.min_v_);
69 BOOST_TEST((f.min_v_ + 1) + 1 == f.min_v_ + 2);
70 BOOST_TEST((f.max_v_ - 1) - 1 == f.max_v_ - 2);
72 using is_mutable_t = boost::mpl::bool_
74 gil::channel_traits<typename fixture_t::channel_t>::is_mutable
76 test_channel_arithmetic_mutable<ChannelFixtureBase>(is_mutable_t());
79 BOOST_AUTO_TEST_CASE_TEMPLATE(channel_value, Channel, fixture::channel_byte_types)
81 using fixture_t = fixture::channel_value<Channel>;
82 test_channel_arithmetic<fixture_t>();
85 BOOST_AUTO_TEST_CASE_TEMPLATE(channel_reference, Channel, fixture::channel_byte_types)
87 using fixture_t = fixture::channel_reference<Channel&>;
88 test_channel_arithmetic<fixture_t>();
91 BOOST_AUTO_TEST_CASE_TEMPLATE(
92 channel_reference_const, Channel, fixture::channel_byte_types)
94 using fixture_t = fixture::channel_reference<Channel const&>;
95 test_channel_arithmetic<fixture_t>();
98 BOOST_AUTO_TEST_CASE_TEMPLATE(
99 packed_channel_reference, BitField, fixture::channel_bitfield_types)
101 using channels565_t = fixture::packed_channels565<BitField>;
102 test_channel_arithmetic<typename channels565_t::fixture_0_5_t>();
103 test_channel_arithmetic<typename channels565_t::fixture_5_6_t >();
104 test_channel_arithmetic<typename channels565_t::fixture_11_5_t>();
107 BOOST_AUTO_TEST_CASE_TEMPLATE(
108 packed_dynamic_channel_reference, BitField, fixture::channel_bitfield_types)
110 using channels565_t = fixture::packed_dynamic_channels565<BitField>;
111 test_channel_arithmetic<typename channels565_t::fixture_5_t>();
112 test_channel_arithmetic<typename channels565_t::fixture_6_t>();