test_png_io.cpp 1.7 KB

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