8 #ifndef BOOST_GIL_CHANNEL_HPP
9 #define BOOST_GIL_CHANNEL_HPP
11 #include <boost/gil/utilities.hpp>
13 #include <boost/config.hpp>
14 #include <boost/config/pragma_message.hpp>
15 #include <boost/integer/integer_mask.hpp>
16 #include <boost/type_traits/remove_cv.hpp>
22 #ifdef BOOST_GIL_DOXYGEN_ONLY
23 #define BOOST_GIL_CONFIG_HAS_UNALIGNED_ACCESS
33 #ifdef BOOST_GIL_CONFIG_HAS_UNALIGNED_ACCESS
34 #if defined(sun) || defined(__sun) || \ // SunOS
35 defined(__osf__) || defined(__osf) || \
36 defined(_hpux) || defined(hpux) || \
37 defined(__arm__) || defined(__ARM_ARCH) || \
39 #error Unaligned access strictly disabled for some UNIX platforms or ARM architecture
40 #elif defined(__i386__) || defined(__x86_64__) || defined(__vax__)
48 BOOST_PRAGMA_MESSAGE(
"CAUTION: Unaligned access tolerated on little-endian may cause undefined behaviour")
50 #error Unaligned access disabled for unknown platforms and architectures
52 #endif // defined(BOOST_GIL_CONFIG_HAS_UNALIGNED_ACCESS)
54 namespace boost {
namespace gil {
72 template <
typename T,
bool is_
class>
struct channel_traits_impl;
76 struct channel_traits_impl<T, true> {
77 typedef typename T::value_type value_type;
78 typedef typename T::reference reference;
79 typedef typename T::pointer pointer;
80 typedef typename T::const_reference const_reference;
81 typedef typename T::const_pointer const_pointer;
82 BOOST_STATIC_CONSTANT(
bool, is_mutable=T::is_mutable);
83 static value_type min_value() {
return T::min_value(); }
84 static value_type max_value() {
return T::max_value(); }
89 struct channel_traits_impl<T, false> {
93 typedef const T& const_reference;
94 typedef T
const* const_pointer;
95 BOOST_STATIC_CONSTANT(
bool, is_mutable=
true);
96 static value_type min_value() {
return (std::numeric_limits<T>::min)(); }
97 static value_type max_value() {
return (std::numeric_limits<T>::max)(); }
101 template <
typename T>
102 struct channel_traits_impl<const T, false> :
public channel_traits_impl<T, false> {
103 typedef const T& reference;
104 typedef const T* pointer;
105 BOOST_STATIC_CONSTANT(
bool, is_mutable=
false);
127 template <
typename T>
128 struct channel_traits :
public detail::channel_traits_impl<T, is_class<T>::value> {};
131 template <
typename T>
struct channel_traits< T&> :
public channel_traits<T> {};
134 template <
typename T>
struct channel_traits<const T&> :
public channel_traits<T> {
135 typedef typename channel_traits<T>::const_reference reference;
136 typedef typename channel_traits<T>::const_pointer pointer;
137 BOOST_STATIC_CONSTANT(
bool, is_mutable=
false);
166 template <
typename BaseChannelValue,
169 typename MinVal,
typename MaxVal>
170 struct scoped_channel_value {
171 typedef scoped_channel_value value_type;
172 typedef value_type& reference;
173 typedef value_type* pointer;
174 typedef const value_type& const_reference;
175 typedef const value_type* const_pointer;
176 BOOST_STATIC_CONSTANT(
bool, is_mutable=channel_traits<BaseChannelValue>::is_mutable);
178 typedef BaseChannelValue base_channel_t;
180 static value_type min_value() {
return MinVal::apply(); }
181 static value_type max_value() {
return MaxVal::apply(); }
183 scoped_channel_value() {}
184 scoped_channel_value(
const scoped_channel_value& c) : _value(c._value) {}
185 scoped_channel_value(BaseChannelValue val) : _value(val) {}
187 scoped_channel_value& operator++() { ++_value;
return *
this; }
188 scoped_channel_value& operator--() { --_value;
return *
this; }
190 scoped_channel_value operator++(
int) { scoped_channel_value tmp=*
this; this->operator++();
return tmp; }
191 scoped_channel_value operator--(
int) { scoped_channel_value tmp=*
this; this->operator--();
return tmp; }
193 template <
typename Scalar2> scoped_channel_value& operator+=(Scalar2 v) { _value+=v;
return *
this; }
194 template <
typename Scalar2> scoped_channel_value& operator-=(Scalar2 v) { _value-=v;
return *
this; }
195 template <
typename Scalar2> scoped_channel_value& operator*=(Scalar2 v) { _value*=v;
return *
this; }
196 template <
typename Scalar2> scoped_channel_value& operator/=(Scalar2 v) { _value/=v;
return *
this; }
198 scoped_channel_value& operator=(BaseChannelValue v) { _value=v;
return *
this; }
199 operator BaseChannelValue()
const {
return _value; }
201 BaseChannelValue _value;
204 template <
typename T>
205 struct float_point_zero
207 static constexpr T apply() {
return 0.0f; }
210 template <
typename T>
211 struct float_point_one
213 static constexpr T apply() {
return 1.0f; }
228 template <
int NumBits>
229 struct min_fast_uint :
public mpl::if_c< (NumBits<=8),
231 typename mpl::if_c< (NumBits<=16),
233 typename mpl::if_c< (NumBits<=32),
240 template <int NumBits>
241 struct num_value_fn : public mpl::if_c< ( NumBits < 32 )
246 template <int NumBits>
247 struct max_value_fn : public mpl::if_c< ( NumBits <= 32 )
269 template <int NumBits>
272 class packed_channel_value {
275 typedef typename detail::min_fast_uint<NumBits>::type integer_t;
278 typedef packed_channel_value value_type;
279 typedef value_type& reference;
280 typedef const value_type& const_reference;
281 typedef value_type* pointer;
282 typedef const value_type* const_pointer;
284 static value_type min_value() { return 0; }
285 static value_type max_value() { return low_bits_mask_t< NumBits >::sig_bits; }
287 BOOST_STATIC_CONSTANT(bool, is_mutable=true);
289 packed_channel_value() {}
291 packed_channel_value(integer_t v) { _value = static_cast< integer_t >( v & low_bits_mask_t<NumBits>::sig_bits_fast ); }
292 template <typename Scalar> packed_channel_value(Scalar v) { _value = packed_channel_value( static_cast< integer_t >( v ) ); }
294 static unsigned int num_bits() { return NumBits; }
296 operator integer_t() const { return _value; }
303 template <std::size_t K>
304 struct static_copy_bytes {
305 void operator()(const unsigned char* from, unsigned char* to) const {
307 static_copy_bytes<K-1>()(++from,++to);
312 struct static_copy_bytes<0> {
313 void operator()(const unsigned char* , unsigned char*) const {}
316 template <typename Derived, typename BitField, int NumBits, bool Mutable>
317 class packed_channel_reference_base {
319 typedef typename mpl::if_c<Mutable,void*,const void*>::type data_ptr_t;
321 data_ptr_t _data_ptr;
323 typedef packed_channel_value<NumBits> value_type;
324 typedef const Derived reference;
325 typedef value_type* pointer;
326 typedef const value_type* const_pointer;
327 BOOST_STATIC_CONSTANT(int, num_bits=NumBits);
328 BOOST_STATIC_CONSTANT(bool, is_mutable=Mutable);
330 static value_type min_value() { return channel_traits<value_type>::min_value(); }
331 static value_type max_value() { return channel_traits<value_type>::max_value(); }
333 typedef BitField bitfield_t;
334 typedef typename value_type::integer_t integer_t;
336 packed_channel_reference_base(data_ptr_t data_ptr) : _data_ptr(data_ptr) {}
337 packed_channel_reference_base(const packed_channel_reference_base& ref) : _data_ptr(ref._data_ptr) {}
338 const Derived& operator=(integer_t v) const { set(v); return derived(); }
340 const Derived& operator++() const { set(get()+1); return derived(); }
341 const Derived& operator--() const { set(get()-1); return derived(); }
343 Derived operator++(int) const { Derived tmp=derived(); this->operator++(); return tmp; }
344 Derived operator--(int) const { Derived tmp=derived(); this->operator--(); return tmp; }
346 template <typename Scalar2> const Derived& operator+=(Scalar2 v) const { set( static_cast<integer_t>( get() + v )); return derived(); }
347 template <typename Scalar2> const Derived& operator-=(Scalar2 v) const { set( static_cast<integer_t>( get() - v )); return derived(); }
348 template <typename Scalar2> const Derived& operator*=(Scalar2 v) const { set( static_cast<integer_t>( get() * v )); return derived(); }
349 template <typename Scalar2> const Derived& operator/=(Scalar2 v) const { set( static_cast<integer_t>( get() / v )); return derived(); }
351 operator integer_t() const { return get(); }
352 data_ptr_t operator &() const {return _data_ptr;}
355 typedef typename detail::num_value_fn< NumBits >::type num_value_t;
356 typedef typename detail::max_value_fn< NumBits >::type max_value_t;
358 static const num_value_t num_values = static_cast< num_value_t >( 1 ) << NumBits ;
359 static const max_value_t max_val = static_cast< max_value_t >( num_values - 1 );
361 #if defined(BOOST_GIL_CONFIG_HAS_UNALIGNED_ACCESS)
362 const bitfield_t& get_data() const { return *static_cast<const bitfield_t*>(_data_ptr); }
363 void set_data(const bitfield_t& val) const { *static_cast< bitfield_t*>(_data_ptr) = val; }
365 bitfield_t get_data() const {
367 static_copy_bytes<sizeof(bitfield_t) >()(gil_reinterpret_cast_c<const unsigned char*>(_data_ptr),gil_reinterpret_cast<unsigned char*>(&ret));
370 void set_data(const bitfield_t& val) const {
371 static_copy_bytes<sizeof(bitfield_t) >()(gil_reinterpret_cast_c<const unsigned char*>(&val),gil_reinterpret_cast<unsigned char*>(_data_ptr));
376 void set(integer_t value) const {
377 this->derived().set_unsafe(((value % num_values) + num_values) % num_values);
379 integer_t get() const { return derived().get(); }
380 const Derived& derived() const { return static_cast<const Derived&>(*this); }
400 template <typename BitField,
401 int FirstBit, int NumBits,
403 class packed_channel_reference;
405 template <typename BitField,
408 class packed_dynamic_channel_reference;
412 template <typename BitField, int FirstBit, int NumBits>
413 class packed_channel_reference<BitField,FirstBit,NumBits,false>
414 : public detail::packed_channel_reference_base<packed_channel_reference<BitField,FirstBit,NumBits,false>,BitField,NumBits,false> {
415 typedef detail::packed_channel_reference_base<packed_channel_reference<BitField,FirstBit,NumBits,false>,BitField,NumBits,false> parent_t;
416 friend class packed_channel_reference<BitField,FirstBit,NumBits,true>;
418 static const BitField channel_mask = static_cast< BitField >( parent_t::max_val ) << FirstBit;
420 void operator=(const packed_channel_reference&);
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;
426 explicit packed_channel_reference(const void* data_ptr) : parent_t(data_ptr) {}
427 packed_channel_reference(const packed_channel_reference& ref) : parent_t(ref._data_ptr) {}
428 packed_channel_reference(const mutable_reference& ref) : parent_t(ref._data_ptr) {}
430 unsigned first_bit() const { return FirstBit; }
432 integer_t get() const { return integer_t((this->get_data()&channel_mask) >> FirstBit); }
437 template <typename BitField, int FirstBit, int NumBits>
438 class packed_channel_reference<BitField,FirstBit,NumBits,true>
439 : public detail::packed_channel_reference_base<packed_channel_reference<BitField,FirstBit,NumBits,true>,BitField,NumBits,true> {
440 typedef detail::packed_channel_reference_base<packed_channel_reference<BitField,FirstBit,NumBits,true>,BitField,NumBits,true> parent_t;
441 friend class packed_channel_reference<BitField,FirstBit,NumBits,false>;
443 static const BitField channel_mask = static_cast< BitField >( parent_t::max_val ) << FirstBit;
446 typedef const packed_channel_reference<BitField,FirstBit,NumBits,false> const_reference;
447 typedef const packed_channel_reference<BitField,FirstBit,NumBits,true> mutable_reference;
448 typedef typename parent_t::integer_t integer_t;
450 explicit packed_channel_reference(void* data_ptr) : parent_t(data_ptr) {}
451 packed_channel_reference(const packed_channel_reference& ref) : parent_t(ref._data_ptr) {}
453 const packed_channel_reference& operator=(integer_t value) const { assert(value<=parent_t::max_val); set_unsafe(value); return *this; }
454 const packed_channel_reference& operator=(const mutable_reference& ref) const { set_from_reference(ref.get_data()); return *this; }
455 const packed_channel_reference& operator=(const const_reference& ref) const { set_from_reference(ref.get_data()); return *this; }
457 template <bool Mutable1>
458 const packed_channel_reference& operator=(const packed_dynamic_channel_reference<BitField,NumBits,Mutable1>& ref) const { set_unsafe(ref.get()); return *this; }
460 unsigned first_bit() const { return FirstBit; }
462 integer_t get() const { return integer_t((this->get_data()&channel_mask) >> FirstBit); }
463 void set_unsafe(integer_t value) const { this->set_data((this->get_data() & ~channel_mask) | (( static_cast< BitField >( value )<<FirstBit))); }
465 void set_from_reference(const BitField& other_bits) const { this->set_data((this->get_data() & ~channel_mask) | (other_bits & channel_mask)); }
479 template <typename BF, int FB, int NB, bool M, typename R> inline
480 void swap(const boost::gil::packed_channel_reference<BF,FB,NB,M> x, R& y) {
481 boost::gil::swap_proxy<typename boost::gil::packed_channel_reference<BF,FB,NB,M>::value_type>(x,y);
487 template <typename BF, int FB, int NB, bool M> inline
488 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) {
489 boost::gil::swap_proxy<typename boost::gil::packed_channel_reference<BF,FB,NB,M>::value_type>(x,y);
494 template <typename BF, int FB, int NB, bool M> inline
495 void swap(const boost::gil::packed_channel_reference<BF,FB,NB,M> x, const boost::gil::packed_channel_reference<BF,FB,NB,M> y) {
496 boost::gil::swap_proxy<typename boost::gil::packed_channel_reference<BF,FB,NB,M>::value_type>(x,y);
500 namespace boost { namespace gil {
519 template <typename BitField, int NumBits>
523 class packed_dynamic_channel_reference<BitField,NumBits,false>
524 : public detail::packed_channel_reference_base<packed_dynamic_channel_reference<BitField,NumBits,false>,BitField,NumBits,false> {
525 typedef detail::packed_channel_reference_base<packed_dynamic_channel_reference<BitField,NumBits,false>,BitField,NumBits,false> parent_t;
526 friend class packed_dynamic_channel_reference<BitField,NumBits,true>;
530 void operator=(const packed_dynamic_channel_reference&);
532 typedef const packed_dynamic_channel_reference<BitField,NumBits,false> const_reference;
533 typedef const packed_dynamic_channel_reference<BitField,NumBits,true> mutable_reference;
534 typedef typename parent_t::integer_t integer_t;
536 packed_dynamic_channel_reference(const void* data_ptr, unsigned first_bit) : parent_t(data_ptr), _first_bit(first_bit) {}
537 packed_dynamic_channel_reference(const const_reference& ref) : parent_t(ref._data_ptr), _first_bit(ref._first_bit) {}
538 packed_dynamic_channel_reference(const mutable_reference& ref) : parent_t(ref._data_ptr), _first_bit(ref._first_bit) {}
540 unsigned first_bit() const { return _first_bit; }
542 integer_t get() const {
543 const BitField channel_mask = static_cast< integer_t >( parent_t::max_val ) <<_first_bit;
544 return static_cast< integer_t >(( this->get_data()&channel_mask ) >> _first_bit );
551 template <typename BitField, int NumBits>
552 class packed_dynamic_channel_reference<BitField,NumBits,true>
553 :
public detail::packed_channel_reference_base<packed_dynamic_channel_reference<BitField,NumBits,true>,BitField,NumBits,true> {
554 typedef detail::packed_channel_reference_base<packed_dynamic_channel_reference<BitField,NumBits,true>,BitField,NumBits,
true> parent_t;
555 friend class packed_dynamic_channel_reference<BitField,NumBits,false>;
562 typedef typename parent_t::integer_t integer_t;
564 packed_dynamic_channel_reference(
void* data_ptr,
unsigned first_bit) : parent_t(data_ptr), _first_bit(first_bit) {}
565 packed_dynamic_channel_reference(
const packed_dynamic_channel_reference& ref) : parent_t(ref._data_ptr), _first_bit(ref._first_bit) {}
567 const packed_dynamic_channel_reference& operator=(integer_t value)
const { assert(value<=parent_t::max_val); set_unsafe(value);
return *
this; }
568 const packed_dynamic_channel_reference& operator=(
const mutable_reference& ref)
const { set_unsafe(ref.get());
return *
this; }
569 const packed_dynamic_channel_reference& operator=(
const const_reference& ref)
const { set_unsafe(ref.get());
return *
this; }
571 template <
typename BitField1,
int FirstBit1,
bool Mutable1>
572 const packed_dynamic_channel_reference& operator=(
const packed_channel_reference<BitField1, FirstBit1, NumBits, Mutable1>& ref)
const
573 { set_unsafe(ref.get());
return *
this; }
575 unsigned first_bit()
const {
return _first_bit; }
577 integer_t
get()
const {
578 const BitField channel_mask =
static_cast< integer_t
>( parent_t::max_val ) << _first_bit;
579 return static_cast< integer_t
>(( this->get_data()&channel_mask ) >> _first_bit );
582 void set_unsafe(integer_t value)
const {
583 const BitField channel_mask =
static_cast< integer_t
>( parent_t::max_val ) << _first_bit;
584 this->set_data((this->get_data() & ~channel_mask) | value<<_first_bit);
599 template <
typename BF,
int NB,
bool M,
typename R>
inline
600 void swap(
const boost::gil::packed_dynamic_channel_reference<BF,NB,M> x, R& y) {
601 boost::gil::swap_proxy<typename boost::gil::packed_dynamic_channel_reference<BF,NB,M>::value_type>(x,y);
607 template <
typename BF,
int NB,
bool M>
inline
608 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) {
609 boost::gil::swap_proxy<typename boost::gil::packed_dynamic_channel_reference<BF,NB,M>::value_type>(x,y);
614 template <
typename BF,
int NB,
bool M>
inline
615 void swap(
const boost::gil::packed_dynamic_channel_reference<BF,NB,M> x,
const boost::gil::packed_dynamic_channel_reference<BF,NB,M> y) {
616 boost::gil::swap_proxy<typename boost::gil::packed_dynamic_channel_reference<BF,NB,M>::value_type>(x,y);
622 template <
int NumBits>
623 struct is_integral<gil::packed_channel_value<NumBits> > :
public mpl::true_ {};
625 template <
typename BitField,
int FirstBit,
int NumBits,
bool IsMutable>
626 struct is_integral<gil::packed_channel_reference<BitField,FirstBit,NumBits,IsMutable> > :
public mpl::true_ {};
628 template <
typename BitField,
int NumBits,
bool IsMutable>
629 struct is_integral<gil::packed_dynamic_channel_reference<BitField,NumBits,IsMutable> > :
public mpl::true_ {};
631 template <
typename BaseChannelValue,
typename MinVal,
typename MaxVal>
632 struct is_integral<gil::scoped_channel_value<BaseChannelValue,MinVal,MaxVal> > :
public is_integral<BaseChannelValue> {};
637 namespace boost {
namespace gil {
638 template <
typename T>
639 struct base_channel_type_impl {
typedef T type; };
642 struct base_channel_type_impl<packed_channel_value<N> >
643 {
typedef typename packed_channel_value<N>::integer_t type; };
645 template <
typename B,
int F,
int N,
bool M>
646 struct base_channel_type_impl<packed_channel_reference<B, F, N, M> >
647 {
typedef typename packed_channel_reference<B,F,N,M>::integer_t type; };
649 template <
typename B,
int N,
bool M>
650 struct base_channel_type_impl<packed_dynamic_channel_reference<B, N, M> >
651 {
typedef typename packed_dynamic_channel_reference<B,N,M>::integer_t type; };
653 template <
typename ChannelValue,
typename MinV,
typename MaxV>
654 struct base_channel_type_impl<scoped_channel_value<ChannelValue, MinV, MaxV> >
655 {
typedef ChannelValue type; };
657 template <
typename T>
658 struct base_channel_type : base_channel_type_impl<typename remove_cv<T>::type > {};
void swap(const boost::gil::packed_dynamic_channel_reference< BF, NB, M > x, const boost::gil::packed_dynamic_channel_reference< BF, NB, M > y)
swap for packed_dynamic_channel_reference
Definition: channel.hpp:615
Models a constant subbyte channel reference whose bit offset is a runtime parameter. Models ChannelConcept Same as packed_channel_reference, except that the offset is a runtime parameter.
Definition: channel.hpp:523
Models a mutable subbyte channel reference whose bit offset is a runtime parameter. Models ChannelConcept Same as packed_channel_reference, except that the offset is a runtime parameter.
Definition: channel.hpp:552