Rasterizer.hpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. #ifndef RASTERIZER_HPP
  2. #define RASTERIZER_HPP
  3. #include <ostream>
  4. #include <memory>
  5. #include <vector>
  6. #include <cstdint>
  7. namespace Slic3r {
  8. class ExPolygon;
  9. // Raw byte buffer paired with its size. Suitable for compressed PNG data.
  10. class RawBytes {
  11. class MinzDeleter {
  12. public:
  13. void operator()(std::uint8_t *rawptr);
  14. };
  15. public:
  16. std::unique_ptr<std::uint8_t, MinzDeleter> buffer = nullptr;
  17. size_t size = 0;
  18. // FIXME: the following is needed for MSVC2013 compatibility
  19. RawBytes() = default;
  20. RawBytes(const RawBytes&) = delete;
  21. RawBytes(RawBytes&& mv): buffer(std::move(mv.buffer)), size(mv.size) {}
  22. RawBytes& operator=(const RawBytes&) = delete;
  23. RawBytes& operator=(RawBytes&& mv) {
  24. buffer.swap(mv.buffer);
  25. size = mv.size;
  26. return *this;
  27. }
  28. };
  29. /**
  30. * @brief Raster captures an anti-aliased monochrome canvas where vectorial
  31. * polygons can be rasterized. Fill color is always white and the background is
  32. * black. Contours are anti-aliased.
  33. *
  34. * It also supports saving the raster data into a standard output stream in raw
  35. * or PNG format.
  36. */
  37. class Raster {
  38. class Impl;
  39. std::unique_ptr<Impl> m_impl;
  40. public:
  41. /// Supported compression types
  42. enum class Compression {
  43. RAW, //!> Uncompressed pixel data
  44. PNG //!> PNG compression
  45. };
  46. /// The Rasterizer expects the input polygons to have their coordinate
  47. /// system origin in the bottom left corner. If the raster is then
  48. /// configured with the TOP_LEFT origin parameter (in the constructor) than
  49. /// it will flip the Y axis in output to maintain the correct orientation.
  50. /// This is the default case with PNG images. They have the origin in the
  51. /// top left corner. Without the flipping, the image would be upside down
  52. /// with the scaled (clipper) coordinate system of the input polygons.
  53. enum class Origin {
  54. TOP_LEFT,
  55. BOTTOM_LEFT
  56. };
  57. /// Type that represents a resolution in pixels.
  58. struct Resolution {
  59. unsigned width_px;
  60. unsigned height_px;
  61. inline Resolution(unsigned w, unsigned h): width_px(w), height_px(h) {}
  62. inline unsigned pixels() const /*noexcept*/ {
  63. return width_px * height_px;
  64. }
  65. };
  66. /// Types that represents the dimension of a pixel in millimeters.
  67. struct PixelDim {
  68. double w_mm;
  69. double h_mm;
  70. inline PixelDim(double px_width_mm, double px_height_mm ):
  71. w_mm(px_width_mm), h_mm(px_height_mm) {}
  72. };
  73. /// Constructor taking the resolution and the pixel dimension.
  74. explicit Raster(const Resolution& r, const PixelDim& pd,
  75. Origin o = Origin::BOTTOM_LEFT );
  76. Raster();
  77. Raster(const Raster& cpy) = delete;
  78. Raster& operator=(const Raster& cpy) = delete;
  79. Raster(Raster&& m);
  80. ~Raster();
  81. /// Reallocated everything for the given resolution and pixel dimension.
  82. void reset(const Resolution& r, const PixelDim& pd);
  83. void reset(const Resolution& r, const PixelDim& pd, Origin o);
  84. /**
  85. * Release the allocated resources. Drawing in this state ends in
  86. * unspecified behavior.
  87. */
  88. void reset();
  89. /// Get the resolution of the raster.
  90. Resolution resolution() const;
  91. /// Clear the raster with black color.
  92. void clear();
  93. /// Draw a polygon with holes.
  94. void draw(const ExPolygon& poly);
  95. /// Save the raster on the specified stream.
  96. void save(std::ostream& stream, Compression comp = Compression::RAW);
  97. RawBytes save(Compression comp = Compression::RAW);
  98. };
  99. }
  100. #endif // RASTERIZER_HPP