stuff-from-scratch/test/image/TestPngWriter.cpp

102 lines
2.8 KiB
C++
Raw Normal View History

2022-01-01 18:46:31 +00:00
#include "Image.h"
#include "PngWriter.h"
2022-11-21 17:45:12 +00:00
2022-11-23 17:51:36 +00:00
#include "File.h"
2022-11-23 15:41:33 +00:00
#include "BitStream.h"
2022-11-28 10:16:04 +00:00
#include "ByteUtils.h"
2023-01-16 10:51:18 +00:00
#include "Grid.h"
2022-11-21 17:45:12 +00:00
#include "ImagePrimitives.h"
2022-01-01 18:46:31 +00:00
2022-11-29 18:00:19 +00:00
#include "TestFramework.h"
2022-12-01 10:52:48 +00:00
#include "TestUtils.h"
2022-05-18 07:42:44 +00:00
#include <iostream>
2022-11-29 18:00:19 +00:00
TEST_CASE(TestCompressedPng, "image")
2022-01-01 18:46:31 +00:00
{
2022-11-25 09:43:14 +00:00
unsigned width = 20;
unsigned height = 20;
2022-11-24 16:15:41 +00:00
unsigned numChannels = 1;
2023-01-11 14:31:29 +00:00
auto image = Image::Create(width, height);
image->setNumChannels(numChannels);
2022-11-24 16:15:41 +00:00
image->setBitDepth(8);
2022-01-01 18:46:31 +00:00
2022-11-24 16:15:41 +00:00
std::vector<unsigned char> data(width*height, 0);
for (unsigned idx=0; idx<width*height; idx++)
{
2022-11-25 09:43:14 +00:00
unsigned char val = 255 * idx /(width*height);
data[idx] = val;
2022-11-24 16:15:41 +00:00
}
2022-11-21 17:45:12 +00:00
2023-01-16 10:51:18 +00:00
image->getGridT<unsigned char>()->setData(data);
2022-01-01 18:46:31 +00:00
PngWriter writer;
2023-01-16 11:56:46 +00:00
//writer.setCompressionMethod(Deflate::CompressionMethod::NONE);
writer.write(TestUtils::getTestOutputDir() / "test_compressed.png", image.get());
2022-01-01 18:46:31 +00:00
2022-11-28 10:16:04 +00:00
return;
2022-12-01 10:52:48 +00:00
File test_file(TestUtils::getTestOutputDir() / "test_compressed.png");
test_file.open(File::AccessMode::Read);
2022-11-23 17:51:36 +00:00
while(auto byte = test_file.readNextByte())
{
2022-11-30 18:28:50 +00:00
//std::cout << static_cast<unsigned>(*byte) << std::endl;
2022-11-23 17:51:36 +00:00
}
2022-11-28 10:16:04 +00:00
}
2022-11-23 17:51:36 +00:00
2022-11-29 18:00:19 +00:00
TEST_CASE(TestFixedPng, "image")
2022-11-28 10:16:04 +00:00
{
unsigned width = 10;
unsigned height = 10;
unsigned numChannels = 1;
2023-01-11 14:31:29 +00:00
auto image = Image::Create(width, height);
2022-11-28 10:16:04 +00:00
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;
}
2023-01-16 10:51:18 +00:00
image->getGridT<unsigned char>()->setData(data);
2022-11-28 10:16:04 +00:00
PngWriter writer;
2023-01-16 11:56:46 +00:00
//writer.setCompressionMethod(Deflate::CompressionMethod::FIXED_HUFFMAN);
writer.write(TestUtils::getTestOutputDir() / "test_fixed.png", image.get());
2022-11-28 10:16:04 +00:00
//return;
2022-12-01 10:52:48 +00:00
File test_file(TestUtils::getTestOutputDir() / "test_fixed.png");
2022-11-30 18:28:50 +00:00
//std::cout << test_file.dumpBinary();
2022-11-28 10:16:04 +00:00
}
2022-11-29 18:00:19 +00:00
TEST_CASE(TestDynamicCompressedPng, "image")
{
unsigned width = 10;
unsigned height = 10;
unsigned numChannels = 1;
2023-01-11 14:31:29 +00:00
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;
}
2023-01-16 10:51:18 +00:00
image->getGridT<unsigned char>()->setData(data);
PngWriter writer;
2023-01-16 11:56:46 +00:00
//writer.setPath(TestUtils::getTestOutputDir() / "test_dynamic.png");
writer.write(TestUtils::getTestOutputDir() / "test_dynamic.png", image.get());
//return;
2022-12-01 10:52:48 +00:00
File test_file(TestUtils::getTestOutputDir() / "test_dynamic.png");
2022-11-30 18:28:50 +00:00
//std::cout << test_file.dumpBinary();
2022-01-01 18:46:31 +00:00
}