Add some bit utils and initial l77 encoder.

This commit is contained in:
James Grogan 2022-11-22 17:37:06 +00:00
parent ff962a6b16
commit 318b481ccc
12 changed files with 508 additions and 117 deletions

View file

@ -0,0 +1,56 @@
#include "BitStream.h"
#include "ByteUtils.h"
bool BitStream::loadNextByte()
{
if (mByteOffset + 1 == mBuffer.size())
{
return false;
}
else
{
mByteOffset++;
mCurrentByte = mBuffer[mByteOffset];
return true;
}
}
bool BitStream::getNextNBits(unsigned n, unsigned char& buffer)
{
int overshoot = n + mBitOffset - 7;
if (overshoot > 0)
{
unsigned char last_byte = mCurrentByte;
if (!loadNextByte())
{
return false;
}
auto num_lower = 7 - mBitOffset;
char lower_bits = ByteUtils::getHigherNBits(last_byte, num_lower);
char higher_bits = ByteUtils::getLowerNBits(mCurrentByte, overshoot);
buffer = (higher_bits << (8 - num_lower)) | (lower_bits >> mBitOffset);
mBitOffset = overshoot;
return true;
}
else
{
buffer = ByteUtils::getMBitsAtN(mCurrentByte, n, mBitOffset);
mBitOffset += n;
return true;
}
}
void BitStream::setByte(unsigned idx, unsigned char data)
{
mBuffer[idx] = data;
}
void BitStream::setBufferSize(std::size_t size)
{
mBuffer = std::vector<unsigned char>(size);
}