Chris@16: /* Chris@16: Copyright 2005-2007 Adobe Systems Incorporated Chris@16: Chris@16: Use, modification and distribution are subject to the Boost Software License, Chris@16: Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at Chris@16: http://www.boost.org/LICENSE_1_0.txt). Chris@16: Chris@16: See http://stlab.adobe.com/gil for most recent version including documentation. Chris@16: */ Chris@16: Chris@16: /*************************************************************************************************/ Chris@16: Chris@16: #ifndef GIL_CHANNEL_HPP Chris@16: #define GIL_CHANNEL_HPP Chris@16: Chris@16: //////////////////////////////////////////////////////////////////////////////////////// Chris@16: /// \file Chris@16: /// \brief Channel utilities Chris@16: /// \author Lubomir Bourdev and Hailin Jin \n Chris@16: /// Adobe Systems Incorporated Chris@16: /// \date 2005-2007 \n Last updated on May 6, 2007 Chris@16: /// Chris@16: /// Definitions of standard GIL channel models Chris@16: /// Chris@16: //////////////////////////////////////////////////////////////////////////////////////// Chris@16: Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include "gil_config.hpp" Chris@16: #include "utilities.hpp" Chris@16: Chris@16: namespace boost { namespace gil { Chris@16: Chris@16: Chris@16: /////////////////////////////////////////// Chris@16: //// channel_traits Chris@16: //// Chris@16: //// \ingroup ChannelModel Chris@16: //// \class channel_traits Chris@16: //// \brief defines properties of channels, such as their range and associated types Chris@16: //// Chris@16: //// The channel traits must be defined for every model of ChannelConcept Chris@16: //// Default traits are provided. For built-in types the default traits use Chris@16: //// built-in pointer and reference and the channel range is the physical Chris@16: //// range of the type. For classes, the default traits forward the associated types Chris@16: //// and range to the class. Chris@16: //// Chris@16: /////////////////////////////////////////// Chris@16: Chris@16: namespace detail { Chris@16: template struct channel_traits_impl; Chris@16: Chris@16: // channel traits for custom class Chris@16: template Chris@16: struct channel_traits_impl { Chris@16: typedef typename T::value_type value_type; Chris@16: typedef typename T::reference reference; Chris@16: typedef typename T::pointer pointer; Chris@16: typedef typename T::const_reference const_reference; Chris@16: typedef typename T::const_pointer const_pointer; Chris@16: BOOST_STATIC_CONSTANT(bool, is_mutable=T::is_mutable); Chris@16: static value_type min_value() { return T::min_value(); } Chris@16: static value_type max_value() { return T::max_value(); } Chris@16: }; Chris@16: Chris@16: // channel traits implementation for built-in integral or floating point channel type Chris@16: template Chris@16: struct channel_traits_impl { Chris@16: typedef T value_type; Chris@16: typedef T& reference; Chris@16: typedef T* pointer; Chris@16: typedef const T& const_reference; Chris@16: typedef T const* const_pointer; Chris@16: BOOST_STATIC_CONSTANT(bool, is_mutable=true); Chris@16: static value_type min_value() { return (std::numeric_limits::min)(); } Chris@16: static value_type max_value() { return (std::numeric_limits::max)(); } Chris@16: }; Chris@16: Chris@16: // channel traits implementation for constant built-in scalar or floating point type Chris@16: template Chris@16: struct channel_traits_impl : public channel_traits_impl { Chris@16: typedef const T& reference; Chris@16: typedef const T* pointer; Chris@16: BOOST_STATIC_CONSTANT(bool, is_mutable=false); Chris@16: }; Chris@16: } Chris@16: Chris@16: /** Chris@16: \ingroup ChannelModel Chris@16: \brief Traits for channels. Contains the following members: Chris@16: \code Chris@16: template Chris@16: struct channel_traits { Chris@16: typedef ... value_type; Chris@16: typedef ... reference; Chris@16: typedef ... pointer; Chris@16: typedef ... const_reference; Chris@16: typedef ... const_pointer; Chris@16: Chris@16: static const bool is_mutable; Chris@16: static value_type min_value(); Chris@16: static value_type max_value(); Chris@16: }; Chris@16: \endcode Chris@16: */ Chris@16: template Chris@16: struct channel_traits : public detail::channel_traits_impl::value> {}; Chris@16: Chris@16: // Channel traits for C++ reference type - remove the reference Chris@16: template struct channel_traits< T&> : public channel_traits {}; Chris@16: Chris@16: // Channel traits for constant C++ reference type Chris@16: template struct channel_traits : public channel_traits { Chris@16: typedef typename channel_traits::const_reference reference; Chris@16: typedef typename channel_traits::const_pointer pointer; Chris@16: BOOST_STATIC_CONSTANT(bool, is_mutable=false); Chris@16: }; Chris@16: Chris@16: /////////////////////////////////////////// Chris@16: //// Chris@16: //// scoped_channel_value Chris@16: //// Chris@16: /////////////////////////////////////////// Chris@16: Chris@16: /** Chris@16: \defgroup ScopedChannelValue scoped_channel_value Chris@16: \ingroup ChannelModel Chris@16: \brief A channel adaptor that modifies the range of the source channel. Models: ChannelValueConcept Chris@16: Chris@16: Example: Chris@16: \code Chris@16: // Create a double channel with range [-0.5 .. 0.5] Chris@16: struct double_minus_half { static double apply() { return -0.5; } }; Chris@16: struct double_plus_half { static double apply() { return 0.5; } }; Chris@16: typedef scoped_channel_value bits64custom_t; Chris@16: Chris@16: // channel_convert its maximum should map to the maximum Chris@16: bits64custom_t x = channel_traits::max_value(); Chris@16: assert(x == 0.5); Chris@16: bits16 y = channel_convert(x); Chris@16: assert(y == 65535); Chris@16: \endcode Chris@16: */ Chris@16: Chris@16: /// \ingroup ScopedChannelValue Chris@16: /// \brief A channel adaptor that modifies the range of the source channel. Models: ChannelValueConcept Chris@16: template // classes with a static apply() function returning the minimum/maximum channel values Chris@16: struct scoped_channel_value { Chris@16: typedef scoped_channel_value value_type; Chris@16: typedef value_type& reference; Chris@16: typedef value_type* pointer; Chris@16: typedef const value_type& const_reference; Chris@16: typedef const value_type* const_pointer; Chris@16: BOOST_STATIC_CONSTANT(bool, is_mutable=channel_traits::is_mutable); Chris@16: Chris@16: typedef BaseChannelValue base_channel_t; Chris@16: Chris@16: static value_type min_value() { return MinVal::apply(); } Chris@16: static value_type max_value() { return MaxVal::apply(); } Chris@16: Chris@16: scoped_channel_value() {} Chris@16: scoped_channel_value(const scoped_channel_value& c) : _value(c._value) {} Chris@16: scoped_channel_value(BaseChannelValue val) : _value(val) {} Chris@16: Chris@16: scoped_channel_value& operator++() { ++_value; return *this; } Chris@16: scoped_channel_value& operator--() { --_value; return *this; } Chris@16: Chris@16: scoped_channel_value operator++(int) { scoped_channel_value tmp=*this; this->operator++(); return tmp; } Chris@16: scoped_channel_value operator--(int) { scoped_channel_value tmp=*this; this->operator--(); return tmp; } Chris@16: Chris@16: template scoped_channel_value& operator+=(Scalar2 v) { _value+=v; return *this; } Chris@16: template scoped_channel_value& operator-=(Scalar2 v) { _value-=v; return *this; } Chris@16: template scoped_channel_value& operator*=(Scalar2 v) { _value*=v; return *this; } Chris@16: template scoped_channel_value& operator/=(Scalar2 v) { _value/=v; return *this; } Chris@16: Chris@16: scoped_channel_value& operator=(BaseChannelValue v) { _value=v; return *this; } Chris@16: operator BaseChannelValue() const { return _value; } Chris@16: private: Chris@16: BaseChannelValue _value; Chris@16: }; Chris@16: Chris@16: struct float_zero { static float apply() { return 0.0f; } }; Chris@16: struct float_one { static float apply() { return 1.0f; } }; Chris@16: Chris@16: Chris@16: /////////////////////////////////////////// Chris@16: //// Chris@16: //// Support for sub-byte channels. These are integral channels whose value is contained in a range of bits inside an integral type Chris@16: //// Chris@16: /////////////////////////////////////////// Chris@16: Chris@16: // 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: // - Any operation that requires returning the result by value will otherwise return the built-in integral type, which will have incorrect range Chris@16: // That means that after getting the value of the channel we cannot properly do channel_convert, channel_invert, etc. Chris@16: // - 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: namespace detail { Chris@16: // returns the smallest fast unsigned integral type that has at least NumBits bits Chris@16: template Chris@16: struct min_fast_uint : public mpl::if_c< (NumBits<=8), Chris@16: uint_least8_t, Chris@16: typename mpl::if_c< (NumBits<=16), Chris@16: uint_least16_t, Chris@16: typename mpl::if_c< (NumBits<=32), Chris@16: uint_least32_t, Chris@16: uintmax_t Chris@16: >::type Chris@16: >::type Chris@16: > {}; Chris@16: Chris@16: template Chris@16: struct num_value_fn : public mpl::if_c< ( NumBits < 32 ) Chris@16: , uint32_t Chris@16: , uint64_t Chris@16: > {}; Chris@16: Chris@16: template Chris@16: struct max_value_fn : public mpl::if_c< ( NumBits <= 32 ) Chris@16: , uint32_t Chris@16: , uint64_t Chris@16: > {}; Chris@16: } Chris@16: Chris@16: /** Chris@16: \defgroup PackedChannelValueModel packed_channel_value Chris@16: \ingroup ChannelModel Chris@16: \brief Represents the value of an unsigned integral channel operating over a bit range. Models: ChannelValueConcept Chris@16: Example: Chris@16: \code Chris@16: // A 4-bit unsigned integral channel. Chris@16: typedef packed_channel_value<4> bits4; Chris@16: Chris@16: assert(channel_traits::min_value()==0); Chris@16: assert(channel_traits::max_value()==15); Chris@16: assert(sizeof(bits4)==1); Chris@16: BOOST_STATIC_ASSERT((boost::is_integral::value)); Chris@16: \endcode Chris@16: */ Chris@16: Chris@16: /// \ingroup PackedChannelValueModel Chris@16: /// \brief The value of a subbyte channel. Models: ChannelValueConcept Chris@16: template Chris@16: class packed_channel_value { Chris@16: Chris@16: typedef typename detail::num_value_fn< NumBits >::type num_value_t; Chris@16: static const num_value_t num_values = static_cast< num_value_t >( 1 ) << NumBits ; Chris@16: Chris@16: public: Chris@16: typedef typename detail::min_fast_uint::type integer_t; Chris@16: Chris@16: Chris@16: typedef packed_channel_value value_type; Chris@16: typedef value_type& reference; Chris@16: typedef const value_type& const_reference; Chris@16: typedef value_type* pointer; Chris@16: typedef const value_type* const_pointer; Chris@16: Chris@16: static value_type min_value() { return value_type(0); } Chris@16: static value_type max_value() { return value_type(num_values-1); } Chris@16: BOOST_STATIC_CONSTANT(bool, is_mutable=true); Chris@16: Chris@16: packed_channel_value() {} Chris@16: packed_channel_value(integer_t v) { _value = static_cast< integer_t >( v % num_values ); } Chris@16: packed_channel_value(const packed_channel_value& v) : _value(v._value) {} Chris@16: template packed_channel_value(Scalar v) { _value = static_cast< integer_t >( v ) % num_values; } Chris@16: Chris@16: static unsigned int num_bits() { return NumBits; } Chris@16: Chris@16: Chris@16: operator integer_t() const { return _value; } Chris@16: private: Chris@16: integer_t _value; Chris@16: }; Chris@16: Chris@16: namespace detail { Chris@16: Chris@16: template Chris@16: struct static_copy_bytes { Chris@16: void operator()(const unsigned char* from, unsigned char* to) const { Chris@16: *to = *from; Chris@16: static_copy_bytes()(++from,++to); Chris@16: } Chris@16: }; Chris@16: Chris@16: template <> Chris@16: struct static_copy_bytes<0> { Chris@16: void operator()(const unsigned char* , unsigned char*) const {} Chris@16: }; Chris@16: Chris@16: template Chris@16: class packed_channel_reference_base { Chris@16: protected: Chris@16: typedef typename mpl::if_c::type data_ptr_t; Chris@16: public: Chris@16: data_ptr_t _data_ptr; // void* pointer to the first byte of the bit range Chris@16: Chris@16: typedef packed_channel_value value_type; Chris@16: typedef const Derived reference; Chris@16: typedef value_type* pointer; Chris@16: typedef const value_type* const_pointer; Chris@16: BOOST_STATIC_CONSTANT(int, num_bits=NumBits); Chris@16: BOOST_STATIC_CONSTANT(bool, is_mutable=Mutable); Chris@16: Chris@16: static value_type min_value() { return channel_traits::min_value(); } Chris@16: static value_type max_value() { return channel_traits::max_value(); } Chris@16: Chris@16: typedef BitField bitfield_t; Chris@16: typedef typename value_type::integer_t integer_t; Chris@16: Chris@16: packed_channel_reference_base(data_ptr_t data_ptr) : _data_ptr(data_ptr) {} Chris@16: packed_channel_reference_base(const packed_channel_reference_base& ref) : _data_ptr(ref._data_ptr) {} Chris@16: const Derived& operator=(integer_t v) const { set(v); return derived(); } Chris@16: Chris@16: const Derived& operator++() const { set(get()+1); return derived(); } Chris@16: const Derived& operator--() const { set(get()-1); return derived(); } Chris@16: Chris@16: Derived operator++(int) const { Derived tmp=derived(); this->operator++(); return tmp; } Chris@16: Derived operator--(int) const { Derived tmp=derived(); this->operator--(); return tmp; } Chris@16: Chris@16: template const Derived& operator+=(Scalar2 v) const { set(get()+v); return derived(); } Chris@16: template const Derived& operator-=(Scalar2 v) const { set(get()-v); return derived(); } Chris@16: template const Derived& operator*=(Scalar2 v) const { set(get()*v); return derived(); } Chris@16: template const Derived& operator/=(Scalar2 v) const { set(get()/v); return derived(); } Chris@16: Chris@16: operator integer_t() const { return get(); } Chris@16: data_ptr_t operator &() const {return _data_ptr;} Chris@16: protected: Chris@16: Chris@16: typedef typename detail::num_value_fn< NumBits >::type num_value_t; Chris@16: typedef typename detail::max_value_fn< NumBits >::type max_value_t; Chris@16: Chris@16: static const num_value_t num_values = static_cast< num_value_t >( 1 ) << NumBits ; Chris@16: static const max_value_t max_val = static_cast< max_value_t >( num_values - 1 ); Chris@16: Chris@16: #ifdef GIL_NONWORD_POINTER_ALIGNMENT_SUPPORTED Chris@16: const bitfield_t& get_data() const { return *static_cast(_data_ptr); } Chris@16: void set_data(const bitfield_t& val) const { *static_cast< bitfield_t*>(_data_ptr) = val; } Chris@16: #else Chris@16: bitfield_t get_data() const { Chris@16: bitfield_t ret; Chris@16: static_copy_bytes()(gil_reinterpret_cast_c(_data_ptr),gil_reinterpret_cast(&ret)); Chris@16: return ret; Chris@16: } Chris@16: void set_data(const bitfield_t& val) const { Chris@16: static_copy_bytes()(gil_reinterpret_cast_c(&val),gil_reinterpret_cast(_data_ptr)); Chris@16: } Chris@16: #endif Chris@16: Chris@16: private: Chris@16: void set(integer_t value) const { // can this be done faster?? Chris@16: const integer_t num_values = max_val+1; Chris@16: this->derived().set_unsafe(((value % num_values) + num_values) % num_values); Chris@16: } Chris@16: integer_t get() const { return derived().get(); } Chris@16: const Derived& derived() const { return static_cast(*this); } Chris@16: }; Chris@16: } // namespace detail Chris@16: Chris@16: /** Chris@16: \defgroup PackedChannelReferenceModel packed_channel_reference Chris@16: \ingroup ChannelModel Chris@16: \brief Represents a reference proxy to a channel operating over a bit range whose offset is fixed at compile time. Models ChannelConcept Chris@16: Example: Chris@16: \code Chris@16: // Reference to a 2-bit channel starting at bit 1 (i.e. the second bit) Chris@16: typedef const packed_channel_reference bits2_1_ref_t; Chris@16: Chris@16: uint16_t data=0; Chris@16: bits2_1_ref_t channel_ref(&data); Chris@16: channel_ref = channel_traits::max_value(); // == 3 Chris@16: assert(data == 6); // == 3<<1 == 6 Chris@16: \endcode Chris@16: */ Chris@16: Chris@16: template // true if the reference is mutable Chris@16: class packed_channel_reference; Chris@16: Chris@16: template // true if the reference is mutable Chris@16: class packed_dynamic_channel_reference; Chris@16: Chris@16: /// \ingroup PackedChannelReferenceModel Chris@16: /// \brief A constant subbyte channel reference whose bit offset is fixed at compile time. Models ChannelConcept Chris@16: template Chris@16: class packed_channel_reference Chris@16: : public detail::packed_channel_reference_base,BitField,NumBits,false> { Chris@16: typedef detail::packed_channel_reference_base,BitField,NumBits,false> parent_t; Chris@16: friend class packed_channel_reference; Chris@16: Chris@16: static const BitField channel_mask = static_cast< BitField >( parent_t::max_val ) << FirstBit; Chris@16: Chris@16: void operator=(const packed_channel_reference&); Chris@16: public: Chris@16: typedef const packed_channel_reference const_reference; Chris@16: typedef const packed_channel_reference mutable_reference; Chris@16: typedef typename parent_t::integer_t integer_t; Chris@16: Chris@16: explicit packed_channel_reference(const void* data_ptr) : parent_t(data_ptr) {} Chris@16: packed_channel_reference(const packed_channel_reference& ref) : parent_t(ref._data_ptr) {} Chris@16: packed_channel_reference(const mutable_reference& ref) : parent_t(ref._data_ptr) {} Chris@16: Chris@16: unsigned first_bit() const { return FirstBit; } Chris@16: Chris@16: integer_t get() const { return integer_t((this->get_data()&channel_mask) >> FirstBit); } Chris@16: }; Chris@16: Chris@16: /// \ingroup PackedChannelReferenceModel Chris@16: /// \brief A mutable subbyte channel reference whose bit offset is fixed at compile time. Models ChannelConcept Chris@16: template Chris@16: class packed_channel_reference Chris@16: : public detail::packed_channel_reference_base,BitField,NumBits,true> { Chris@16: typedef detail::packed_channel_reference_base,BitField,NumBits,true> parent_t; Chris@16: friend class packed_channel_reference; Chris@16: Chris@16: static const BitField channel_mask = static_cast< BitField >( parent_t::max_val ) << FirstBit; Chris@16: Chris@16: public: Chris@16: typedef const packed_channel_reference const_reference; Chris@16: typedef const packed_channel_reference mutable_reference; Chris@16: typedef typename parent_t::integer_t integer_t; Chris@16: Chris@16: explicit packed_channel_reference(void* data_ptr) : parent_t(data_ptr) {} Chris@16: packed_channel_reference(const packed_channel_reference& ref) : parent_t(ref._data_ptr) {} Chris@16: Chris@16: const packed_channel_reference& operator=(integer_t value) const { assert(value<=parent_t::max_val); set_unsafe(value); return *this; } Chris@16: const packed_channel_reference& operator=(const mutable_reference& ref) const { set_from_reference(ref.get_data()); return *this; } Chris@16: const packed_channel_reference& operator=(const const_reference& ref) const { set_from_reference(ref.get_data()); return *this; } Chris@16: Chris@16: template Chris@16: const packed_channel_reference& operator=(const packed_dynamic_channel_reference& ref) const { set_unsafe(ref.get()); return *this; } Chris@16: Chris@16: unsigned first_bit() const { return FirstBit; } Chris@16: Chris@16: integer_t get() const { return integer_t((this->get_data()&channel_mask) >> FirstBit); } Chris@16: void set_unsafe(integer_t value) const { this->set_data((this->get_data() & ~channel_mask) | (( static_cast< BitField >( value )<set_data((this->get_data() & ~channel_mask) | (other_bits & channel_mask)); } Chris@16: }; Chris@16: Chris@16: } } // namespace boost::gil Chris@16: Chris@16: namespace std { Chris@16: // We are forced to define swap inside std namespace because on some platforms (Visual Studio 8) STL calls swap qualified. Chris@16: // swap with 'left bias': Chris@16: // - swap between proxy and anything Chris@16: // - swap between value type and proxy Chris@16: // - swap between proxy and proxy Chris@16: Chris@16: /// \ingroup PackedChannelReferenceModel Chris@16: /// \brief swap for packed_channel_reference Chris@16: template inline Chris@16: void swap(const boost::gil::packed_channel_reference x, R& y) { Chris@16: boost::gil::swap_proxy::value_type>(x,y); Chris@16: } Chris@16: Chris@16: Chris@16: /// \ingroup PackedChannelReferenceModel Chris@16: /// \brief swap for packed_channel_reference Chris@16: template inline Chris@16: void swap(typename boost::gil::packed_channel_reference::value_type& x, const boost::gil::packed_channel_reference y) { Chris@16: boost::gil::swap_proxy::value_type>(x,y); Chris@16: } Chris@16: Chris@16: Chris@16: /// \ingroup PackedChannelReferenceModel Chris@16: /// \brief swap for packed_channel_reference Chris@16: template inline Chris@16: void swap(const boost::gil::packed_channel_reference x, const boost::gil::packed_channel_reference y) { Chris@16: boost::gil::swap_proxy::value_type>(x,y); Chris@16: } Chris@16: } // namespace std Chris@16: Chris@16: namespace boost { namespace gil { Chris@16: Chris@16: /** Chris@16: \defgroup PackedChannelDynamicReferenceModel packed_dynamic_channel_reference Chris@16: \ingroup ChannelModel Chris@16: \brief Represents a reference proxy to a channel operating over a bit range whose offset is specified at run time. Models ChannelConcept Chris@16: Chris@16: Example: Chris@16: \code Chris@16: // Reference to a 2-bit channel whose offset is specified at construction time Chris@16: typedef const packed_dynamic_channel_reference bits2_dynamic_ref_t; Chris@16: Chris@16: uint16_t data=0; Chris@16: bits2_dynamic_ref_t channel_ref(&data,1); Chris@16: channel_ref = channel_traits::max_value(); // == 3 Chris@16: assert(data == 6); // == (3<<1) == 6 Chris@16: \endcode Chris@16: */ Chris@16: Chris@16: /// \brief Models a constant subbyte channel reference whose bit offset is a runtime parameter. Models ChannelConcept Chris@16: /// Same as packed_channel_reference, except that the offset is a runtime parameter Chris@16: /// \ingroup PackedChannelDynamicReferenceModel Chris@16: template Chris@16: class packed_dynamic_channel_reference Chris@16: : public detail::packed_channel_reference_base,BitField,NumBits,false> { Chris@16: typedef detail::packed_channel_reference_base,BitField,NumBits,false> parent_t; Chris@16: friend class packed_dynamic_channel_reference; Chris@16: Chris@16: unsigned _first_bit; // 0..7 Chris@16: Chris@16: void operator=(const packed_dynamic_channel_reference&); Chris@16: public: Chris@16: typedef const packed_dynamic_channel_reference const_reference; Chris@16: typedef const packed_dynamic_channel_reference mutable_reference; Chris@16: typedef typename parent_t::integer_t integer_t; Chris@16: Chris@16: packed_dynamic_channel_reference(const void* data_ptr, unsigned first_bit) : parent_t(data_ptr), _first_bit(first_bit) {} Chris@16: packed_dynamic_channel_reference(const const_reference& ref) : parent_t(ref._data_ptr), _first_bit(ref._first_bit) {} Chris@16: packed_dynamic_channel_reference(const mutable_reference& ref) : parent_t(ref._data_ptr), _first_bit(ref._first_bit) {} Chris@16: Chris@16: unsigned first_bit() const { return _first_bit; } Chris@16: Chris@16: integer_t get() const { Chris@16: const BitField channel_mask = static_cast< integer_t >( parent_t::max_val ) <<_first_bit; Chris@16: return static_cast< integer_t >(( this->get_data()&channel_mask ) >> _first_bit ); Chris@16: } Chris@16: }; Chris@16: Chris@16: /// \brief Models a mutable subbyte channel reference whose bit offset is a runtime parameter. Models ChannelConcept Chris@16: /// Same as packed_channel_reference, except that the offset is a runtime parameter Chris@16: /// \ingroup PackedChannelDynamicReferenceModel Chris@16: template Chris@16: class packed_dynamic_channel_reference Chris@16: : public detail::packed_channel_reference_base,BitField,NumBits,true> { Chris@16: typedef detail::packed_channel_reference_base,BitField,NumBits,true> parent_t; Chris@16: friend class packed_dynamic_channel_reference; Chris@16: Chris@16: unsigned _first_bit; Chris@16: Chris@16: public: Chris@16: typedef const packed_dynamic_channel_reference const_reference; Chris@16: typedef const packed_dynamic_channel_reference mutable_reference; Chris@16: typedef typename parent_t::integer_t integer_t; Chris@16: Chris@16: packed_dynamic_channel_reference(void* data_ptr, unsigned first_bit) : parent_t(data_ptr), _first_bit(first_bit) {} Chris@16: packed_dynamic_channel_reference(const packed_dynamic_channel_reference& ref) : parent_t(ref._data_ptr), _first_bit(ref._first_bit) {} Chris@16: Chris@16: const packed_dynamic_channel_reference& operator=(integer_t value) const { assert(value<=parent_t::max_val); set_unsafe(value); return *this; } Chris@16: const packed_dynamic_channel_reference& operator=(const mutable_reference& ref) const { set_unsafe(ref.get()); return *this; } Chris@16: const packed_dynamic_channel_reference& operator=(const const_reference& ref) const { set_unsafe(ref.get()); return *this; } Chris@16: Chris@16: template Chris@16: const packed_dynamic_channel_reference& operator=(const packed_channel_reference& ref) const Chris@16: { set_unsafe(ref.get()); return *this; } Chris@16: Chris@16: unsigned first_bit() const { return _first_bit; } Chris@16: Chris@16: integer_t get() const { Chris@16: const BitField channel_mask = static_cast< integer_t >( parent_t::max_val ) << _first_bit; Chris@16: return static_cast< integer_t >(( this->get_data()&channel_mask ) >> _first_bit ); Chris@16: } Chris@16: Chris@16: void set_unsafe(integer_t value) const { Chris@16: const BitField channel_mask = static_cast< integer_t >( parent_t::max_val ) << _first_bit; Chris@16: this->set_data((this->get_data() & ~channel_mask) | value<<_first_bit); Chris@16: } Chris@16: }; Chris@16: } } // namespace boost::gil Chris@16: Chris@16: namespace std { Chris@16: // We are forced to define swap inside std namespace because on some platforms (Visual Studio 8) STL calls swap qualified. Chris@16: // swap with 'left bias': Chris@16: // - swap between proxy and anything Chris@16: // - swap between value type and proxy Chris@16: // - swap between proxy and proxy Chris@16: Chris@16: Chris@16: /// \ingroup PackedChannelDynamicReferenceModel Chris@16: /// \brief swap for packed_dynamic_channel_reference Chris@16: template inline Chris@16: void swap(const boost::gil::packed_dynamic_channel_reference x, R& y) { Chris@16: boost::gil::swap_proxy::value_type>(x,y); Chris@16: } Chris@16: Chris@16: Chris@16: /// \ingroup PackedChannelDynamicReferenceModel Chris@16: /// \brief swap for packed_dynamic_channel_reference Chris@16: template inline Chris@16: void swap(typename boost::gil::packed_dynamic_channel_reference::value_type& x, const boost::gil::packed_dynamic_channel_reference y) { Chris@16: boost::gil::swap_proxy::value_type>(x,y); Chris@16: } Chris@16: Chris@16: Chris@16: /// \ingroup PackedChannelDynamicReferenceModel Chris@16: /// \brief swap for packed_dynamic_channel_reference Chris@16: template inline Chris@16: void swap(const boost::gil::packed_dynamic_channel_reference x, const boost::gil::packed_dynamic_channel_reference y) { Chris@16: boost::gil::swap_proxy::value_type>(x,y); Chris@16: } Chris@16: } // namespace std Chris@16: Chris@16: namespace boost { namespace gil { Chris@16: /////////////////////////////////////////// Chris@16: //// Chris@16: //// Built-in channel models Chris@16: //// Chris@16: /////////////////////////////////////////// Chris@16: Chris@16: /// \defgroup bits8 bits8 Chris@16: /// \ingroup ChannelModel Chris@16: /// \brief 8-bit unsigned integral channel type (typedef from uint8_t). Models ChannelValueConcept Chris@16: Chris@16: /// \ingroup bits8 Chris@16: typedef uint8_t bits8; Chris@16: Chris@16: /// \defgroup bits16 bits16 Chris@16: /// \ingroup ChannelModel Chris@16: /// \brief 16-bit unsigned integral channel type (typedef from uint16_t). Models ChannelValueConcept Chris@16: Chris@16: /// \ingroup bits16 Chris@16: typedef uint16_t bits16; Chris@16: Chris@16: /// \defgroup bits32 bits32 Chris@16: /// \ingroup ChannelModel Chris@16: /// \brief 32-bit unsigned integral channel type (typedef from uint32_t). Models ChannelValueConcept Chris@16: Chris@16: /// \ingroup bits32 Chris@16: typedef uint32_t bits32; Chris@16: Chris@16: /// \defgroup bits8s bits8s Chris@16: /// \ingroup ChannelModel Chris@16: /// \brief 8-bit signed integral channel type (typedef from int8_t). Models ChannelValueConcept Chris@16: Chris@16: /// \ingroup bits8s Chris@16: typedef int8_t bits8s; Chris@16: Chris@16: /// \defgroup bits16s bits16s Chris@16: /// \ingroup ChannelModel Chris@16: /// \brief 16-bit signed integral channel type (typedef from int16_t). Models ChannelValueConcept Chris@16: Chris@16: /// \ingroup bits16s Chris@16: typedef int16_t bits16s; Chris@16: Chris@16: /// \defgroup bits32s bits32s Chris@16: /// \ingroup ChannelModel Chris@16: /// \brief 32-bit signed integral channel type (typedef from int32_t). Models ChannelValueConcept Chris@16: Chris@16: /// \ingroup bits32s Chris@16: typedef int32_t bits32s; Chris@16: Chris@16: /// \defgroup bits32f bits32f Chris@16: /// \ingroup ChannelModel Chris@16: /// \brief 32-bit floating point channel type with range [0.0f ... 1.0f]. Models ChannelValueConcept Chris@16: Chris@16: /// \ingroup bits32f Chris@16: typedef scoped_channel_value bits32f; Chris@16: Chris@16: } } // namespace boost::gil Chris@16: Chris@16: namespace boost { Chris@16: Chris@16: template Chris@16: struct is_integral > : public mpl::true_ {}; Chris@16: Chris@16: template Chris@16: struct is_integral > : public mpl::true_ {}; Chris@16: Chris@16: template Chris@16: struct is_integral > : public mpl::true_ {}; Chris@16: Chris@16: template Chris@16: struct is_integral > : public is_integral {}; Chris@16: Chris@16: } Chris@16: Chris@16: #endif