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 #ifndef BOOST_GIL_TEST_TEST_FIXTURE_HPP
10 #define BOOST_GIL_TEST_TEST_FIXTURE_HPP
12 #include <boost/gil/channel.hpp>
13 #include <boost/gil/concepts.hpp>
14 #include <boost/gil/typedefs.hpp>
18 #include <type_traits>
20 namespace boost { namespace gil { namespace test { namespace fixture {
22 using channel_byte_types = std::tuple
34 using channel_integer_types = std::tuple
44 // FIXME: If float types are convertible between each other,
45 // currently they are not, then move to channel_byte_types and
46 // remove channel_integer_types as redundant.
47 using channel_float_types = std::tuple
53 using channel_bitfield_types = std::tuple
58 // TODO: Shall we test signed types for unexpected conversions, etc.?
62 template <typename ChannelValue>
63 struct channel_minmax_value
65 //static_assert(std::)
68 channel_minmax_value()
69 : min_v_(gil::channel_traits<ChannelValue>::min_value())
70 , max_v_(gil::channel_traits<ChannelValue>::max_value())
74 template <typename ChannelFixtureBase>
75 struct channel : public ChannelFixtureBase
77 using channel_t = typename ChannelFixtureBase::channel_t;
78 using channel_value_t = typename gil::channel_traits<channel_t>::value_type;
82 BOOST_TEST(this->min_v_ == gil::channel_traits<channel_t>::min_value());
83 BOOST_TEST(this->max_v_ == gil::channel_traits<channel_t>::max_value());
87 // The channel fixtures are defined for different types of channels
88 // (ie. channel values, references and subbyte references)
89 // ensure there are two members, min_v_ and max_v_ initialized
90 // with the minimum and maximum channel value.
91 // The different channel types have different ways to initialize them,
92 // thus require different fixtures provided.
94 // For basic channel types values can be initialized directly.
95 template <typename ChannelValue>
98 using channel_t = ChannelValue;
103 : min_v_(gil::channel_traits<ChannelValue>::min_value())
104 , max_v_(gil::channel_traits<ChannelValue>::max_value())
106 boost::function_requires<gil::ChannelValueConcept<ChannelValue>>();
110 // For channel references we need to have separate channel values.
111 template <typename ChannelRef>
112 struct channel_reference
113 : public channel_value<typename gil::channel_traits<ChannelRef>::value_type>
115 using parent_t = channel_value<typename gil::channel_traits<ChannelRef>::value_type>;
116 using channel_t = ChannelRef;
122 , min_v_(parent_t::min_v_)
123 , max_v_(parent_t::max_v_)
125 boost::function_requires<ChannelConcept<ChannelRef>>();
129 // For sub-byte channel references we need to store the bit buffers somewhere
130 template <typename ChannelSubbyteRef, typename ChannelMutableRef = ChannelSubbyteRef>
131 struct packed_channel_reference
133 using channel_t = ChannelSubbyteRef;
134 using integer_t = typename channel_t::integer_t;
137 integer_t min_bitbuf_;
138 integer_t max_bitbuf_;
140 packed_channel_reference() : min_v_(&min_bitbuf_), max_v_(&max_bitbuf_)
142 boost::function_requires<ChannelConcept<ChannelSubbyteRef>>();
144 ChannelMutableRef b1(&min_bitbuf_);
145 b1 = gil::channel_traits<channel_t>::min_value();
146 ChannelMutableRef b2(&max_bitbuf_);
147 b2 = gil::channel_traits<channel_t>::max_value();
151 // For sub-byte channel references we need to store the bit buffers somewhere
152 template <typename ChannelSubbyteRef, typename ChannelMutableRef = ChannelSubbyteRef>
153 struct packed_dynamic_channel_reference
155 using channel_t = ChannelSubbyteRef;
156 using integer_t = typename channel_t::integer_t;
159 integer_t min_bitbuf_;
160 integer_t max_bitbuf_;
162 packed_dynamic_channel_reference(int first_bit1 = 1, int first_bit2 = 2)
163 : min_v_(&min_bitbuf_, first_bit1)
164 , max_v_(&max_bitbuf_, first_bit2)
166 boost::function_requires<ChannelConcept<ChannelSubbyteRef>>();
168 ChannelMutableRef b1(&min_bitbuf_, 1);
169 b1 = gil::channel_traits<channel_t>::min_value();
170 ChannelMutableRef b2(&max_bitbuf_, 2);
171 b2 = gil::channel_traits<channel_t>::max_value();
175 // Concrete fixture for 16-bit pack of 5,6,5-bit channels
176 template <typename BitField>
177 struct packed_channels565
179 static_assert(sizeof(BitField) >= sizeof(std::uint16_t), "16-bit or more required");
180 using channel_0_5_t = gil::packed_channel_reference<BitField, 0, 5,true>;
181 using channel_5_6_t = gil::packed_channel_reference<BitField, 5, 6,true>;
182 using channel_11_5_t = gil::packed_channel_reference<BitField, 11, 5,true>;
184 using fixture_0_5_t = fixture::packed_channel_reference<channel_0_5_t>;
185 using fixture_5_6_t = fixture::packed_channel_reference<channel_5_6_t>;
186 using fixture_11_5_t = fixture::packed_channel_reference<channel_11_5_t>;
188 std::uint16_t data_ = 0;
189 channel_0_5_t channel1_;
190 channel_5_6_t channel2_;
191 channel_11_5_t channel3_;
193 packed_channels565() : channel1_(&data_), channel2_(&data_), channel3_(&data_)
195 channel1_ = gil::channel_traits<channel_0_5_t>::max_value();
196 channel2_ = gil::channel_traits<channel_5_6_t>::max_value();
197 channel3_ = gil::channel_traits<channel_11_5_t>::max_value();
198 BOOST_TEST(data_ == 65535);
202 // Concrete fixture for dynamically-referenced 16-bit pack of 5,6,5-bit channels
203 template <typename BitField>
204 struct packed_dynamic_channels565
206 static_assert(sizeof(BitField) >= sizeof(std::uint16_t), "16-bit or more required");
207 using channel_5_t = gil::packed_dynamic_channel_reference<BitField,5,true>;
208 using channel_6_t = gil::packed_dynamic_channel_reference<BitField,6,true>;
210 using fixture_5_t = fixture::packed_dynamic_channel_reference<channel_5_t>;
211 using fixture_6_t = fixture::packed_dynamic_channel_reference<channel_6_t>;
213 std::uint16_t data_ = 0;
214 channel_5_t channel1_;
215 channel_6_t channel2_;
216 channel_5_t channel3_;
218 packed_dynamic_channels565()
219 : channel1_(&data_, 0)
220 , channel2_(&data_, 5)
221 , channel3_(&data_, 11)
223 channel1_ = gil::channel_traits<channel_5_t>::max_value();
224 channel2_ = gil::channel_traits<channel_6_t>::max_value();
225 channel3_ = gil::channel_traits<channel_5_t>::max_value();
226 BOOST_TEST(data_ == 65535);
230 }}}} // namespace boost::gil::test::fixture