Circle buffer and png cleaning.

This commit is contained in:
James Grogan 2022-11-24 09:05:39 +00:00
parent 59cc910d58
commit 5400a232dd
13 changed files with 353 additions and 122 deletions

View file

@ -0,0 +1,64 @@
#pragma once
#include <vector>
#include <iostream>
template<typename T>
class CircleBuffer
{
public:
CircleBuffer(std::size_t size)
: mData(size)
{
}
void addItem(const T& item)
{
if (mEndPointer < mData.size())
{
mData[mEndPointer] = item;
mEndPointer++;
}
else
{
mData[mStartPointer] = item;
if (mStartPointer < mData.size() - 1)
{
mStartPointer++;
}
else
{
mStartPointer = 0;
}
}
}
std::size_t getNumItems() const
{
return mEndPointer;
}
const T& getItem(std::size_t index) const
{
if (mEndPointer < mData.size())
{
return mData[index];
}
else
{
auto offset = mStartPointer + index;
if (offset >= mData.size())
{
offset -= mData.size();
}
return mData[offset];
}
}
private:
std::size_t mStartPointer{0};
std::size_t mEndPointer{0};
std::vector<T> mData;
};

View file

@ -1,12 +1,13 @@
list(APPEND image_HEADERS list(APPEND image_HEADERS
Image.h Image.h
PngWriter.h png/PngWriter.h
) )
list(APPEND image_LIB_INCLUDES list(APPEND image_LIB_INCLUDES
Image.cpp Image.cpp
PngWriter.cpp png/PngWriter.cpp
PngReader.cpp png/PngReader.cpp
png/PngHeader.cpp
ImageBitStream.cpp ImageBitStream.cpp
) )
@ -17,7 +18,9 @@ list(APPEND image_DEFINES "")
add_library(image SHARED ${image_LIB_INCLUDES} ${image_HEADERS}) add_library(image SHARED ${image_LIB_INCLUDES} ${image_HEADERS})
#target_compile_definitions(image PRIVATE ${image_DEFINES}) #target_compile_definitions(image PRIVATE ${image_DEFINES})
target_include_directories(image PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}") target_include_directories(image PUBLIC
${CMAKE_CURRENT_SOURCE_DIR}
${CMAKE_CURRENT_SOURCE_DIR}/png)
set_target_properties( image PROPERTIES WINDOWS_EXPORT_ALL_SYMBOLS ON ) set_target_properties( image PROPERTIES WINDOWS_EXPORT_ALL_SYMBOLS ON )
target_link_libraries( image PUBLIC ${image_LIBS}) target_link_libraries( image PUBLIC ${image_LIBS})

View file

@ -1,91 +0,0 @@
#pragma once
#include "CyclicRedundancyChecker.h"
#include "ByteUtils.h"
#include "StringUtils.h"
#include <string>
#include <sstream>
namespace Png
{
inline unsigned char getHighBitCheck()
{
return 0x89;
}
inline std::vector<unsigned char> getSignature()
{
return {13, 10, 26, 10};
}
inline std::string getName()
{
return "PNG";
}
struct IHDRChunk
{
uint32_t width{0};
uint32_t height{0};
unsigned char bitDepth{0};
unsigned char colorType{0};
unsigned char compressionMethod{0};
unsigned char filterMethod{0};
unsigned char interlaceMethod{0};
std::string name{"IHDR"};
std::vector<unsigned char> mData;
std::string toString() const
{
std::stringstream sstr;
sstr << "width: " << width << "\n";
sstr << "height: " << height << "\n";
sstr << "bitDepth: " << (int)bitDepth << "\n";
sstr << "colorType: " << (int)colorType << "\n";
sstr << "compressionMethod: " << (int)compressionMethod << "\n";
sstr << "filterMethod: " << (int)filterMethod << "\n";
sstr << "interlaceMethod: " << (int)interlaceMethod << "\n";
return sstr.str();
}
uint32_t getLength() const
{
return 13;
}
void updateData()
{
mData.clear();
unsigned num_bytes = sizeof(uint32_t);
for(unsigned idx=0; idx<num_bytes;idx++)
{
mData.push_back(ByteUtils::getByteN(width, idx));
}
for(unsigned idx=0; idx<num_bytes;idx++)
{
mData.push_back(ByteUtils::getByteN(height, idx));
}
mData.push_back(bitDepth);
mData.push_back(colorType);
mData.push_back(compressionMethod);
mData.push_back(filterMethod);
mData.push_back(interlaceMethod);
}
uint32_t getCrc() const
{
CyclicRedundancyChecker crc_check;
std::vector<unsigned char> char_data = StringUtils::toBytes(name);
std::copy(mData.begin(), mData.end(), std::back_inserter(char_data));
auto result = crc_check.doCrc(char_data.data(), char_data.size());
return result;
}
};
}

View file

@ -0,0 +1,89 @@
#include "PngHeader.h"
#include "ByteUtils.h"
#include "StringUtils.h"
#include "CyclicRedundancyChecker.h"
std::string PngHeader::toString() const
{
std::stringstream sstr;
sstr << "PngHeader" << "\n";
sstr << "width: " << mWidth << "\n";
sstr << "height: " << mHeight << "\n";
sstr << "bitDepth: " << (int)mBitDepth << "\n";
sstr << mPngInfo.toString();
return sstr.str();
}
uint32_t PngHeader::getLength() const
{
return 13;
}
unsigned char PngHeader::getHighBitCheck() const
{
return 0x89;
}
std::vector<unsigned char> PngHeader::getSignature() const
{
return {13, 10, 26, 10};
}
std::string PngHeader::getFileName() const
{
return "PNG";
}
const std::string& PngHeader::getChunkName() const
{
return "IHDR";
}
const std::vector<unsigned char>& PngHeader::getData() const
{
return mData;
}
void PngHeader::updateData()
{
mData.clear();
unsigned num_bytes = sizeof(uint32_t);
for(unsigned idx=0; idx<num_bytes;idx++)
{
mData.push_back(ByteUtils::getByteN(mWidth, idx));
}
for(unsigned idx=0; idx<num_bytes;idx++)
{
mData.push_back(ByteUtils::getByteN(mHeight, idx));
}
mData.push_back(mBitDepth);
mData.push_back(static_cast<unsigned char>(mPngInfo.mColorType));
mData.push_back(mPngInfo.mCompressionMethod);
mData.push_back(mPngInfo.mFilterMethod);
mData.push_back(mPngInfo.mInterlaceMethod);
}
uint32_t PngHeader::getCrc() const
{
CyclicRedundancyChecker crc_check;
std::vector<unsigned char> char_data = StringUtils::toBytes(mName);
std::copy(mData.begin(), mData.end(), std::back_inserter(char_data));
auto result = crc_check.doCrc(char_data.data(), char_data.size());
return result;
}
void PngHeader::setPngInfo(const PngInfo& info)
{
mPngInfo = info;
}
void PngHeader::setImageData(uint32_t width, uint32_t height, unsigned char bitDepth)
{
mWidth = width;
mHeight = height;
mBitDepth = bitDepth;
}

40
src/image/png/PngHeader.h Normal file
View file

@ -0,0 +1,40 @@
#pragma once
#include "PngInfo.h"
#include <vector>
class PngHeader
{
public:
uint32_t getLength() const;
uint32_t getCrc() const;
const std::vector<unsigned char>& getData() const;
unsigned char getHighBitCheck() const;
std::vector<unsigned char> getSignature() const;
std::string getFileName() const;
const std::string& getChunkName() const;
void setPngInfo(const PngInfo& info);
void setImageData(uint32_t width, uint32_t height, unsigned char bitDepth);
std::string toString() const;
void updateData();
private:
uint32_t mWidth{0};
uint32_t mHeight{0};
unsigned char mBitDepth{0};
PngInfo mPngInfo;
std::string mName{"IHDR"};
std::vector<unsigned char> mData;
};

56
src/image/png/PngInfo.h Normal file
View file

@ -0,0 +1,56 @@
#pragma once
#include <string>
#include <sstream>
class PngInfo
{
public:
enum class ColorType : unsigned char
{
GREYSCALE = 0,
RGB = 2,
PALETTE = 3,
GREYSCALE_ALPHA = 4,
RGB_ALPHA = 6,
};
std::string toString(ColorType colorType) const
{
switch(colorType)
{
case ColorType::GREYSCALE:
return "GREYSCALE";
case ColorType::RGB:
return "RGB";
case ColorType::PALETTE:
return "PALETTE";
case ColorType::GREYSCALE_ALPHA:
return "GREYSCALE_ALPHA";
case ColorType::RGB_ALPHA:
return "RGB_ALPHA";
default:
return "UNKNOWN";
}
}
std::string toString() const
{
std::stringstream sstr;
sstr << "PngInfo" << "\n";
sstr << "colorType: " << toString(mColorType) << "\n";
sstr << "compressionMethod: " << (int)mCompressionMethod << "\n";
sstr << "filterMethod: " << (int)mFilterMethod << "\n";
sstr << "interlaceMethod: " << (int)mInterlaceMethod << "\n";
return sstr.str();
}
ColorType mColorType{ColorType::RGB};
unsigned char mCompressionMethod{0};
unsigned char mFilterMethod{0};
unsigned char mInterlaceMethod{0};
};

View file

@ -107,13 +107,19 @@ void PngReader::readIDATChunk(unsigned length)
void PngReader::readHeaderChunk() void PngReader::readHeaderChunk()
{ {
mIHDRChunk.width = *BinaryStream::getNextDWord(mFile->GetInHandle()); auto width = *BinaryStream::getNextDWord(mFile->GetInHandle());
mIHDRChunk.height = *BinaryStream::getNextDWord(mFile->GetInHandle()); auto height = *BinaryStream::getNextDWord(mFile->GetInHandle());
mIHDRChunk.bitDepth = mFile->GetInHandle()->get(); auto bitDepth = mFile->GetInHandle()->get();
mIHDRChunk.colorType = mFile->GetInHandle()->get();
mIHDRChunk.compressionMethod = mFile->GetInHandle()->get(); mHeader.setImageData(width, height, bitDepth);
mIHDRChunk.filterMethod = mFile->GetInHandle()->get();
mIHDRChunk.interlaceMethod = mFile->GetInHandle()->get(); PngInfo info;
info.mColorType = static_cast<PngInfo::ColorType>(mFile->GetInHandle()->get());
info.mCompressionMethod = mFile->GetInHandle()->get();
info.mFilterMethod = mFile->GetInHandle()->get();
info.mInterlaceMethod = mFile->GetInHandle()->get();
mHeader.setPngInfo(info);
mCurrentOffset += 13; mCurrentOffset += 13;
@ -122,7 +128,7 @@ void PngReader::readHeaderChunk()
void PngReader::logHeader() void PngReader::logHeader()
{ {
std::cout << "IHDR\n" << mIHDRChunk.toString() << "*************\n"; std::cout << "IHDR\n" << mHeader.toString() << "*************\n";
} }
std::unique_ptr<Image<unsigned char> > PngReader::read() std::unique_ptr<Image<unsigned char> > PngReader::read()

View file

@ -2,7 +2,7 @@
#include "File.h" #include "File.h"
#include "Image.h" #include "Image.h"
#include "PngElements.h" #include "PngHeader.h"
#include "ZlibEncoder.h" #include "ZlibEncoder.h"
#include <string> #include <string>
@ -34,7 +34,7 @@ private:
unsigned mCurrentOffset{0}; unsigned mCurrentOffset{0};
Png::IHDRChunk mIHDRChunk; PngHeader mHeader;
std::unique_ptr<Image<unsigned char> > mWorkingImage; std::unique_ptr<Image<unsigned char> > mWorkingImage;
std::unique_ptr<File> mFile; std::unique_ptr<File> mFile;

View file

@ -1,6 +1,5 @@
#include "PngWriter.h" #include "PngWriter.h"
#include "PngElements.h"
#include "Image.h" #include "Image.h"
#include "File.h" #include "File.h"
#include "BufferBitStream.h" #include "BufferBitStream.h"
@ -35,32 +34,55 @@ void PngWriter::setPath(const Path& path)
mPath = path; mPath = path;
} }
void PngWriter::setPngInfo(const PngInfo& info)
{
mPngInfoUserSet = true;
mPngInfo = info;
}
void PngWriter::writeSignature() void PngWriter::writeSignature()
{ {
mOutStream->writeByte(Png::getHighBitCheck()); mOutStream->writeByte(mPngHeader.getHighBitCheck());
mOutStream->writeBytes(StringUtils::toBytes(Png::getName())); mOutStream->writeBytes(StringUtils::toBytes(mPngHeader.getFileName()));
mOutStream->writeBytes(Png::getSignature()); mOutStream->writeBytes(mPngHeader.getSignature());
} }
void PngWriter::writeHeader() void PngWriter::writeHeader()
{ {
writeSignature(); writeSignature();
Png::IHDRChunk header_chunk; if (!mPngInfoUserSet)
header_chunk.width = mWorkingImage->getWidth(); {
header_chunk.height = mWorkingImage->getHeight(); if (mWorkingImage->getNumChannels() == 1)
header_chunk.bitDepth = mWorkingImage->getBitDepth(); {
header_chunk.colorType = 6; mPngInfo.mColorType = PngInfo::ColorType::GREYSCALE;
}
else if (mWorkingImage->getNumChannels() == 2)
{
mPngInfo.mColorType = PngInfo::ColorType::GREYSCALE_ALPHA;
}
else if (mWorkingImage->getNumChannels() == 3)
{
mPngInfo.mColorType = PngInfo::ColorType::RGB;
}
else if (mWorkingImage->getNumChannels() == 4)
{
mPngInfo.mColorType = PngInfo::ColorType::RGB_ALPHA;
}
}
auto length = header_chunk.getLength(); mPngHeader.setPngInfo(mPngInfo);
mPngHeader.setImageData(mWorkingImage->getWidth(), mWorkingImage->getHeight(), mWorkingImage->getBitDepth());
auto length = mPngHeader.getLength();
mOutStream->write(length); mOutStream->write(length);
mOutStream->writeBytes(StringUtils::toBytes(header_chunk.name)); mOutStream->writeBytes(StringUtils::toBytes(mPngHeader.getChunkName()));
header_chunk.updateData(); mPngHeader.updateData();
mOutStream->writeBytes(header_chunk.mData); mOutStream->writeBytes(mPngHeader.getData());
auto crc = header_chunk.getCrc(); auto crc = mPngHeader.getCrc();
mOutStream->write(crc); mOutStream->write(crc);
} }

View file

@ -1,5 +1,6 @@
#pragma once #pragma once
#include "PngHeader.h"
#include "Image.h" #include "Image.h"
#include <memory> #include <memory>
@ -22,6 +23,8 @@ public:
void setPath(const Path& path); void setPath(const Path& path);
void setPngInfo(const PngInfo& info);
void write(const std::unique_ptr<Image<unsigned char> >& image); void write(const std::unique_ptr<Image<unsigned char> >& image);
private: private:
@ -39,6 +42,10 @@ private:
std::unique_ptr<BitStream> mInStream; std::unique_ptr<BitStream> mInStream;
std::unique_ptr<BitStream> mOutStream; std::unique_ptr<BitStream> mOutStream;
std::unique_ptr<File> mWorkingFile; std::unique_ptr<File> mWorkingFile;
unsigned mPngInfoUserSet{false};
PngInfo mPngInfo;
PngHeader mPngHeader;
}; };
using PngWriterPtr = std::unique_ptr<PngWriter>; using PngWriterPtr = std::unique_ptr<PngWriter>;

View file

@ -14,6 +14,7 @@ list(APPEND TestFiles
core/TestBinaryStream.cpp core/TestBinaryStream.cpp
core/TestBitStream.cpp core/TestBitStream.cpp
core/TestTomlReader.cpp core/TestTomlReader.cpp
core/TestDataStructures.cpp
compiler/TestLexer.cpp compiler/TestLexer.cpp
compiler/TestTemplatingEngine.cpp compiler/TestTemplatingEngine.cpp
compression/TestStreamCompressor.cpp compression/TestStreamCompressor.cpp

View file

@ -0,0 +1,34 @@
#include "CircleBuffer.h"
#include <iostream>
int main()
{
CircleBuffer<unsigned> buffer(3);
for (auto item : {1, 2, 3})
{
std::cout << "Add item: " << item << std::endl;
buffer.addItem(item);
}
for (std::size_t idx=0; idx<3; idx++)
{
auto item = buffer.getItem(idx);
std::cout << "Got item: " << idx << " " << item << std::endl;
}
for (auto item : {4, 5})
{
std::cout << "Add item: " << item << std::endl;
buffer.addItem(item);
}
for (std::size_t idx=0; idx<3; idx++)
{
auto item = buffer.getItem(idx);
std::cout << "Got item: " << idx << " " << item << std::endl;
}
return 0;
}

View file

@ -9,15 +9,15 @@
int main() int main()
{ {
unsigned width = 200; unsigned width = 20;
unsigned height = 200; unsigned height = 20;
unsigned numChannels = 3; unsigned numChannels = 3;
auto image = Image<unsigned char>::Create(width, height); auto image = Image<unsigned char>::Create(width, height);
image->setNumChannels(numChannels); image->setNumChannels(numChannels);
std::vector<unsigned char> data(image->getBytesPerRow()*height, 0); std::vector<unsigned char> data(image->getBytesPerRow()*height, 0);
ImagePrimitives::drawAlternatingStrips(data, width,height, numChannels, image->getBytesPerRow()); ImagePrimitives::drawAlternatingStrips(data, width, height, numChannels, image->getBytesPerRow());
image->setData(data); image->setData(data);
PngWriter writer; PngWriter writer;