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(std::false_type) {}
23 template <typename ChannelFixtureBase>
24 void test_channel_arithmetic_mutable(std::true_type)
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 = std::integral_constant
75 gil::channel_traits<typename fixture_t::channel_t>::is_mutable
77 test_channel_arithmetic_mutable<ChannelFixtureBase>(is_mutable_t{});
80 BOOST_AUTO_TEST_CASE_TEMPLATE(channel_value, Channel, fixture::channel_byte_types)
82 using fixture_t = fixture::channel_value<Channel>;
83 test_channel_arithmetic<fixture_t>();
86 BOOST_AUTO_TEST_CASE_TEMPLATE(channel_reference, Channel, fixture::channel_byte_types)
88 using fixture_t = fixture::channel_reference<Channel&>;
89 test_channel_arithmetic<fixture_t>();
92 BOOST_AUTO_TEST_CASE_TEMPLATE(
93 channel_reference_const, Channel, fixture::channel_byte_types)
95 using fixture_t = fixture::channel_reference<Channel const&>;
96 test_channel_arithmetic<fixture_t>();
99 BOOST_AUTO_TEST_CASE_TEMPLATE(
100 packed_channel_reference, BitField, fixture::channel_bitfield_types)
102 using channels565_t = fixture::packed_channels565<BitField>;
103 test_channel_arithmetic<typename channels565_t::fixture_0_5_t>();
104 test_channel_arithmetic<typename channels565_t::fixture_5_6_t >();
105 test_channel_arithmetic<typename channels565_t::fixture_11_5_t>();
108 BOOST_AUTO_TEST_CASE_TEMPLATE(
109 packed_dynamic_channel_reference, BitField, fixture::channel_bitfield_types)
111 using channels565_t = fixture::packed_dynamic_channels565<BitField>;
112 test_channel_arithmetic<typename channels565_t::fixture_5_t>();
113 test_channel_arithmetic<typename channels565_t::fixture_6_t>();