channel.hpp 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672
  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. template <int NumBits>
  179. struct num_value_fn : public mpl::if_c< ( NumBits < 32 )
  180. , uint32_t
  181. , uint64_t
  182. > {};
  183. template <int NumBits>
  184. struct max_value_fn : public mpl::if_c< ( NumBits <= 32 )
  185. , uint32_t
  186. , uint64_t
  187. > {};
  188. }
  189. /**
  190. \defgroup PackedChannelValueModel packed_channel_value
  191. \ingroup ChannelModel
  192. \brief Represents the value of an unsigned integral channel operating over a bit range. Models: ChannelValueConcept
  193. Example:
  194. \code
  195. // A 4-bit unsigned integral channel.
  196. typedef packed_channel_value<4> bits4;
  197. assert(channel_traits<bits4>::min_value()==0);
  198. assert(channel_traits<bits4>::max_value()==15);
  199. assert(sizeof(bits4)==1);
  200. BOOST_STATIC_ASSERT((boost::is_integral<bits4>::value));
  201. \endcode
  202. */
  203. /// \ingroup PackedChannelValueModel
  204. /// \brief The value of a subbyte channel. Models: ChannelValueConcept
  205. template <int NumBits>
  206. class packed_channel_value {
  207. typedef typename detail::num_value_fn< NumBits >::type num_value_t;
  208. static const num_value_t num_values = static_cast< num_value_t >( 1 ) << NumBits ;
  209. public:
  210. typedef typename detail::min_fast_uint<NumBits>::type integer_t;
  211. typedef packed_channel_value value_type;
  212. typedef value_type& reference;
  213. typedef const value_type& const_reference;
  214. typedef value_type* pointer;
  215. typedef const value_type* const_pointer;
  216. static value_type min_value() { return value_type(0); }
  217. static value_type max_value() { return value_type(num_values-1); }
  218. BOOST_STATIC_CONSTANT(bool, is_mutable=true);
  219. packed_channel_value() {}
  220. packed_channel_value(integer_t v) { _value = static_cast< integer_t >( v % num_values ); }
  221. packed_channel_value(const packed_channel_value& v) : _value(v._value) {}
  222. template <typename Scalar> packed_channel_value(Scalar v) { _value = static_cast< integer_t >( v ) % num_values; }
  223. static unsigned int num_bits() { return NumBits; }
  224. operator integer_t() const { return _value; }
  225. private:
  226. integer_t _value;
  227. };
  228. namespace detail {
  229. template <std::size_t K>
  230. struct static_copy_bytes {
  231. void operator()(const unsigned char* from, unsigned char* to) const {
  232. *to = *from;
  233. static_copy_bytes<K-1>()(++from,++to);
  234. }
  235. };
  236. template <>
  237. struct static_copy_bytes<0> {
  238. void operator()(const unsigned char* , unsigned char*) const {}
  239. };
  240. template <typename Derived, typename BitField, int NumBits, bool Mutable>
  241. class packed_channel_reference_base {
  242. protected:
  243. typedef typename mpl::if_c<Mutable,void*,const void*>::type data_ptr_t;
  244. public:
  245. data_ptr_t _data_ptr; // void* pointer to the first byte of the bit range
  246. typedef packed_channel_value<NumBits> value_type;
  247. typedef const Derived reference;
  248. typedef value_type* pointer;
  249. typedef const value_type* const_pointer;
  250. BOOST_STATIC_CONSTANT(int, num_bits=NumBits);
  251. BOOST_STATIC_CONSTANT(bool, is_mutable=Mutable);
  252. static value_type min_value() { return channel_traits<value_type>::min_value(); }
  253. static value_type max_value() { return channel_traits<value_type>::max_value(); }
  254. typedef BitField bitfield_t;
  255. typedef typename value_type::integer_t integer_t;
  256. packed_channel_reference_base(data_ptr_t data_ptr) : _data_ptr(data_ptr) {}
  257. packed_channel_reference_base(const packed_channel_reference_base& ref) : _data_ptr(ref._data_ptr) {}
  258. const Derived& operator=(integer_t v) const { set(v); return derived(); }
  259. const Derived& operator++() const { set(get()+1); return derived(); }
  260. const Derived& operator--() const { set(get()-1); return derived(); }
  261. Derived operator++(int) const { Derived tmp=derived(); this->operator++(); return tmp; }
  262. Derived operator--(int) const { Derived tmp=derived(); this->operator--(); return tmp; }
  263. template <typename Scalar2> const Derived& operator+=(Scalar2 v) const { set(get()+v); return derived(); }
  264. template <typename Scalar2> const Derived& operator-=(Scalar2 v) const { set(get()-v); return derived(); }
  265. template <typename Scalar2> const Derived& operator*=(Scalar2 v) const { set(get()*v); return derived(); }
  266. template <typename Scalar2> const Derived& operator/=(Scalar2 v) const { set(get()/v); return derived(); }
  267. operator integer_t() const { return get(); }
  268. data_ptr_t operator &() const {return _data_ptr;}
  269. protected:
  270. typedef typename detail::num_value_fn< NumBits >::type num_value_t;
  271. typedef typename detail::max_value_fn< NumBits >::type max_value_t;
  272. static const num_value_t num_values = static_cast< num_value_t >( 1 ) << NumBits ;
  273. static const max_value_t max_val = static_cast< max_value_t >( num_values - 1 );
  274. #ifdef GIL_NONWORD_POINTER_ALIGNMENT_SUPPORTED
  275. const bitfield_t& get_data() const { return *static_cast<const bitfield_t*>(_data_ptr); }
  276. void set_data(const bitfield_t& val) const { *static_cast< bitfield_t*>(_data_ptr) = val; }
  277. #else
  278. bitfield_t get_data() const {
  279. bitfield_t ret;
  280. static_copy_bytes<sizeof(bitfield_t) >()(gil_reinterpret_cast_c<const unsigned char*>(_data_ptr),gil_reinterpret_cast<unsigned char*>(&ret));
  281. return ret;
  282. }
  283. void set_data(const bitfield_t& val) const {
  284. static_copy_bytes<sizeof(bitfield_t) >()(gil_reinterpret_cast_c<const unsigned char*>(&val),gil_reinterpret_cast<unsigned char*>(_data_ptr));
  285. }
  286. #endif
  287. private:
  288. void set(integer_t value) const { // can this be done faster??
  289. const integer_t num_values = max_val+1;
  290. this->derived().set_unsafe(((value % num_values) + num_values) % num_values);
  291. }
  292. integer_t get() const { return derived().get(); }
  293. const Derived& derived() const { return static_cast<const Derived&>(*this); }
  294. };
  295. } // namespace detail
  296. /**
  297. \defgroup PackedChannelReferenceModel packed_channel_reference
  298. \ingroup ChannelModel
  299. \brief Represents a reference proxy to a channel operating over a bit range whose offset is fixed at compile time. Models ChannelConcept
  300. Example:
  301. \code
  302. // Reference to a 2-bit channel starting at bit 1 (i.e. the second bit)
  303. typedef const packed_channel_reference<uint16_t,1,2,true> bits2_1_ref_t;
  304. uint16_t data=0;
  305. bits2_1_ref_t channel_ref(&data);
  306. channel_ref = channel_traits<bits2_1_ref_t>::max_value(); // == 3
  307. assert(data == 6); // == 3<<1 == 6
  308. \endcode
  309. */
  310. 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
  311. int FirstBit, int NumBits,// Defines the sequence of bits in the data value that contain the channel
  312. bool Mutable> // true if the reference is mutable
  313. class packed_channel_reference;
  314. 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
  315. int NumBits, // Defines the sequence of bits in the data value that contain the channel
  316. bool Mutable> // true if the reference is mutable
  317. class packed_dynamic_channel_reference;
  318. /// \ingroup PackedChannelReferenceModel
  319. /// \brief A constant subbyte channel reference whose bit offset is fixed at compile time. Models ChannelConcept
  320. template <typename BitField, int FirstBit, int NumBits>
  321. class packed_channel_reference<BitField,FirstBit,NumBits,false>
  322. : public detail::packed_channel_reference_base<packed_channel_reference<BitField,FirstBit,NumBits,false>,BitField,NumBits,false> {
  323. typedef detail::packed_channel_reference_base<packed_channel_reference<BitField,FirstBit,NumBits,false>,BitField,NumBits,false> parent_t;
  324. friend class packed_channel_reference<BitField,FirstBit,NumBits,true>;
  325. static const BitField channel_mask = static_cast< BitField >( parent_t::max_val ) << FirstBit;
  326. void operator=(const packed_channel_reference&);
  327. public:
  328. typedef const packed_channel_reference<BitField,FirstBit,NumBits,false> const_reference;
  329. typedef const packed_channel_reference<BitField,FirstBit,NumBits,true> mutable_reference;
  330. typedef typename parent_t::integer_t integer_t;
  331. explicit packed_channel_reference(const void* data_ptr) : parent_t(data_ptr) {}
  332. packed_channel_reference(const packed_channel_reference& ref) : parent_t(ref._data_ptr) {}
  333. packed_channel_reference(const mutable_reference& ref) : parent_t(ref._data_ptr) {}
  334. unsigned first_bit() const { return FirstBit; }
  335. integer_t get() const { return integer_t((this->get_data()&channel_mask) >> FirstBit); }
  336. };
  337. /// \ingroup PackedChannelReferenceModel
  338. /// \brief A mutable subbyte channel reference whose bit offset is fixed at compile time. Models ChannelConcept
  339. template <typename BitField, int FirstBit, int NumBits>
  340. class packed_channel_reference<BitField,FirstBit,NumBits,true>
  341. : public detail::packed_channel_reference_base<packed_channel_reference<BitField,FirstBit,NumBits,true>,BitField,NumBits,true> {
  342. typedef detail::packed_channel_reference_base<packed_channel_reference<BitField,FirstBit,NumBits,true>,BitField,NumBits,true> parent_t;
  343. friend class packed_channel_reference<BitField,FirstBit,NumBits,false>;
  344. static const BitField channel_mask = static_cast< BitField >( parent_t::max_val ) << FirstBit;
  345. public:
  346. typedef const packed_channel_reference<BitField,FirstBit,NumBits,false> const_reference;
  347. typedef const packed_channel_reference<BitField,FirstBit,NumBits,true> mutable_reference;
  348. typedef typename parent_t::integer_t integer_t;
  349. explicit packed_channel_reference(void* data_ptr) : parent_t(data_ptr) {}
  350. packed_channel_reference(const packed_channel_reference& ref) : parent_t(ref._data_ptr) {}
  351. const packed_channel_reference& operator=(integer_t value) const { assert(value<=parent_t::max_val); set_unsafe(value); return *this; }
  352. const packed_channel_reference& operator=(const mutable_reference& ref) const { set_from_reference(ref.get_data()); return *this; }
  353. const packed_channel_reference& operator=(const const_reference& ref) const { set_from_reference(ref.get_data()); return *this; }
  354. template <bool Mutable1>
  355. const packed_channel_reference& operator=(const packed_dynamic_channel_reference<BitField,NumBits,Mutable1>& ref) const { set_unsafe(ref.get()); return *this; }
  356. unsigned first_bit() const { return FirstBit; }
  357. integer_t get() const { return integer_t((this->get_data()&channel_mask) >> FirstBit); }
  358. void set_unsafe(integer_t value) const { this->set_data((this->get_data() & ~channel_mask) | (( static_cast< BitField >( value )<<FirstBit))); }
  359. private:
  360. void set_from_reference(const BitField& other_bits) const { this->set_data((this->get_data() & ~channel_mask) | (other_bits & channel_mask)); }
  361. };
  362. } } // namespace boost::gil
  363. namespace std {
  364. // We are forced to define swap inside std namespace because on some platforms (Visual Studio 8) STL calls swap qualified.
  365. // swap with 'left bias':
  366. // - swap between proxy and anything
  367. // - swap between value type and proxy
  368. // - swap between proxy and proxy
  369. /// \ingroup PackedChannelReferenceModel
  370. /// \brief swap for packed_channel_reference
  371. template <typename BF, int FB, int NB, bool M, typename R> inline
  372. void swap(const boost::gil::packed_channel_reference<BF,FB,NB,M> x, R& y) {
  373. boost::gil::swap_proxy<typename boost::gil::packed_channel_reference<BF,FB,NB,M>::value_type>(x,y);
  374. }
  375. /// \ingroup PackedChannelReferenceModel
  376. /// \brief swap for packed_channel_reference
  377. template <typename BF, int FB, int NB, bool M> inline
  378. 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) {
  379. boost::gil::swap_proxy<typename boost::gil::packed_channel_reference<BF,FB,NB,M>::value_type>(x,y);
  380. }
  381. /// \ingroup PackedChannelReferenceModel
  382. /// \brief swap for packed_channel_reference
  383. template <typename BF, int FB, int NB, bool M> inline
  384. void swap(const boost::gil::packed_channel_reference<BF,FB,NB,M> x, const boost::gil::packed_channel_reference<BF,FB,NB,M> y) {
  385. boost::gil::swap_proxy<typename boost::gil::packed_channel_reference<BF,FB,NB,M>::value_type>(x,y);
  386. }
  387. } // namespace std
  388. namespace boost { namespace gil {
  389. /**
  390. \defgroup PackedChannelDynamicReferenceModel packed_dynamic_channel_reference
  391. \ingroup ChannelModel
  392. \brief Represents a reference proxy to a channel operating over a bit range whose offset is specified at run time. Models ChannelConcept
  393. Example:
  394. \code
  395. // Reference to a 2-bit channel whose offset is specified at construction time
  396. typedef const packed_dynamic_channel_reference<uint8_t,2,true> bits2_dynamic_ref_t;
  397. uint16_t data=0;
  398. bits2_dynamic_ref_t channel_ref(&data,1);
  399. channel_ref = channel_traits<bits2_dynamic_ref_t>::max_value(); // == 3
  400. assert(data == 6); // == (3<<1) == 6
  401. \endcode
  402. */
  403. /// \brief Models a constant subbyte channel reference whose bit offset is a runtime parameter. Models ChannelConcept
  404. /// Same as packed_channel_reference, except that the offset is a runtime parameter
  405. /// \ingroup PackedChannelDynamicReferenceModel
  406. template <typename BitField, int NumBits>
  407. class packed_dynamic_channel_reference<BitField,NumBits,false>
  408. : public detail::packed_channel_reference_base<packed_dynamic_channel_reference<BitField,NumBits,false>,BitField,NumBits,false> {
  409. typedef detail::packed_channel_reference_base<packed_dynamic_channel_reference<BitField,NumBits,false>,BitField,NumBits,false> parent_t;
  410. friend class packed_dynamic_channel_reference<BitField,NumBits,true>;
  411. unsigned _first_bit; // 0..7
  412. void operator=(const packed_dynamic_channel_reference&);
  413. public:
  414. typedef const packed_dynamic_channel_reference<BitField,NumBits,false> const_reference;
  415. typedef const packed_dynamic_channel_reference<BitField,NumBits,true> mutable_reference;
  416. typedef typename parent_t::integer_t integer_t;
  417. packed_dynamic_channel_reference(const void* data_ptr, unsigned first_bit) : parent_t(data_ptr), _first_bit(first_bit) {}
  418. packed_dynamic_channel_reference(const const_reference& ref) : parent_t(ref._data_ptr), _first_bit(ref._first_bit) {}
  419. packed_dynamic_channel_reference(const mutable_reference& ref) : parent_t(ref._data_ptr), _first_bit(ref._first_bit) {}
  420. unsigned first_bit() const { return _first_bit; }
  421. integer_t get() const {
  422. const BitField channel_mask = static_cast< integer_t >( parent_t::max_val ) <<_first_bit;
  423. return static_cast< integer_t >(( this->get_data()&channel_mask ) >> _first_bit );
  424. }
  425. };
  426. /// \brief Models a mutable subbyte channel reference whose bit offset is a runtime parameter. Models ChannelConcept
  427. /// Same as packed_channel_reference, except that the offset is a runtime parameter
  428. /// \ingroup PackedChannelDynamicReferenceModel
  429. template <typename BitField, int NumBits>
  430. class packed_dynamic_channel_reference<BitField,NumBits,true>
  431. : public detail::packed_channel_reference_base<packed_dynamic_channel_reference<BitField,NumBits,true>,BitField,NumBits,true> {
  432. typedef detail::packed_channel_reference_base<packed_dynamic_channel_reference<BitField,NumBits,true>,BitField,NumBits,true> parent_t;
  433. friend class packed_dynamic_channel_reference<BitField,NumBits,false>;
  434. unsigned _first_bit;
  435. public:
  436. typedef const packed_dynamic_channel_reference<BitField,NumBits,false> const_reference;
  437. typedef const packed_dynamic_channel_reference<BitField,NumBits,true> mutable_reference;
  438. typedef typename parent_t::integer_t integer_t;
  439. packed_dynamic_channel_reference(void* data_ptr, unsigned first_bit) : parent_t(data_ptr), _first_bit(first_bit) {}
  440. packed_dynamic_channel_reference(const packed_dynamic_channel_reference& ref) : parent_t(ref._data_ptr), _first_bit(ref._first_bit) {}
  441. const packed_dynamic_channel_reference& operator=(integer_t value) const { assert(value<=parent_t::max_val); set_unsafe(value); return *this; }
  442. const packed_dynamic_channel_reference& operator=(const mutable_reference& ref) const { set_unsafe(ref.get()); return *this; }
  443. const packed_dynamic_channel_reference& operator=(const const_reference& ref) const { set_unsafe(ref.get()); return *this; }
  444. template <typename BitField1, int FirstBit1, bool Mutable1>
  445. const packed_dynamic_channel_reference& operator=(const packed_channel_reference<BitField1, FirstBit1, NumBits, Mutable1>& ref) const
  446. { set_unsafe(ref.get()); return *this; }
  447. unsigned first_bit() const { return _first_bit; }
  448. integer_t get() const {
  449. const BitField channel_mask = static_cast< integer_t >( parent_t::max_val ) << _first_bit;
  450. return static_cast< integer_t >(( this->get_data()&channel_mask ) >> _first_bit );
  451. }
  452. void set_unsafe(integer_t value) const {
  453. const BitField channel_mask = static_cast< integer_t >( parent_t::max_val ) << _first_bit;
  454. this->set_data((this->get_data() & ~channel_mask) | value<<_first_bit);
  455. }
  456. };
  457. } } // namespace boost::gil
  458. namespace std {
  459. // We are forced to define swap inside std namespace because on some platforms (Visual Studio 8) STL calls swap qualified.
  460. // swap with 'left bias':
  461. // - swap between proxy and anything
  462. // - swap between value type and proxy
  463. // - swap between proxy and proxy
  464. /// \ingroup PackedChannelDynamicReferenceModel
  465. /// \brief swap for packed_dynamic_channel_reference
  466. template <typename BF, int NB, bool M, typename R> inline
  467. void swap(const boost::gil::packed_dynamic_channel_reference<BF,NB,M> x, R& y) {
  468. boost::gil::swap_proxy<typename boost::gil::packed_dynamic_channel_reference<BF,NB,M>::value_type>(x,y);
  469. }
  470. /// \ingroup PackedChannelDynamicReferenceModel
  471. /// \brief swap for packed_dynamic_channel_reference
  472. template <typename BF, int NB, bool M> inline
  473. 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) {
  474. boost::gil::swap_proxy<typename boost::gil::packed_dynamic_channel_reference<BF,NB,M>::value_type>(x,y);
  475. }
  476. /// \ingroup PackedChannelDynamicReferenceModel
  477. /// \brief swap for packed_dynamic_channel_reference
  478. template <typename BF, int NB, bool M> inline
  479. void swap(const boost::gil::packed_dynamic_channel_reference<BF,NB,M> x, const boost::gil::packed_dynamic_channel_reference<BF,NB,M> y) {
  480. boost::gil::swap_proxy<typename boost::gil::packed_dynamic_channel_reference<BF,NB,M>::value_type>(x,y);
  481. }
  482. } // namespace std
  483. namespace boost { namespace gil {
  484. ///////////////////////////////////////////
  485. ////
  486. //// Built-in channel models
  487. ////
  488. ///////////////////////////////////////////
  489. /// \defgroup bits8 bits8
  490. /// \ingroup ChannelModel
  491. /// \brief 8-bit unsigned integral channel type (typedef from uint8_t). Models ChannelValueConcept
  492. /// \ingroup bits8
  493. typedef uint8_t bits8;
  494. /// \defgroup bits16 bits16
  495. /// \ingroup ChannelModel
  496. /// \brief 16-bit unsigned integral channel type (typedef from uint16_t). Models ChannelValueConcept
  497. /// \ingroup bits16
  498. typedef uint16_t bits16;
  499. /// \defgroup bits32 bits32
  500. /// \ingroup ChannelModel
  501. /// \brief 32-bit unsigned integral channel type (typedef from uint32_t). Models ChannelValueConcept
  502. /// \ingroup bits32
  503. typedef uint32_t bits32;
  504. /// \defgroup bits8s bits8s
  505. /// \ingroup ChannelModel
  506. /// \brief 8-bit signed integral channel type (typedef from int8_t). Models ChannelValueConcept
  507. /// \ingroup bits8s
  508. typedef int8_t bits8s;
  509. /// \defgroup bits16s bits16s
  510. /// \ingroup ChannelModel
  511. /// \brief 16-bit signed integral channel type (typedef from int16_t). Models ChannelValueConcept
  512. /// \ingroup bits16s
  513. typedef int16_t bits16s;
  514. /// \defgroup bits32s bits32s
  515. /// \ingroup ChannelModel
  516. /// \brief 32-bit signed integral channel type (typedef from int32_t). Models ChannelValueConcept
  517. /// \ingroup bits32s
  518. typedef int32_t bits32s;
  519. /// \defgroup bits32f bits32f
  520. /// \ingroup ChannelModel
  521. /// \brief 32-bit floating point channel type with range [0.0f ... 1.0f]. Models ChannelValueConcept
  522. /// \ingroup bits32f
  523. typedef scoped_channel_value<float,float_zero,float_one> bits32f;
  524. } } // namespace boost::gil
  525. namespace boost {
  526. template <int NumBits>
  527. struct is_integral<gil::packed_channel_value<NumBits> > : public mpl::true_ {};
  528. template <typename BitField, int FirstBit, int NumBits, bool IsMutable>
  529. struct is_integral<gil::packed_channel_reference<BitField,FirstBit,NumBits,IsMutable> > : public mpl::true_ {};
  530. template <typename BitField, int NumBits, bool IsMutable>
  531. struct is_integral<gil::packed_dynamic_channel_reference<BitField,NumBits,IsMutable> > : public mpl::true_ {};
  532. template <typename BaseChannelValue, typename MinVal, typename MaxVal>
  533. struct is_integral<gil::scoped_channel_value<BaseChannelValue,MinVal,MaxVal> > : public is_integral<BaseChannelValue> {};
  534. }
  535. #endif