PNG image read with libpng

This commit is contained in:
tamasmeszaros 2020-04-27 18:43:47 +02:00
parent 19e1d877aa
commit 2bcd36d155
6 changed files with 164 additions and 15 deletions

View file

@ -161,6 +161,8 @@ add_library(libslic3r STATIC
PrintConfig.hpp
PrintObject.cpp
PrintRegion.cpp
PNGRead.hpp
PNGRead.cpp
Semver.cpp
ShortestPath.cpp
ShortestPath.hpp
@ -308,6 +310,8 @@ target_link_libraries(libslic3r
TBB::tbb
libslic3r_cgal
${CMAKE_DL_LIBS}
PNG::PNG
ZLIB::ZLIB
)
if (TARGET OpenVDB::openvdb)

59
src/libslic3r/PNGRead.cpp Normal file
View file

@ -0,0 +1,59 @@
#include "PNGRead.hpp"
#include <memory>
#include <cstdio>
#include <png.h>
namespace Slic3r { namespace png {
struct png_deleter { void operator()(png_struct *p) {
png_destroy_read_struct( &p, nullptr, nullptr); }
};
using png_ptr_t = std::unique_ptr<png_struct_def, png_deleter>;
bool is_png(const ReadBuf &rb)
{
static const constexpr int PNG_SIG_BYTES = 8;
return rb.sz >= PNG_SIG_BYTES &&
!png_sig_cmp(static_cast<png_const_bytep>(rb.buf), 0, PNG_SIG_BYTES);
}
bool decode_png(const ReadBuf &rb, ImageGreyscale &img)
{
if (!is_png(rb)) return false;
png_ptr_t png{png_create_read_struct(PNG_LIBPNG_VER_STRING, nullptr, nullptr, nullptr)};
if(!png) return false;
png_infop info = png_create_info_struct(png.get());
if(!info) return {};
FILE *io = ::fmemopen(const_cast<void *>(rb.buf), rb.sz, "rb");
png_init_io(png.get(), io);
png_read_info(png.get(), info);
img.cols = png_get_image_width(png.get(), info);
img.rows = png_get_image_height(png.get(), info);
size_t color_type = png_get_color_type(png.get(), info);
size_t bit_depth = png_get_bit_depth(png.get(), info);
if (color_type != PNG_COLOR_TYPE_GRAY || bit_depth != 8)
return false;
img.buf.resize(img.rows * img.cols);
auto readbuf = static_cast<png_bytep>(img.buf.data());
for (size_t r = 0; r < img.rows; ++r)
png_read_row(png.get(), readbuf + r * img.cols, nullptr);
fclose(io);
return true;
}
}}

30
src/libslic3r/PNGRead.hpp Normal file
View file

@ -0,0 +1,30 @@
#ifndef PNGREAD_HPP
#define PNGREAD_HPP
#include <vector>
#include <string>
namespace Slic3r { namespace png {
struct ReadBuf { const void *buf = nullptr; const size_t sz = 0; };
template<class PxT> struct Image {
std::vector<PxT> buf;
size_t rows, cols;
PxT get(size_t row, size_t col) const { return buf[row * cols + col]; }
};
struct RGB { uint8_t r, g, b; };
using ImageRGB = Image<RGB>;
using ImageGreyscale = Image<uint8_t>;
// TODO
// bool decode_png(Buffer &&pngbuf, ImageRGB &img);
bool is_png(const ReadBuf &pngbuf);
bool decode_png(const ReadBuf &pngbuf, ImageGreyscale &img);
}}
#endif // PNGREAD_HPP