2
0

msdfgen.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. #pragma once
  2. /*
  3. * MULTI-CHANNEL SIGNED DISTANCE FIELD GENERATOR
  4. * ---------------------------------------------
  5. * A utility by Viktor Chlumsky, (c) 2014 - 2025
  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/base.h"
  16. #include "core/arithmetics.hpp"
  17. #include "core/Vector2.hpp"
  18. #include "core/Range.hpp"
  19. #include "core/Projection.h"
  20. #include "core/DistanceMapping.h"
  21. #include "core/SDFTransformation.h"
  22. #include "core/Scanline.h"
  23. #include "core/Shape.h"
  24. #include "core/BitmapRef.hpp"
  25. #include "core/Bitmap.h"
  26. #include "core/bitmap-interpolation.hpp"
  27. #include "core/pixel-conversion.hpp"
  28. #include "core/edge-coloring.h"
  29. #include "core/generator-config.h"
  30. #include "core/msdf-error-correction.h"
  31. #include "core/render-sdf.h"
  32. #include "core/rasterization.h"
  33. #include "core/sdf-error-estimation.h"
  34. #include "core/save-bmp.h"
  35. #include "core/save-tiff.h"
  36. #include "core/save-rgba.h"
  37. #include "core/save-fl32.h"
  38. #include "core/shape-description.h"
  39. #include "core/export-svg.h"
  40. namespace msdfgen {
  41. /// Generates a conventional single-channel signed distance field.
  42. void generateSDF(const BitmapRef<float, 1> &output, const Shape &shape, const SDFTransformation &transformation, const GeneratorConfig &config = GeneratorConfig());
  43. /// Generates a single-channel signed perpendicular distance field.
  44. void generatePSDF(const BitmapRef<float, 1> &output, const Shape &shape, const SDFTransformation &transformation, const GeneratorConfig &config = GeneratorConfig());
  45. /// Generates a multi-channel signed distance field. Edge colors must be assigned first! (See edgeColoringSimple)
  46. void generateMSDF(const BitmapRef<float, 3> &output, const Shape &shape, const SDFTransformation &transformation, const MSDFGeneratorConfig &config = MSDFGeneratorConfig());
  47. /// Generates a multi-channel signed distance field with true distance in the alpha channel. Edge colors must be assigned first.
  48. void generateMTSDF(const BitmapRef<float, 4> &output, const Shape &shape, const SDFTransformation &transformation, const MSDFGeneratorConfig &config = MSDFGeneratorConfig());
  49. // Old version of the function API's kept for backwards compatibility
  50. void generateSDF(const BitmapRef<float, 1> &output, const Shape &shape, const Projection &projection, Range range, const GeneratorConfig &config = GeneratorConfig());
  51. void generatePSDF(const BitmapRef<float, 1> &output, const Shape &shape, const Projection &projection, Range range, const GeneratorConfig &config = GeneratorConfig());
  52. void generatePseudoSDF(const BitmapRef<float, 1> &output, const Shape &shape, const Projection &projection, Range range, const GeneratorConfig &config = GeneratorConfig());
  53. void generateMSDF(const BitmapRef<float, 3> &output, const Shape &shape, const Projection &projection, Range range, const MSDFGeneratorConfig &config = MSDFGeneratorConfig());
  54. void generateMTSDF(const BitmapRef<float, 4> &output, const Shape &shape, const Projection &projection, Range range, const MSDFGeneratorConfig &config = MSDFGeneratorConfig());
  55. void generateSDF(const BitmapRef<float, 1> &output, const Shape &shape, Range range, const Vector2 &scale, const Vector2 &translate, bool overlapSupport = true);
  56. void generatePSDF(const BitmapRef<float, 1> &output, const Shape &shape, Range range, const Vector2 &scale, const Vector2 &translate, bool overlapSupport = true);
  57. void generatePseudoSDF(const BitmapRef<float, 1> &output, const Shape &shape, Range range, const Vector2 &scale, const Vector2 &translate, bool overlapSupport = true);
  58. void generateMSDF(const BitmapRef<float, 3> &output, const Shape &shape, Range range, const Vector2 &scale, const Vector2 &translate, const ErrorCorrectionConfig &errorCorrectionConfig = ErrorCorrectionConfig(), bool overlapSupport = true);
  59. void generateMTSDF(const BitmapRef<float, 4> &output, const Shape &shape, Range range, const Vector2 &scale, const Vector2 &translate, const ErrorCorrectionConfig &errorCorrectionConfig = ErrorCorrectionConfig(), bool overlapSupport = true);
  60. // Original simpler versions of the previous functions, which work well under normal circumstances, but cannot deal with overlapping contours.
  61. void generateSDF_legacy(const BitmapRef<float, 1> &output, const Shape &shape, Range range, const Vector2 &scale, const Vector2 &translate);
  62. void generatePSDF_legacy(const BitmapRef<float, 1> &output, const Shape &shape, Range range, const Vector2 &scale, const Vector2 &translate);
  63. void generatePseudoSDF_legacy(const BitmapRef<float, 1> &output, const Shape &shape, Range range, const Vector2 &scale, const Vector2 &translate);
  64. void generateMSDF_legacy(const BitmapRef<float, 3> &output, const Shape &shape, Range range, const Vector2 &scale, const Vector2 &translate, ErrorCorrectionConfig errorCorrectionConfig = ErrorCorrectionConfig());
  65. void generateMTSDF_legacy(const BitmapRef<float, 4> &output, const Shape &shape, Range range, const Vector2 &scale, const Vector2 &translate, ErrorCorrectionConfig errorCorrectionConfig = ErrorCorrectionConfig());
  66. }