bit_aligned_pixel_reference.hpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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_BIT_ALIGNED_PIXEL_REFERENCE_HPP
  9. #define BOOST_GIL_BIT_ALIGNED_PIXEL_REFERENCE_HPP
  10. #include <boost/gil/pixel.hpp>
  11. #include <boost/gil/channel.hpp>
  12. #include <boost/assert.hpp>
  13. #include <boost/config.hpp>
  14. #include <boost/mpl/accumulate.hpp>
  15. #include <boost/mpl/at.hpp>
  16. #include <boost/mpl/bool.hpp>
  17. #include <boost/mpl/if.hpp>
  18. #include <boost/mpl/plus.hpp>
  19. #include <boost/mpl/push_back.hpp>
  20. #include <boost/mpl/vector.hpp>
  21. #include <functional>
  22. #include <type_traits>
  23. namespace boost { namespace gil {
  24. /// A model of a heterogeneous pixel that is not byte aligned.
  25. /// Examples are bitmap (1-bit pixels) or 6-bit RGB (222).
  26. /////////////////////////////
  27. // bit_range
  28. //
  29. // 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.
  30. /////////////////////////////
  31. template <int RangeSize, bool Mutable>
  32. class bit_range {
  33. public:
  34. using byte_t = typename mpl::if_c<Mutable,unsigned char,const unsigned char>::type;
  35. using difference_type = std::ptrdiff_t;
  36. template <int RS, bool M> friend class bit_range;
  37. private:
  38. byte_t* _current_byte; // the starting byte of the bit range
  39. int _bit_offset; // offset from the beginning of the current byte. 0<=_bit_offset<=7
  40. public:
  41. bit_range() : _current_byte(nullptr), _bit_offset(0) {}
  42. bit_range(byte_t* current_byte, int bit_offset)
  43. : _current_byte(current_byte)
  44. , _bit_offset(bit_offset)
  45. {
  46. BOOST_ASSERT(bit_offset >= 0 && bit_offset < 8);
  47. }
  48. bit_range(const bit_range& br) : _current_byte(br._current_byte), _bit_offset(br._bit_offset) {}
  49. template <bool M> bit_range(const bit_range<RangeSize,M>& br) : _current_byte(br._current_byte), _bit_offset(br._bit_offset) {}
  50. bit_range& operator=(const bit_range& br) { _current_byte = br._current_byte; _bit_offset=br._bit_offset; return *this; }
  51. bool operator==(const bit_range& br) const { return _current_byte==br._current_byte && _bit_offset==br._bit_offset; }
  52. bit_range& operator++() {
  53. _current_byte += (_bit_offset+RangeSize) / 8;
  54. _bit_offset = (_bit_offset+RangeSize) % 8;
  55. return *this;
  56. }
  57. bit_range& operator--() { bit_advance(-RangeSize); return *this; }
  58. void bit_advance(difference_type num_bits) {
  59. int new_offset = int(_bit_offset+num_bits);
  60. _current_byte += new_offset / 8;
  61. _bit_offset = new_offset % 8;
  62. if (_bit_offset<0) {
  63. _bit_offset+=8;
  64. --_current_byte;
  65. }
  66. }
  67. difference_type bit_distance_to(const bit_range& b) const {
  68. return (b.current_byte() - current_byte())*8 + b.bit_offset()-bit_offset();
  69. }
  70. byte_t* current_byte() const { return _current_byte; }
  71. int bit_offset() const { return _bit_offset; }
  72. };
  73. /// \defgroup ColorBaseModelNonAlignedPixel bit_aligned_pixel_reference
  74. /// \ingroup ColorBaseModel
  75. /// \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.
  76. ///
  77. /// \defgroup PixelModelNonAlignedPixel bit_aligned_pixel_reference
  78. /// \ingroup PixelModel
  79. /// \brief A heterogeneous pixel reference used to represent non-byte-aligned pixels. Models PixelConcept
  80. ///
  81. /// Example:
  82. /// \code
  83. /// unsigned char data=0;
  84. ///
  85. /// // A mutable reference to a 6-bit BGR pixel in "123" format (1 bit for red, 2 bits for green, 3 bits for blue)
  86. /// using rgb123_ref_t = bit_aligned_pixel_reference<unsigned char, mpl::vector3_c<int,1,2,3>, rgb_layout_t, true> const;
  87. ///
  88. /// // create the pixel reference at bit offset 2
  89. /// // (i.e. red = [2], green = [3,4], blue = [5,6,7] bits)
  90. /// rgb123_ref_t ref(&data, 2);
  91. /// get_color(ref, red_t()) = 1;
  92. /// assert(data == 0x04);
  93. /// get_color(ref, green_t()) = 3;
  94. /// assert(data == 0x1C);
  95. /// get_color(ref, blue_t()) = 7;
  96. /// assert(data == 0xFC);
  97. /// \endcode
  98. ///
  99. /// \ingroup ColorBaseModelNonAlignedPixel PixelModelNonAlignedPixel PixelBasedModel
  100. /// \brief Heterogeneous pixel reference corresponding to non-byte-aligned bit range. Models ColorBaseConcept, PixelConcept, PixelBasedConcept
  101. ///
  102. /// \tparam BitField
  103. /// \tparam ChannelBitSizes MPL integral vector defining the number of bits for each channel. For example, for 565RGB, vector_c<int,5,6,5>
  104. /// \tparam Layout
  105. /// \tparam IsMutable
  106. template <typename BitField, typename ChannelBitSizes, typename Layout, bool IsMutable>
  107. struct bit_aligned_pixel_reference
  108. {
  109. static constexpr int bit_size =
  110. mpl::accumulate
  111. <
  112. ChannelBitSizes,
  113. mpl::int_<0>,
  114. mpl::plus<mpl::_1, mpl::_2>
  115. >::type::value;
  116. using bit_range_t = boost::gil::bit_range<bit_size,IsMutable>;
  117. using bitfield_t = BitField;
  118. using data_ptr_t =typename mpl::if_c<IsMutable,unsigned char*,const unsigned char*>::type;
  119. using layout_t = Layout;
  120. using value_type = typename packed_pixel_type<bitfield_t,ChannelBitSizes,Layout>::type;
  121. using reference = const bit_aligned_pixel_reference<BitField, ChannelBitSizes, Layout, IsMutable>;
  122. using const_reference = bit_aligned_pixel_reference<BitField,ChannelBitSizes,Layout,false> const;
  123. static constexpr bool is_mutable = IsMutable;
  124. bit_aligned_pixel_reference(){}
  125. bit_aligned_pixel_reference(data_ptr_t data_ptr, int bit_offset) : _bit_range(data_ptr, bit_offset) {}
  126. explicit bit_aligned_pixel_reference(const bit_range_t& bit_range) : _bit_range(bit_range) {}
  127. template <bool IsMutable2> bit_aligned_pixel_reference(const bit_aligned_pixel_reference<BitField,ChannelBitSizes,Layout,IsMutable2>& p) : _bit_range(p._bit_range) {}
  128. // Grayscale references can be constructed from the channel reference
  129. explicit bit_aligned_pixel_reference(typename kth_element_type<bit_aligned_pixel_reference,0>::type const channel0)
  130. : _bit_range(static_cast<data_ptr_t>(&channel0), channel0.first_bit())
  131. {
  132. static_assert(num_channels<bit_aligned_pixel_reference>::value == 1, "");
  133. }
  134. // Construct from another compatible pixel type
  135. bit_aligned_pixel_reference(const bit_aligned_pixel_reference& p) : _bit_range(p._bit_range) {}
  136. template <typename BF, typename CR> bit_aligned_pixel_reference(packed_pixel<BF,CR,Layout>& p) : _bit_range(static_cast<data_ptr_t>(&gil::at_c<0>(p)), gil::at_c<0>(p).first_bit()) {
  137. check_compatible<packed_pixel<BF,CR,Layout> >();
  138. }
  139. const bit_aligned_pixel_reference& operator=(const bit_aligned_pixel_reference& p) const { static_copy(p,*this); return *this; }
  140. template <typename P> const bit_aligned_pixel_reference& operator=(const P& p) const { assign(p, mpl::bool_<is_pixel<P>::value>()); return *this; }
  141. template <typename P> bool operator==(const P& p) const { return equal(p, mpl::bool_<is_pixel<P>::value>()); }
  142. template <typename P> bool operator!=(const P& p) const { return !(*this==p); }
  143. const bit_aligned_pixel_reference* operator->() const { return this; }
  144. const bit_range_t& bit_range() const { return _bit_range; }
  145. private:
  146. mutable bit_range_t _bit_range;
  147. template <typename B, typename C, typename L, bool M> friend struct bit_aligned_pixel_reference;
  148. template <typename Pixel> static void check_compatible() { gil_function_requires<PixelsCompatibleConcept<Pixel,bit_aligned_pixel_reference> >(); }
  149. template <typename Pixel> void assign(const Pixel& p, mpl::true_) const { check_compatible<Pixel>(); static_copy(p,*this); }
  150. template <typename Pixel> bool equal(const Pixel& p, mpl::true_) const { check_compatible<Pixel>(); return static_equal(*this,p); }
  151. private:
  152. static void check_gray()
  153. {
  154. static_assert(std::is_same<typename Layout::color_space_t, gray_t>::value, "");
  155. }
  156. template <typename Channel> void assign(const Channel& chan, mpl::false_) const { check_gray(); gil::at_c<0>(*this)=chan; }
  157. template <typename Channel> bool equal (const Channel& chan, mpl::false_) const { check_gray(); return gil::at_c<0>(*this)==chan; }
  158. };
  159. /////////////////////////////
  160. // ColorBasedConcept
  161. /////////////////////////////
  162. template <typename BitField, typename ChannelBitSizes, typename L, bool IsMutable, int K>
  163. struct kth_element_type<bit_aligned_pixel_reference<BitField,ChannelBitSizes,L,IsMutable>, K>
  164. {
  165. using type = packed_dynamic_channel_reference<BitField, mpl::at_c<ChannelBitSizes,K>::type::value, IsMutable> const;
  166. };
  167. template <typename B, typename C, typename L, bool M, int K>
  168. struct kth_element_reference_type<bit_aligned_pixel_reference<B,C,L,M>, K>
  169. : public kth_element_type<bit_aligned_pixel_reference<B,C,L,M>, K> {};
  170. template <typename B, typename C, typename L, bool M, int K>
  171. struct kth_element_const_reference_type<bit_aligned_pixel_reference<B,C,L,M>, K>
  172. : public kth_element_type<bit_aligned_pixel_reference<B,C,L,M>, K> {};
  173. namespace detail {
  174. // returns sum of IntegralVector[0] ... IntegralVector[K-1]
  175. template <typename IntegralVector, int K>
  176. struct sum_k : public mpl::plus<sum_k<IntegralVector,K-1>, typename mpl::at_c<IntegralVector,K-1>::type > {};
  177. template <typename IntegralVector> struct sum_k<IntegralVector,0> : public mpl::int_<0> {};
  178. }
  179. // at_c required by MutableColorBaseConcept
  180. template <int K, typename BitField, typename ChannelBitSizes, typename L, bool Mutable> inline
  181. typename kth_element_reference_type<bit_aligned_pixel_reference<BitField,ChannelBitSizes,L,Mutable>,K>::type
  182. at_c(const bit_aligned_pixel_reference<BitField,ChannelBitSizes,L,Mutable>& p)
  183. {
  184. using pixel_t = bit_aligned_pixel_reference<BitField,ChannelBitSizes,L,Mutable>;
  185. using channel_t = typename kth_element_reference_type<pixel_t,K>::type;
  186. using bit_range_t = typename pixel_t::bit_range_t;
  187. bit_range_t bit_range(p.bit_range());
  188. bit_range.bit_advance(detail::sum_k<ChannelBitSizes,K>::value);
  189. return channel_t(bit_range.current_byte(), bit_range.bit_offset());
  190. }
  191. /////////////////////////////
  192. // PixelConcept
  193. /////////////////////////////
  194. /// Metafunction predicate that flags bit_aligned_pixel_reference as a model of PixelConcept. Required by PixelConcept
  195. template <typename B, typename C, typename L, bool M>
  196. struct is_pixel<bit_aligned_pixel_reference<B,C,L,M> > : public mpl::true_{};
  197. /////////////////////////////
  198. // PixelBasedConcept
  199. /////////////////////////////
  200. template <typename B, typename C, typename L, bool M>
  201. struct color_space_type<bit_aligned_pixel_reference<B,C,L,M> > {
  202. using type = typename L::color_space_t;
  203. };
  204. template <typename B, typename C, typename L, bool M>
  205. struct channel_mapping_type<bit_aligned_pixel_reference<B,C,L,M> > {
  206. using type = typename L::channel_mapping_t;
  207. };
  208. template <typename B, typename C, typename L, bool M>
  209. struct is_planar<bit_aligned_pixel_reference<B,C,L,M> > : mpl::false_ {};
  210. /////////////////////////////
  211. // pixel_reference_type
  212. /////////////////////////////
  213. namespace detail {
  214. // returns a vector containing K copies of the type T
  215. template <unsigned K, typename T> struct k_copies;
  216. template <typename T> struct k_copies<0,T> {
  217. using type = mpl::vector0<>;
  218. };
  219. template <unsigned K, typename T> struct k_copies : public mpl::push_back<typename k_copies<K-1,T>::type, T> {};
  220. }
  221. // Constructs a homogeneous bit_aligned_pixel_reference given a channel reference
  222. template <typename BitField, int NumBits, typename Layout>
  223. struct pixel_reference_type<const packed_dynamic_channel_reference<BitField,NumBits,false>, Layout, false, false>
  224. {
  225. private:
  226. using size_t = typename mpl::size<typename Layout::color_space_t>::type;
  227. using channel_bit_sizes_t = typename detail::k_copies<size_t::value,mpl::integral_c<unsigned,NumBits> >::type;
  228. public:
  229. using type = bit_aligned_pixel_reference<BitField, channel_bit_sizes_t, Layout, false>;
  230. };
  231. // Same but for the mutable case. We cannot combine the mutable and read-only cases because this triggers ambiguity
  232. template <typename BitField, int NumBits, typename Layout>
  233. struct pixel_reference_type<const packed_dynamic_channel_reference<BitField,NumBits,true>, Layout, false, true>
  234. {
  235. private:
  236. using size_t = typename mpl::size<typename Layout::color_space_t>::type;
  237. using channel_bit_sizes_t = typename detail::k_copies<size_t::value,mpl::integral_c<unsigned,NumBits>>::type;
  238. public:
  239. using type = bit_aligned_pixel_reference<BitField, channel_bit_sizes_t, Layout, true>;
  240. };
  241. } } // namespace boost::gil
  242. namespace std {
  243. // We are forced to define swap inside std namespace because on some platforms (Visual Studio 8) STL calls swap qualified.
  244. // swap with 'left bias':
  245. // - swap between proxy and anything
  246. // - swap between value type and proxy
  247. // - swap between proxy and proxy
  248. // Having three overloads allows us to swap between different (but compatible) models of PixelConcept
  249. template <typename B, typename C, typename L, typename R> inline
  250. void swap(const boost::gil::bit_aligned_pixel_reference<B,C,L,true> x, R& y) {
  251. boost::gil::swap_proxy<typename boost::gil::bit_aligned_pixel_reference<B,C,L,true>::value_type>(x,y);
  252. }
  253. template <typename B, typename C, typename L> inline
  254. 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) {
  255. boost::gil::swap_proxy<typename boost::gil::bit_aligned_pixel_reference<B,C,L,true>::value_type>(x,y);
  256. }
  257. template <typename B, typename C, typename L> inline
  258. 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) {
  259. boost::gil::swap_proxy<typename boost::gil::bit_aligned_pixel_reference<B,C,L,true>::value_type>(x,y);
  260. }
  261. } // namespace std
  262. #endif