bit_operations.hpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. /*
  2. Copyright 2007-2008 Christian Henning, Andreas Pokorny, Lubomir Bourdev
  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. */
  7. /*************************************************************************************************/
  8. #ifndef BOOST_GIL_IO_BIT_OPERATIONS_HPP
  9. #define BOOST_GIL_IO_BIT_OPERATIONS_HPP
  10. ////////////////////////////////////////////////////////////////////////////////////////
  11. /// \file
  12. /// \brief
  13. /// \author Christian Henning \n
  14. ///
  15. /// \date 2008 \n
  16. ///
  17. ////////////////////////////////////////////////////////////////////////////////////////
  18. #include <boost/bind.hpp>
  19. #include <array>
  20. namespace boost { namespace gil { namespace detail {
  21. // 1110 1100 -> 0011 0111
  22. template< typename Buffer
  23. , typename IsBitAligned
  24. >
  25. struct mirror_bits
  26. {
  27. mirror_bits( bool ) {}
  28. void operator() ( Buffer& ) {}
  29. void operator() ( byte_t* , std::size_t )
  30. {
  31. }
  32. };
  33. // The functor will generate a lookup table since the
  34. // mirror operation is quite costly.
  35. template< typename Buffer >
  36. struct mirror_bits< Buffer
  37. , mpl::true_
  38. >
  39. {
  40. mirror_bits( bool apply_operation = true )
  41. : _apply_operation( apply_operation )
  42. {
  43. if( _apply_operation == true )
  44. {
  45. byte_t i = 0;
  46. do
  47. {
  48. _lookup[i] = mirror( i );
  49. }
  50. while( i++ != 255 );
  51. }
  52. }
  53. void operator() ( Buffer& buf )
  54. {
  55. if( _apply_operation == true )
  56. {
  57. for_each( buf.begin()
  58. , buf.end()
  59. , boost::bind( &mirror_bits< Buffer
  60. , mpl::true_
  61. >::lookup
  62. , *this
  63. , ::_1
  64. )
  65. );
  66. }
  67. }
  68. void operator() ( byte_t* dst, std::size_t size )
  69. {
  70. for( std::size_t i = 0; i < size; ++i )
  71. {
  72. lookup(*dst);
  73. ++dst;
  74. }
  75. }
  76. private:
  77. void lookup( byte_t& c )
  78. {
  79. c = _lookup[ c ];
  80. }
  81. static byte_t mirror( byte_t c )
  82. {
  83. byte_t result = 0;
  84. for( int i = 0; i < 8; ++i )
  85. {
  86. result = result << 1;
  87. result |= ( c & 1 );
  88. c = c >> 1;
  89. }
  90. return result;
  91. }
  92. private:
  93. bool _apply_operation;
  94. std::array< byte_t, 256 > _lookup;
  95. };
  96. // 0011 1111 -> 1100 0000
  97. template< typename Buffer
  98. , typename IsBitAligned
  99. >
  100. struct negate_bits
  101. {
  102. void operator() ( Buffer& ) {}
  103. };
  104. template< typename Buffer >
  105. struct negate_bits< Buffer, mpl::true_ >
  106. {
  107. void operator() ( Buffer& buf )
  108. {
  109. for_each( buf.begin()
  110. , buf.end()
  111. , negate_bits< Buffer, mpl::true_ >::negate
  112. );
  113. }
  114. void operator() ( byte_t* dst, std::size_t size )
  115. {
  116. for( std::size_t i = 0; i < size; ++i )
  117. {
  118. negate(*dst);
  119. ++dst;
  120. }
  121. }
  122. private:
  123. static void negate( byte_t& b )
  124. {
  125. b = ~b;
  126. }
  127. };
  128. // 11101100 -> 11001110
  129. template< typename Buffer
  130. , typename IsBitAligned
  131. >
  132. struct swap_half_bytes
  133. {
  134. void operator() ( Buffer& ) {}
  135. };
  136. template< typename Buffer >
  137. struct swap_half_bytes< Buffer
  138. , mpl::true_
  139. >
  140. {
  141. void operator() ( Buffer& buf )
  142. {
  143. for_each( buf.begin()
  144. , buf.end()
  145. , swap_half_bytes< Buffer, mpl::true_ >::swap
  146. );
  147. }
  148. void operator() ( byte_t* dst, std::size_t size )
  149. {
  150. for( std::size_t i = 0; i < size; ++i )
  151. {
  152. swap(*dst);
  153. ++dst;
  154. }
  155. }
  156. private:
  157. static void swap( byte_t& c )
  158. {
  159. c = (( c << 4 ) & 0xF0 ) | (( c >> 4 ) & 0x0F );
  160. }
  161. };
  162. template< typename Buffer >
  163. struct do_nothing
  164. {
  165. do_nothing() {}
  166. void operator() ( Buffer& ) {}
  167. };
  168. /// Count consecutive zeros on the right
  169. template< typename T >
  170. inline
  171. unsigned int trailing_zeros( T x )
  172. throw()
  173. {
  174. unsigned int n = 0;
  175. x = ~x & (x - 1);
  176. while( x )
  177. {
  178. n = n + 1;
  179. x = x >> 1;
  180. }
  181. return n;
  182. }
  183. /// Counts ones in a bit-set
  184. template< typename T >
  185. inline
  186. unsigned int count_ones( T x )
  187. throw()
  188. {
  189. unsigned int n = 0;
  190. while( x )
  191. {
  192. // clear the least significant bit set
  193. x &= x - 1;
  194. ++n;
  195. }
  196. return n;
  197. }
  198. } // namespace detail
  199. } // namespace gil
  200. } // namespace boost
  201. #endif