channel.hpp 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782
  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. auto operator++() -> scoped_channel_value& { ++value_; return *this; }
  178. auto operator--() -> scoped_channel_value& { --value_; return *this; }
  179. auto operator++(int) -> scoped_channel_value
  180. {
  181. scoped_channel_value tmp=*this;
  182. this->operator++(); return tmp;
  183. }
  184. auto operator--(int) -> scoped_channel_value
  185. {
  186. scoped_channel_value tmp=*this;
  187. this->operator--(); return tmp;
  188. }
  189. template <typename Scalar2>
  190. auto operator+=(Scalar2 v) -> scoped_channel_value& { value_+=v; return *this; }
  191. template <typename Scalar2>
  192. auto operator-=(Scalar2 v) -> scoped_channel_value& { value_-=v; return *this; }
  193. template <typename Scalar2>
  194. auto operator*=(Scalar2 v) -> scoped_channel_value& { value_*=v; return *this; }
  195. template <typename Scalar2>
  196. auto operator/=(Scalar2 v) -> scoped_channel_value& { value_/=v; return *this; }
  197. operator BaseChannelValue() const { return value_; }
  198. private:
  199. BaseChannelValue value_{};
  200. };
  201. template <typename T>
  202. struct float_point_zero
  203. {
  204. static constexpr T apply() { return 0.0f; }
  205. };
  206. template <typename T>
  207. struct float_point_one
  208. {
  209. static constexpr T apply() { return 1.0f; }
  210. };
  211. ///////////////////////////////////////////
  212. //// Support for sub-byte channels. These are integral channels whose value is contained in a range of bits inside an integral type
  213. ///////////////////////////////////////////
  214. // 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:
  215. // - Any operation that requires returning the result by value will otherwise return the built-in integral type, which will have incorrect range
  216. // That means that after getting the value of the channel we cannot properly do channel_convert, channel_invert, etc.
  217. // - 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
  218. namespace detail {
  219. // returns the smallest fast unsigned integral type that has at least NumBits bits
  220. template <int NumBits>
  221. struct min_fast_uint :
  222. std::conditional
  223. <
  224. NumBits <= 8,
  225. std::uint_least8_t,
  226. typename std::conditional
  227. <
  228. NumBits <= 16,
  229. std::uint_least16_t,
  230. typename std::conditional
  231. <
  232. NumBits <= 32,
  233. std::uint_least32_t,
  234. std::uintmax_t
  235. >::type
  236. >::type
  237. >
  238. {};
  239. template <int NumBits>
  240. struct num_value_fn
  241. : std::conditional<NumBits < 32, std::uint32_t, std::uint64_t>
  242. {};
  243. template <int NumBits>
  244. struct max_value_fn
  245. : std::conditional<NumBits <= 32, std::uint32_t, std::uint64_t>
  246. {};
  247. } // namespace detail
  248. /// \defgroup PackedChannelValueModel packed_channel_value
  249. /// \ingroup ChannelModel
  250. /// \brief Represents the value of an unsigned integral channel operating over a bit range. Models: ChannelValueConcept
  251. /// Example:
  252. /// \code
  253. /// // A 4-bit unsigned integral channel.
  254. /// using bits4 = packed_channel_value<4>;
  255. ///
  256. /// assert(channel_traits<bits4>::min_value()==0);
  257. /// assert(channel_traits<bits4>::max_value()==15);
  258. /// assert(sizeof(bits4)==1);
  259. /// static_assert(gil::is_channel_integral<bits4>::value, "");
  260. /// \endcode
  261. /// \ingroup PackedChannelValueModel
  262. /// \brief The value of a subbyte channel. Models: ChannelValueConcept
  263. template <int NumBits>
  264. class packed_channel_value
  265. {
  266. public:
  267. using integer_t = typename detail::min_fast_uint<NumBits>::type;
  268. using value_type = packed_channel_value<NumBits>;
  269. using reference = value_type&;
  270. using const_reference = value_type const&;
  271. using pointer = value_type*;
  272. using const_pointer = value_type const*;
  273. static constexpr bool is_mutable = true;
  274. static value_type min_value() { return 0; }
  275. static value_type max_value() { return low_bits_mask_t< NumBits >::sig_bits; }
  276. packed_channel_value() = default;
  277. packed_channel_value(integer_t v)
  278. {
  279. value_ = static_cast<integer_t>(v & low_bits_mask_t<NumBits>::sig_bits_fast);
  280. }
  281. template <typename Scalar>
  282. packed_channel_value(Scalar v)
  283. {
  284. value_ = packed_channel_value(static_cast<integer_t>(v));
  285. }
  286. static auto num_bits() -> unsigned int { return NumBits; }
  287. operator integer_t() const { return value_; }
  288. private:
  289. integer_t value_{};
  290. };
  291. namespace detail {
  292. template <std::size_t K>
  293. struct static_copy_bytes
  294. {
  295. void operator()(unsigned char const* from, unsigned char* to) const
  296. {
  297. *to = *from;
  298. static_copy_bytes<K - 1>()(++from, ++to);
  299. }
  300. };
  301. template <>
  302. struct static_copy_bytes<0>
  303. {
  304. void operator()(unsigned char const*, unsigned char*) const {}
  305. };
  306. template <typename Derived, typename BitField, int NumBits, bool IsMutable>
  307. class packed_channel_reference_base
  308. {
  309. protected:
  310. using data_ptr_t = typename std::conditional<IsMutable, void*, void const*>::type;
  311. public:
  312. data_ptr_t _data_ptr; // void* pointer to the first byte of the bit range
  313. using value_type = packed_channel_value<NumBits>;
  314. using reference = Derived const;
  315. using pointer = value_type*;
  316. using const_pointer = value_type const*;
  317. static constexpr int num_bits = NumBits;
  318. static constexpr bool is_mutable = IsMutable;
  319. static auto min_value() -> value_type { return channel_traits<value_type>::min_value(); }
  320. static auto max_value() -> value_type { return channel_traits<value_type>::max_value(); }
  321. using bitfield_t = BitField;
  322. using integer_t = typename value_type::integer_t;
  323. packed_channel_reference_base(data_ptr_t data_ptr) : _data_ptr(data_ptr) {}
  324. packed_channel_reference_base(packed_channel_reference_base const& ref) : _data_ptr(ref._data_ptr) {}
  325. auto operator=(integer_t v) const -> Derived const& { set(v); return derived(); }
  326. auto operator++() const -> Derived const& { set(get()+1); return derived(); }
  327. auto operator--() const -> Derived const& { set(get()-1); return derived(); }
  328. auto operator++(int) const -> Derived
  329. {
  330. Derived tmp=derived();
  331. this->operator++(); return tmp;
  332. }
  333. auto operator--(int) const -> Derived
  334. {
  335. Derived tmp=derived();
  336. this->operator--();
  337. return tmp;
  338. }
  339. template <typename Scalar2>
  340. auto operator+=(Scalar2 v) const -> Derived const&
  341. {
  342. set( static_cast<integer_t>( get() + v ));
  343. return derived();
  344. }
  345. template <typename Scalar2>
  346. auto operator-=(Scalar2 v) const -> Derived const&
  347. {
  348. set( static_cast<integer_t>( get() - v )); return derived();
  349. }
  350. template <typename Scalar2>
  351. auto operator*=(Scalar2 v) const -> Derived const&
  352. {
  353. set( static_cast<integer_t>( get() * v ));
  354. return derived();
  355. }
  356. template <typename Scalar2>
  357. auto operator/=(Scalar2 v) const -> Derived const&
  358. {
  359. set( static_cast<integer_t>( get() / v ));
  360. return derived();
  361. }
  362. operator integer_t() const { return get(); }
  363. auto operator&() const -> data_ptr_t {return _data_ptr;}
  364. protected:
  365. using num_value_t = typename detail::num_value_fn<NumBits>::type;
  366. using max_value_t = typename detail::max_value_fn<NumBits>::type;
  367. static const num_value_t num_values = static_cast< num_value_t >( 1 ) << NumBits ;
  368. static const max_value_t max_val = static_cast< max_value_t >( num_values - 1 );
  369. #if defined(BOOST_GIL_CONFIG_HAS_UNALIGNED_ACCESS)
  370. const bitfield_t& get_data() const { return *static_cast<const bitfield_t*>(_data_ptr); }
  371. void set_data(const bitfield_t& val) const { *static_cast< bitfield_t*>(_data_ptr) = val; }
  372. #else
  373. auto get_data() const -> bitfield_t
  374. {
  375. bitfield_t ret;
  376. static_copy_bytes<sizeof(bitfield_t) >()(gil_reinterpret_cast_c<const unsigned char*>(_data_ptr),gil_reinterpret_cast<unsigned char*>(&ret));
  377. return ret;
  378. }
  379. void set_data(bitfield_t const& val) const
  380. {
  381. static_copy_bytes<sizeof(bitfield_t) >()(gil_reinterpret_cast_c<const unsigned char*>(&val),gil_reinterpret_cast<unsigned char*>(_data_ptr));
  382. }
  383. #endif
  384. private:
  385. void set(integer_t value) const { // can this be done faster??
  386. this->derived().set_unsafe(((value % num_values) + num_values) % num_values);
  387. }
  388. auto get() const -> integer_t { return derived().get(); }
  389. auto derived() const -> Derived const& { return static_cast<const Derived&>(*this); }
  390. };
  391. } // namespace detail
  392. /// \defgroup PackedChannelReferenceModel packed_channel_reference
  393. /// \ingroup ChannelModel
  394. /// \brief Represents a reference proxy to a channel operating over a bit range whose offset is fixed at compile time. Models ChannelConcept
  395. /// Example:
  396. /// \code
  397. /// // Reference to a 2-bit channel starting at bit 1 (i.e. the second bit)
  398. /// using bits2_1_ref_t = packed_channel_reference<uint16_t,1,2,true> const;
  399. ///
  400. /// uint16_t data=0;
  401. /// bits2_1_ref_t channel_ref(&data);
  402. /// channel_ref = channel_traits<bits2_1_ref_t>::max_value(); // == 3
  403. /// assert(data == 6); // == 3<<1 == 6
  404. /// \endcode
  405. /// \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
  406. /// \tparam Defines the sequence of bits in the data value that contain the channel
  407. /// \tparam true if the reference is mutable
  408. template <typename BitField, int FirstBit, int NumBits, bool IsMutable>
  409. class packed_channel_reference;
  410. /// \tparam A type that holds the bits of the pixel from which the channel is referenced. Typically an integral type, like std::uint16_t
  411. /// \tparam Defines the sequence of bits in the data value that contain the channel
  412. /// \tparam true if the reference is mutable
  413. template <typename BitField, int NumBits, bool IsMutable>
  414. class packed_dynamic_channel_reference;
  415. /// \ingroup PackedChannelReferenceModel
  416. /// \brief A constant subbyte channel reference whose bit offset is fixed at compile time. Models ChannelConcept
  417. template <typename BitField, int FirstBit, int NumBits>
  418. class packed_channel_reference<BitField, FirstBit, NumBits, false>
  419. : public detail::packed_channel_reference_base
  420. <
  421. packed_channel_reference<BitField, FirstBit, NumBits, false>,
  422. BitField,
  423. NumBits,
  424. false
  425. >
  426. {
  427. using parent_t = detail::packed_channel_reference_base
  428. <
  429. packed_channel_reference<BitField, FirstBit, NumBits, false>,
  430. BitField,
  431. NumBits,
  432. false
  433. >;
  434. friend class packed_channel_reference<BitField, FirstBit, NumBits, true>;
  435. static const BitField channel_mask = static_cast<BitField>(parent_t::max_val) << FirstBit;
  436. void operator=(packed_channel_reference const&);
  437. public:
  438. using const_reference = packed_channel_reference<BitField,FirstBit,NumBits,false> const;
  439. using mutable_reference = packed_channel_reference<BitField,FirstBit,NumBits,true> const;
  440. using integer_t = typename parent_t::integer_t;
  441. explicit packed_channel_reference(const void* data_ptr) : parent_t(data_ptr) {}
  442. packed_channel_reference(const packed_channel_reference& ref) : parent_t(ref._data_ptr) {}
  443. packed_channel_reference(const mutable_reference& ref) : parent_t(ref._data_ptr) {}
  444. auto first_bit() const -> unsigned int { return FirstBit; }
  445. auto get() const -> integer_t { return integer_t((this->get_data()&channel_mask) >> FirstBit); }
  446. };
  447. /// \ingroup PackedChannelReferenceModel
  448. /// \brief A mutable subbyte channel reference whose bit offset is fixed at compile time. Models ChannelConcept
  449. template <typename BitField, int FirstBit, int NumBits>
  450. class packed_channel_reference<BitField,FirstBit,NumBits,true>
  451. : public detail::packed_channel_reference_base<packed_channel_reference<BitField,FirstBit,NumBits,true>,BitField,NumBits,true>
  452. {
  453. using parent_t = detail::packed_channel_reference_base<packed_channel_reference<BitField,FirstBit,NumBits,true>,BitField,NumBits,true>;
  454. friend class packed_channel_reference<BitField,FirstBit,NumBits,false>;
  455. static const BitField channel_mask = static_cast< BitField >( parent_t::max_val ) << FirstBit;
  456. public:
  457. using const_reference = packed_channel_reference<BitField,FirstBit,NumBits,false> const;
  458. using mutable_reference = packed_channel_reference<BitField,FirstBit,NumBits,true> const;
  459. using integer_t = typename parent_t::integer_t;
  460. explicit packed_channel_reference(void* data_ptr) : parent_t(data_ptr) {}
  461. packed_channel_reference(const packed_channel_reference& ref) : parent_t(ref._data_ptr) {}
  462. packed_channel_reference const& operator=(integer_t value) const
  463. {
  464. BOOST_ASSERT(value <= parent_t::max_val);
  465. set_unsafe(value);
  466. return *this;
  467. }
  468. auto operator=(mutable_reference const& ref) const -> packed_channel_reference const& { set_from_reference(ref.get_data()); return *this; }
  469. auto operator=(const_reference const& ref) const -> packed_channel_reference const& { set_from_reference(ref.get_data()); return *this; }
  470. template <bool Mutable1>
  471. auto operator=(packed_dynamic_channel_reference<BitField,NumBits,Mutable1> const& ref) const -> packed_channel_reference const& { set_unsafe(ref.get()); return *this; }
  472. auto first_bit() const -> unsigned int { return FirstBit; }
  473. auto get() const -> integer_t { return integer_t((this->get_data()&channel_mask) >> FirstBit); }
  474. void set_unsafe(integer_t value) const { this->set_data((this->get_data() & ~channel_mask) | (( static_cast< BitField >( value )<<FirstBit))); }
  475. private:
  476. void set_from_reference(const BitField& other_bits) const { this->set_data((this->get_data() & ~channel_mask) | (other_bits & channel_mask)); }
  477. };
  478. }} // namespace boost::gil
  479. namespace std {
  480. // We are forced to define swap inside std namespace because on some platforms (Visual Studio 8) STL calls swap qualified.
  481. // swap with 'left bias':
  482. // - swap between proxy and anything
  483. // - swap between value type and proxy
  484. // - swap between proxy and proxy
  485. /// \ingroup PackedChannelReferenceModel
  486. /// \brief swap for packed_channel_reference
  487. template <typename BF, int FB, int NB, bool M, typename R>
  488. inline
  489. void swap(boost::gil::packed_channel_reference<BF, FB, NB, M> const x, R& y)
  490. {
  491. boost::gil::swap_proxy
  492. <
  493. typename boost::gil::packed_channel_reference<BF, FB, NB, M>::value_type
  494. >(x, y);
  495. }
  496. /// \ingroup PackedChannelReferenceModel
  497. /// \brief swap for packed_channel_reference
  498. template <typename BF, int FB, int NB, bool M>
  499. inline
  500. void swap(
  501. typename boost::gil::packed_channel_reference<BF, FB, NB, M>::value_type& x,
  502. boost::gil::packed_channel_reference<BF, FB, NB, M> const y)
  503. {
  504. boost::gil::swap_proxy
  505. <
  506. typename boost::gil::packed_channel_reference<BF, FB, NB, M>::value_type
  507. >(x,y);
  508. }
  509. /// \ingroup PackedChannelReferenceModel
  510. /// \brief swap for packed_channel_reference
  511. template <typename BF, int FB, int NB, bool M> inline
  512. void swap(
  513. boost::gil::packed_channel_reference<BF, FB, NB, M> const x,
  514. boost::gil::packed_channel_reference<BF, FB, NB, M> const y)
  515. {
  516. boost::gil::swap_proxy
  517. <
  518. typename boost::gil::packed_channel_reference<BF, FB, NB, M>::value_type
  519. >(x,y);
  520. }
  521. } // namespace std
  522. namespace boost { namespace gil {
  523. /// \defgroup PackedChannelDynamicReferenceModel packed_dynamic_channel_reference
  524. /// \ingroup ChannelModel
  525. /// \brief Represents a reference proxy to a channel operating over a bit range whose offset is specified at run time. Models ChannelConcept
  526. ///
  527. /// Example:
  528. /// \code
  529. /// // Reference to a 2-bit channel whose offset is specified at construction time
  530. /// using bits2_dynamic_ref_t = packed_dynamic_channel_reference<uint8_t,2,true> const;
  531. ///
  532. /// uint16_t data=0;
  533. /// bits2_dynamic_ref_t channel_ref(&data,1);
  534. /// channel_ref = channel_traits<bits2_dynamic_ref_t>::max_value(); // == 3
  535. /// assert(data == 6); // == (3<<1) == 6
  536. /// \endcode
  537. /// \brief Models a constant subbyte channel reference whose bit offset is a runtime parameter. Models ChannelConcept
  538. /// Same as packed_channel_reference, except that the offset is a runtime parameter
  539. /// \ingroup PackedChannelDynamicReferenceModel
  540. template <typename BitField, int NumBits>
  541. class packed_dynamic_channel_reference<BitField,NumBits,false>
  542. : public detail::packed_channel_reference_base<packed_dynamic_channel_reference<BitField,NumBits,false>,BitField,NumBits,false>
  543. {
  544. using parent_t = detail::packed_channel_reference_base<packed_dynamic_channel_reference<BitField,NumBits,false>,BitField,NumBits,false>;
  545. friend class packed_dynamic_channel_reference<BitField,NumBits,true>;
  546. unsigned _first_bit; // 0..7
  547. void operator=(const packed_dynamic_channel_reference&);
  548. public:
  549. using const_reference = packed_dynamic_channel_reference<BitField,NumBits,false> const;
  550. using mutable_reference = packed_dynamic_channel_reference<BitField,NumBits,true> const;
  551. using integer_t = typename parent_t::integer_t;
  552. packed_dynamic_channel_reference(void const* data_ptr, unsigned first_bit) : parent_t(data_ptr), _first_bit(first_bit) {}
  553. packed_dynamic_channel_reference(const_reference const& ref) : parent_t(ref._data_ptr), _first_bit(ref._first_bit) {}
  554. packed_dynamic_channel_reference(mutable_reference const& ref) : parent_t(ref._data_ptr), _first_bit(ref._first_bit) {}
  555. auto first_bit() const -> unsigned int { return _first_bit; }
  556. auto get() const -> integer_t
  557. {
  558. const BitField channel_mask = static_cast< integer_t >( parent_t::max_val ) <<_first_bit;
  559. return static_cast< integer_t >(( this->get_data()&channel_mask ) >> _first_bit );
  560. }
  561. };
  562. /// \brief Models a mutable subbyte channel reference whose bit offset is a runtime parameter. Models ChannelConcept
  563. /// Same as packed_channel_reference, except that the offset is a runtime parameter
  564. /// \ingroup PackedChannelDynamicReferenceModel
  565. template <typename BitField, int NumBits>
  566. class packed_dynamic_channel_reference<BitField,NumBits,true>
  567. : public detail::packed_channel_reference_base<packed_dynamic_channel_reference<BitField,NumBits,true>,BitField,NumBits,true>
  568. {
  569. using parent_t = detail::packed_channel_reference_base<packed_dynamic_channel_reference<BitField,NumBits,true>,BitField,NumBits,true>;
  570. friend class packed_dynamic_channel_reference<BitField,NumBits,false>;
  571. unsigned _first_bit;
  572. public:
  573. using const_reference = packed_dynamic_channel_reference<BitField,NumBits,false> const;
  574. using mutable_reference = packed_dynamic_channel_reference<BitField,NumBits,true> const;
  575. using integer_t = typename parent_t::integer_t;
  576. packed_dynamic_channel_reference(void* data_ptr, unsigned first_bit) : parent_t(data_ptr), _first_bit(first_bit) {}
  577. packed_dynamic_channel_reference(packed_dynamic_channel_reference const& ref) : parent_t(ref._data_ptr), _first_bit(ref._first_bit) {}
  578. auto operator=(integer_t value) const -> packed_dynamic_channel_reference const&
  579. {
  580. BOOST_ASSERT(value <= parent_t::max_val);
  581. set_unsafe(value);
  582. return *this;
  583. }
  584. auto operator=(mutable_reference const& ref) const -> packed_dynamic_channel_reference const& { set_unsafe(ref.get()); return *this; }
  585. auto operator=(const_reference const& ref) const -> packed_dynamic_channel_reference const& { set_unsafe(ref.get()); return *this; }
  586. template <typename BitField1, int FirstBit1, bool Mutable1>
  587. auto operator=(packed_channel_reference<BitField1, FirstBit1, NumBits, Mutable1> const& ref) const -> packed_dynamic_channel_reference const&
  588. {
  589. set_unsafe(ref.get());
  590. return *this;
  591. }
  592. auto first_bit() const -> unsigned int { return _first_bit; }
  593. auto get() const -> integer_t
  594. {
  595. BitField const channel_mask = static_cast< integer_t >( parent_t::max_val ) << _first_bit;
  596. return static_cast< integer_t >(( this->get_data()&channel_mask ) >> _first_bit );
  597. }
  598. void set_unsafe(integer_t value) const {
  599. const BitField channel_mask = static_cast< integer_t >( parent_t::max_val ) << _first_bit;
  600. this->set_data((this->get_data() & ~channel_mask) | value<<_first_bit);
  601. }
  602. };
  603. } } // namespace boost::gil
  604. namespace std {
  605. // We are forced to define swap inside std namespace because on some platforms (Visual Studio 8) STL calls swap qualified.
  606. // swap with 'left bias':
  607. // - swap between proxy and anything
  608. // - swap between value type and proxy
  609. // - swap between proxy and proxy
  610. /// \ingroup PackedChannelDynamicReferenceModel
  611. /// \brief swap for packed_dynamic_channel_reference
  612. template <typename BF, int NB, bool M, typename R> inline
  613. void swap(const boost::gil::packed_dynamic_channel_reference<BF,NB,M> x, R& y) {
  614. boost::gil::swap_proxy<typename boost::gil::packed_dynamic_channel_reference<BF,NB,M>::value_type>(x,y);
  615. }
  616. /// \ingroup PackedChannelDynamicReferenceModel
  617. /// \brief swap for packed_dynamic_channel_reference
  618. template <typename BF, int NB, bool M> inline
  619. 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) {
  620. boost::gil::swap_proxy<typename boost::gil::packed_dynamic_channel_reference<BF,NB,M>::value_type>(x,y);
  621. }
  622. /// \ingroup PackedChannelDynamicReferenceModel
  623. /// \brief swap for packed_dynamic_channel_reference
  624. template <typename BF, int NB, bool M> inline
  625. void swap(const boost::gil::packed_dynamic_channel_reference<BF,NB,M> x, const boost::gil::packed_dynamic_channel_reference<BF,NB,M> y) {
  626. boost::gil::swap_proxy<typename boost::gil::packed_dynamic_channel_reference<BF,NB,M>::value_type>(x,y);
  627. }
  628. } // namespace std
  629. // \brief Determines the fundamental type which may be used, e.g., to cast from larger to smaller channel types.
  630. namespace boost { namespace gil {
  631. template <typename T>
  632. struct base_channel_type_impl { using type = T; };
  633. template <int N>
  634. struct base_channel_type_impl<packed_channel_value<N> >
  635. { using type = typename packed_channel_value<N>::integer_t; };
  636. template <typename B, int F, int N, bool M>
  637. struct base_channel_type_impl<packed_channel_reference<B, F, N, M> >
  638. {
  639. using type = typename packed_channel_reference<B,F,N,M>::integer_t;
  640. };
  641. template <typename B, int N, bool M>
  642. struct base_channel_type_impl<packed_dynamic_channel_reference<B, N, M> >
  643. {
  644. using type = typename packed_dynamic_channel_reference<B,N,M>::integer_t;
  645. };
  646. template <typename ChannelValue, typename MinV, typename MaxV>
  647. struct base_channel_type_impl<scoped_channel_value<ChannelValue, MinV, MaxV> >
  648. { using type = ChannelValue; };
  649. template <typename T>
  650. struct base_channel_type : base_channel_type_impl<typename std::remove_cv<T>::type> {};
  651. }} //namespace boost::gil
  652. #endif