stuff-from-scratch/src/image/PngWriter.cpp
2022-11-23 15:41:33 +00:00

128 lines
2.7 KiB
C++

#include "PngWriter.h"
#include "PngElements.h"
#include "Image.h"
#include "File.h"
#include "BufferBitStream.h"
#include "OutputBitStream.h"
#include "ImageBitStream.h"
#include "Lz77Encoder.h"
#include <stdio.h>
PngWriter::PngWriter()
{
}
PngWriter::~PngWriter()
{
}
std::unique_ptr<PngWriter> PngWriter::Create()
{
return std::make_unique<PngWriter>();
}
void PngWriter::setPath(const Path& path)
{
mPath = path;
}
void PngWriter::writeSignature()
{
mOutStream->writeByte(Png::getHighBitCheck());
for (auto byte : Png::getSignature())
{
mOutStream->writeByte(byte);
}
}
void PngWriter::writeHeader()
{
writeSignature();
}
void PngWriter::write(const std::unique_ptr<Image<unsigned char> >& image)
{
if (!mPath.empty())
{
mWorkingFile = std::make_unique<File>(mPath);
mWorkingFile->Open(true);
mOutStream = std::make_unique<OutputBitStream>(mWorkingFile->GetOutHandle());
}
else
{
mOutStream = std::make_unique<BufferBitStream>();
}
mWorkingImage = image.get();
mInStream = std::make_unique<ImageBitStream>(image.get());
writeHeader();
//mImpl->write(image);
//auto fp = fopen(mPath.c_str(), "wb");
//auto png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, nullptr, nullptr, nullptr);
//auto info_ptr = png_create_info_struct(png_ptr);
//if (setjmp(png_jmpbuf(png_ptr)))
//{
// return;
//}
//png_init_io(png_ptr, fp);
//if (setjmp(png_jmpbuf(png_ptr)))
//{
// return;
//}
//auto color_type = PNG_COLOR_TYPE_RGB;
//png_set_IHDR(png_ptr, info_ptr, image->GetWidth(), image->GetHeight(),
// image->GetBitDepth(), color_type, PNG_INTERLACE_NONE,
// PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
//png_write_info(png_ptr, info_ptr);
//if (setjmp(png_jmpbuf(png_ptr)))
//{
// return;
//}
//png_bytep* row_pointers = (png_bytep*)malloc(sizeof(png_bytep) * image->GetHeight());
//auto row_size = image->GetBytesPerRow();
//for(unsigned jdx=0;jdx<image->GetHeight();jdx++)
//{
// row_pointers[jdx]=(png_byte*)malloc(sizeof(png_byte)*row_size);
// for(unsigned idx=0;idx<row_size;idx++)
// {
// row_pointers[jdx][idx] = image->GetByte(idx, jdx);
// }
//}
//png_write_image(png_ptr, row_pointers);
//if (setjmp(png_jmpbuf(png_ptr)))
//{
// return;
//}
//png_write_end(png_ptr, nullptr);
//for (unsigned y=0; y<image->GetHeight(); y++)
//{
// free(row_pointers[y]);
//}
//free(row_pointers);
//fclose(fp);
//return;
if (mWorkingFile)
{
mWorkingFile->Close();
}
}