channel.hpp 29 KB

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