stuff-from-scratch/test/image/TestPngWriter.cpp
2023-01-11 14:31:29 +00:00

102 lines
2.8 KiB
C++

#include "Image.h"
#include "PngWriter.h"
#include "File.h"
#include "BitStream.h"
#include "ByteUtils.h"
#include "ImagePrimitives.h"
#include "TestFramework.h"
#include "TestUtils.h"
#include <iostream>
TEST_CASE(TestCompressedPng, "image")
{
unsigned width = 20;
unsigned height = 20;
unsigned numChannels = 1;
auto image = Image::Create(width, height);
image->setNumChannels(numChannels);
image->setBitDepth(8);
std::vector<unsigned char> data(width*height, 0);
for (unsigned idx=0; idx<width*height; idx++)
{
unsigned char val = 255 * idx /(width*height);
data[idx] = val;
}
dynamic_cast<ImageDataT<unsigned char>*>(image->getData())->setData(data);
PngWriter writer;
writer.setPath(TestUtils::getTestOutputDir() / "test_compressed.png");
writer.setCompressionMethod(Deflate::CompressionMethod::NONE);
writer.write(image);
return;
File test_file(TestUtils::getTestOutputDir() / "test_compressed.png");
test_file.open(File::AccessMode::Read);
while(auto byte = test_file.readNextByte())
{
//std::cout << static_cast<unsigned>(*byte) << std::endl;
}
}
TEST_CASE(TestFixedPng, "image")
{
unsigned width = 10;
unsigned height = 10;
unsigned numChannels = 1;
auto image = Image::Create(width, height);
image->setNumChannels(numChannels);
image->setBitDepth(8);
std::vector<unsigned char> data(width*height, 0);
for (unsigned idx=0; idx<width*height; idx++)
{
//unsigned char val = 100 * idx /(width*height);
unsigned char val = 10;
data[idx] = val;
}
dynamic_cast<ImageDataT<unsigned char>*>(image->getData())->setData(data);
PngWriter writer;
writer.setPath(TestUtils::getTestOutputDir() / "test_fixed.png");
writer.setCompressionMethod(Deflate::CompressionMethod::FIXED_HUFFMAN);
writer.write(image);
//return;
File test_file(TestUtils::getTestOutputDir() / "test_fixed.png");
//std::cout << test_file.dumpBinary();
}
TEST_CASE(TestDynamicCompressedPng, "image")
{
unsigned width = 10;
unsigned height = 10;
unsigned numChannels = 1;
auto image = Image::Create(width, height);
image->setNumChannels(numChannels);
image->setBitDepth(8);
std::vector<unsigned char> data(width*height, 0);
for (unsigned idx=0; idx<width*height; idx++)
{
//unsigned char val = 100 * idx /(width*height);
unsigned char val = 10;
data[idx] = val;
}
dynamic_cast<ImageDataT<unsigned char>*>(image->getData())->setData(data);
PngWriter writer;
writer.setPath(TestUtils::getTestOutputDir() / "test_dynamic.png");
writer.write(image);
//return;
File test_file(TestUtils::getTestOutputDir() / "test_dynamic.png");
//std::cout << test_file.dumpBinary();
}