Further fixes and some refactor.

This commit is contained in:
tamasmeszaros 2019-03-25 14:53:25 +01:00
parent c1186340d2
commit b21133d16b
3 changed files with 43 additions and 33 deletions

View file

@ -114,14 +114,14 @@ public:
template<> class FilePrinter<FilePrinterFormat::SLA_PNGZIP> template<> class FilePrinter<FilePrinterFormat::SLA_PNGZIP>
{ {
struct Layer { struct Layer {
Raster first; Raster raster;
RawBytes second; RawBytes rawbytes;
Layer() {} Layer() {}
Layer(const Layer&) = delete; Layer(const Layer&) = delete;
Layer(Layer&& m): Layer(Layer&& m):
first(std::move(m.first))/*, second(std::move(m.second))*/ {} raster(std::move(m.raster)) {}
}; };
// We will save the compressed PNG data into stringstreams which can be done // We will save the compressed PNG data into stringstreams which can be done
@ -139,14 +139,11 @@ template<> class FilePrinter<FilePrinterFormat::SLA_PNGZIP>
int m_cnt_fast_layers = 0; int m_cnt_fast_layers = 0;
std::string createIniContent(const std::string& projectname) { std::string createIniContent(const std::string& projectname) {
// double layer_height = m_layer_height;
using std::string; using std::string;
using std::to_string; using std::to_string;
auto expt_str = to_string(m_exp_time_s); auto expt_str = to_string(m_exp_time_s);
auto expt_first_str = to_string(m_exp_time_first_s); auto expt_first_str = to_string(m_exp_time_first_s);
// auto stepnum_str = to_string(static_cast<unsigned>(800*layer_height));
auto layerh_str = to_string(m_layer_height); auto layerh_str = to_string(m_layer_height);
const std::string cnt_fade_layers = to_string(m_cnt_fade_layers); const std::string cnt_fade_layers = to_string(m_cnt_fade_layers);
@ -215,31 +212,31 @@ public:
inline void draw_polygon(const ExPolygon& p, unsigned lyr) { inline void draw_polygon(const ExPolygon& p, unsigned lyr) {
assert(lyr < m_layers_rst.size()); assert(lyr < m_layers_rst.size());
m_layers_rst[lyr].first.draw(p); m_layers_rst[lyr].raster.draw(p);
} }
inline void begin_layer(unsigned lyr) { inline void begin_layer(unsigned lyr) {
if(m_layers_rst.size() <= lyr) m_layers_rst.resize(lyr+1); if(m_layers_rst.size() <= lyr) m_layers_rst.resize(lyr+1);
m_layers_rst[lyr].first.reset(m_res, m_pxdim, m_o); m_layers_rst[lyr].raster.reset(m_res, m_pxdim, m_o);
} }
inline void begin_layer() { inline void begin_layer() {
m_layers_rst.emplace_back(); m_layers_rst.emplace_back();
m_layers_rst.front().first.reset(m_res, m_pxdim, m_o); m_layers_rst.front().raster.reset(m_res, m_pxdim, m_o);
} }
inline void finish_layer(unsigned lyr_id) { inline void finish_layer(unsigned lyr_id) {
assert(lyr_id < m_layers_rst.size()); assert(lyr_id < m_layers_rst.size());
m_layers_rst[lyr_id].second = m_layers_rst[lyr_id].rawbytes =
m_layers_rst[lyr_id].first.save(Raster::Compression::PNG); m_layers_rst[lyr_id].raster.save(Raster::Compression::PNG);
m_layers_rst[lyr_id].first.reset(); m_layers_rst[lyr_id].raster.reset();
} }
inline void finish_layer() { inline void finish_layer() {
if(!m_layers_rst.empty()) { if(!m_layers_rst.empty()) {
m_layers_rst.back().second = m_layers_rst.back().rawbytes =
m_layers_rst.back().first.save(Raster::Compression::PNG); m_layers_rst.back().raster.save(Raster::Compression::PNG);
m_layers_rst.back().first.reset(); m_layers_rst.back().raster.reset();
} }
} }
@ -258,15 +255,15 @@ public:
for(unsigned i = 0; i < m_layers_rst.size() && writer.is_ok(); i++) for(unsigned i = 0; i < m_layers_rst.size() && writer.is_ok(); i++)
{ {
if(m_layers_rst[i].second.size > 0) { if(m_layers_rst[i].rawbytes.size() > 0) {
char lyrnum[6]; char lyrnum[6];
std::sprintf(lyrnum, "%.5d", i); std::sprintf(lyrnum, "%.5d", i);
auto zfilename = project + lyrnum + ".png"; auto zfilename = project + lyrnum + ".png";
if(!writer.is_ok()) break; if(!writer.is_ok()) break;
writer.binary_entry(zfilename, writer.binary_entry(zfilename,
m_layers_rst[i].second.buffer.get(), m_layers_rst[i].rawbytes.data(),
m_layers_rst[i].second.size); m_layers_rst[i].rawbytes.size());
} }
} }
} catch(std::exception& e) { } catch(std::exception& e) {
@ -286,13 +283,13 @@ public:
std::fstream out(loc, std::fstream::out | std::fstream::binary); std::fstream out(loc, std::fstream::out | std::fstream::binary);
if(out.good()) { if(out.good()) {
m_layers_rst[i].first.save(out, Raster::Compression::PNG); m_layers_rst[i].raster.save(out, Raster::Compression::PNG);
} else { } else {
BOOST_LOG_TRIVIAL(error) << "Can't create file for layer"; BOOST_LOG_TRIVIAL(error) << "Can't create file for layer";
} }
out.close(); out.close();
m_layers_rst[i].first.reset(); m_layers_rst[i].raster.reset();
} }
void set_statistics(const std::vector<double> statistics) void set_statistics(const std::vector<double> statistics)

View file

@ -213,7 +213,7 @@ RawBytes Raster::save(Raster::Compression comp)
{ {
assert(m_impl); assert(m_impl);
RawBytes ret; std::uint8_t *ptr = nullptr; size_t s = 0;
switch(comp) { switch(comp) {
case Compression::PNG: { case Compression::PNG: {
@ -221,11 +221,11 @@ RawBytes Raster::save(Raster::Compression comp)
void *rawdata = tdefl_write_image_to_png_file_in_memory( void *rawdata = tdefl_write_image_to_png_file_in_memory(
m_impl->buffer().data(), m_impl->buffer().data(),
int(resolution().width_px), int(resolution().width_px),
int(resolution().height_px), 1, &ret.size); int(resolution().height_px), 1, &s);
if(rawdata == nullptr) break; if(rawdata == nullptr) break;
ret.buffer.reset(static_cast<std::uint8_t*>(rawdata)); ptr = static_cast<std::uint8_t*>(rawdata);
break; break;
} }
@ -236,14 +236,15 @@ RawBytes Raster::save(Raster::Compression comp)
auto sz = m_impl->buffer().size()*sizeof(Impl::TBuffer::value_type); auto sz = m_impl->buffer().size()*sizeof(Impl::TBuffer::value_type);
ret.buffer.reset(new std::uint8_t[sz + header.size()]); s = sz + header.size();
ptr = static_cast<std::uint8_t*>(MZ_MALLOC(s));
auto buff = reinterpret_cast<std::uint8_t*>(m_impl->buffer().data()); auto buff = reinterpret_cast<std::uint8_t*>(m_impl->buffer().data());
std::copy(buff, buff+sz, ret.buffer.get() + header.size()); std::copy(buff, buff+sz, ptr + header.size());
} }
} }
return std::move(ret); return {ptr, s};
} }
void RawBytes::MinzDeleter::operator()(uint8_t *rawptr) void RawBytes::MinzDeleter::operator()(uint8_t *rawptr)

View file

@ -12,27 +12,39 @@ class ExPolygon;
// Raw byte buffer paired with its size. Suitable for compressed PNG data. // Raw byte buffer paired with its size. Suitable for compressed PNG data.
class RawBytes { class RawBytes {
class MinzDeleter { class MinzDeleter {
public: public:
void operator()(std::uint8_t *rawptr); void operator()(std::uint8_t *rawptr);
}; };
std::unique_ptr<std::uint8_t, MinzDeleter> m_buffer = nullptr;
size_t m_size = 0;
public: public:
std::unique_ptr<std::uint8_t, MinzDeleter> buffer = nullptr;
size_t size = 0;
// FIXME: the following is needed for MSVC2013 compatibility
RawBytes() = default; RawBytes() = default;
RawBytes(std::uint8_t *rawptr, size_t s): m_buffer(rawptr), m_size(s) {}
size_t size() const { return m_size; }
const uint8_t * data() { return m_buffer.get(); }
// /////////////////////////////////////////////////////////////////////////
// FIXME: the following is needed for MSVC2013 compatibility
// /////////////////////////////////////////////////////////////////////////
RawBytes(const RawBytes&) = delete; RawBytes(const RawBytes&) = delete;
RawBytes(RawBytes&& mv): buffer(std::move(mv.buffer)), size(mv.size) {} RawBytes(RawBytes&& mv):
m_buffer(std::move(mv.m_buffer)), m_size(mv.m_size) {}
RawBytes& operator=(const RawBytes&) = delete; RawBytes& operator=(const RawBytes&) = delete;
RawBytes& operator=(RawBytes&& mv) { RawBytes& operator=(RawBytes&& mv) {
buffer.swap(mv.buffer); m_buffer.swap(mv.m_buffer);
size = mv.size; m_size = mv.m_size;
return *this; return *this;
} }
// /////////////////////////////////////////////////////////////////////////
}; };
/** /**