1 #ifndef SLA_RASTERBASE_HPP
2 #define SLA_RASTERBASE_HPP
3 
4 #include <ostream>
5 #include <memory>
6 #include <vector>
7 #include <array>
8 #include <utility>
9 #include <cstdint>
10 
11 #include <libslic3r/ExPolygon.hpp>
12 #include <libslic3r/SLA/Concurrency.hpp>
13 
14 namespace ClipperLib { struct Polygon; }
15 
16 namespace Slic3r {
17 
18 template<class T> using uqptr = std::unique_ptr<T>;
19 template<class T> using shptr = std::shared_ptr<T>;
20 template<class T> using wkptr = std::weak_ptr<T>;
21 
22 namespace sla {
23 
24 // Raw byte buffer paired with its size. Suitable for compressed image data.
25 class EncodedRaster {
26 protected:
27     std::vector<uint8_t> m_buffer;
28     std::string m_ext;
29 public:
30     EncodedRaster() = default;
EncodedRaster(std::vector<uint8_t> && buf,std::string ext)31     explicit EncodedRaster(std::vector<uint8_t> &&buf, std::string ext)
32         : m_buffer(std::move(buf)), m_ext(std::move(ext))
33     {}
34 
size() const35     size_t size() const { return m_buffer.size(); }
data() const36     const void * data() const { return m_buffer.data(); }
extension() const37     const char * extension() const { return m_ext.c_str(); }
38 };
39 
40 using RasterEncoder =
41     std::function<EncodedRaster(const void *ptr, size_t w, size_t h, size_t num_components)>;
42 
43 class RasterBase {
44 public:
45 
46     enum Orientation { roLandscape, roPortrait };
47 
48     using TMirroring = std::array<bool, 2>;
49     static const TMirroring NoMirror;
50     static const TMirroring MirrorX;
51     static const TMirroring MirrorY;
52     static const TMirroring MirrorXY;
53 
54     struct Trafo {
55         bool mirror_x = false, mirror_y = false, flipXY = false;
56         coord_t center_x = 0, center_y = 0;
57 
58         // Portrait orientation will make sure the drawed polygons are rotated
59         // by 90 degrees.
TrafoSlic3r::sla::RasterBase::Trafo60         Trafo(Orientation o = roLandscape, const TMirroring &mirror = NoMirror)
61             // XY flipping implicitly does an X mirror
62             : mirror_x(o == roPortrait ? !mirror[0] : mirror[0])
63             , mirror_y(!mirror[1]) // Makes raster origin to be top left corner
64             , flipXY(o == roPortrait)
65         {}
66 
get_mirrorSlic3r::sla::RasterBase::Trafo67         TMirroring get_mirror() const { return { (roPortrait ? !mirror_x : mirror_x), mirror_y}; }
get_orientationSlic3r::sla::RasterBase::Trafo68         Orientation get_orientation() const { return flipXY ? roPortrait : roLandscape; }
get_centerSlic3r::sla::RasterBase::Trafo69         Point get_center() const { return {center_x, center_y}; }
70     };
71 
72     /// Type that represents a resolution in pixels.
73     struct Resolution {
74         size_t width_px = 0;
75         size_t height_px = 0;
76 
ResolutionSlic3r::sla::RasterBase::Resolution77         Resolution(size_t w = 0, size_t h = 0) : width_px(w), height_px(h) {}
pixelsSlic3r::sla::RasterBase::Resolution78         size_t pixels() const { return width_px * height_px; }
79     };
80 
81     /// Types that represents the dimension of a pixel in millimeters.
82     struct PixelDim {
83         double w_mm = 0.;
84         double h_mm = 0.;
85 
PixelDimSlic3r::sla::RasterBase::PixelDim86         PixelDim(double px_width_mm = 0.0, double px_height_mm = 0.0)
87             : w_mm(px_width_mm), h_mm(px_height_mm)
88         {}
89     };
90 
91     virtual ~RasterBase() = default;
92 
93     /// Draw a polygon with holes.
94     virtual void draw(const ExPolygon& poly) = 0;
95     virtual void draw(const ClipperLib::Polygon& poly) = 0;
96 
97     /// Get the resolution of the raster.
98     virtual Resolution resolution() const = 0;
99     virtual PixelDim   pixel_dimensions() const = 0;
100     virtual Trafo      trafo() const = 0;
101 
102     virtual EncodedRaster encode(RasterEncoder encoder) const = 0;
103 };
104 
105 struct PNGRasterEncoder {
106     EncodedRaster operator()(const void *ptr, size_t w, size_t h, size_t num_components);
107 };
108 
109 struct PPMRasterEncoder {
110     EncodedRaster operator()(const void *ptr, size_t w, size_t h, size_t num_components);
111 };
112 
113 std::ostream& operator<<(std::ostream &stream, const EncodedRaster &bytes);
114 
115 // If gamma is zero, thresholding will be performed which disables AA.
116 uqptr<RasterBase> create_raster_grayscale_aa(
117     const RasterBase::Resolution &res,
118     const RasterBase::PixelDim &  pxdim,
119     double                        gamma = 1.0,
120     const RasterBase::Trafo &     tr    = {});
121 
122 }} // namespace Slic3r::sla
123 
124 #endif // SLARASTERBASE_HPP
125