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