Reducing copies when writing png data.

This commit is contained in:
tamasmeszaros 2019-03-18 18:02:50 +01:00
parent 24145cc14f
commit 04e03c840d
6 changed files with 88 additions and 12 deletions

View file

@ -18,6 +18,8 @@
// Experimental minz image write:
#include <miniz/miniz_tdef.h>
#include <miniz/miniz_tdef.h>
namespace Slic3r {
class Raster::Impl {
@ -211,4 +213,41 @@ void Raster::save(std::ostream& stream, Compression comp)
}
}
RawBytes Raster::save(Raster::Compression comp)
{
assert(m_impl);
RawBytes ret;
switch(comp) {
case Compression::PNG: {
void *rawdata = tdefl_write_image_to_png_file_in_memory(
m_impl->buffer().data(),
int(resolution().width_px),
int(resolution().height_px), 1, &ret.size);
if(rawdata == nullptr) break;
ret.buffer.reset(static_cast<std::uint8_t*>(rawdata));
break;
}
case Compression::RAW: {
auto header = std::string("P5 ") +
std::to_string(m_impl->resolution().width_px) + " " +
std::to_string(m_impl->resolution().height_px) + " " + "255 ";
auto sz = m_impl->buffer().size()*sizeof(Impl::TBuffer::value_type);
ret.buffer.reset(new std::uint8_t[sz + header.size()]);
auto buff = reinterpret_cast<std::uint8_t*>(m_impl->buffer().data());
std::copy(buff, buff+sz, ret.buffer.get() + header.size());
}
}
return ret;
}
}

View file

@ -3,11 +3,18 @@
#include <ostream>
#include <memory>
#include <vector>
namespace Slic3r {
class ExPolygon;
// Raw byte buffer paired with its size. Suitable for compressed PNG data.
struct RawBytes {
std::unique_ptr<std::uint8_t> buffer = nullptr;
size_t size = 0;
};
/**
* @brief Raster captures an anti-aliased monochrome canvas where vectorial
* polygons can be rasterized. Fill color is always white and the background is
@ -87,6 +94,8 @@ public:
/// Save the raster on the specified stream.
void save(std::ostream& stream, Compression comp = Compression::RAW);
RawBytes save(Compression comp = Compression::RAW);
};
}