Further compression and png work.

This commit is contained in:
James Grogan 2022-11-23 15:41:33 +00:00
parent 318b481ccc
commit 9c8faa534b
34 changed files with 1164 additions and 203 deletions

View file

@ -2,37 +2,70 @@
#include "ByteUtils.h"
bool BitStream::loadNextByte()
#include <sstream>
#include <iostream>
BitStream::~BitStream()
{
if (mByteOffset + 1 == mBuffer.size())
{
return false;
}
else
{
mByteOffset++;
mCurrentByte = mBuffer[mByteOffset];
return true;
}
}
bool BitStream::getNextNBits(unsigned n, unsigned char& buffer)
unsigned char BitStream::getCurrentByte()
{
int overshoot = n + mBitOffset - 7;
if (mByteOffset < 0)
{
readNextByte();
}
return mCurrentByte;
}
int BitStream::getCurrentByteOffset() const
{
return mByteOffset;
}
unsigned BitStream::getCurrentBitOffset() const
{
return mBitOffset;
}
std::string BitStream::logNextNBytes(unsigned n) const
{
std::stringstream sstr;
unsigned count{0};
for(auto byte : peekNextNBytes(n))
{
sstr << count << " | " << ByteUtils::toString(byte) + '\n';
count++;
}
return sstr.str();
}
bool BitStream::readNextNBits(unsigned n, unsigned char& buffer)
{
if (mByteOffset < 0)
{
if (!readNextByte())
{
return false;
}
}
int overshoot = n + mBitOffset - 8;
if (overshoot > 0)
{
unsigned char last_byte = mCurrentByte;
if (!loadNextByte())
if (!readNextByte())
{
return false;
}
auto num_lower = 7 - mBitOffset;
auto num_lower = 8 - 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);
buffer = (higher_bits << num_lower) | lower_bits;
mBitOffset = overshoot;
return true;
@ -44,13 +77,3 @@ bool BitStream::getNextNBits(unsigned n, unsigned char& buffer)
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);
}