home *** CD-ROM | disk | FTP | other *** search
/ Compressed Image File Formats / CompressedImageFileFormatsJohnMiano.iso / pc / Examples / c06 / src / jpegenco.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1998-12-20  |  783 b   |  43 lines

  1. #include "jpegenco.h"
  2.  
  3. using namespace std ;
  4.  
  5. JpegEncoder::JpegEncoder (ostream &strm)
  6. {
  7.   output_stream = &strm ;
  8.   bit_count = 0 ;
  9.   return ;
  10. }
  11. void JpegEncoder::OutputByte (UBYTE1 data)
  12. {
  13.   output_stream->write ((char *) &data, 1) ;
  14.   return ;
  15. }
  16.  
  17. void JpegEncoder::OutputBits (int bits, unsigned int count)
  18. {
  19.   for (unsigned int ii = 0 ; ii < count ; ++ ii)
  20.   {
  21.     bit_buffer <<= 1 ;
  22.     ++ bit_count ;
  23.     bit_buffer |= ((bits >> (count - ii - 1)) & 0x1) ;
  24.     if (bit_count == 8)
  25.     {
  26.       OutputByte (bit_buffer) ;
  27.       bit_buffer = 0 ;
  28.       bit_count = 0 ;
  29.     }
  30.   }
  31.   return ;
  32. }
  33.  
  34. void JpegEncoder::FlushBitBuffer ()
  35. {
  36.   if (bit_count != 0)
  37.   {
  38.     bit_buffer <<= (8 - bit_count) ;
  39.     OutputByte (bit_buffer) ;
  40.   }
  41.   return ;
  42. }
  43.