2
0

msdfgen.h 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. #pragma once
  2. /*
  3. * MULTI-CHANNEL SIGNED DISTANCE FIELD GENERATOR v1.7 (2020-03-07)
  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/interpolation-error-correction.h"
  25. #include "core/render-sdf.h"
  26. #include "core/rasterization.h"
  27. #include "core/estimate-sdf-error.h"
  28. #include "core/save-bmp.h"
  29. #include "core/save-tiff.h"
  30. #include "core/shape-description.h"
  31. #define MSDFGEN_VERSION "1.7"
  32. #define MSDFGEN_DEFAULT_ERROR_CORRECTION_THRESHOLD 1.001
  33. namespace msdfgen {
  34. /// Generates a conventional single-channel signed distance field.
  35. void generateSDF(const BitmapRef<float, 1> &output, const Shape &shape, double range, const Vector2 &scale, const Vector2 &translate, bool overlapSupport = true);
  36. /// Generates a single-channel signed pseudo-distance field.
  37. void generatePseudoSDF(const BitmapRef<float, 1> &output, const Shape &shape, double range, const Vector2 &scale, const Vector2 &translate, bool overlapSupport = true);
  38. /// Generates a multi-channel signed distance field. Edge colors must be assigned first! (See edgeColoringSimple)
  39. 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);
  40. /// Generates a multi-channel signed distance field with true distance in the alpha channel. Edge colors must be assigned first.
  41. 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);
  42. /// Resolves multi-channel signed distance field values that may cause interpolation artifacts. (Already called by generateMSDF)
  43. void msdfErrorCorrection(const BitmapRef<float, 3> &output, const Vector2 &threshold);
  44. void msdfErrorCorrection(const BitmapRef<float, 4> &output, const Vector2 &threshold);
  45. // Original simpler versions of the previous functions, which work well under normal circumstances, but cannot deal with overlapping contours.
  46. void generateSDF_legacy(const BitmapRef<float, 1> &output, const Shape &shape, double range, const Vector2 &scale, const Vector2 &translate);
  47. void generatePseudoSDF_legacy(const BitmapRef<float, 1> &output, const Shape &shape, double range, const Vector2 &scale, const Vector2 &translate);
  48. 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);
  49. 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);
  50. }