bit_aligned_pixel_reference.hpp 14 KB

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