channel.hpp 28 KB

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