Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "OF12BitStream.h"
- using namespace std;
- OF12BitStream::OF12BitStream()
- {
- init();
- }
- OF12BitStream::OF12BitStream(const char * aFileName)
- {
- open(aFileName);
- init();
- }
- OF12BitStream::~OF12BitStream()
- {
- close();
- }
- void OF12BitStream::init()
- {
- for (int i = 0; i < 32; i++) {
- fBuffer[i] = 0;
- }
- fByteIndex = 0;
- fBitIndex = 8;
- }
- void OF12BitStream::writeBit0()
- {
- fBitIndex--;
- finishWriteBit();
- }
- void OF12BitStream::writeBit1()
- {
- fBuffer[fByteIndex] += 1 << (fBitIndex - 1);
- fBitIndex--;
- finishWriteBit();
- }
- void OF12BitStream::finishWriteBit()
- {
- if (fBitIndex == 0)
- {
- if (fByteIndex == 31)
- {
- fByteIndex++;
- flush();
- }
- else
- {
- fByteIndex++;
- fBitIndex = 8;
- }
- }
- }
- void OF12BitStream::open(const char * aFileName)
- {
- ofstream::open(aFileName, ofstream::binary);
- }
- void OF12BitStream::close()
- {
- flush();
- ofstream::close();
- }
- OF12BitStream & OF12BitStream::put(int aValue)
- {
- fBuffer[fByteIndex] = aValue;
- fByteIndex++;
- return *this;
- }
- OF12BitStream & OF12BitStream::write(int aValues[], unsigned int aNElements)
- {
- for (int i = 0; i < aNElements; i++) {
- aValues[i] = aValues[i] & 0x0fff; // mask 12 lower bits
- for (int i = 0; i < 12; i++) // write 12 bits
- {
- if (aValues[i] & 0x01) // the current lowest bit is set.
- writeBit1();
- else
- writeBit0();
- aValues[i] >>= 1; // code := code / 2
- }
- put(aValues[i]);
- }
- return *this;
- }
- OF12BitStream & OF12BitStream::operator<<(int aValue)
- {
- put(aValue);
- return *this;
- }
- OF12BitStream & OF12BitStream::flush()
- {
- write((int*)fBuffer, fByteIndex + (fBitIndex % 8 ? 1 : 0));
- return *this;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement