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_CORE_CHANNEL_TEST_FIXTURE_HPP
10 #define BOOST_GIL_TEST_CORE_CHANNEL_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 #include "test_utility_output_stream.hpp"
22 namespace boost { namespace gil { namespace test { namespace fixture {
24 using channel_byte_types = std::tuple
36 using channel_integer_types = std::tuple
46 using channel_integer_signed_types = std::tuple
53 using channel_integer_unsigned_types = std::tuple
60 // FIXME: If float types are convertible between each other,
61 // currently they are not, then move to channel_byte_types and
62 // remove channel_integer_types as redundant.
63 using channel_float_types = std::tuple
69 using channel_bitfield_types = std::tuple
74 // TODO: Shall we test signed types for unexpected conversions, etc.?
78 template <typename ChannelValue>
79 struct channel_minmax_value
81 //static_assert(std::)
84 channel_minmax_value()
85 : min_v_(gil::channel_traits<ChannelValue>::min_value())
86 , max_v_(gil::channel_traits<ChannelValue>::max_value())
90 template <typename ChannelFixtureBase>
91 struct channel : public ChannelFixtureBase
93 using channel_t = typename ChannelFixtureBase::channel_t;
94 using channel_value_t = typename gil::channel_traits<channel_t>::value_type;
98 BOOST_TEST_EQ(this->min_v_, gil::channel_traits<channel_t>::min_value());
99 BOOST_TEST_EQ(this->max_v_, gil::channel_traits<channel_t>::max_value());
103 // The channel fixtures are defined for different types of channels
104 // (ie. channel values, references and subbyte references)
105 // ensure there are two members, min_v_ and max_v_ initialized
106 // with the minimum and maximum channel value.
107 // The different channel types have different ways to initialize them,
108 // thus require different fixtures provided.
110 // For basic channel types values can be initialized directly.
111 template <typename ChannelValue>
114 using channel_t = ChannelValue;
119 : min_v_(gil::channel_traits<ChannelValue>::min_value())
120 , max_v_(gil::channel_traits<ChannelValue>::max_value())
122 boost::function_requires<gil::ChannelValueConcept<ChannelValue>>();
126 // For channel references we need to have separate channel values.
127 template <typename ChannelRef>
128 struct channel_reference
129 : public channel_value<typename gil::channel_traits<ChannelRef>::value_type>
131 using parent_t = channel_value<typename gil::channel_traits<ChannelRef>::value_type>;
132 using channel_t = ChannelRef;
138 , min_v_(parent_t::min_v_)
139 , max_v_(parent_t::max_v_)
141 boost::function_requires<ChannelConcept<ChannelRef>>();
145 // For sub-byte channel references we need to store the bit buffers somewhere
146 template <typename ChannelSubbyteRef, typename ChannelMutableRef = ChannelSubbyteRef>
147 struct packed_channel_reference
149 using channel_t = ChannelSubbyteRef;
150 using integer_t = typename channel_t::integer_t;
153 integer_t min_bitbuf_;
154 integer_t max_bitbuf_;
156 packed_channel_reference() : min_v_(&min_bitbuf_), max_v_(&max_bitbuf_)
158 boost::function_requires<ChannelConcept<ChannelSubbyteRef>>();
160 ChannelMutableRef b1(&min_bitbuf_);
161 b1 = gil::channel_traits<channel_t>::min_value();
162 ChannelMutableRef b2(&max_bitbuf_);
163 b2 = gil::channel_traits<channel_t>::max_value();
167 // For sub-byte channel references we need to store the bit buffers somewhere
168 template <typename ChannelSubbyteRef, typename ChannelMutableRef = ChannelSubbyteRef>
169 struct packed_dynamic_channel_reference
171 using channel_t = ChannelSubbyteRef;
172 using integer_t = typename channel_t::integer_t;
175 integer_t min_bitbuf_;
176 integer_t max_bitbuf_;
178 packed_dynamic_channel_reference(int first_bit1 = 1, int first_bit2 = 2)
179 : min_v_(&min_bitbuf_, first_bit1)
180 , max_v_(&max_bitbuf_, first_bit2)
182 boost::function_requires<ChannelConcept<ChannelSubbyteRef>>();
184 ChannelMutableRef b1(&min_bitbuf_, 1);
185 b1 = gil::channel_traits<channel_t>::min_value();
186 ChannelMutableRef b2(&max_bitbuf_, 2);
187 b2 = gil::channel_traits<channel_t>::max_value();
191 // Concrete fixture for 16-bit pack of 5,6,5-bit channels
192 template <typename BitField>
193 struct packed_channels565
195 static_assert(sizeof(BitField) >= sizeof(std::uint16_t), "16-bit or more required");
196 using channel_0_5_t = gil::packed_channel_reference<BitField, 0, 5,true>;
197 using channel_5_6_t = gil::packed_channel_reference<BitField, 5, 6,true>;
198 using channel_11_5_t = gil::packed_channel_reference<BitField, 11, 5,true>;
200 using fixture_0_5_t = fixture::packed_channel_reference<channel_0_5_t>;
201 using fixture_5_6_t = fixture::packed_channel_reference<channel_5_6_t>;
202 using fixture_11_5_t = fixture::packed_channel_reference<channel_11_5_t>;
204 std::uint16_t data_ = 0;
205 channel_0_5_t channel1_;
206 channel_5_6_t channel2_;
207 channel_11_5_t channel3_;
209 packed_channels565() : channel1_(&data_), channel2_(&data_), channel3_(&data_)
211 channel1_ = gil::channel_traits<channel_0_5_t>::max_value();
212 channel2_ = gil::channel_traits<channel_5_6_t>::max_value();
213 channel3_ = gil::channel_traits<channel_11_5_t>::max_value();
215 BOOST_TEST_EQ(data_, 65535);
220 // Concrete fixture for dynamically-referenced 16-bit pack of 5,6,5-bit channels
221 template <typename BitField>
222 struct packed_dynamic_channels565
224 static_assert(sizeof(BitField) >= sizeof(std::uint16_t), "16-bit or more required");
225 using channel_5_t = gil::packed_dynamic_channel_reference<BitField,5,true>;
226 using channel_6_t = gil::packed_dynamic_channel_reference<BitField,6,true>;
228 using fixture_5_t = fixture::packed_dynamic_channel_reference<channel_5_t>;
229 using fixture_6_t = fixture::packed_dynamic_channel_reference<channel_6_t>;
231 std::uint16_t data_ = 0;
232 channel_5_t channel1_;
233 channel_6_t channel2_;
234 channel_5_t channel3_;
236 packed_dynamic_channels565()
237 : channel1_(&data_, 0)
238 , channel2_(&data_, 5)
239 , channel3_(&data_, 11)
241 channel1_ = gil::channel_traits<channel_5_t>::max_value();
242 channel2_ = gil::channel_traits<channel_6_t>::max_value();
243 channel3_ = gil::channel_traits<channel_5_t>::max_value();
245 BOOST_TEST_EQ(data_, 65535);
250 }}}} // namespace boost::gil::test::fixture