Non compressing png writer is ok.

This commit is contained in:
James Grogan 2022-11-25 09:43:14 +00:00
parent 33369b1775
commit e4f9393ee7
18 changed files with 196 additions and 23 deletions

View file

@ -62,7 +62,32 @@ public:
void decode()
{
auto image_stream = dynamic_cast<ImageBitStream*>(mOutputStream);
if (!image_stream)
{
MLOG_ERROR("Expected ImageStream in PngFilter decode - aborting.");
return;
}
const auto bytes_per_scanline = image_stream->getBytesPerScanline();
unsigned count{0};
FilterType working_filter_type = FilterType::NONE;
while(auto byte = mInputStream->readNextByte())
{
if (count % bytes_per_scanline == 0)
{
working_filter_type = static_cast<FilterType>(*byte);
}
else
{
if (working_filter_type == FilterType::NONE)
{
image_stream->writeByte(*byte);
}
}
count++;
}
}
private:

View file

@ -13,6 +13,26 @@ public:
const std::vector<unsigned char>& getData() const;
uint32_t getWidth() const
{
return mWidth;
}
uint32_t getHeight() const
{
return mHeight;
}
unsigned char getBitDepth() const
{
return mBitDepth;
}
const PngInfo& getPngInfo() const
{
return mPngInfo;
}
unsigned char getHighBitCheck() const;
std::vector<unsigned char> getSignature() const;

View file

@ -19,6 +19,25 @@ std::string PngInfo::toString(ColorType colorType) const
}
}
unsigned PngInfo::getNumChannels() const
{
switch(mColorType)
{
case ColorType::GREYSCALE:
return 1;
case ColorType::RGB:
return 3;
case ColorType::PALETTE:
return 1;
case ColorType::GREYSCALE_ALPHA:
return 2;
case ColorType::RGB_ALPHA:
return 4;
default:
return 1;
}
}
std::string PngInfo::toString(CompressionMethod method) const
{
switch(method)

View file

@ -31,6 +31,8 @@ public:
ADAM7 = 1
};
unsigned getNumChannels() const;
std::string toString(ColorType colorType) const;
std::string toString(CompressionMethod method) const;

View file

@ -3,6 +3,8 @@
#include "BinaryStream.h"
#include "BitStream.h"
#include "BufferBitStream.h"
#include "ImageBitStream.h"
#include "PngFilter.h"
#include "ZlibEncoder.h"
#include "CyclicRedundancyChecker.h"
@ -180,6 +182,17 @@ std::unique_ptr<Image<unsigned char> > PngReader::read()
{
}
image->setWidth(mHeader.getWidth());
image->setHeight(mHeader.getHeight());
image->setBitDepth(mHeader.getBitDepth());
image->setNumChannels(mHeader.getPngInfo().getNumChannels());
image->initialize();
auto image_bit_stream = std::make_unique<ImageBitStream>(image.get());
PngFilter filter(mOutputStream.get(), image_bit_stream.get());
filter.decode();
return std::move(image);
}