msdfgen.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. #pragma once
  2. /*
  3. * MULTI-CHANNEL SIGNED DISTANCE FIELD GENERATOR v1.8 (2020-10-17)
  4. * ---------------------------------------------------------------
  5. * A utility by Viktor Chlumsky, (c) 2014 - 2020
  6. *
  7. * The technique used to generate multi-channel distance fields in this code
  8. * has been developed by Viktor Chlumsky in 2014 for his master's thesis,
  9. * "Shape Decomposition for Multi-Channel Distance Fields". It provides improved
  10. * quality of sharp corners in glyphs and other 2D shapes compared to monochrome
  11. * distance fields. To reconstruct an image of the shape, apply the median of three
  12. * operation on the triplet of sampled signed distance values.
  13. *
  14. */
  15. #include "core/arithmetics.hpp"
  16. #include "core/Vector2.h"
  17. #include "core/Scanline.h"
  18. #include "core/Shape.h"
  19. #include "core/BitmapRef.hpp"
  20. #include "core/Bitmap.h"
  21. #include "core/bitmap-interpolation.hpp"
  22. #include "core/pixel-conversion.hpp"
  23. #include "core/edge-coloring.h"
  24. #include "core/msdf-error-correction.h"
  25. #include "core/render-sdf.h"
  26. #include "core/rasterization.h"
  27. #include "core/sdf-error-estimation.h"
  28. #include "core/save-bmp.h"
  29. #include "core/save-tiff.h"
  30. #include "core/shape-description.h"
  31. #define MSDFGEN_VERSION "1.8"
  32. namespace msdfgen {
  33. /// Generates a conventional single-channel signed distance field.
  34. void generateSDF(const BitmapRef<float, 1> &output, const Shape &shape, double range, const Vector2 &scale, const Vector2 &translate, bool overlapSupport = true);
  35. /// Generates a single-channel signed pseudo-distance field.
  36. void generatePseudoSDF(const BitmapRef<float, 1> &output, const Shape &shape, double range, const Vector2 &scale, const Vector2 &translate, bool overlapSupport = true);
  37. /// Generates a multi-channel signed distance field. Edge colors must be assigned first! (See edgeColoringSimple)
  38. void generateMSDF(const BitmapRef<float, 3> &output, const Shape &shape, double range, const Vector2 &scale, const Vector2 &translate, double edgeThreshold = MSDFGEN_DEFAULT_ERROR_CORRECTION_THRESHOLD, bool overlapSupport = true);
  39. /// Generates a multi-channel signed distance field with true distance in the alpha channel. Edge colors must be assigned first.
  40. void generateMTSDF(const BitmapRef<float, 4> &output, const Shape &shape, double range, const Vector2 &scale, const Vector2 &translate, double edgeThreshold = MSDFGEN_DEFAULT_ERROR_CORRECTION_THRESHOLD, bool overlapSupport = true);
  41. // Original simpler versions of the previous functions, which work well under normal circumstances, but cannot deal with overlapping contours.
  42. void generateSDF_legacy(const BitmapRef<float, 1> &output, const Shape &shape, double range, const Vector2 &scale, const Vector2 &translate);
  43. void generatePseudoSDF_legacy(const BitmapRef<float, 1> &output, const Shape &shape, double range, const Vector2 &scale, const Vector2 &translate);
  44. void generateMSDF_legacy(const BitmapRef<float, 3> &output, const Shape &shape, double range, const Vector2 &scale, const Vector2 &translate, double edgeThreshold = MSDFGEN_DEFAULT_ERROR_CORRECTION_THRESHOLD);
  45. void generateMTSDF_legacy(const BitmapRef<float, 4> &output, const Shape &shape, double range, const Vector2 &scale, const Vector2 &translate, double edgeThreshold = MSDFGEN_DEFAULT_ERROR_CORRECTION_THRESHOLD);
  46. }