PrusaSlicer-NonPlainar/tests/libslic3r/test_png_io.cpp

56 lines
1.7 KiB
C++
Raw Normal View History

2020-04-27 16:43:47 +00:00
#define NOMINMAX
#include <catch2/catch.hpp>
2020-08-03 13:27:58 +00:00
#include <numeric>
#include "libslic3r/PNGReadWrite.hpp"
2020-04-27 16:43:47 +00:00
#include "libslic3r/SLA/AGGRaster.hpp"
2020-08-03 13:27:58 +00:00
#include "libslic3r/BoundingBox.hpp"
2020-04-27 16:43:47 +00:00
using namespace Slic3r;
2022-01-12 12:54:18 +00:00
static sla::RasterGrayscaleAA create_raster(const sla::Resolution &res)
2020-04-27 16:43:47 +00:00
{
2022-01-12 12:54:18 +00:00
sla::PixelDim pixdim{1., 1.};
2020-04-27 16:43:47 +00:00
auto bb = BoundingBox({0, 0}, {scaled(1.), scaled(1.)});
sla::RasterBase::Trafo trafo;
trafo.center_x = bb.center().x();
trafo.center_y = bb.center().y();
return sla::RasterGrayscaleAA{res, pixdim, trafo, agg::gamma_threshold(.5)};
}
TEST_CASE("PNG read", "[PNG]") {
auto rst = create_raster({100, 100});
size_t rstsum = 0;
for (size_t r = 0; r < rst.resolution().height_px; ++r)
for (size_t c = 0; c < rst.resolution().width_px; ++c)
rstsum += rst.read_pixel(c, r);
SECTION("Correct png buffer should be recognized as such.") {
auto enc_rst = rst.encode(sla::PNGRasterEncoder{});
REQUIRE(Slic3r::png::is_png({enc_rst.data(), enc_rst.size()}));
}
SECTION("Fake png buffer should be recognized as such.") {
std::vector<uint8_t> fake(10, '\0');
REQUIRE(!Slic3r::png::is_png({fake.data(), fake.size()}));
}
SECTION("Decoded PNG buffer resolution should match the original") {
auto enc_rst = rst.encode(sla::PNGRasterEncoder{});
png::ImageGreyscale img;
png::decode_png({enc_rst.data(), enc_rst.size()}, img);
REQUIRE(img.rows == rst.resolution().height_px);
REQUIRE(img.cols == rst.resolution().width_px);
size_t sum = std::accumulate(img.buf.begin(), img.buf.end(), size_t(0));
REQUIRE(sum == rstsum);
}
}