bit_aligned_pixel_reference.hpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. //
  2. // Copyright 2005-2007 Adobe Systems Incorporated
  3. // Copyright 2019 Mateusz Loskot <mateusz at loskot dot net>
  4. //
  5. // Distributed under the Boost Software License, Version 1.0
  6. // See accompanying file LICENSE_1_0.txt or copy at
  7. // http://www.boost.org/LICENSE_1_0.txt
  8. //
  9. #ifndef BOOST_GIL_BIT_ALIGNED_PIXEL_REFERENCE_HPP
  10. #define BOOST_GIL_BIT_ALIGNED_PIXEL_REFERENCE_HPP
  11. #include <boost/gil/pixel.hpp>
  12. #include <boost/gil/channel.hpp>
  13. #include <boost/gil/detail/mp11.hpp>
  14. #include <boost/assert.hpp>
  15. #include <boost/config.hpp>
  16. #include <functional>
  17. #include <type_traits>
  18. namespace boost { namespace gil {
  19. /// A model of a heterogeneous pixel that is not byte aligned.
  20. /// Examples are bitmap (1-bit pixels) or 6-bit RGB (222).
  21. /////////////////////////////
  22. // bit_range
  23. //
  24. // Represents a range of bits that can span multiple consecutive bytes. The range has a size fixed at compile time, but the offset is specified at run time.
  25. /////////////////////////////
  26. template <int RangeSize, bool IsMutable>
  27. class bit_range {
  28. public:
  29. using byte_t = mp11::mp_if_c<IsMutable, unsigned char, unsigned char const>;
  30. using difference_type = std::ptrdiff_t;
  31. template <int RS, bool M> friend class bit_range;
  32. private:
  33. byte_t* _current_byte; // the starting byte of the bit range
  34. int _bit_offset; // offset from the beginning of the current byte. 0<=_bit_offset<=7
  35. public:
  36. bit_range() : _current_byte(nullptr), _bit_offset(0) {}
  37. bit_range(byte_t* current_byte, int bit_offset)
  38. : _current_byte(current_byte)
  39. , _bit_offset(bit_offset)
  40. {
  41. BOOST_ASSERT(bit_offset >= 0 && bit_offset < 8);
  42. }
  43. bit_range(bit_range const& br) : _current_byte(br._current_byte), _bit_offset(br._bit_offset) {}
  44. template <bool M> bit_range(const bit_range<RangeSize,M>& br) : _current_byte(br._current_byte), _bit_offset(br._bit_offset) {}
  45. auto operator=(bit_range const& br) -> bit_range& { _current_byte = br._current_byte; _bit_offset=br._bit_offset; return *this; }
  46. bool operator==(bit_range const& br) const { return _current_byte==br._current_byte && _bit_offset==br._bit_offset; }
  47. auto operator++() -> bit_range& {
  48. _current_byte += (_bit_offset+RangeSize) / 8;
  49. _bit_offset = (_bit_offset+RangeSize) % 8;
  50. return *this;
  51. }
  52. auto operator--() -> bit_range& { bit_advance(-RangeSize); return *this; }
  53. void bit_advance(difference_type num_bits) {
  54. int new_offset = int(_bit_offset+num_bits);
  55. _current_byte += new_offset / 8;
  56. _bit_offset = new_offset % 8;
  57. if (_bit_offset<0) {
  58. _bit_offset+=8;
  59. --_current_byte;
  60. }
  61. }
  62. auto bit_distance_to(bit_range const& b) const -> difference_type
  63. {
  64. return (b.current_byte() - current_byte())*8 + b.bit_offset()-bit_offset();
  65. }
  66. auto current_byte() const -> byte_t* { return _current_byte; }
  67. auto bit_offset() const -> int { return _bit_offset; }
  68. };
  69. /// \defgroup ColorBaseModelNonAlignedPixel bit_aligned_pixel_reference
  70. /// \ingroup ColorBaseModel
  71. /// \brief A heterogeneous color base representing pixel that may not be byte aligned, i.e. it may correspond to a bit range that does not start/end at a byte boundary. Models ColorBaseConcept.
  72. ///
  73. /// \defgroup PixelModelNonAlignedPixel bit_aligned_pixel_reference
  74. /// \ingroup PixelModel
  75. /// \brief A heterogeneous pixel reference used to represent non-byte-aligned pixels. Models PixelConcept
  76. ///
  77. /// Example:
  78. /// \code
  79. /// unsigned char data=0;
  80. ///
  81. /// // A mutable reference to a 6-bit BGR pixel in "123" format (1 bit for red, 2 bits for green, 3 bits for blue)
  82. /// using rgb123_ref_t = bit_aligned_pixel_reference<unsigned char, mp11::mp_list_c<int,1,2,3>, rgb_layout_t, true> const;
  83. ///
  84. /// // create the pixel reference at bit offset 2
  85. /// // (i.e. red = [2], green = [3,4], blue = [5,6,7] bits)
  86. /// rgb123_ref_t ref(&data, 2);
  87. /// get_color(ref, red_t()) = 1;
  88. /// assert(data == 0x04);
  89. /// get_color(ref, green_t()) = 3;
  90. /// assert(data == 0x1C);
  91. /// get_color(ref, blue_t()) = 7;
  92. /// assert(data == 0xFC);
  93. /// \endcode
  94. ///
  95. /// \ingroup ColorBaseModelNonAlignedPixel PixelModelNonAlignedPixel PixelBasedModel
  96. /// \brief Heterogeneous pixel reference corresponding to non-byte-aligned bit range. Models ColorBaseConcept, PixelConcept, PixelBasedConcept
  97. ///
  98. /// \tparam BitField
  99. /// \tparam ChannelBitSizes Boost.MP11-compatible list of integral types defining the number of bits for each channel. For example, for 565RGB, mp_list_c<int,5,6,5>
  100. /// \tparam Layout
  101. /// \tparam IsMutable
  102. template <typename BitField, typename ChannelBitSizes, typename Layout, bool IsMutable>
  103. struct bit_aligned_pixel_reference
  104. {
  105. static constexpr int bit_size =
  106. mp11::mp_fold
  107. <
  108. ChannelBitSizes,
  109. std::integral_constant<int, 0>,
  110. mp11::mp_plus
  111. >::value;
  112. using bit_range_t = boost::gil::bit_range<bit_size,IsMutable>;
  113. using bitfield_t = BitField;
  114. using data_ptr_t = mp11::mp_if_c<IsMutable, unsigned char*, const unsigned char*>;
  115. using layout_t = Layout;
  116. using value_type = typename packed_pixel_type<bitfield_t,ChannelBitSizes,Layout>::type;
  117. using reference = const bit_aligned_pixel_reference<BitField, ChannelBitSizes, Layout, IsMutable>;
  118. using const_reference = bit_aligned_pixel_reference<BitField,ChannelBitSizes,Layout,false> const;
  119. static constexpr bool is_mutable = IsMutable;
  120. bit_aligned_pixel_reference(){}
  121. bit_aligned_pixel_reference(data_ptr_t data_ptr, int bit_offset) : _bit_range(data_ptr, bit_offset) {}
  122. explicit bit_aligned_pixel_reference(bit_range_t const& bit_range) : _bit_range(bit_range) {}
  123. template <bool IsMutable2>
  124. bit_aligned_pixel_reference(bit_aligned_pixel_reference<BitField,ChannelBitSizes,Layout,IsMutable2> const& p) : _bit_range(p._bit_range) {}
  125. // Grayscale references can be constructed from the channel reference
  126. explicit bit_aligned_pixel_reference(typename kth_element_type<bit_aligned_pixel_reference,0>::type const channel0)
  127. : _bit_range(static_cast<data_ptr_t>(&channel0), channel0.first_bit())
  128. {
  129. static_assert(num_channels<bit_aligned_pixel_reference>::value == 1, "");
  130. }
  131. // Construct from another compatible pixel type
  132. bit_aligned_pixel_reference(bit_aligned_pixel_reference const& p)
  133. : _bit_range(p._bit_range) {}
  134. // TODO: Why p by non-const reference?
  135. template <typename BF, typename CR>
  136. bit_aligned_pixel_reference(packed_pixel<BF, CR, Layout>& p)
  137. : _bit_range(static_cast<data_ptr_t>(&gil::at_c<0>(p)), gil::at_c<0>(p).first_bit())
  138. {
  139. check_compatible<packed_pixel<BF, CR, Layout>>();
  140. }
  141. auto operator=(bit_aligned_pixel_reference const& p) const
  142. -> bit_aligned_pixel_reference const&
  143. {
  144. static_copy(p, *this);
  145. return *this;
  146. }
  147. template <typename P>
  148. auto operator=(P const& p) const -> bit_aligned_pixel_reference const&
  149. {
  150. assign(p, is_pixel<P>());
  151. return *this;
  152. }
  153. template <typename P>
  154. bool operator==(P const& p) const
  155. {
  156. return equal(p, is_pixel<P>());
  157. }
  158. template <typename P>
  159. bool operator!=(P const& p) const { return !(*this==p); }
  160. auto operator->() const -> bit_aligned_pixel_reference const* { return this; }
  161. auto bit_range() const -> bit_range_t const& { return _bit_range; }
  162. private:
  163. mutable bit_range_t _bit_range;
  164. template <typename B, typename C, typename L, bool M>
  165. friend struct bit_aligned_pixel_reference;
  166. template <typename Pixel> static void check_compatible() { gil_function_requires<PixelsCompatibleConcept<Pixel,bit_aligned_pixel_reference> >(); }
  167. template <typename Pixel>
  168. void assign(Pixel const& p, std::true_type) const
  169. {
  170. check_compatible<Pixel>();
  171. static_copy(p, *this);
  172. }
  173. template <typename Pixel>
  174. bool equal(Pixel const& p, std::true_type) const
  175. {
  176. check_compatible<Pixel>();
  177. return static_equal(*this, p);
  178. }
  179. private:
  180. static void check_gray()
  181. {
  182. static_assert(std::is_same<typename Layout::color_space_t, gray_t>::value, "");
  183. }
  184. template <typename Channel>
  185. void assign(Channel const& channel, std::false_type) const
  186. {
  187. check_gray();
  188. gil::at_c<0>(*this) = channel;
  189. }
  190. template <typename Channel>
  191. bool equal (Channel const& channel, std::false_type) const
  192. {
  193. check_gray();
  194. return gil::at_c<0>(*this) == channel;
  195. }
  196. };
  197. /////////////////////////////
  198. // ColorBasedConcept
  199. /////////////////////////////
  200. template <typename BitField, typename ChannelBitSizes, typename L, bool IsMutable, int K>
  201. struct kth_element_type
  202. <
  203. bit_aligned_pixel_reference<BitField, ChannelBitSizes, L, IsMutable>,
  204. K
  205. >
  206. {
  207. using type = packed_dynamic_channel_reference
  208. <
  209. BitField,
  210. mp11::mp_at_c<ChannelBitSizes, K>::value,
  211. IsMutable
  212. > const;
  213. };
  214. template <typename B, typename C, typename L, bool M, int K>
  215. struct kth_element_reference_type<bit_aligned_pixel_reference<B,C,L,M>, K>
  216. : public kth_element_type<bit_aligned_pixel_reference<B,C,L,M>, K> {};
  217. template <typename B, typename C, typename L, bool M, int K>
  218. struct kth_element_const_reference_type<bit_aligned_pixel_reference<B,C,L,M>, K>
  219. : public kth_element_type<bit_aligned_pixel_reference<B,C,L,M>, K> {};
  220. namespace detail {
  221. // returns sum of IntegralVector[0] ... IntegralVector[K-1]
  222. template <typename IntegralVector, int K>
  223. struct sum_k
  224. : mp11::mp_plus
  225. <
  226. sum_k<IntegralVector, K - 1>,
  227. typename mp11::mp_at_c<IntegralVector, K - 1>::type
  228. >
  229. {};
  230. template <typename IntegralVector>
  231. struct sum_k<IntegralVector, 0> : std::integral_constant<int, 0> {};
  232. } // namespace detail
  233. // at_c required by MutableColorBaseConcept
  234. template <int K, typename BitField, typename ChannelBitSizes, typename L, bool IsMutable>
  235. inline
  236. auto at_c(const bit_aligned_pixel_reference<BitField, ChannelBitSizes, L, IsMutable>& p)
  237. -> typename kth_element_reference_type<bit_aligned_pixel_reference<BitField, ChannelBitSizes, L, IsMutable>, K>::type
  238. {
  239. using pixel_t = bit_aligned_pixel_reference<BitField, ChannelBitSizes, L, IsMutable>;
  240. using channel_t = typename kth_element_reference_type<pixel_t, K>::type;
  241. using bit_range_t = typename pixel_t::bit_range_t;
  242. bit_range_t bit_range(p.bit_range());
  243. bit_range.bit_advance(detail::sum_k<ChannelBitSizes, K>::value);
  244. return channel_t(bit_range.current_byte(), bit_range.bit_offset());
  245. }
  246. /////////////////////////////
  247. // PixelConcept
  248. /////////////////////////////
  249. /// Metafunction predicate that flags bit_aligned_pixel_reference as a model of PixelConcept. Required by PixelConcept
  250. template <typename B, typename C, typename L, bool M>
  251. struct is_pixel<bit_aligned_pixel_reference<B, C, L, M> > : std::true_type {};
  252. /////////////////////////////
  253. // PixelBasedConcept
  254. /////////////////////////////
  255. template <typename B, typename C, typename L, bool M>
  256. struct color_space_type<bit_aligned_pixel_reference<B, C, L, M>>
  257. {
  258. using type = typename L::color_space_t;
  259. };
  260. template <typename B, typename C, typename L, bool M>
  261. struct channel_mapping_type<bit_aligned_pixel_reference<B, C, L, M>>
  262. {
  263. using type = typename L::channel_mapping_t;
  264. };
  265. template <typename B, typename C, typename L, bool M>
  266. struct is_planar<bit_aligned_pixel_reference<B, C, L, M>> : std::false_type {};
  267. /////////////////////////////
  268. // pixel_reference_type
  269. /////////////////////////////
  270. // Constructs a homogeneous bit_aligned_pixel_reference given a channel reference
  271. template <typename BitField, int NumBits, typename Layout>
  272. struct pixel_reference_type
  273. <
  274. packed_dynamic_channel_reference<BitField, NumBits, false> const,
  275. Layout, false, false
  276. >
  277. {
  278. private:
  279. using channel_bit_sizes_t = mp11::mp_repeat
  280. <
  281. mp11::mp_list<std::integral_constant<unsigned, NumBits>>,
  282. mp11::mp_size<typename Layout::color_space_t>
  283. >;
  284. public:
  285. using type =
  286. bit_aligned_pixel_reference<BitField, channel_bit_sizes_t, Layout, false>;
  287. };
  288. // Same but for the mutable case. We cannot combine the mutable
  289. // and read-only cases because this triggers ambiguity
  290. template <typename BitField, int NumBits, typename Layout>
  291. struct pixel_reference_type
  292. <
  293. packed_dynamic_channel_reference<BitField, NumBits, true> const,
  294. Layout, false, true
  295. >
  296. {
  297. private:
  298. using channel_bit_sizes_t = mp11::mp_repeat
  299. <
  300. mp11::mp_list<std::integral_constant<unsigned, NumBits>>,
  301. mp11::mp_size<typename Layout::color_space_t>
  302. >;
  303. public:
  304. using type = bit_aligned_pixel_reference<BitField, channel_bit_sizes_t, Layout, true>;
  305. };
  306. } } // namespace boost::gil
  307. namespace std {
  308. // We are forced to define swap inside std namespace because on some platforms (Visual Studio 8) STL calls swap qualified.
  309. // swap with 'left bias':
  310. // - swap between proxy and anything
  311. // - swap between value type and proxy
  312. // - swap between proxy and proxy
  313. // Having three overloads allows us to swap between different (but compatible) models of PixelConcept
  314. template <typename B, typename C, typename L, typename R> inline
  315. void swap(boost::gil::bit_aligned_pixel_reference<B,C,L,true> const x, R& y) {
  316. boost::gil::swap_proxy<typename boost::gil::bit_aligned_pixel_reference<B,C,L,true>::value_type>(x,y);
  317. }
  318. template <typename B, typename C, typename L> inline
  319. void swap(typename boost::gil::bit_aligned_pixel_reference<B,C,L,true>::value_type& x, const boost::gil::bit_aligned_pixel_reference<B,C,L,true> y) {
  320. boost::gil::swap_proxy<typename boost::gil::bit_aligned_pixel_reference<B,C,L,true>::value_type>(x,y);
  321. }
  322. template <typename B, typename C, typename L> inline
  323. void swap(boost::gil::bit_aligned_pixel_reference<B,C,L,true> const x, const boost::gil::bit_aligned_pixel_reference<B,C,L,true> y) {
  324. boost::gil::swap_proxy<typename boost::gil::bit_aligned_pixel_reference<B,C,L,true>::value_type>(x,y);
  325. }
  326. } // namespace std
  327. #endif