1 #ifndef SLARASTER_CPP
2 #define SLARASTER_CPP
3 
4 #include <functional>
5 
6 #include <libslic3r/SLA/RasterBase.hpp>
7 #include <libslic3r/SLA/AGGRaster.hpp>
8 
9 // minz image write:
10 #include <miniz.h>
11 
12 namespace Slic3r { namespace sla {
13 
14 const RasterBase::TMirroring RasterBase::NoMirror = {false, false};
15 const RasterBase::TMirroring RasterBase::MirrorX  = {true, false};
16 const RasterBase::TMirroring RasterBase::MirrorY  = {false, true};
17 const RasterBase::TMirroring RasterBase::MirrorXY = {true, true};
18 
operator ()(const void * ptr,size_t w,size_t h,size_t num_components)19 EncodedRaster PNGRasterEncoder::operator()(const void *ptr, size_t w, size_t h,
20                                            size_t      num_components)
21 {
22     std::vector<uint8_t> buf;
23     size_t s = 0;
24 
25     void *rawdata = tdefl_write_image_to_png_file_in_memory(
26         ptr, int(w), int(h), int(num_components), &s);
27 
28     // On error, data() will return an empty vector. No other info can be
29     // retrieved from miniz anyway...
30     if (rawdata == nullptr) return EncodedRaster({}, "png");
31 
32     auto pptr = static_cast<std::uint8_t*>(rawdata);
33 
34     buf.reserve(s);
35     std::copy(pptr, pptr + s, std::back_inserter(buf));
36 
37     MZ_FREE(rawdata);
38     return EncodedRaster(std::move(buf), "png");
39 }
40 
operator <<(std::ostream & stream,const EncodedRaster & bytes)41 std::ostream &operator<<(std::ostream &stream, const EncodedRaster &bytes)
42 {
43     stream.write(reinterpret_cast<const char *>(bytes.data()),
44                  std::streamsize(bytes.size()));
45 
46     return stream;
47 }
48 
operator ()(const void * ptr,size_t w,size_t h,size_t num_components)49 EncodedRaster PPMRasterEncoder::operator()(const void *ptr, size_t w, size_t h,
50                                            size_t      num_components)
51 {
52     std::vector<uint8_t> buf;
53 
54     auto header = std::string("P5 ") +
55             std::to_string(w) + " " +
56             std::to_string(h) + " " + "255 ";
57 
58     auto sz = w * h * num_components;
59     size_t s = sz + header.size();
60 
61     buf.reserve(s);
62 
63     auto buff = reinterpret_cast<const std::uint8_t*>(ptr);
64     std::copy(header.begin(), header.end(), std::back_inserter(buf));
65     std::copy(buff, buff+sz, std::back_inserter(buf));
66 
67     return EncodedRaster(std::move(buf), "ppm");
68 }
69 
create_raster_grayscale_aa(const RasterBase::Resolution & res,const RasterBase::PixelDim & pxdim,double gamma,const RasterBase::Trafo & tr)70 std::unique_ptr<RasterBase> create_raster_grayscale_aa(
71     const RasterBase::Resolution &res,
72     const RasterBase::PixelDim &  pxdim,
73     double                        gamma,
74     const RasterBase::Trafo &     tr)
75 {
76     std::unique_ptr<RasterBase> rst;
77 
78     if (gamma > 0)
79         rst = std::make_unique<RasterGrayscaleAAGammaPower>(res, pxdim, tr, gamma);
80     else
81         rst = std::make_unique<RasterGrayscaleAA>(res, pxdim, tr, agg::gamma_threshold(.5));
82 
83     return rst;
84 }
85 
86 } // namespace sla
87 } // namespace Slic3r
88 
89 #endif // SLARASTER_CPP
90