bit_aligned_pixel_reference.hpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  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(const bit_range& 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. bit_range& operator=(const bit_range& br) { _current_byte = br._current_byte; _bit_offset=br._bit_offset; return *this; }
  46. bool operator==(const bit_range& br) const { return _current_byte==br._current_byte && _bit_offset==br._bit_offset; }
  47. bit_range& operator++() {
  48. _current_byte += (_bit_offset+RangeSize) / 8;
  49. _bit_offset = (_bit_offset+RangeSize) % 8;
  50. return *this;
  51. }
  52. bit_range& operator--() { 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. difference_type bit_distance_to(const bit_range& b) const {
  63. return (b.current_byte() - current_byte())*8 + b.bit_offset()-bit_offset();
  64. }
  65. byte_t* current_byte() const { return _current_byte; }
  66. int bit_offset() const { return _bit_offset; }
  67. };
  68. /// \defgroup ColorBaseModelNonAlignedPixel bit_aligned_pixel_reference
  69. /// \ingroup ColorBaseModel
  70. /// \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.
  71. ///
  72. /// \defgroup PixelModelNonAlignedPixel bit_aligned_pixel_reference
  73. /// \ingroup PixelModel
  74. /// \brief A heterogeneous pixel reference used to represent non-byte-aligned pixels. Models PixelConcept
  75. ///
  76. /// Example:
  77. /// \code
  78. /// unsigned char data=0;
  79. ///
  80. /// // A mutable reference to a 6-bit BGR pixel in "123" format (1 bit for red, 2 bits for green, 3 bits for blue)
  81. /// using rgb123_ref_t = bit_aligned_pixel_reference<unsigned char, mp11::mp_list_c<int,1,2,3>, rgb_layout_t, true> const;
  82. ///
  83. /// // create the pixel reference at bit offset 2
  84. /// // (i.e. red = [2], green = [3,4], blue = [5,6,7] bits)
  85. /// rgb123_ref_t ref(&data, 2);
  86. /// get_color(ref, red_t()) = 1;
  87. /// assert(data == 0x04);
  88. /// get_color(ref, green_t()) = 3;
  89. /// assert(data == 0x1C);
  90. /// get_color(ref, blue_t()) = 7;
  91. /// assert(data == 0xFC);
  92. /// \endcode
  93. ///
  94. /// \ingroup ColorBaseModelNonAlignedPixel PixelModelNonAlignedPixel PixelBasedModel
  95. /// \brief Heterogeneous pixel reference corresponding to non-byte-aligned bit range. Models ColorBaseConcept, PixelConcept, PixelBasedConcept
  96. ///
  97. /// \tparam BitField
  98. /// \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>
  99. /// \tparam Layout
  100. /// \tparam IsMutable
  101. template <typename BitField, typename ChannelBitSizes, typename Layout, bool IsMutable>
  102. struct bit_aligned_pixel_reference
  103. {
  104. static constexpr int bit_size =
  105. mp11::mp_fold
  106. <
  107. ChannelBitSizes,
  108. std::integral_constant<int, 0>,
  109. mp11::mp_plus
  110. >::value;
  111. using bit_range_t = boost::gil::bit_range<bit_size,IsMutable>;
  112. using bitfield_t = BitField;
  113. using data_ptr_t = mp11::mp_if_c<IsMutable, unsigned char*, const unsigned char*>;
  114. using layout_t = Layout;
  115. using value_type = typename packed_pixel_type<bitfield_t,ChannelBitSizes,Layout>::type;
  116. using reference = const bit_aligned_pixel_reference<BitField, ChannelBitSizes, Layout, IsMutable>;
  117. using const_reference = bit_aligned_pixel_reference<BitField,ChannelBitSizes,Layout,false> const;
  118. static constexpr bool is_mutable = IsMutable;
  119. bit_aligned_pixel_reference(){}
  120. bit_aligned_pixel_reference(data_ptr_t data_ptr, int bit_offset) : _bit_range(data_ptr, bit_offset) {}
  121. explicit bit_aligned_pixel_reference(const bit_range_t& bit_range) : _bit_range(bit_range) {}
  122. template <bool IsMutable2> bit_aligned_pixel_reference(const bit_aligned_pixel_reference<BitField,ChannelBitSizes,Layout,IsMutable2>& p) : _bit_range(p._bit_range) {}
  123. // Grayscale references can be constructed from the channel reference
  124. explicit bit_aligned_pixel_reference(typename kth_element_type<bit_aligned_pixel_reference,0>::type const channel0)
  125. : _bit_range(static_cast<data_ptr_t>(&channel0), channel0.first_bit())
  126. {
  127. static_assert(num_channels<bit_aligned_pixel_reference>::value == 1, "");
  128. }
  129. // Construct from another compatible pixel type
  130. bit_aligned_pixel_reference(bit_aligned_pixel_reference const& p)
  131. : _bit_range(p._bit_range) {}
  132. // TODO: Why p by non-const reference?
  133. template <typename BF, typename CR>
  134. bit_aligned_pixel_reference(packed_pixel<BF, CR, Layout>& p)
  135. : _bit_range(static_cast<data_ptr_t>(&gil::at_c<0>(p)), gil::at_c<0>(p).first_bit())
  136. {
  137. check_compatible<packed_pixel<BF, CR, Layout>>();
  138. }
  139. auto operator=(bit_aligned_pixel_reference const& p) const
  140. -> bit_aligned_pixel_reference const&
  141. {
  142. static_copy(p, *this);
  143. return *this;
  144. }
  145. template <typename P>
  146. auto operator=(P const& p) const -> bit_aligned_pixel_reference const&
  147. {
  148. assign(p, is_pixel<P>());
  149. return *this;
  150. }
  151. template <typename P>
  152. bool operator==(P const& p) const
  153. {
  154. return equal(p, is_pixel<P>());
  155. }
  156. template <typename P>
  157. bool operator!=(P const& p) const { return !(*this==p); }
  158. auto operator->() const -> bit_aligned_pixel_reference const* { return this; }
  159. bit_range_t const& bit_range() const { return _bit_range; }
  160. private:
  161. mutable bit_range_t _bit_range;
  162. template <typename B, typename C, typename L, bool M> friend struct bit_aligned_pixel_reference;
  163. template <typename Pixel> static void check_compatible() { gil_function_requires<PixelsCompatibleConcept<Pixel,bit_aligned_pixel_reference> >(); }
  164. template <typename Pixel>
  165. void assign(Pixel const& p, std::true_type) const
  166. {
  167. check_compatible<Pixel>();
  168. static_copy(p, *this);
  169. }
  170. template <typename Pixel>
  171. bool equal(Pixel const& p, std::true_type) const
  172. {
  173. check_compatible<Pixel>();
  174. return static_equal(*this, p);
  175. }
  176. private:
  177. static void check_gray()
  178. {
  179. static_assert(std::is_same<typename Layout::color_space_t, gray_t>::value, "");
  180. }
  181. template <typename Channel>
  182. void assign(Channel const& channel, std::false_type) const
  183. {
  184. check_gray();
  185. gil::at_c<0>(*this) = channel;
  186. }
  187. template <typename Channel>
  188. bool equal (Channel const& channel, std::false_type) const
  189. {
  190. check_gray();
  191. return gil::at_c<0>(*this) == channel;
  192. }
  193. };
  194. /////////////////////////////
  195. // ColorBasedConcept
  196. /////////////////////////////
  197. template <typename BitField, typename ChannelBitSizes, typename L, bool IsMutable, int K>
  198. struct kth_element_type
  199. <
  200. bit_aligned_pixel_reference<BitField, ChannelBitSizes, L, IsMutable>,
  201. K
  202. >
  203. {
  204. using type = packed_dynamic_channel_reference
  205. <
  206. BitField,
  207. mp11::mp_at_c<ChannelBitSizes, K>::value,
  208. IsMutable
  209. > const;
  210. };
  211. template <typename B, typename C, typename L, bool M, int K>
  212. struct kth_element_reference_type<bit_aligned_pixel_reference<B,C,L,M>, K>
  213. : public kth_element_type<bit_aligned_pixel_reference<B,C,L,M>, K> {};
  214. template <typename B, typename C, typename L, bool M, int K>
  215. struct kth_element_const_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. namespace detail {
  218. // returns sum of IntegralVector[0] ... IntegralVector[K-1]
  219. template <typename IntegralVector, int K>
  220. struct sum_k
  221. : mp11::mp_plus
  222. <
  223. sum_k<IntegralVector, K - 1>,
  224. typename mp11::mp_at_c<IntegralVector, K - 1>::type
  225. >
  226. {};
  227. template <typename IntegralVector>
  228. struct sum_k<IntegralVector, 0> : std::integral_constant<int, 0> {};
  229. } // namespace detail
  230. // at_c required by MutableColorBaseConcept
  231. template <int K, typename BitField, typename ChannelBitSizes, typename L, bool IsMutable>
  232. inline
  233. auto at_c(const bit_aligned_pixel_reference<BitField, ChannelBitSizes, L, IsMutable>& p)
  234. -> typename kth_element_reference_type<bit_aligned_pixel_reference<BitField, ChannelBitSizes, L, IsMutable>, K>::type
  235. {
  236. using pixel_t = bit_aligned_pixel_reference<BitField, ChannelBitSizes, L, IsMutable>;
  237. using channel_t = typename kth_element_reference_type<pixel_t, K>::type;
  238. using bit_range_t = typename pixel_t::bit_range_t;
  239. bit_range_t bit_range(p.bit_range());
  240. bit_range.bit_advance(detail::sum_k<ChannelBitSizes, K>::value);
  241. return channel_t(bit_range.current_byte(), bit_range.bit_offset());
  242. }
  243. /////////////////////////////
  244. // PixelConcept
  245. /////////////////////////////
  246. /// Metafunction predicate that flags bit_aligned_pixel_reference as a model of PixelConcept. Required by PixelConcept
  247. template <typename B, typename C, typename L, bool M>
  248. struct is_pixel<bit_aligned_pixel_reference<B, C, L, M> > : std::true_type {};
  249. /////////////////////////////
  250. // PixelBasedConcept
  251. /////////////////////////////
  252. template <typename B, typename C, typename L, bool M>
  253. struct color_space_type<bit_aligned_pixel_reference<B, C, L, M>>
  254. {
  255. using type = typename L::color_space_t;
  256. };
  257. template <typename B, typename C, typename L, bool M>
  258. struct channel_mapping_type<bit_aligned_pixel_reference<B, C, L, M>>
  259. {
  260. using type = typename L::channel_mapping_t;
  261. };
  262. template <typename B, typename C, typename L, bool M>
  263. struct is_planar<bit_aligned_pixel_reference<B, C, L, M>> : std::false_type {};
  264. /////////////////////////////
  265. // pixel_reference_type
  266. /////////////////////////////
  267. // Constructs a homogeneous bit_aligned_pixel_reference given a channel reference
  268. template <typename BitField, int NumBits, typename Layout>
  269. struct pixel_reference_type
  270. <
  271. packed_dynamic_channel_reference<BitField, NumBits, false> const,
  272. Layout, false, false
  273. >
  274. {
  275. private:
  276. using channel_bit_sizes_t = mp11::mp_repeat
  277. <
  278. mp11::mp_list<std::integral_constant<unsigned, NumBits>>,
  279. mp11::mp_size<typename Layout::color_space_t>
  280. >;
  281. public:
  282. using type =
  283. bit_aligned_pixel_reference<BitField, channel_bit_sizes_t, Layout, false>;
  284. };
  285. // Same but for the mutable case. We cannot combine the mutable
  286. // and read-only cases because this triggers ambiguity
  287. template <typename BitField, int NumBits, typename Layout>
  288. struct pixel_reference_type
  289. <
  290. packed_dynamic_channel_reference<BitField, NumBits, true> const,
  291. Layout, false, true
  292. >
  293. {
  294. private:
  295. using channel_bit_sizes_t = mp11::mp_repeat
  296. <
  297. mp11::mp_list<std::integral_constant<unsigned, NumBits>>,
  298. mp11::mp_size<typename Layout::color_space_t>
  299. >;
  300. public:
  301. using type = bit_aligned_pixel_reference<BitField, channel_bit_sizes_t, Layout, true>;
  302. };
  303. } } // namespace boost::gil
  304. namespace std {
  305. // We are forced to define swap inside std namespace because on some platforms (Visual Studio 8) STL calls swap qualified.
  306. // swap with 'left bias':
  307. // - swap between proxy and anything
  308. // - swap between value type and proxy
  309. // - swap between proxy and proxy
  310. // Having three overloads allows us to swap between different (but compatible) models of PixelConcept
  311. template <typename B, typename C, typename L, typename R> inline
  312. void swap(const boost::gil::bit_aligned_pixel_reference<B,C,L,true> x, R& y) {
  313. boost::gil::swap_proxy<typename boost::gil::bit_aligned_pixel_reference<B,C,L,true>::value_type>(x,y);
  314. }
  315. template <typename B, typename C, typename L> inline
  316. 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) {
  317. boost::gil::swap_proxy<typename boost::gil::bit_aligned_pixel_reference<B,C,L,true>::value_type>(x,y);
  318. }
  319. template <typename B, typename C, typename L> inline
  320. void swap(const boost::gil::bit_aligned_pixel_reference<B,C,L,true> x, const boost::gil::bit_aligned_pixel_reference<B,C,L,true> y) {
  321. boost::gil::swap_proxy<typename boost::gil::bit_aligned_pixel_reference<B,C,L,true>::value_type>(x,y);
  322. }
  323. } // namespace std
  324. #endif