msdfgen.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. #pragma once
  2. /*
  3. * MULTI-CHANNEL SIGNED DISTANCE FIELD GENERATOR
  4. * ---------------------------------------------
  5. * A utility by Viktor Chlumsky, (c) 2014 - 2023
  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.hpp"
  17. #include "core/Projection.h"
  18. #include "core/Scanline.h"
  19. #include "core/Shape.h"
  20. #include "core/BitmapRef.hpp"
  21. #include "core/Bitmap.h"
  22. #include "core/bitmap-interpolation.hpp"
  23. #include "core/pixel-conversion.hpp"
  24. #include "core/edge-coloring.h"
  25. #include "core/generator-config.h"
  26. #include "core/msdf-error-correction.h"
  27. #include "core/render-sdf.h"
  28. #include "core/rasterization.h"
  29. #include "core/sdf-error-estimation.h"
  30. #include "core/save-bmp.h"
  31. #include "core/save-tiff.h"
  32. #include "core/shape-description.h"
  33. namespace msdfgen {
  34. /// Generates a conventional single-channel signed distance field.
  35. void generateSDF(const BitmapRef<float, 1> &output, const Shape &shape, const Projection &projection, double range, const GeneratorConfig &config = GeneratorConfig());
  36. /// Generates a single-channel signed pseudo-distance field.
  37. void generatePseudoSDF(const BitmapRef<float, 1> &output, const Shape &shape, const Projection &projection, double range, const GeneratorConfig &config = GeneratorConfig());
  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, const Projection &projection, double range, const MSDFGeneratorConfig &config = MSDFGeneratorConfig());
  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, const Projection &projection, double range, const MSDFGeneratorConfig &config = MSDFGeneratorConfig());
  42. // Old version of the function API's kept for backwards compatibility
  43. void generateSDF(const BitmapRef<float, 1> &output, const Shape &shape, double range, const Vector2 &scale, const Vector2 &translate, bool overlapSupport = true);
  44. void generatePseudoSDF(const BitmapRef<float, 1> &output, const Shape &shape, double range, const Vector2 &scale, const Vector2 &translate, bool overlapSupport = true);
  45. void generateMSDF(const BitmapRef<float, 3> &output, const Shape &shape, double range, const Vector2 &scale, const Vector2 &translate, const ErrorCorrectionConfig &errorCorrectionConfig = ErrorCorrectionConfig(), bool overlapSupport = true);
  46. void generateMTSDF(const BitmapRef<float, 4> &output, const Shape &shape, double range, const Vector2 &scale, const Vector2 &translate, const ErrorCorrectionConfig &errorCorrectionConfig = ErrorCorrectionConfig(), bool overlapSupport = true);
  47. // Original simpler versions of the previous functions, which work well under normal circumstances, but cannot deal with overlapping contours.
  48. void generateSDF_legacy(const BitmapRef<float, 1> &output, const Shape &shape, double range, const Vector2 &scale, const Vector2 &translate);
  49. void generatePseudoSDF_legacy(const BitmapRef<float, 1> &output, const Shape &shape, double range, const Vector2 &scale, const Vector2 &translate);
  50. void generateMSDF_legacy(const BitmapRef<float, 3> &output, const Shape &shape, double range, const Vector2 &scale, const Vector2 &translate, ErrorCorrectionConfig errorCorrectionConfig = ErrorCorrectionConfig());
  51. void generateMTSDF_legacy(const BitmapRef<float, 4> &output, const Shape &shape, double range, const Vector2 &scale, const Vector2 &translate, ErrorCorrectionConfig errorCorrectionConfig = ErrorCorrectionConfig());
  52. }