test_png_io.cpp 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. #define NOMINMAX
  2. #include <catch2/catch.hpp>
  3. #include <numeric>
  4. #include "libslic3r/PNGReadWrite.hpp"
  5. #include "libslic3r/SLA/AGGRaster.hpp"
  6. #include "libslic3r/BoundingBox.hpp"
  7. using namespace Slic3r;
  8. static sla::RasterGrayscaleAA create_raster(const sla::RasterBase::Resolution &res)
  9. {
  10. sla::RasterBase::PixelDim pixdim{1., 1.};
  11. auto bb = BoundingBox({0, 0}, {scaled(1.), scaled(1.)});
  12. sla::RasterBase::Trafo trafo;
  13. trafo.center_x = bb.center().x();
  14. trafo.center_y = bb.center().y();
  15. return sla::RasterGrayscaleAA{res, pixdim, trafo, agg::gamma_threshold(.5)};
  16. }
  17. TEST_CASE("PNG read", "[PNG]") {
  18. auto rst = create_raster({100, 100});
  19. size_t rstsum = 0;
  20. for (size_t r = 0; r < rst.resolution().height_px; ++r)
  21. for (size_t c = 0; c < rst.resolution().width_px; ++c)
  22. rstsum += rst.read_pixel(c, r);
  23. SECTION("Correct png buffer should be recognized as such.") {
  24. auto enc_rst = rst.encode(sla::PNGRasterEncoder{});
  25. REQUIRE(Slic3r::png::is_png({enc_rst.data(), enc_rst.size()}));
  26. }
  27. SECTION("Fake png buffer should be recognized as such.") {
  28. std::vector<uint8_t> fake(10, '\0');
  29. REQUIRE(!Slic3r::png::is_png({fake.data(), fake.size()}));
  30. }
  31. SECTION("Decoded PNG buffer resolution should match the original") {
  32. auto enc_rst = rst.encode(sla::PNGRasterEncoder{});
  33. png::ImageGreyscale img;
  34. png::decode_png({enc_rst.data(), enc_rst.size()}, img);
  35. REQUIRE(img.rows == rst.resolution().height_px);
  36. REQUIRE(img.cols == rst.resolution().width_px);
  37. size_t sum = std::accumulate(img.buf.begin(), img.buf.end(), size_t(0));
  38. REQUIRE(sum == rstsum);
  39. }
  40. }