PrusaSlicer-NonPlainar/src/libslic3r/PNGRead.cpp

95 lines
2.7 KiB
C++
Raw Normal View History

2020-04-27 16:43:47 +00:00
#include "PNGRead.hpp"
#include <memory>
#include <cstdio>
#include <png.h>
namespace Slic3r { namespace png {
struct PNGDescr {
png_struct *png = nullptr; png_info *info = nullptr;
PNGDescr() = default;
PNGDescr(const PNGDescr&) = delete;
PNGDescr(PNGDescr&&) = delete;
PNGDescr& operator=(const PNGDescr&) = delete;
PNGDescr& operator=(PNGDescr&&) = delete;
~PNGDescr()
{
if (png && info) png_destroy_info_struct(png, &info);
if (png) png_destroy_read_struct( &png, nullptr, nullptr);
}
2020-04-27 16:43:47 +00:00
};
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);
}
2020-08-03 16:00:32 +00:00
// A wrapper around ReadBuf to be read repeatedly like a stream. libpng needs
// this form for its buffer read callback.
struct ReadBufReader {
const ReadBuf &rdbuf; size_t pos;
ReadBufReader(const ReadBuf &rd): rdbuf{rd}, pos{0} {}
};
// Buffer read callback for libpng. It provides an allocated output buffer and
// the amount of data it desires to read from the input.
void png_read_callback(png_struct *png_ptr,
png_bytep outBytes,
png_size_t byteCountToRead)
{
// Retrieve our input buffer through the png_ptr
auto reader = static_cast<ReadBufReader *>(png_get_io_ptr(png_ptr));
if (!reader || byteCountToRead > reader->rdbuf.sz - reader->pos) return;
auto buf = static_cast<const png_byte *>(reader->rdbuf.buf);
size_t pos = reader->pos;
std::copy(buf + pos, buf + (pos + byteCountToRead), outBytes);
reader->pos += byteCountToRead;
}
2020-04-27 16:43:47 +00:00
bool decode_png(const ReadBuf &rb, ImageGreyscale &img)
{
if (!is_png(rb)) return false;
PNGDescr dsc;
2020-08-03 16:00:32 +00:00
dsc.png = png_create_read_struct(PNG_LIBPNG_VER_STRING, nullptr, nullptr,
nullptr);
2020-04-27 16:43:47 +00:00
if(!dsc.png) return false;
2020-04-27 16:43:47 +00:00
dsc.info = png_create_info_struct(dsc.png);
if(!dsc.info) return {};
2020-04-27 16:43:47 +00:00
2020-08-03 16:00:32 +00:00
ReadBufReader reader {rb};
png_set_read_fn(dsc.png, static_cast<void *>(&reader), png_read_callback);
2020-04-27 16:43:47 +00:00
png_read_info(dsc.png, dsc.info);
2020-04-27 16:43:47 +00:00
img.cols = png_get_image_width(dsc.png, dsc.info);
img.rows = png_get_image_height(dsc.png, dsc.info);
size_t color_type = png_get_color_type(dsc.png, dsc.info);
size_t bit_depth = png_get_bit_depth(dsc.png, dsc.info);
2020-04-27 16:43:47 +00:00
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(dsc.png, readbuf + r * img.cols, nullptr);
2020-04-27 16:43:47 +00:00
return true;
}
2020-08-03 16:00:32 +00:00
}} // namespace Slic3r::png