Chris@16
|
1 /*
|
Chris@16
|
2 Copyright 2005-2007 Adobe Systems Incorporated
|
Chris@16
|
3
|
Chris@16
|
4 Use, modification and distribution are subject to the Boost Software License,
|
Chris@16
|
5 Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
|
Chris@16
|
6 http://www.boost.org/LICENSE_1_0.txt).
|
Chris@16
|
7
|
Chris@16
|
8 See http://stlab.adobe.com/gil for most recent version including documentation.
|
Chris@16
|
9 */
|
Chris@16
|
10
|
Chris@16
|
11 /*************************************************************************************************/
|
Chris@16
|
12
|
Chris@16
|
13 #ifndef GIL_CHANNEL_HPP
|
Chris@16
|
14 #define GIL_CHANNEL_HPP
|
Chris@16
|
15
|
Chris@16
|
16 ////////////////////////////////////////////////////////////////////////////////////////
|
Chris@16
|
17 /// \file
|
Chris@16
|
18 /// \brief Channel utilities
|
Chris@16
|
19 /// \author Lubomir Bourdev and Hailin Jin \n
|
Chris@16
|
20 /// Adobe Systems Incorporated
|
Chris@16
|
21 /// \date 2005-2007 \n Last updated on May 6, 2007
|
Chris@16
|
22 ///
|
Chris@16
|
23 /// Definitions of standard GIL channel models
|
Chris@16
|
24 ///
|
Chris@16
|
25 ////////////////////////////////////////////////////////////////////////////////////////
|
Chris@16
|
26
|
Chris@16
|
27 #include <limits>
|
Chris@16
|
28 #include <cassert>
|
Chris@16
|
29 #include <boost/cstdint.hpp>
|
Chris@16
|
30 #include "gil_config.hpp"
|
Chris@16
|
31 #include "utilities.hpp"
|
Chris@16
|
32
|
Chris@16
|
33 namespace boost { namespace gil {
|
Chris@16
|
34
|
Chris@16
|
35
|
Chris@16
|
36 ///////////////////////////////////////////
|
Chris@16
|
37 //// channel_traits
|
Chris@16
|
38 ////
|
Chris@16
|
39 //// \ingroup ChannelModel
|
Chris@16
|
40 //// \class channel_traits
|
Chris@16
|
41 //// \brief defines properties of channels, such as their range and associated types
|
Chris@16
|
42 ////
|
Chris@16
|
43 //// The channel traits must be defined for every model of ChannelConcept
|
Chris@16
|
44 //// Default traits are provided. For built-in types the default traits use
|
Chris@16
|
45 //// built-in pointer and reference and the channel range is the physical
|
Chris@16
|
46 //// range of the type. For classes, the default traits forward the associated types
|
Chris@16
|
47 //// and range to the class.
|
Chris@16
|
48 ////
|
Chris@16
|
49 ///////////////////////////////////////////
|
Chris@16
|
50
|
Chris@16
|
51 namespace detail {
|
Chris@16
|
52 template <typename T, bool is_class> struct channel_traits_impl;
|
Chris@16
|
53
|
Chris@16
|
54 // channel traits for custom class
|
Chris@16
|
55 template <typename T>
|
Chris@16
|
56 struct channel_traits_impl<T, true> {
|
Chris@16
|
57 typedef typename T::value_type value_type;
|
Chris@16
|
58 typedef typename T::reference reference;
|
Chris@16
|
59 typedef typename T::pointer pointer;
|
Chris@16
|
60 typedef typename T::const_reference const_reference;
|
Chris@16
|
61 typedef typename T::const_pointer const_pointer;
|
Chris@16
|
62 BOOST_STATIC_CONSTANT(bool, is_mutable=T::is_mutable);
|
Chris@16
|
63 static value_type min_value() { return T::min_value(); }
|
Chris@16
|
64 static value_type max_value() { return T::max_value(); }
|
Chris@16
|
65 };
|
Chris@16
|
66
|
Chris@16
|
67 // channel traits implementation for built-in integral or floating point channel type
|
Chris@16
|
68 template <typename T>
|
Chris@16
|
69 struct channel_traits_impl<T, false> {
|
Chris@16
|
70 typedef T value_type;
|
Chris@16
|
71 typedef T& reference;
|
Chris@16
|
72 typedef T* pointer;
|
Chris@16
|
73 typedef const T& const_reference;
|
Chris@16
|
74 typedef T const* const_pointer;
|
Chris@16
|
75 BOOST_STATIC_CONSTANT(bool, is_mutable=true);
|
Chris@16
|
76 static value_type min_value() { return (std::numeric_limits<T>::min)(); }
|
Chris@16
|
77 static value_type max_value() { return (std::numeric_limits<T>::max)(); }
|
Chris@16
|
78 };
|
Chris@16
|
79
|
Chris@16
|
80 // channel traits implementation for constant built-in scalar or floating point type
|
Chris@16
|
81 template <typename T>
|
Chris@16
|
82 struct channel_traits_impl<const T, false> : public channel_traits_impl<T, false> {
|
Chris@16
|
83 typedef const T& reference;
|
Chris@16
|
84 typedef const T* pointer;
|
Chris@16
|
85 BOOST_STATIC_CONSTANT(bool, is_mutable=false);
|
Chris@16
|
86 };
|
Chris@16
|
87 }
|
Chris@16
|
88
|
Chris@16
|
89 /**
|
Chris@16
|
90 \ingroup ChannelModel
|
Chris@16
|
91 \brief Traits for channels. Contains the following members:
|
Chris@16
|
92 \code
|
Chris@16
|
93 template <typename Channel>
|
Chris@16
|
94 struct channel_traits {
|
Chris@16
|
95 typedef ... value_type;
|
Chris@16
|
96 typedef ... reference;
|
Chris@16
|
97 typedef ... pointer;
|
Chris@16
|
98 typedef ... const_reference;
|
Chris@16
|
99 typedef ... const_pointer;
|
Chris@16
|
100
|
Chris@16
|
101 static const bool is_mutable;
|
Chris@16
|
102 static value_type min_value();
|
Chris@16
|
103 static value_type max_value();
|
Chris@16
|
104 };
|
Chris@16
|
105 \endcode
|
Chris@16
|
106 */
|
Chris@16
|
107 template <typename T>
|
Chris@16
|
108 struct channel_traits : public detail::channel_traits_impl<T, is_class<T>::value> {};
|
Chris@16
|
109
|
Chris@16
|
110 // Channel traits for C++ reference type - remove the reference
|
Chris@16
|
111 template <typename T> struct channel_traits< T&> : public channel_traits<T> {};
|
Chris@16
|
112
|
Chris@16
|
113 // Channel traits for constant C++ reference type
|
Chris@16
|
114 template <typename T> struct channel_traits<const T&> : public channel_traits<T> {
|
Chris@16
|
115 typedef typename channel_traits<T>::const_reference reference;
|
Chris@16
|
116 typedef typename channel_traits<T>::const_pointer pointer;
|
Chris@16
|
117 BOOST_STATIC_CONSTANT(bool, is_mutable=false);
|
Chris@16
|
118 };
|
Chris@16
|
119
|
Chris@16
|
120 ///////////////////////////////////////////
|
Chris@16
|
121 ////
|
Chris@16
|
122 //// scoped_channel_value
|
Chris@16
|
123 ////
|
Chris@16
|
124 ///////////////////////////////////////////
|
Chris@16
|
125
|
Chris@16
|
126 /**
|
Chris@16
|
127 \defgroup ScopedChannelValue scoped_channel_value
|
Chris@16
|
128 \ingroup ChannelModel
|
Chris@16
|
129 \brief A channel adaptor that modifies the range of the source channel. Models: ChannelValueConcept
|
Chris@16
|
130
|
Chris@16
|
131 Example:
|
Chris@16
|
132 \code
|
Chris@16
|
133 // Create a double channel with range [-0.5 .. 0.5]
|
Chris@16
|
134 struct double_minus_half { static double apply() { return -0.5; } };
|
Chris@16
|
135 struct double_plus_half { static double apply() { return 0.5; } };
|
Chris@16
|
136 typedef scoped_channel_value<double, double_minus_half, double_plus_half> bits64custom_t;
|
Chris@16
|
137
|
Chris@16
|
138 // channel_convert its maximum should map to the maximum
|
Chris@16
|
139 bits64custom_t x = channel_traits<bits64custom_t>::max_value();
|
Chris@16
|
140 assert(x == 0.5);
|
Chris@16
|
141 bits16 y = channel_convert<bits16>(x);
|
Chris@16
|
142 assert(y == 65535);
|
Chris@16
|
143 \endcode
|
Chris@16
|
144 */
|
Chris@16
|
145
|
Chris@16
|
146 /// \ingroup ScopedChannelValue
|
Chris@16
|
147 /// \brief A channel adaptor that modifies the range of the source channel. Models: ChannelValueConcept
|
Chris@16
|
148 template <typename BaseChannelValue, // base channel (models ChannelValueConcept)
|
Chris@16
|
149 typename MinVal, typename MaxVal> // classes with a static apply() function returning the minimum/maximum channel values
|
Chris@16
|
150 struct scoped_channel_value {
|
Chris@16
|
151 typedef scoped_channel_value value_type;
|
Chris@16
|
152 typedef value_type& reference;
|
Chris@16
|
153 typedef value_type* pointer;
|
Chris@16
|
154 typedef const value_type& const_reference;
|
Chris@16
|
155 typedef const value_type* const_pointer;
|
Chris@16
|
156 BOOST_STATIC_CONSTANT(bool, is_mutable=channel_traits<BaseChannelValue>::is_mutable);
|
Chris@16
|
157
|
Chris@16
|
158 typedef BaseChannelValue base_channel_t;
|
Chris@16
|
159
|
Chris@16
|
160 static value_type min_value() { return MinVal::apply(); }
|
Chris@16
|
161 static value_type max_value() { return MaxVal::apply(); }
|
Chris@16
|
162
|
Chris@16
|
163 scoped_channel_value() {}
|
Chris@16
|
164 scoped_channel_value(const scoped_channel_value& c) : _value(c._value) {}
|
Chris@16
|
165 scoped_channel_value(BaseChannelValue val) : _value(val) {}
|
Chris@16
|
166
|
Chris@16
|
167 scoped_channel_value& operator++() { ++_value; return *this; }
|
Chris@16
|
168 scoped_channel_value& operator--() { --_value; return *this; }
|
Chris@16
|
169
|
Chris@16
|
170 scoped_channel_value operator++(int) { scoped_channel_value tmp=*this; this->operator++(); return tmp; }
|
Chris@16
|
171 scoped_channel_value operator--(int) { scoped_channel_value tmp=*this; this->operator--(); return tmp; }
|
Chris@16
|
172
|
Chris@16
|
173 template <typename Scalar2> scoped_channel_value& operator+=(Scalar2 v) { _value+=v; return *this; }
|
Chris@16
|
174 template <typename Scalar2> scoped_channel_value& operator-=(Scalar2 v) { _value-=v; return *this; }
|
Chris@16
|
175 template <typename Scalar2> scoped_channel_value& operator*=(Scalar2 v) { _value*=v; return *this; }
|
Chris@16
|
176 template <typename Scalar2> scoped_channel_value& operator/=(Scalar2 v) { _value/=v; return *this; }
|
Chris@16
|
177
|
Chris@16
|
178 scoped_channel_value& operator=(BaseChannelValue v) { _value=v; return *this; }
|
Chris@16
|
179 operator BaseChannelValue() const { return _value; }
|
Chris@16
|
180 private:
|
Chris@16
|
181 BaseChannelValue _value;
|
Chris@16
|
182 };
|
Chris@16
|
183
|
Chris@16
|
184 struct float_zero { static float apply() { return 0.0f; } };
|
Chris@16
|
185 struct float_one { static float apply() { return 1.0f; } };
|
Chris@16
|
186
|
Chris@16
|
187
|
Chris@16
|
188 ///////////////////////////////////////////
|
Chris@16
|
189 ////
|
Chris@16
|
190 //// Support for sub-byte channels. These are integral channels whose value is contained in a range of bits inside an integral type
|
Chris@16
|
191 ////
|
Chris@16
|
192 ///////////////////////////////////////////
|
Chris@16
|
193
|
Chris@16
|
194 // It is necessary for packed channels to have their own value type. They cannot simply use an integral large enough to store the data. Here is why:
|
Chris@16
|
195 // - Any operation that requires returning the result by value will otherwise return the built-in integral type, which will have incorrect range
|
Chris@16
|
196 // That means that after getting the value of the channel we cannot properly do channel_convert, channel_invert, etc.
|
Chris@16
|
197 // - Two channels are declared compatible if they have the same value type. That means that a packed channel is incorrectly declared compatible with an integral type
|
Chris@16
|
198 namespace detail {
|
Chris@16
|
199 // returns the smallest fast unsigned integral type that has at least NumBits bits
|
Chris@16
|
200 template <int NumBits>
|
Chris@16
|
201 struct min_fast_uint : public mpl::if_c< (NumBits<=8),
|
Chris@16
|
202 uint_least8_t,
|
Chris@16
|
203 typename mpl::if_c< (NumBits<=16),
|
Chris@16
|
204 uint_least16_t,
|
Chris@16
|
205 typename mpl::if_c< (NumBits<=32),
|
Chris@16
|
206 uint_least32_t,
|
Chris@16
|
207 uintmax_t
|
Chris@16
|
208 >::type
|
Chris@16
|
209 >::type
|
Chris@16
|
210 > {};
|
Chris@16
|
211
|
Chris@16
|
212 template <int NumBits>
|
Chris@16
|
213 struct num_value_fn : public mpl::if_c< ( NumBits < 32 )
|
Chris@16
|
214 , uint32_t
|
Chris@16
|
215 , uint64_t
|
Chris@16
|
216 > {};
|
Chris@16
|
217
|
Chris@16
|
218 template <int NumBits>
|
Chris@16
|
219 struct max_value_fn : public mpl::if_c< ( NumBits <= 32 )
|
Chris@16
|
220 , uint32_t
|
Chris@16
|
221 , uint64_t
|
Chris@16
|
222 > {};
|
Chris@16
|
223 }
|
Chris@16
|
224
|
Chris@16
|
225 /**
|
Chris@16
|
226 \defgroup PackedChannelValueModel packed_channel_value
|
Chris@16
|
227 \ingroup ChannelModel
|
Chris@16
|
228 \brief Represents the value of an unsigned integral channel operating over a bit range. Models: ChannelValueConcept
|
Chris@16
|
229 Example:
|
Chris@16
|
230 \code
|
Chris@16
|
231 // A 4-bit unsigned integral channel.
|
Chris@16
|
232 typedef packed_channel_value<4> bits4;
|
Chris@16
|
233
|
Chris@16
|
234 assert(channel_traits<bits4>::min_value()==0);
|
Chris@16
|
235 assert(channel_traits<bits4>::max_value()==15);
|
Chris@16
|
236 assert(sizeof(bits4)==1);
|
Chris@16
|
237 BOOST_STATIC_ASSERT((boost::is_integral<bits4>::value));
|
Chris@16
|
238 \endcode
|
Chris@16
|
239 */
|
Chris@16
|
240
|
Chris@16
|
241 /// \ingroup PackedChannelValueModel
|
Chris@16
|
242 /// \brief The value of a subbyte channel. Models: ChannelValueConcept
|
Chris@16
|
243 template <int NumBits>
|
Chris@16
|
244 class packed_channel_value {
|
Chris@16
|
245
|
Chris@16
|
246 typedef typename detail::num_value_fn< NumBits >::type num_value_t;
|
Chris@16
|
247 static const num_value_t num_values = static_cast< num_value_t >( 1 ) << NumBits ;
|
Chris@16
|
248
|
Chris@16
|
249 public:
|
Chris@16
|
250 typedef typename detail::min_fast_uint<NumBits>::type integer_t;
|
Chris@16
|
251
|
Chris@16
|
252
|
Chris@16
|
253 typedef packed_channel_value value_type;
|
Chris@16
|
254 typedef value_type& reference;
|
Chris@16
|
255 typedef const value_type& const_reference;
|
Chris@16
|
256 typedef value_type* pointer;
|
Chris@16
|
257 typedef const value_type* const_pointer;
|
Chris@16
|
258
|
Chris@16
|
259 static value_type min_value() { return value_type(0); }
|
Chris@16
|
260 static value_type max_value() { return value_type(num_values-1); }
|
Chris@16
|
261 BOOST_STATIC_CONSTANT(bool, is_mutable=true);
|
Chris@16
|
262
|
Chris@16
|
263 packed_channel_value() {}
|
Chris@16
|
264 packed_channel_value(integer_t v) { _value = static_cast< integer_t >( v % num_values ); }
|
Chris@16
|
265 packed_channel_value(const packed_channel_value& v) : _value(v._value) {}
|
Chris@16
|
266 template <typename Scalar> packed_channel_value(Scalar v) { _value = static_cast< integer_t >( v ) % num_values; }
|
Chris@16
|
267
|
Chris@16
|
268 static unsigned int num_bits() { return NumBits; }
|
Chris@16
|
269
|
Chris@16
|
270
|
Chris@16
|
271 operator integer_t() const { return _value; }
|
Chris@16
|
272 private:
|
Chris@16
|
273 integer_t _value;
|
Chris@16
|
274 };
|
Chris@16
|
275
|
Chris@16
|
276 namespace detail {
|
Chris@16
|
277
|
Chris@16
|
278 template <std::size_t K>
|
Chris@16
|
279 struct static_copy_bytes {
|
Chris@16
|
280 void operator()(const unsigned char* from, unsigned char* to) const {
|
Chris@16
|
281 *to = *from;
|
Chris@16
|
282 static_copy_bytes<K-1>()(++from,++to);
|
Chris@16
|
283 }
|
Chris@16
|
284 };
|
Chris@16
|
285
|
Chris@16
|
286 template <>
|
Chris@16
|
287 struct static_copy_bytes<0> {
|
Chris@16
|
288 void operator()(const unsigned char* , unsigned char*) const {}
|
Chris@16
|
289 };
|
Chris@16
|
290
|
Chris@16
|
291 template <typename Derived, typename BitField, int NumBits, bool Mutable>
|
Chris@16
|
292 class packed_channel_reference_base {
|
Chris@16
|
293 protected:
|
Chris@16
|
294 typedef typename mpl::if_c<Mutable,void*,const void*>::type data_ptr_t;
|
Chris@16
|
295 public:
|
Chris@16
|
296 data_ptr_t _data_ptr; // void* pointer to the first byte of the bit range
|
Chris@16
|
297
|
Chris@16
|
298 typedef packed_channel_value<NumBits> value_type;
|
Chris@16
|
299 typedef const Derived reference;
|
Chris@16
|
300 typedef value_type* pointer;
|
Chris@16
|
301 typedef const value_type* const_pointer;
|
Chris@16
|
302 BOOST_STATIC_CONSTANT(int, num_bits=NumBits);
|
Chris@16
|
303 BOOST_STATIC_CONSTANT(bool, is_mutable=Mutable);
|
Chris@16
|
304
|
Chris@16
|
305 static value_type min_value() { return channel_traits<value_type>::min_value(); }
|
Chris@16
|
306 static value_type max_value() { return channel_traits<value_type>::max_value(); }
|
Chris@16
|
307
|
Chris@16
|
308 typedef BitField bitfield_t;
|
Chris@16
|
309 typedef typename value_type::integer_t integer_t;
|
Chris@16
|
310
|
Chris@16
|
311 packed_channel_reference_base(data_ptr_t data_ptr) : _data_ptr(data_ptr) {}
|
Chris@16
|
312 packed_channel_reference_base(const packed_channel_reference_base& ref) : _data_ptr(ref._data_ptr) {}
|
Chris@16
|
313 const Derived& operator=(integer_t v) const { set(v); return derived(); }
|
Chris@16
|
314
|
Chris@16
|
315 const Derived& operator++() const { set(get()+1); return derived(); }
|
Chris@16
|
316 const Derived& operator--() const { set(get()-1); return derived(); }
|
Chris@16
|
317
|
Chris@16
|
318 Derived operator++(int) const { Derived tmp=derived(); this->operator++(); return tmp; }
|
Chris@16
|
319 Derived operator--(int) const { Derived tmp=derived(); this->operator--(); return tmp; }
|
Chris@16
|
320
|
Chris@16
|
321 template <typename Scalar2> const Derived& operator+=(Scalar2 v) const { set(get()+v); return derived(); }
|
Chris@16
|
322 template <typename Scalar2> const Derived& operator-=(Scalar2 v) const { set(get()-v); return derived(); }
|
Chris@16
|
323 template <typename Scalar2> const Derived& operator*=(Scalar2 v) const { set(get()*v); return derived(); }
|
Chris@16
|
324 template <typename Scalar2> const Derived& operator/=(Scalar2 v) const { set(get()/v); return derived(); }
|
Chris@16
|
325
|
Chris@16
|
326 operator integer_t() const { return get(); }
|
Chris@16
|
327 data_ptr_t operator &() const {return _data_ptr;}
|
Chris@16
|
328 protected:
|
Chris@16
|
329
|
Chris@16
|
330 typedef typename detail::num_value_fn< NumBits >::type num_value_t;
|
Chris@16
|
331 typedef typename detail::max_value_fn< NumBits >::type max_value_t;
|
Chris@16
|
332
|
Chris@16
|
333 static const num_value_t num_values = static_cast< num_value_t >( 1 ) << NumBits ;
|
Chris@16
|
334 static const max_value_t max_val = static_cast< max_value_t >( num_values - 1 );
|
Chris@16
|
335
|
Chris@16
|
336 #ifdef GIL_NONWORD_POINTER_ALIGNMENT_SUPPORTED
|
Chris@16
|
337 const bitfield_t& get_data() const { return *static_cast<const bitfield_t*>(_data_ptr); }
|
Chris@16
|
338 void set_data(const bitfield_t& val) const { *static_cast< bitfield_t*>(_data_ptr) = val; }
|
Chris@16
|
339 #else
|
Chris@16
|
340 bitfield_t get_data() const {
|
Chris@16
|
341 bitfield_t ret;
|
Chris@16
|
342 static_copy_bytes<sizeof(bitfield_t) >()(gil_reinterpret_cast_c<const unsigned char*>(_data_ptr),gil_reinterpret_cast<unsigned char*>(&ret));
|
Chris@16
|
343 return ret;
|
Chris@16
|
344 }
|
Chris@16
|
345 void set_data(const bitfield_t& val) const {
|
Chris@16
|
346 static_copy_bytes<sizeof(bitfield_t) >()(gil_reinterpret_cast_c<const unsigned char*>(&val),gil_reinterpret_cast<unsigned char*>(_data_ptr));
|
Chris@16
|
347 }
|
Chris@16
|
348 #endif
|
Chris@16
|
349
|
Chris@16
|
350 private:
|
Chris@16
|
351 void set(integer_t value) const { // can this be done faster??
|
Chris@16
|
352 const integer_t num_values = max_val+1;
|
Chris@16
|
353 this->derived().set_unsafe(((value % num_values) + num_values) % num_values);
|
Chris@16
|
354 }
|
Chris@16
|
355 integer_t get() const { return derived().get(); }
|
Chris@16
|
356 const Derived& derived() const { return static_cast<const Derived&>(*this); }
|
Chris@16
|
357 };
|
Chris@16
|
358 } // namespace detail
|
Chris@16
|
359
|
Chris@16
|
360 /**
|
Chris@16
|
361 \defgroup PackedChannelReferenceModel packed_channel_reference
|
Chris@16
|
362 \ingroup ChannelModel
|
Chris@16
|
363 \brief Represents a reference proxy to a channel operating over a bit range whose offset is fixed at compile time. Models ChannelConcept
|
Chris@16
|
364 Example:
|
Chris@16
|
365 \code
|
Chris@16
|
366 // Reference to a 2-bit channel starting at bit 1 (i.e. the second bit)
|
Chris@16
|
367 typedef const packed_channel_reference<uint16_t,1,2,true> bits2_1_ref_t;
|
Chris@16
|
368
|
Chris@16
|
369 uint16_t data=0;
|
Chris@16
|
370 bits2_1_ref_t channel_ref(&data);
|
Chris@16
|
371 channel_ref = channel_traits<bits2_1_ref_t>::max_value(); // == 3
|
Chris@16
|
372 assert(data == 6); // == 3<<1 == 6
|
Chris@16
|
373 \endcode
|
Chris@16
|
374 */
|
Chris@16
|
375
|
Chris@16
|
376 template <typename BitField, // A type that holds the bits of the pixel from which the channel is referenced. Typically an integral type, like boost::uint16_t
|
Chris@16
|
377 int FirstBit, int NumBits,// Defines the sequence of bits in the data value that contain the channel
|
Chris@16
|
378 bool Mutable> // true if the reference is mutable
|
Chris@16
|
379 class packed_channel_reference;
|
Chris@16
|
380
|
Chris@16
|
381 template <typename BitField, // A type that holds the bits of the pixel from which the channel is referenced. Typically an integral type, like boost::uint16_t
|
Chris@16
|
382 int NumBits, // Defines the sequence of bits in the data value that contain the channel
|
Chris@16
|
383 bool Mutable> // true if the reference is mutable
|
Chris@16
|
384 class packed_dynamic_channel_reference;
|
Chris@16
|
385
|
Chris@16
|
386 /// \ingroup PackedChannelReferenceModel
|
Chris@16
|
387 /// \brief A constant subbyte channel reference whose bit offset is fixed at compile time. Models ChannelConcept
|
Chris@16
|
388 template <typename BitField, int FirstBit, int NumBits>
|
Chris@16
|
389 class packed_channel_reference<BitField,FirstBit,NumBits,false>
|
Chris@16
|
390 : public detail::packed_channel_reference_base<packed_channel_reference<BitField,FirstBit,NumBits,false>,BitField,NumBits,false> {
|
Chris@16
|
391 typedef detail::packed_channel_reference_base<packed_channel_reference<BitField,FirstBit,NumBits,false>,BitField,NumBits,false> parent_t;
|
Chris@16
|
392 friend class packed_channel_reference<BitField,FirstBit,NumBits,true>;
|
Chris@16
|
393
|
Chris@16
|
394 static const BitField channel_mask = static_cast< BitField >( parent_t::max_val ) << FirstBit;
|
Chris@16
|
395
|
Chris@16
|
396 void operator=(const packed_channel_reference&);
|
Chris@16
|
397 public:
|
Chris@16
|
398 typedef const packed_channel_reference<BitField,FirstBit,NumBits,false> const_reference;
|
Chris@16
|
399 typedef const packed_channel_reference<BitField,FirstBit,NumBits,true> mutable_reference;
|
Chris@16
|
400 typedef typename parent_t::integer_t integer_t;
|
Chris@16
|
401
|
Chris@16
|
402 explicit packed_channel_reference(const void* data_ptr) : parent_t(data_ptr) {}
|
Chris@16
|
403 packed_channel_reference(const packed_channel_reference& ref) : parent_t(ref._data_ptr) {}
|
Chris@16
|
404 packed_channel_reference(const mutable_reference& ref) : parent_t(ref._data_ptr) {}
|
Chris@16
|
405
|
Chris@16
|
406 unsigned first_bit() const { return FirstBit; }
|
Chris@16
|
407
|
Chris@16
|
408 integer_t get() const { return integer_t((this->get_data()&channel_mask) >> FirstBit); }
|
Chris@16
|
409 };
|
Chris@16
|
410
|
Chris@16
|
411 /// \ingroup PackedChannelReferenceModel
|
Chris@16
|
412 /// \brief A mutable subbyte channel reference whose bit offset is fixed at compile time. Models ChannelConcept
|
Chris@16
|
413 template <typename BitField, int FirstBit, int NumBits>
|
Chris@16
|
414 class packed_channel_reference<BitField,FirstBit,NumBits,true>
|
Chris@16
|
415 : public detail::packed_channel_reference_base<packed_channel_reference<BitField,FirstBit,NumBits,true>,BitField,NumBits,true> {
|
Chris@16
|
416 typedef detail::packed_channel_reference_base<packed_channel_reference<BitField,FirstBit,NumBits,true>,BitField,NumBits,true> parent_t;
|
Chris@16
|
417 friend class packed_channel_reference<BitField,FirstBit,NumBits,false>;
|
Chris@16
|
418
|
Chris@16
|
419 static const BitField channel_mask = static_cast< BitField >( parent_t::max_val ) << FirstBit;
|
Chris@16
|
420
|
Chris@16
|
421 public:
|
Chris@16
|
422 typedef const packed_channel_reference<BitField,FirstBit,NumBits,false> const_reference;
|
Chris@16
|
423 typedef const packed_channel_reference<BitField,FirstBit,NumBits,true> mutable_reference;
|
Chris@16
|
424 typedef typename parent_t::integer_t integer_t;
|
Chris@16
|
425
|
Chris@16
|
426 explicit packed_channel_reference(void* data_ptr) : parent_t(data_ptr) {}
|
Chris@16
|
427 packed_channel_reference(const packed_channel_reference& ref) : parent_t(ref._data_ptr) {}
|
Chris@16
|
428
|
Chris@16
|
429 const packed_channel_reference& operator=(integer_t value) const { assert(value<=parent_t::max_val); set_unsafe(value); return *this; }
|
Chris@16
|
430 const packed_channel_reference& operator=(const mutable_reference& ref) const { set_from_reference(ref.get_data()); return *this; }
|
Chris@16
|
431 const packed_channel_reference& operator=(const const_reference& ref) const { set_from_reference(ref.get_data()); return *this; }
|
Chris@16
|
432
|
Chris@16
|
433 template <bool Mutable1>
|
Chris@16
|
434 const packed_channel_reference& operator=(const packed_dynamic_channel_reference<BitField,NumBits,Mutable1>& ref) const { set_unsafe(ref.get()); return *this; }
|
Chris@16
|
435
|
Chris@16
|
436 unsigned first_bit() const { return FirstBit; }
|
Chris@16
|
437
|
Chris@16
|
438 integer_t get() const { return integer_t((this->get_data()&channel_mask) >> FirstBit); }
|
Chris@16
|
439 void set_unsafe(integer_t value) const { this->set_data((this->get_data() & ~channel_mask) | (( static_cast< BitField >( value )<<FirstBit))); }
|
Chris@16
|
440 private:
|
Chris@16
|
441 void set_from_reference(const BitField& other_bits) const { this->set_data((this->get_data() & ~channel_mask) | (other_bits & channel_mask)); }
|
Chris@16
|
442 };
|
Chris@16
|
443
|
Chris@16
|
444 } } // namespace boost::gil
|
Chris@16
|
445
|
Chris@16
|
446 namespace std {
|
Chris@16
|
447 // We are forced to define swap inside std namespace because on some platforms (Visual Studio 8) STL calls swap qualified.
|
Chris@16
|
448 // swap with 'left bias':
|
Chris@16
|
449 // - swap between proxy and anything
|
Chris@16
|
450 // - swap between value type and proxy
|
Chris@16
|
451 // - swap between proxy and proxy
|
Chris@16
|
452
|
Chris@16
|
453 /// \ingroup PackedChannelReferenceModel
|
Chris@16
|
454 /// \brief swap for packed_channel_reference
|
Chris@16
|
455 template <typename BF, int FB, int NB, bool M, typename R> inline
|
Chris@16
|
456 void swap(const boost::gil::packed_channel_reference<BF,FB,NB,M> x, R& y) {
|
Chris@16
|
457 boost::gil::swap_proxy<typename boost::gil::packed_channel_reference<BF,FB,NB,M>::value_type>(x,y);
|
Chris@16
|
458 }
|
Chris@16
|
459
|
Chris@16
|
460
|
Chris@16
|
461 /// \ingroup PackedChannelReferenceModel
|
Chris@16
|
462 /// \brief swap for packed_channel_reference
|
Chris@16
|
463 template <typename BF, int FB, int NB, bool M> inline
|
Chris@16
|
464 void swap(typename boost::gil::packed_channel_reference<BF,FB,NB,M>::value_type& x, const boost::gil::packed_channel_reference<BF,FB,NB,M> y) {
|
Chris@16
|
465 boost::gil::swap_proxy<typename boost::gil::packed_channel_reference<BF,FB,NB,M>::value_type>(x,y);
|
Chris@16
|
466 }
|
Chris@16
|
467
|
Chris@16
|
468
|
Chris@16
|
469 /// \ingroup PackedChannelReferenceModel
|
Chris@16
|
470 /// \brief swap for packed_channel_reference
|
Chris@16
|
471 template <typename BF, int FB, int NB, bool M> inline
|
Chris@16
|
472 void swap(const boost::gil::packed_channel_reference<BF,FB,NB,M> x, const boost::gil::packed_channel_reference<BF,FB,NB,M> y) {
|
Chris@16
|
473 boost::gil::swap_proxy<typename boost::gil::packed_channel_reference<BF,FB,NB,M>::value_type>(x,y);
|
Chris@16
|
474 }
|
Chris@16
|
475 } // namespace std
|
Chris@16
|
476
|
Chris@16
|
477 namespace boost { namespace gil {
|
Chris@16
|
478
|
Chris@16
|
479 /**
|
Chris@16
|
480 \defgroup PackedChannelDynamicReferenceModel packed_dynamic_channel_reference
|
Chris@16
|
481 \ingroup ChannelModel
|
Chris@16
|
482 \brief Represents a reference proxy to a channel operating over a bit range whose offset is specified at run time. Models ChannelConcept
|
Chris@16
|
483
|
Chris@16
|
484 Example:
|
Chris@16
|
485 \code
|
Chris@16
|
486 // Reference to a 2-bit channel whose offset is specified at construction time
|
Chris@16
|
487 typedef const packed_dynamic_channel_reference<uint8_t,2,true> bits2_dynamic_ref_t;
|
Chris@16
|
488
|
Chris@16
|
489 uint16_t data=0;
|
Chris@16
|
490 bits2_dynamic_ref_t channel_ref(&data,1);
|
Chris@16
|
491 channel_ref = channel_traits<bits2_dynamic_ref_t>::max_value(); // == 3
|
Chris@16
|
492 assert(data == 6); // == (3<<1) == 6
|
Chris@16
|
493 \endcode
|
Chris@16
|
494 */
|
Chris@16
|
495
|
Chris@16
|
496 /// \brief Models a constant subbyte channel reference whose bit offset is a runtime parameter. Models ChannelConcept
|
Chris@16
|
497 /// Same as packed_channel_reference, except that the offset is a runtime parameter
|
Chris@16
|
498 /// \ingroup PackedChannelDynamicReferenceModel
|
Chris@16
|
499 template <typename BitField, int NumBits>
|
Chris@16
|
500 class packed_dynamic_channel_reference<BitField,NumBits,false>
|
Chris@16
|
501 : public detail::packed_channel_reference_base<packed_dynamic_channel_reference<BitField,NumBits,false>,BitField,NumBits,false> {
|
Chris@16
|
502 typedef detail::packed_channel_reference_base<packed_dynamic_channel_reference<BitField,NumBits,false>,BitField,NumBits,false> parent_t;
|
Chris@16
|
503 friend class packed_dynamic_channel_reference<BitField,NumBits,true>;
|
Chris@16
|
504
|
Chris@16
|
505 unsigned _first_bit; // 0..7
|
Chris@16
|
506
|
Chris@16
|
507 void operator=(const packed_dynamic_channel_reference&);
|
Chris@16
|
508 public:
|
Chris@16
|
509 typedef const packed_dynamic_channel_reference<BitField,NumBits,false> const_reference;
|
Chris@16
|
510 typedef const packed_dynamic_channel_reference<BitField,NumBits,true> mutable_reference;
|
Chris@16
|
511 typedef typename parent_t::integer_t integer_t;
|
Chris@16
|
512
|
Chris@16
|
513 packed_dynamic_channel_reference(const void* data_ptr, unsigned first_bit) : parent_t(data_ptr), _first_bit(first_bit) {}
|
Chris@16
|
514 packed_dynamic_channel_reference(const const_reference& ref) : parent_t(ref._data_ptr), _first_bit(ref._first_bit) {}
|
Chris@16
|
515 packed_dynamic_channel_reference(const mutable_reference& ref) : parent_t(ref._data_ptr), _first_bit(ref._first_bit) {}
|
Chris@16
|
516
|
Chris@16
|
517 unsigned first_bit() const { return _first_bit; }
|
Chris@16
|
518
|
Chris@16
|
519 integer_t get() const {
|
Chris@16
|
520 const BitField channel_mask = static_cast< integer_t >( parent_t::max_val ) <<_first_bit;
|
Chris@16
|
521 return static_cast< integer_t >(( this->get_data()&channel_mask ) >> _first_bit );
|
Chris@16
|
522 }
|
Chris@16
|
523 };
|
Chris@16
|
524
|
Chris@16
|
525 /// \brief Models a mutable subbyte channel reference whose bit offset is a runtime parameter. Models ChannelConcept
|
Chris@16
|
526 /// Same as packed_channel_reference, except that the offset is a runtime parameter
|
Chris@16
|
527 /// \ingroup PackedChannelDynamicReferenceModel
|
Chris@16
|
528 template <typename BitField, int NumBits>
|
Chris@16
|
529 class packed_dynamic_channel_reference<BitField,NumBits,true>
|
Chris@16
|
530 : public detail::packed_channel_reference_base<packed_dynamic_channel_reference<BitField,NumBits,true>,BitField,NumBits,true> {
|
Chris@16
|
531 typedef detail::packed_channel_reference_base<packed_dynamic_channel_reference<BitField,NumBits,true>,BitField,NumBits,true> parent_t;
|
Chris@16
|
532 friend class packed_dynamic_channel_reference<BitField,NumBits,false>;
|
Chris@16
|
533
|
Chris@16
|
534 unsigned _first_bit;
|
Chris@16
|
535
|
Chris@16
|
536 public:
|
Chris@16
|
537 typedef const packed_dynamic_channel_reference<BitField,NumBits,false> const_reference;
|
Chris@16
|
538 typedef const packed_dynamic_channel_reference<BitField,NumBits,true> mutable_reference;
|
Chris@16
|
539 typedef typename parent_t::integer_t integer_t;
|
Chris@16
|
540
|
Chris@16
|
541 packed_dynamic_channel_reference(void* data_ptr, unsigned first_bit) : parent_t(data_ptr), _first_bit(first_bit) {}
|
Chris@16
|
542 packed_dynamic_channel_reference(const packed_dynamic_channel_reference& ref) : parent_t(ref._data_ptr), _first_bit(ref._first_bit) {}
|
Chris@16
|
543
|
Chris@16
|
544 const packed_dynamic_channel_reference& operator=(integer_t value) const { assert(value<=parent_t::max_val); set_unsafe(value); return *this; }
|
Chris@16
|
545 const packed_dynamic_channel_reference& operator=(const mutable_reference& ref) const { set_unsafe(ref.get()); return *this; }
|
Chris@16
|
546 const packed_dynamic_channel_reference& operator=(const const_reference& ref) const { set_unsafe(ref.get()); return *this; }
|
Chris@16
|
547
|
Chris@16
|
548 template <typename BitField1, int FirstBit1, bool Mutable1>
|
Chris@16
|
549 const packed_dynamic_channel_reference& operator=(const packed_channel_reference<BitField1, FirstBit1, NumBits, Mutable1>& ref) const
|
Chris@16
|
550 { set_unsafe(ref.get()); return *this; }
|
Chris@16
|
551
|
Chris@16
|
552 unsigned first_bit() const { return _first_bit; }
|
Chris@16
|
553
|
Chris@16
|
554 integer_t get() const {
|
Chris@16
|
555 const BitField channel_mask = static_cast< integer_t >( parent_t::max_val ) << _first_bit;
|
Chris@16
|
556 return static_cast< integer_t >(( this->get_data()&channel_mask ) >> _first_bit );
|
Chris@16
|
557 }
|
Chris@16
|
558
|
Chris@16
|
559 void set_unsafe(integer_t value) const {
|
Chris@16
|
560 const BitField channel_mask = static_cast< integer_t >( parent_t::max_val ) << _first_bit;
|
Chris@16
|
561 this->set_data((this->get_data() & ~channel_mask) | value<<_first_bit);
|
Chris@16
|
562 }
|
Chris@16
|
563 };
|
Chris@16
|
564 } } // namespace boost::gil
|
Chris@16
|
565
|
Chris@16
|
566 namespace std {
|
Chris@16
|
567 // We are forced to define swap inside std namespace because on some platforms (Visual Studio 8) STL calls swap qualified.
|
Chris@16
|
568 // swap with 'left bias':
|
Chris@16
|
569 // - swap between proxy and anything
|
Chris@16
|
570 // - swap between value type and proxy
|
Chris@16
|
571 // - swap between proxy and proxy
|
Chris@16
|
572
|
Chris@16
|
573
|
Chris@16
|
574 /// \ingroup PackedChannelDynamicReferenceModel
|
Chris@16
|
575 /// \brief swap for packed_dynamic_channel_reference
|
Chris@16
|
576 template <typename BF, int NB, bool M, typename R> inline
|
Chris@16
|
577 void swap(const boost::gil::packed_dynamic_channel_reference<BF,NB,M> x, R& y) {
|
Chris@16
|
578 boost::gil::swap_proxy<typename boost::gil::packed_dynamic_channel_reference<BF,NB,M>::value_type>(x,y);
|
Chris@16
|
579 }
|
Chris@16
|
580
|
Chris@16
|
581
|
Chris@16
|
582 /// \ingroup PackedChannelDynamicReferenceModel
|
Chris@16
|
583 /// \brief swap for packed_dynamic_channel_reference
|
Chris@16
|
584 template <typename BF, int NB, bool M> inline
|
Chris@16
|
585 void swap(typename boost::gil::packed_dynamic_channel_reference<BF,NB,M>::value_type& x, const boost::gil::packed_dynamic_channel_reference<BF,NB,M> y) {
|
Chris@16
|
586 boost::gil::swap_proxy<typename boost::gil::packed_dynamic_channel_reference<BF,NB,M>::value_type>(x,y);
|
Chris@16
|
587 }
|
Chris@16
|
588
|
Chris@16
|
589
|
Chris@16
|
590 /// \ingroup PackedChannelDynamicReferenceModel
|
Chris@16
|
591 /// \brief swap for packed_dynamic_channel_reference
|
Chris@16
|
592 template <typename BF, int NB, bool M> inline
|
Chris@16
|
593 void swap(const boost::gil::packed_dynamic_channel_reference<BF,NB,M> x, const boost::gil::packed_dynamic_channel_reference<BF,NB,M> y) {
|
Chris@16
|
594 boost::gil::swap_proxy<typename boost::gil::packed_dynamic_channel_reference<BF,NB,M>::value_type>(x,y);
|
Chris@16
|
595 }
|
Chris@16
|
596 } // namespace std
|
Chris@16
|
597
|
Chris@16
|
598 namespace boost { namespace gil {
|
Chris@16
|
599 ///////////////////////////////////////////
|
Chris@16
|
600 ////
|
Chris@16
|
601 //// Built-in channel models
|
Chris@16
|
602 ////
|
Chris@16
|
603 ///////////////////////////////////////////
|
Chris@16
|
604
|
Chris@16
|
605 /// \defgroup bits8 bits8
|
Chris@16
|
606 /// \ingroup ChannelModel
|
Chris@16
|
607 /// \brief 8-bit unsigned integral channel type (typedef from uint8_t). Models ChannelValueConcept
|
Chris@16
|
608
|
Chris@16
|
609 /// \ingroup bits8
|
Chris@16
|
610 typedef uint8_t bits8;
|
Chris@16
|
611
|
Chris@16
|
612 /// \defgroup bits16 bits16
|
Chris@16
|
613 /// \ingroup ChannelModel
|
Chris@16
|
614 /// \brief 16-bit unsigned integral channel type (typedef from uint16_t). Models ChannelValueConcept
|
Chris@16
|
615
|
Chris@16
|
616 /// \ingroup bits16
|
Chris@16
|
617 typedef uint16_t bits16;
|
Chris@16
|
618
|
Chris@16
|
619 /// \defgroup bits32 bits32
|
Chris@16
|
620 /// \ingroup ChannelModel
|
Chris@16
|
621 /// \brief 32-bit unsigned integral channel type (typedef from uint32_t). Models ChannelValueConcept
|
Chris@16
|
622
|
Chris@16
|
623 /// \ingroup bits32
|
Chris@16
|
624 typedef uint32_t bits32;
|
Chris@16
|
625
|
Chris@16
|
626 /// \defgroup bits8s bits8s
|
Chris@16
|
627 /// \ingroup ChannelModel
|
Chris@16
|
628 /// \brief 8-bit signed integral channel type (typedef from int8_t). Models ChannelValueConcept
|
Chris@16
|
629
|
Chris@16
|
630 /// \ingroup bits8s
|
Chris@16
|
631 typedef int8_t bits8s;
|
Chris@16
|
632
|
Chris@16
|
633 /// \defgroup bits16s bits16s
|
Chris@16
|
634 /// \ingroup ChannelModel
|
Chris@16
|
635 /// \brief 16-bit signed integral channel type (typedef from int16_t). Models ChannelValueConcept
|
Chris@16
|
636
|
Chris@16
|
637 /// \ingroup bits16s
|
Chris@16
|
638 typedef int16_t bits16s;
|
Chris@16
|
639
|
Chris@16
|
640 /// \defgroup bits32s bits32s
|
Chris@16
|
641 /// \ingroup ChannelModel
|
Chris@16
|
642 /// \brief 32-bit signed integral channel type (typedef from int32_t). Models ChannelValueConcept
|
Chris@16
|
643
|
Chris@16
|
644 /// \ingroup bits32s
|
Chris@16
|
645 typedef int32_t bits32s;
|
Chris@16
|
646
|
Chris@16
|
647 /// \defgroup bits32f bits32f
|
Chris@16
|
648 /// \ingroup ChannelModel
|
Chris@16
|
649 /// \brief 32-bit floating point channel type with range [0.0f ... 1.0f]. Models ChannelValueConcept
|
Chris@16
|
650
|
Chris@16
|
651 /// \ingroup bits32f
|
Chris@16
|
652 typedef scoped_channel_value<float,float_zero,float_one> bits32f;
|
Chris@16
|
653
|
Chris@16
|
654 } } // namespace boost::gil
|
Chris@16
|
655
|
Chris@16
|
656 namespace boost {
|
Chris@16
|
657
|
Chris@16
|
658 template <int NumBits>
|
Chris@16
|
659 struct is_integral<gil::packed_channel_value<NumBits> > : public mpl::true_ {};
|
Chris@16
|
660
|
Chris@16
|
661 template <typename BitField, int FirstBit, int NumBits, bool IsMutable>
|
Chris@16
|
662 struct is_integral<gil::packed_channel_reference<BitField,FirstBit,NumBits,IsMutable> > : public mpl::true_ {};
|
Chris@16
|
663
|
Chris@16
|
664 template <typename BitField, int NumBits, bool IsMutable>
|
Chris@16
|
665 struct is_integral<gil::packed_dynamic_channel_reference<BitField,NumBits,IsMutable> > : public mpl::true_ {};
|
Chris@16
|
666
|
Chris@16
|
667 template <typename BaseChannelValue, typename MinVal, typename MaxVal>
|
Chris@16
|
668 struct is_integral<gil::scoped_channel_value<BaseChannelValue,MinVal,MaxVal> > : public is_integral<BaseChannelValue> {};
|
Chris@16
|
669
|
Chris@16
|
670 }
|
Chris@16
|
671
|
Chris@16
|
672 #endif
|