80 likes | 239 Views
Bit Output using a BitOStream. next output position. bits already output. data_buffer. 00000 0 10. bit_mask. 00000 1 00. data_buffer holds the bits being output. class BitOStream { private: unsigned char data_buffer ; unsigned char bit_mask ; ofstream fout ;.
E N D
Bit Output using a BitOStream next output position bits already output data_buffer 00000010 bit_mask 00000100 data_bufferholdsthe bits being output class BitOStream { private: unsigned char data_buffer; unsigned char bit_mask; ofstreamfout; bit_maskspecifiesthe position in the data byte for the next output bit A completely filled data byte will be written to fout The maskcontainsexactly one 1and its position corresponds to the bit positionwhere the next bit thatis output willbewritten.
Bit Input using a BitIStream class BitIStream { private: unsigned char data_buffer; unsigned char bit_mask; unsignedchar nextByte; intbitsInLastByte; boolinLastByte; boolvalidData; boolbitmode; ifstreamfin; data_bufferholds the bits to be input bit_maskspecifiesthe position in the data byte for the next input bit nextByte is a prefetched byte that that will be transferred to the databuffer when its bits are consumed bitsInLastByteis the number of valid bits in the last byte of the file inLastByteis a flag showing if the last byte of the file isbeingprocessed indicates switch from character output to bit output validDataindicates bit is available fin contains the bytes from which bits to be input will be obtained.
//----------------BitOStream.h------------------------------ // Class for bit output on a file.*/ #include <string> #include <fstream> using namespace std; class BitOStream { private: unsigned char data_buffer; unsigned char bit_mask; ofstreamfout; public: BitOStream(string fname); ~BitOStream(); void close(); void putBit(char theBit); void putBitstring(string bits // Following must not be used after any call to putBit or // putBitstring void putChar(unsigned char ch); void putInt(int n); };
//------------------BitOStream.h-----------------------------///------------------BitOStream.h-----------------------------/ // Class for bit input from a file producedwith bit output. / //-----------------------------h-----------------------------/ #pragma once #include <iostream> #include <string> #include <fstream> usingnamespacestd; class BitIStream { private: char data_buffer; char nextByte; char bit_mask; intbitsInLastByte; boolvalidData; boolinLastByte; ifstream fin; boolbitmode;
voidBitOStream::putBit(char theBit) { assert(theBit == '0' || theBit == '1'); if (theBit == '1') data_buffer |= bit_mask; bit_mask <<= 1; if (!bit_mask) { fout << data_buffer; data_buffer = 0; bit_mask = 1; } }
boolBitIStream::getBit(char & theBit) { assert(bitmode); staticintbitsconsumed = 0; if (!validData) { return false; } if (data_buffer & bit_mask) theBit = '1'; else theBit = '0'; bit_mask <<= 1; if (inLastByte) { bitsconsumed++; if (bitsconsumed == bitsInLastByte) validData = false; }
elseif (!bit_mask) // All valid bits in data_buffer have been consumed { bit_mask = 1; if (!inLastByte) { data_buffer = nextByte; fin.get(nextByte); inLastByte = fin.eof(); } else validData = false; } return true; }
symbolCount Leaf symbols . . . . . . . . . Bit encoded code tree Encoded text file symbolCount bytes bits in last byte Output as bytes Output as bits Compressed File Format