Files
OrcaSlicer-bambulab/src/libslic3r/SLA/SLARaster.cpp
T

330 lines
8.6 KiB
C++
Raw Normal View History

2019-05-18 22:45:24 +02:00
#ifndef SLARASTER_CPP
#define SLARASTER_CPP
2019-05-20 11:19:43 +02:00
#include <functional>
2019-05-18 22:45:24 +02:00
#include "SLARaster.hpp"
#include "libslic3r/ExPolygon.hpp"
2019-03-27 18:37:50 +01:00
#include <libnest2d/backends/clipper/clipper_polygon.hpp>
// For rasterizing
#include <agg/agg_basics.h>
#include <agg/agg_rendering_buffer.h>
#include <agg/agg_pixfmt_gray.h>
#include <agg/agg_pixfmt_rgb.h>
#include <agg/agg_renderer_base.h>
#include <agg/agg_renderer_scanline.h>
#include <agg/agg_scanline_p.h>
#include <agg/agg_rasterizer_scanline_aa.h>
#include <agg/agg_path_storage.h>
// Experimental minz image write:
2019-05-23 11:49:43 +02:00
#include <miniz.h>
2018-05-16 18:51:28 +02:00
namespace Slic3r {
2019-05-18 22:45:24 +02:00
inline const Polygon& contour(const ExPolygon& p) { return p.contour; }
inline const ClipperLib::Path& contour(const ClipperLib::Polygon& p) { return p.Contour; }
2019-05-18 22:45:24 +02:00
inline const Polygons& holes(const ExPolygon& p) { return p.holes; }
inline const ClipperLib::Paths& holes(const ClipperLib::Polygon& p) { return p.Holes; }
namespace sla {
2018-05-16 18:51:28 +02:00
class Raster::Impl {
public:
using TPixelRenderer = agg::pixfmt_gray8; // agg::pixfmt_rgb24;
using TRawRenderer = agg::renderer_base<TPixelRenderer>;
using TPixel = TPixelRenderer::color_type;
using TRawBuffer = agg::rendering_buffer;
2018-05-16 18:51:28 +02:00
2019-02-07 12:09:10 +01:00
using TBuffer = std::vector<TPixelRenderer::pixel_type>;
using TRendererAA = agg::renderer_scanline_aa_solid<TRawRenderer>;
static const TPixel ColorWhite;
static const TPixel ColorBlack;
2019-05-18 22:45:24 +02:00
using Format = Raster::Format;
private:
Raster::Resolution m_resolution;
2019-04-09 14:52:29 +02:00
// Raster::PixelDim m_pxdim;
Raster::PixelDim m_pxdim_scaled; // used for scaled coordinate polygons
TBuffer m_buf;
TRawBuffer m_rbuf;
TPixelRenderer m_pixfmt;
TRawRenderer m_raw_renderer;
TRendererAA m_renderer;
2019-04-09 14:52:29 +02:00
std::function<double(double)> m_gammafn;
2019-05-18 22:45:24 +02:00
std::array<bool, 2> m_mirror;
2019-05-18 23:21:59 +02:00
Format m_fmt = Format::PNG;
2019-04-09 14:52:29 +02:00
inline void flipy(agg::path_storage& path) const {
path.flip_y(0, m_resolution.height_px);
}
2019-05-18 22:45:24 +02:00
inline void flipx(agg::path_storage& path) const {
path.flip_x(0, m_resolution.width_px);
}
public:
inline Impl(const Raster::Resolution& res, const Raster::PixelDim &pd,
2019-05-18 22:45:24 +02:00
const std::array<bool, 2>& mirror, double gamma = 1.0):
2019-04-09 14:52:29 +02:00
m_resolution(res),
// m_pxdim(pd),
m_pxdim_scaled(SCALING_FACTOR / pd.w_mm, SCALING_FACTOR / pd.h_mm),
m_buf(res.pixels()),
m_rbuf(reinterpret_cast<TPixelRenderer::value_type*>(m_buf.data()),
res.width_px, res.height_px,
2019-02-07 12:09:10 +01:00
int(res.width_px*TPixelRenderer::num_components)),
m_pixfmt(m_rbuf),
m_raw_renderer(m_pixfmt),
m_renderer(m_raw_renderer),
2019-05-18 22:45:24 +02:00
m_mirror(mirror)
{
m_renderer.color(ColorWhite);
2019-04-09 14:52:29 +02:00
if(gamma > 0) m_gammafn = agg::gamma_power(gamma);
else m_gammafn = agg::gamma_threshold(0.5);
clear();
}
2019-05-18 22:45:24 +02:00
inline Impl(const Raster::Resolution& res,
const Raster::PixelDim &pd,
Format fmt,
double gamma = 1.0):
Impl(res, pd, {false, false}, gamma)
{
switch (fmt) {
case Format::PNG: m_mirror = {false, true}; break;
case Format::RAW: m_mirror = {false, false}; break;
}
2019-05-18 23:21:59 +02:00
m_fmt = fmt;
2019-05-18 22:45:24 +02:00
}
template<class P> void draw(const P &poly) {
agg::rasterizer_scanline_aa<> ras;
agg::scanline_p8 scanlines;
2019-04-09 14:52:29 +02:00
ras.gamma(m_gammafn);
auto&& path = to_path(contour(poly));
2019-05-18 22:45:24 +02:00
if(m_mirror[X]) flipx(path);
if(m_mirror[Y]) flipy(path);
ras.add_path(path);
for(auto& h : holes(poly)) {
2019-04-01 12:15:47 +02:00
auto&& holepath = to_path(h);
2019-05-18 22:45:24 +02:00
if(m_mirror[X]) flipx(holepath);
if(m_mirror[Y]) flipy(holepath);
2019-04-01 12:15:47 +02:00
ras.add_path(holepath);
}
agg::render_scanlines(ras, scanlines, m_renderer);
}
inline void clear() {
m_raw_renderer.clear(ColorBlack);
}
inline TBuffer& buffer() { return m_buf; }
2019-05-18 23:21:59 +02:00
inline Format format() const { return m_fmt; }
inline const Raster::Resolution resolution() { return m_resolution; }
2019-05-18 22:45:24 +02:00
private:
2019-04-09 14:52:29 +02:00
inline double getPx(const Point& p) {
return p(0) * m_pxdim_scaled.w_mm;
}
2019-04-09 14:52:29 +02:00
inline double getPy(const Point& p) {
return p(1) * m_pxdim_scaled.h_mm;
}
inline agg::path_storage to_path(const Polygon& poly)
2019-03-27 18:37:50 +01:00
{
return to_path(poly.points);
2019-03-27 18:37:50 +01:00
}
2019-04-09 14:52:29 +02:00
inline double getPx(const ClipperLib::IntPoint& p) {
return p.X * m_pxdim_scaled.w_mm;
2019-03-27 18:37:50 +01:00
}
2019-04-09 14:52:29 +02:00
inline double getPy(const ClipperLib::IntPoint& p) {
return p.Y * m_pxdim_scaled.h_mm;
2019-03-27 18:37:50 +01:00
}
template<class PointVec> agg::path_storage to_path(const PointVec& poly)
2019-03-27 18:37:50 +01:00
{
agg::path_storage path;
2019-04-09 14:52:29 +02:00
2019-03-27 18:37:50 +01:00
auto it = poly.begin();
path.move_to(getPx(*it), getPy(*it));
2019-04-09 14:52:29 +02:00
2019-03-27 18:37:50 +01:00
while(++it != poly.end())
path.line_to(getPx(*it), getPy(*it));
2019-03-27 18:37:50 +01:00
path.line_to(getPx(poly.front()), getPy(poly.front()));
return path;
}
2018-05-16 18:51:28 +02:00
};
const Raster::Impl::TPixel Raster::Impl::ColorWhite = Raster::Impl::TPixel(255);
const Raster::Impl::TPixel Raster::Impl::ColorBlack = Raster::Impl::TPixel(0);
2019-05-20 11:19:43 +02:00
template<> Raster::Raster() { reset(); };
2019-05-18 22:45:24 +02:00
Raster::~Raster() = default;
2019-05-20 11:19:43 +02:00
// Raster::Raster(Raster &&m) = default;
// Raster& Raster::operator=(Raster&&) = default;
// FIXME: remove after migrating to higher version of windows compiler
Raster::Raster(Raster &&m): m_impl(std::move(m.m_impl)) {}
Raster& Raster::operator=(Raster &&m) {
m_impl = std::move(m.m_impl); return *this;
}
2018-05-16 18:51:28 +02:00
2019-05-18 22:45:24 +02:00
void Raster::reset(const Raster::Resolution &r, const Raster::PixelDim &pd,
Format fmt, double gamma)
{
2019-05-18 22:45:24 +02:00
m_impl.reset();
m_impl.reset(new Impl(r, pd, fmt, gamma));
2018-07-25 17:27:02 +02:00
}
2018-05-22 15:13:07 +02:00
2018-07-25 17:27:02 +02:00
void Raster::reset(const Raster::Resolution &r, const Raster::PixelDim &pd,
2019-05-18 22:45:24 +02:00
const std::array<bool, 2>& mirror, double gamma)
2018-07-25 17:27:02 +02:00
{
m_impl.reset();
2019-05-18 22:45:24 +02:00
m_impl.reset(new Impl(r, pd, mirror, gamma));
}
void Raster::reset()
{
m_impl.reset();
}
Raster::Resolution Raster::resolution() const
{
if(m_impl) return m_impl->resolution();
return Resolution(0, 0);
}
2018-05-16 18:51:28 +02:00
void Raster::clear()
{
assert(m_impl);
m_impl->clear();
2018-05-16 18:51:28 +02:00
}
2019-03-27 18:37:50 +01:00
void Raster::draw(const ExPolygon &expoly)
2018-05-16 18:51:28 +02:00
{
2019-04-01 12:15:47 +02:00
m_impl->draw(expoly);
2019-03-27 18:37:50 +01:00
}
void Raster::draw(const ClipperLib::Polygon &poly)
{
2019-04-01 12:15:47 +02:00
m_impl->draw(poly);
2018-05-16 18:51:28 +02:00
}
2019-05-18 22:45:24 +02:00
void Raster::save(std::ostream& stream, Format fmt)
2018-05-16 18:51:28 +02:00
{
assert(m_impl);
2019-03-18 16:03:26 +01:00
if(!stream.good()) return;
2019-05-18 22:45:24 +02:00
switch(fmt) {
case Format::PNG: {
auto& b = m_impl->buffer();
size_t out_len = 0;
void * rawdata = tdefl_write_image_to_png_file_in_memory(
b.data(),
int(resolution().width_px),
int(resolution().height_px), 1, &out_len);
if(rawdata == nullptr) break;
stream.write(static_cast<const char*>(rawdata),
std::streamsize(out_len));
MZ_FREE(rawdata);
break;
}
2019-05-18 22:45:24 +02:00
case Format::RAW: {
stream << "P5 "
<< m_impl->resolution().width_px << " "
<< m_impl->resolution().height_px << " "
<< "255 ";
auto sz = m_impl->buffer().size()*sizeof(Impl::TBuffer::value_type);
stream.write(reinterpret_cast<const char*>(m_impl->buffer().data()),
std::streamsize(sz));
}
}
2018-05-16 18:51:28 +02:00
}
2019-05-18 23:21:59 +02:00
void Raster::save(std::ostream &stream)
{
save(stream, m_impl->format());
}
2019-05-18 22:45:24 +02:00
RawBytes Raster::save(Format fmt)
2019-03-18 18:02:50 +01:00
{
assert(m_impl);
2019-04-25 18:48:31 +02:00
std::vector<std::uint8_t> data; size_t s = 0;
2019-03-18 18:02:50 +01:00
2019-05-18 22:45:24 +02:00
switch(fmt) {
case Format::PNG: {
2019-03-18 18:02:50 +01:00
void *rawdata = tdefl_write_image_to_png_file_in_memory(
m_impl->buffer().data(),
int(resolution().width_px),
2019-03-25 14:53:25 +01:00
int(resolution().height_px), 1, &s);
2019-03-18 18:02:50 +01:00
if(rawdata == nullptr) break;
2019-04-25 18:48:31 +02:00
auto ptr = static_cast<std::uint8_t*>(rawdata);
data.reserve(s); std::copy(ptr, ptr + s, std::back_inserter(data));
MZ_FREE(rawdata);
2019-03-18 18:02:50 +01:00
break;
}
2019-05-18 22:45:24 +02:00
case Format::RAW: {
2019-03-18 18:02:50 +01:00
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);
2019-03-25 14:53:25 +01:00
s = sz + header.size();
2019-04-25 18:48:31 +02:00
data.reserve(s);
2019-03-18 18:02:50 +01:00
auto buff = reinterpret_cast<std::uint8_t*>(m_impl->buffer().data());
2019-04-25 18:48:31 +02:00
std::copy(header.begin(), header.end(), std::back_inserter(data));
std::copy(buff, buff+sz, std::back_inserter(data));
break;
2019-03-18 18:02:50 +01:00
}
}
2019-04-25 18:48:31 +02:00
return {std::move(data)};
}
2019-05-18 23:21:59 +02:00
RawBytes Raster::save()
{
return save(m_impl->format());
}
2018-05-16 18:51:28 +02:00
}
2019-05-18 22:45:24 +02:00
}
#endif // SLARASTER_CPP