channel.hpp 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723
  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 <cstdint>
  16. #include <limits>
  17. #include <type_traits>
  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 IsClass>
  65. struct channel_traits_impl;
  66. // channel traits for custom class
  67. template <typename T>
  68. struct channel_traits_impl<T, true>
  69. {
  70. using value_type = typename T::value_type;
  71. using reference = typename T::reference;
  72. using pointer = typename T::pointer;
  73. using const_reference = typename T::const_reference;
  74. using const_pointer = typename T::const_pointer;
  75. static constexpr bool is_mutable = T::is_mutable;
  76. static value_type min_value() { return T::min_value(); }
  77. static value_type max_value() { return T::max_value(); }
  78. };
  79. // channel traits implementation for built-in integral or floating point channel type
  80. template <typename T>
  81. struct channel_traits_impl<T, false>
  82. {
  83. using value_type = T;
  84. using reference = T&;
  85. using pointer = T*;
  86. using const_reference = T const&;
  87. using const_pointer = T const*;
  88. static constexpr bool is_mutable = true;
  89. static value_type min_value() { return (std::numeric_limits<T>::min)(); }
  90. static value_type max_value() { return (std::numeric_limits<T>::max)(); }
  91. };
  92. // channel traits implementation for constant built-in scalar or floating point type
  93. template <typename T>
  94. struct channel_traits_impl<T const, false> : channel_traits_impl<T, false>
  95. {
  96. using reference = T const&;
  97. using pointer = T const*;
  98. static constexpr bool is_mutable = false;
  99. };
  100. } // namespace detail
  101. /**
  102. \ingroup ChannelModel
  103. \brief Traits for channels. Contains the following members:
  104. \code
  105. template <typename Channel>
  106. struct channel_traits {
  107. using value_type = ...;
  108. using reference = ...;
  109. using pointer = ...;
  110. using const_reference = ...;
  111. using const_pointer = ...;
  112. static const bool is_mutable;
  113. static value_type min_value();
  114. static value_type max_value();
  115. };
  116. \endcode
  117. */
  118. template <typename T>
  119. struct channel_traits : detail::channel_traits_impl<T, std::is_class<T>::value> {};
  120. // Channel traits for C++ reference type - remove the reference
  121. template <typename T>
  122. struct channel_traits<T&> : channel_traits<T> {};
  123. // Channel traits for constant C++ reference type
  124. template <typename T>
  125. struct channel_traits<T const&> : channel_traits<T>
  126. {
  127. using reference = typename channel_traits<T>::const_reference;
  128. using pointer = typename channel_traits<T>::const_pointer;
  129. static constexpr bool is_mutable = false;
  130. };
  131. ///////////////////////////////////////////
  132. //// scoped_channel_value
  133. ///////////////////////////////////////////
  134. /// \defgroup ScopedChannelValue scoped_channel_value
  135. /// \ingroup ChannelModel
  136. /// \brief A channel adaptor that modifies the range of the source channel. Models: ChannelValueConcept
  137. ///
  138. /// Example:
  139. /// \code
  140. /// // Create a double channel with range [-0.5 .. 0.5]
  141. /// struct double_minus_half { static double apply() { return -0.5; } };
  142. /// struct double_plus_half { static double apply() { return 0.5; } };
  143. /// using bits64custom_t = scoped_channel_value<double, double_minus_half, double_plus_half>;
  144. ///
  145. /// // channel_convert its maximum should map to the maximum
  146. /// bits64custom_t x = channel_traits<bits64custom_t>::max_value();
  147. /// assert(x == 0.5);
  148. /// uint16_t y = channel_convert<uint16_t>(x);
  149. /// assert(y == 65535);
  150. /// \endcode
  151. /// \ingroup ScopedChannelValue
  152. /// \brief A channel adaptor that modifies the range of the source channel. Models: ChannelValueConcept
  153. /// \tparam BaseChannelValue base channel (models ChannelValueConcept)
  154. /// \tparam MinVal class with a static apply() function returning the minimum channel values
  155. /// \tparam MaxVal class with a static apply() function returning the maximum channel values
  156. template <typename BaseChannelValue, typename MinVal, typename MaxVal>
  157. struct scoped_channel_value
  158. {
  159. using value_type = scoped_channel_value<BaseChannelValue, MinVal, MaxVal>;
  160. using reference = value_type&;
  161. using pointer = value_type*;
  162. using const_reference = value_type const&;
  163. using const_pointer = value_type const*;
  164. static constexpr bool is_mutable = channel_traits<BaseChannelValue>::is_mutable;
  165. using base_channel_t = BaseChannelValue;
  166. static value_type min_value() { return MinVal::apply(); }
  167. static value_type max_value() { return MaxVal::apply(); }
  168. scoped_channel_value() = default;
  169. scoped_channel_value(scoped_channel_value const& other) : value_(other.value_) {}
  170. scoped_channel_value& operator=(scoped_channel_value const& other) = default;
  171. scoped_channel_value(BaseChannelValue value) : value_(value) {}
  172. scoped_channel_value& operator=(BaseChannelValue value)
  173. {
  174. value_ = value;
  175. return *this;
  176. }
  177. scoped_channel_value& operator++() { ++value_; return *this; }
  178. scoped_channel_value& operator--() { --value_; return *this; }
  179. scoped_channel_value operator++(int) { scoped_channel_value tmp=*this; this->operator++(); return tmp; }
  180. scoped_channel_value operator--(int) { scoped_channel_value tmp=*this; this->operator--(); return tmp; }
  181. template <typename Scalar2> scoped_channel_value& operator+=(Scalar2 v) { value_+=v; return *this; }
  182. template <typename Scalar2> scoped_channel_value& operator-=(Scalar2 v) { value_-=v; return *this; }
  183. template <typename Scalar2> scoped_channel_value& operator*=(Scalar2 v) { value_*=v; return *this; }
  184. template <typename Scalar2> scoped_channel_value& operator/=(Scalar2 v) { value_/=v; return *this; }
  185. operator BaseChannelValue() const { return value_; }
  186. private:
  187. BaseChannelValue value_{};
  188. };
  189. template <typename T>
  190. struct float_point_zero
  191. {
  192. static constexpr T apply() { return 0.0f; }
  193. };
  194. template <typename T>
  195. struct float_point_one
  196. {
  197. static constexpr T apply() { return 1.0f; }
  198. };
  199. ///////////////////////////////////////////
  200. //// Support for sub-byte channels. These are integral channels whose value is contained in a range of bits inside an integral type
  201. ///////////////////////////////////////////
  202. // 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:
  203. // - Any operation that requires returning the result by value will otherwise return the built-in integral type, which will have incorrect range
  204. // That means that after getting the value of the channel we cannot properly do channel_convert, channel_invert, etc.
  205. // - 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
  206. namespace detail {
  207. // returns the smallest fast unsigned integral type that has at least NumBits bits
  208. template <int NumBits>
  209. struct min_fast_uint :
  210. std::conditional
  211. <
  212. NumBits <= 8,
  213. std::uint_least8_t,
  214. typename std::conditional
  215. <
  216. NumBits <= 16,
  217. std::uint_least16_t,
  218. typename std::conditional
  219. <
  220. NumBits <= 32,
  221. std::uint_least32_t,
  222. std::uintmax_t
  223. >::type
  224. >::type
  225. >
  226. {};
  227. template <int NumBits>
  228. struct num_value_fn
  229. : std::conditional<NumBits < 32, std::uint32_t, std::uint64_t>
  230. {};
  231. template <int NumBits>
  232. struct max_value_fn
  233. : std::conditional<NumBits <= 32, std::uint32_t, std::uint64_t>
  234. {};
  235. } // namespace detail
  236. /// \defgroup PackedChannelValueModel packed_channel_value
  237. /// \ingroup ChannelModel
  238. /// \brief Represents the value of an unsigned integral channel operating over a bit range. Models: ChannelValueConcept
  239. /// Example:
  240. /// \code
  241. /// // A 4-bit unsigned integral channel.
  242. /// using bits4 = packed_channel_value<4>;
  243. ///
  244. /// assert(channel_traits<bits4>::min_value()==0);
  245. /// assert(channel_traits<bits4>::max_value()==15);
  246. /// assert(sizeof(bits4)==1);
  247. /// static_assert(gil::is_channel_integral<bits4>::value, "");
  248. /// \endcode
  249. /// \ingroup PackedChannelValueModel
  250. /// \brief The value of a subbyte channel. Models: ChannelValueConcept
  251. template <int NumBits>
  252. class packed_channel_value
  253. {
  254. public:
  255. using integer_t = typename detail::min_fast_uint<NumBits>::type;
  256. using value_type = packed_channel_value<NumBits>;
  257. using reference = value_type&;
  258. using const_reference = value_type const&;
  259. using pointer = value_type*;
  260. using const_pointer = value_type const*;
  261. static constexpr bool is_mutable = true;
  262. static value_type min_value() { return 0; }
  263. static value_type max_value() { return low_bits_mask_t< NumBits >::sig_bits; }
  264. packed_channel_value() = default;
  265. packed_channel_value(integer_t v)
  266. {
  267. value_ = static_cast<integer_t>(v & low_bits_mask_t<NumBits>::sig_bits_fast);
  268. }
  269. template <typename Scalar>
  270. packed_channel_value(Scalar v)
  271. {
  272. value_ = packed_channel_value(static_cast<integer_t>(v));
  273. }
  274. static unsigned int num_bits() { return NumBits; }
  275. operator integer_t() const { return value_; }
  276. private:
  277. integer_t value_{};
  278. };
  279. namespace detail {
  280. template <std::size_t K>
  281. struct static_copy_bytes
  282. {
  283. void operator()(unsigned char const* from, unsigned char* to) const
  284. {
  285. *to = *from;
  286. static_copy_bytes<K - 1>()(++from, ++to);
  287. }
  288. };
  289. template <>
  290. struct static_copy_bytes<0>
  291. {
  292. void operator()(unsigned char const*, unsigned char*) const {}
  293. };
  294. template <typename Derived, typename BitField, int NumBits, bool IsMutable>
  295. class packed_channel_reference_base
  296. {
  297. protected:
  298. using data_ptr_t = typename std::conditional<IsMutable, void*, void const*>::type;
  299. public:
  300. data_ptr_t _data_ptr; // void* pointer to the first byte of the bit range
  301. using value_type = packed_channel_value<NumBits>;
  302. using reference = const Derived;
  303. using pointer = value_type *;
  304. using const_pointer = const value_type *;
  305. static constexpr int num_bits = NumBits;
  306. static constexpr bool is_mutable = IsMutable;
  307. static value_type min_value() { return channel_traits<value_type>::min_value(); }
  308. static value_type max_value() { return channel_traits<value_type>::max_value(); }
  309. using bitfield_t = BitField;
  310. using integer_t = typename value_type::integer_t;
  311. packed_channel_reference_base(data_ptr_t data_ptr) : _data_ptr(data_ptr) {}
  312. packed_channel_reference_base(const packed_channel_reference_base& ref) : _data_ptr(ref._data_ptr) {}
  313. const Derived& operator=(integer_t v) const { set(v); return derived(); }
  314. const Derived& operator++() const { set(get()+1); return derived(); }
  315. const Derived& operator--() const { set(get()-1); return derived(); }
  316. Derived operator++(int) const { Derived tmp=derived(); this->operator++(); return tmp; }
  317. Derived operator--(int) const { Derived tmp=derived(); this->operator--(); return tmp; }
  318. template <typename Scalar2> const Derived& operator+=(Scalar2 v) const { set( static_cast<integer_t>( get() + v )); return derived(); }
  319. template <typename Scalar2> const Derived& operator-=(Scalar2 v) const { set( static_cast<integer_t>( get() - v )); return derived(); }
  320. template <typename Scalar2> const Derived& operator*=(Scalar2 v) const { set( static_cast<integer_t>( get() * v )); return derived(); }
  321. template <typename Scalar2> const Derived& operator/=(Scalar2 v) const { set( static_cast<integer_t>( get() / v )); return derived(); }
  322. operator integer_t() const { return get(); }
  323. data_ptr_t operator &() const {return _data_ptr;}
  324. protected:
  325. using num_value_t = typename detail::num_value_fn<NumBits>::type;
  326. using max_value_t = typename detail::max_value_fn<NumBits>::type;
  327. static const num_value_t num_values = static_cast< num_value_t >( 1 ) << NumBits ;
  328. static const max_value_t max_val = static_cast< max_value_t >( num_values - 1 );
  329. #if defined(BOOST_GIL_CONFIG_HAS_UNALIGNED_ACCESS)
  330. const bitfield_t& get_data() const { return *static_cast<const bitfield_t*>(_data_ptr); }
  331. void set_data(const bitfield_t& val) const { *static_cast< bitfield_t*>(_data_ptr) = val; }
  332. #else
  333. bitfield_t get_data() const {
  334. bitfield_t ret;
  335. static_copy_bytes<sizeof(bitfield_t) >()(gil_reinterpret_cast_c<const unsigned char*>(_data_ptr),gil_reinterpret_cast<unsigned char*>(&ret));
  336. return ret;
  337. }
  338. void set_data(const bitfield_t& val) const {
  339. static_copy_bytes<sizeof(bitfield_t) >()(gil_reinterpret_cast_c<const unsigned char*>(&val),gil_reinterpret_cast<unsigned char*>(_data_ptr));
  340. }
  341. #endif
  342. private:
  343. void set(integer_t value) const { // can this be done faster??
  344. this->derived().set_unsafe(((value % num_values) + num_values) % num_values);
  345. }
  346. integer_t get() const { return derived().get(); }
  347. const Derived& derived() const { return static_cast<const Derived&>(*this); }
  348. };
  349. } // namespace detail
  350. /// \defgroup PackedChannelReferenceModel packed_channel_reference
  351. /// \ingroup ChannelModel
  352. /// \brief Represents a reference proxy to a channel operating over a bit range whose offset is fixed at compile time. Models ChannelConcept
  353. /// Example:
  354. /// \code
  355. /// // Reference to a 2-bit channel starting at bit 1 (i.e. the second bit)
  356. /// using bits2_1_ref_t = packed_channel_reference<uint16_t,1,2,true> const;
  357. ///
  358. /// uint16_t data=0;
  359. /// bits2_1_ref_t channel_ref(&data);
  360. /// channel_ref = channel_traits<bits2_1_ref_t>::max_value(); // == 3
  361. /// assert(data == 6); // == 3<<1 == 6
  362. /// \endcode
  363. /// \tparam BitField A type that holds the bits of the pixel from which the channel is referenced. Typically an integral type, like std::uint16_t
  364. /// \tparam Defines the sequence of bits in the data value that contain the channel
  365. /// \tparam true if the reference is mutable
  366. template <typename BitField, int FirstBit, int NumBits, bool IsMutable>
  367. class packed_channel_reference;
  368. /// \tparam A type that holds the bits of the pixel from which the channel is referenced. Typically an integral type, like std::uint16_t
  369. /// \tparam Defines the sequence of bits in the data value that contain the channel
  370. /// \tparam true if the reference is mutable
  371. template <typename BitField, int NumBits, bool IsMutable>
  372. class packed_dynamic_channel_reference;
  373. /// \ingroup PackedChannelReferenceModel
  374. /// \brief A constant subbyte channel reference whose bit offset is fixed at compile time. Models ChannelConcept
  375. template <typename BitField, int FirstBit, int NumBits>
  376. class packed_channel_reference<BitField, FirstBit, NumBits, false>
  377. : public detail::packed_channel_reference_base
  378. <
  379. packed_channel_reference<BitField, FirstBit, NumBits, false>,
  380. BitField,
  381. NumBits,
  382. false
  383. >
  384. {
  385. using parent_t = detail::packed_channel_reference_base
  386. <
  387. packed_channel_reference<BitField, FirstBit, NumBits, false>,
  388. BitField,
  389. NumBits,
  390. false
  391. >;
  392. friend class packed_channel_reference<BitField, FirstBit, NumBits, true>;
  393. static const BitField channel_mask = static_cast<BitField>(parent_t::max_val) << FirstBit;
  394. void operator=(packed_channel_reference const&);
  395. public:
  396. using const_reference = packed_channel_reference<BitField,FirstBit,NumBits,false> const;
  397. using mutable_reference = packed_channel_reference<BitField,FirstBit,NumBits,true> const;
  398. using integer_t = typename parent_t::integer_t;
  399. explicit packed_channel_reference(const void* data_ptr) : parent_t(data_ptr) {}
  400. packed_channel_reference(const packed_channel_reference& ref) : parent_t(ref._data_ptr) {}
  401. packed_channel_reference(const mutable_reference& ref) : parent_t(ref._data_ptr) {}
  402. unsigned first_bit() const { return FirstBit; }
  403. integer_t get() const { return integer_t((this->get_data()&channel_mask) >> FirstBit); }
  404. };
  405. /// \ingroup PackedChannelReferenceModel
  406. /// \brief A mutable subbyte channel reference whose bit offset is fixed at compile time. Models ChannelConcept
  407. template <typename BitField, int FirstBit, int NumBits>
  408. class packed_channel_reference<BitField,FirstBit,NumBits,true>
  409. : public detail::packed_channel_reference_base<packed_channel_reference<BitField,FirstBit,NumBits,true>,BitField,NumBits,true>
  410. {
  411. using parent_t = detail::packed_channel_reference_base<packed_channel_reference<BitField,FirstBit,NumBits,true>,BitField,NumBits,true>;
  412. friend class packed_channel_reference<BitField,FirstBit,NumBits,false>;
  413. static const BitField channel_mask = static_cast< BitField >( parent_t::max_val ) << FirstBit;
  414. public:
  415. using const_reference = packed_channel_reference<BitField,FirstBit,NumBits,false> const;
  416. using mutable_reference = packed_channel_reference<BitField,FirstBit,NumBits,true> const;
  417. using integer_t = typename parent_t::integer_t;
  418. explicit packed_channel_reference(void* data_ptr) : parent_t(data_ptr) {}
  419. packed_channel_reference(const packed_channel_reference& ref) : parent_t(ref._data_ptr) {}
  420. packed_channel_reference const& operator=(integer_t value) const
  421. {
  422. BOOST_ASSERT(value <= parent_t::max_val);
  423. set_unsafe(value);
  424. return *this;
  425. }
  426. const packed_channel_reference& operator=(const mutable_reference& ref) const { set_from_reference(ref.get_data()); return *this; }
  427. const packed_channel_reference& operator=(const const_reference& ref) const { set_from_reference(ref.get_data()); return *this; }
  428. template <bool Mutable1>
  429. const packed_channel_reference& operator=(const packed_dynamic_channel_reference<BitField,NumBits,Mutable1>& ref) const { set_unsafe(ref.get()); return *this; }
  430. unsigned first_bit() const { return FirstBit; }
  431. integer_t get() const { return integer_t((this->get_data()&channel_mask) >> FirstBit); }
  432. void set_unsafe(integer_t value) const { this->set_data((this->get_data() & ~channel_mask) | (( static_cast< BitField >( value )<<FirstBit))); }
  433. private:
  434. void set_from_reference(const BitField& other_bits) const { this->set_data((this->get_data() & ~channel_mask) | (other_bits & channel_mask)); }
  435. };
  436. }} // namespace boost::gil
  437. namespace std {
  438. // We are forced to define swap inside std namespace because on some platforms (Visual Studio 8) STL calls swap qualified.
  439. // swap with 'left bias':
  440. // - swap between proxy and anything
  441. // - swap between value type and proxy
  442. // - swap between proxy and proxy
  443. /// \ingroup PackedChannelReferenceModel
  444. /// \brief swap for packed_channel_reference
  445. template <typename BF, int FB, int NB, bool M, typename R>
  446. inline
  447. void swap(boost::gil::packed_channel_reference<BF, FB, NB, M> const x, R& y)
  448. {
  449. boost::gil::swap_proxy
  450. <
  451. typename boost::gil::packed_channel_reference<BF, FB, NB, M>::value_type
  452. >(x, y);
  453. }
  454. /// \ingroup PackedChannelReferenceModel
  455. /// \brief swap for packed_channel_reference
  456. template <typename BF, int FB, int NB, bool M>
  457. inline
  458. void swap(
  459. typename boost::gil::packed_channel_reference<BF, FB, NB, M>::value_type& x,
  460. boost::gil::packed_channel_reference<BF, FB, NB, M> const y)
  461. {
  462. boost::gil::swap_proxy
  463. <
  464. typename boost::gil::packed_channel_reference<BF, FB, NB, M>::value_type
  465. >(x,y);
  466. }
  467. /// \ingroup PackedChannelReferenceModel
  468. /// \brief swap for packed_channel_reference
  469. template <typename BF, int FB, int NB, bool M> inline
  470. void swap(
  471. boost::gil::packed_channel_reference<BF, FB, NB, M> const x,
  472. boost::gil::packed_channel_reference<BF, FB, NB, M> const y)
  473. {
  474. boost::gil::swap_proxy
  475. <
  476. typename boost::gil::packed_channel_reference<BF, FB, NB, M>::value_type
  477. >(x,y);
  478. }
  479. } // namespace std
  480. namespace boost { namespace gil {
  481. /// \defgroup PackedChannelDynamicReferenceModel packed_dynamic_channel_reference
  482. /// \ingroup ChannelModel
  483. /// \brief Represents a reference proxy to a channel operating over a bit range whose offset is specified at run time. Models ChannelConcept
  484. ///
  485. /// Example:
  486. /// \code
  487. /// // Reference to a 2-bit channel whose offset is specified at construction time
  488. /// using bits2_dynamic_ref_t = packed_dynamic_channel_reference<uint8_t,2,true> const;
  489. ///
  490. /// uint16_t data=0;
  491. /// bits2_dynamic_ref_t channel_ref(&data,1);
  492. /// channel_ref = channel_traits<bits2_dynamic_ref_t>::max_value(); // == 3
  493. /// assert(data == 6); // == (3<<1) == 6
  494. /// \endcode
  495. /// \brief Models a constant subbyte channel reference whose bit offset is a runtime parameter. Models ChannelConcept
  496. /// Same as packed_channel_reference, except that the offset is a runtime parameter
  497. /// \ingroup PackedChannelDynamicReferenceModel
  498. template <typename BitField, int NumBits>
  499. class packed_dynamic_channel_reference<BitField,NumBits,false>
  500. : public detail::packed_channel_reference_base<packed_dynamic_channel_reference<BitField,NumBits,false>,BitField,NumBits,false>
  501. {
  502. using parent_t = detail::packed_channel_reference_base<packed_dynamic_channel_reference<BitField,NumBits,false>,BitField,NumBits,false>;
  503. friend class packed_dynamic_channel_reference<BitField,NumBits,true>;
  504. unsigned _first_bit; // 0..7
  505. void operator=(const packed_dynamic_channel_reference&);
  506. public:
  507. using const_reference = packed_dynamic_channel_reference<BitField,NumBits,false> const;
  508. using mutable_reference = packed_dynamic_channel_reference<BitField,NumBits,true> const;
  509. using integer_t = typename parent_t::integer_t;
  510. packed_dynamic_channel_reference(const void* data_ptr, unsigned first_bit) : parent_t(data_ptr), _first_bit(first_bit) {}
  511. packed_dynamic_channel_reference(const const_reference& ref) : parent_t(ref._data_ptr), _first_bit(ref._first_bit) {}
  512. packed_dynamic_channel_reference(const mutable_reference& ref) : parent_t(ref._data_ptr), _first_bit(ref._first_bit) {}
  513. unsigned first_bit() const { return _first_bit; }
  514. integer_t get() const {
  515. const BitField channel_mask = static_cast< integer_t >( parent_t::max_val ) <<_first_bit;
  516. return static_cast< integer_t >(( this->get_data()&channel_mask ) >> _first_bit );
  517. }
  518. };
  519. /// \brief Models a mutable subbyte channel reference whose bit offset is a runtime parameter. Models ChannelConcept
  520. /// Same as packed_channel_reference, except that the offset is a runtime parameter
  521. /// \ingroup PackedChannelDynamicReferenceModel
  522. template <typename BitField, int NumBits>
  523. class packed_dynamic_channel_reference<BitField,NumBits,true>
  524. : public detail::packed_channel_reference_base<packed_dynamic_channel_reference<BitField,NumBits,true>,BitField,NumBits,true>
  525. {
  526. using parent_t = detail::packed_channel_reference_base<packed_dynamic_channel_reference<BitField,NumBits,true>,BitField,NumBits,true>;
  527. friend class packed_dynamic_channel_reference<BitField,NumBits,false>;
  528. unsigned _first_bit;
  529. public:
  530. using const_reference = packed_dynamic_channel_reference<BitField,NumBits,false> const;
  531. using mutable_reference = packed_dynamic_channel_reference<BitField,NumBits,true> const;
  532. using integer_t = typename parent_t::integer_t;
  533. packed_dynamic_channel_reference(void* data_ptr, unsigned first_bit) : parent_t(data_ptr), _first_bit(first_bit) {}
  534. packed_dynamic_channel_reference(const packed_dynamic_channel_reference& ref) : parent_t(ref._data_ptr), _first_bit(ref._first_bit) {}
  535. packed_dynamic_channel_reference const& operator=(integer_t value) const
  536. {
  537. BOOST_ASSERT(value <= parent_t::max_val);
  538. set_unsafe(value);
  539. return *this;
  540. }
  541. const packed_dynamic_channel_reference& operator=(const mutable_reference& ref) const { set_unsafe(ref.get()); return *this; }
  542. const packed_dynamic_channel_reference& operator=(const const_reference& ref) const { set_unsafe(ref.get()); return *this; }
  543. template <typename BitField1, int FirstBit1, bool Mutable1>
  544. const packed_dynamic_channel_reference& operator=(const packed_channel_reference<BitField1, FirstBit1, NumBits, Mutable1>& ref) const
  545. { set_unsafe(ref.get()); return *this; }
  546. unsigned first_bit() const { return _first_bit; }
  547. integer_t get() const {
  548. const BitField channel_mask = static_cast< integer_t >( parent_t::max_val ) << _first_bit;
  549. return static_cast< integer_t >(( this->get_data()&channel_mask ) >> _first_bit );
  550. }
  551. void set_unsafe(integer_t value) const {
  552. const BitField channel_mask = static_cast< integer_t >( parent_t::max_val ) << _first_bit;
  553. this->set_data((this->get_data() & ~channel_mask) | value<<_first_bit);
  554. }
  555. };
  556. } } // namespace boost::gil
  557. namespace std {
  558. // We are forced to define swap inside std namespace because on some platforms (Visual Studio 8) STL calls swap qualified.
  559. // swap with 'left bias':
  560. // - swap between proxy and anything
  561. // - swap between value type and proxy
  562. // - swap between proxy and proxy
  563. /// \ingroup PackedChannelDynamicReferenceModel
  564. /// \brief swap for packed_dynamic_channel_reference
  565. template <typename BF, int NB, bool M, typename R> inline
  566. void swap(const boost::gil::packed_dynamic_channel_reference<BF,NB,M> x, R& y) {
  567. boost::gil::swap_proxy<typename boost::gil::packed_dynamic_channel_reference<BF,NB,M>::value_type>(x,y);
  568. }
  569. /// \ingroup PackedChannelDynamicReferenceModel
  570. /// \brief swap for packed_dynamic_channel_reference
  571. template <typename BF, int NB, bool M> inline
  572. 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) {
  573. boost::gil::swap_proxy<typename boost::gil::packed_dynamic_channel_reference<BF,NB,M>::value_type>(x,y);
  574. }
  575. /// \ingroup PackedChannelDynamicReferenceModel
  576. /// \brief swap for packed_dynamic_channel_reference
  577. template <typename BF, int NB, bool M> inline
  578. void swap(const boost::gil::packed_dynamic_channel_reference<BF,NB,M> x, const boost::gil::packed_dynamic_channel_reference<BF,NB,M> y) {
  579. boost::gil::swap_proxy<typename boost::gil::packed_dynamic_channel_reference<BF,NB,M>::value_type>(x,y);
  580. }
  581. } // namespace std
  582. // \brief Determines the fundamental type which may be used, e.g., to cast from larger to smaller channel types.
  583. namespace boost { namespace gil {
  584. template <typename T>
  585. struct base_channel_type_impl { using type = T; };
  586. template <int N>
  587. struct base_channel_type_impl<packed_channel_value<N> >
  588. { using type = typename packed_channel_value<N>::integer_t; };
  589. template <typename B, int F, int N, bool M>
  590. struct base_channel_type_impl<packed_channel_reference<B, F, N, M> >
  591. {
  592. using type = typename packed_channel_reference<B,F,N,M>::integer_t;
  593. };
  594. template <typename B, int N, bool M>
  595. struct base_channel_type_impl<packed_dynamic_channel_reference<B, N, M> >
  596. {
  597. using type = typename packed_dynamic_channel_reference<B,N,M>::integer_t;
  598. };
  599. template <typename ChannelValue, typename MinV, typename MaxV>
  600. struct base_channel_type_impl<scoped_channel_value<ChannelValue, MinV, MaxV> >
  601. { using type = ChannelValue; };
  602. template <typename T>
  603. struct base_channel_type : base_channel_type_impl<typename std::remove_cv<T>::type> {};
  604. }} //namespace boost::gil
  605. #endif