channel.hpp 30 KB

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