MSDFErrorCorrection.h 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. #pragma once
  2. #include "SDFTransformation.h"
  3. #include "Shape.h"
  4. #include "BitmapRef.hpp"
  5. namespace msdfgen {
  6. /// Performs error correction on a computed MSDF to eliminate interpolation artifacts. This is a low-level class, you may want to use the API in msdf-error-correction.h instead.
  7. class MSDFErrorCorrection {
  8. public:
  9. /// Stencil flags.
  10. enum Flags {
  11. /// Texel marked as potentially causing interpolation errors.
  12. ERROR = 1,
  13. /// Texel marked as protected. Protected texels are only given the error flag if they cause inversion artifacts.
  14. PROTECTED = 2
  15. };
  16. MSDFErrorCorrection();
  17. explicit MSDFErrorCorrection(const BitmapRef<byte, 1> &stencil, const SDFTransformation &transformation);
  18. /// Sets the minimum ratio between the actual and maximum expected distance delta to be considered an error.
  19. void setMinDeviationRatio(double minDeviationRatio);
  20. /// Sets the minimum ratio between the pre-correction distance error and the post-correction distance error.
  21. void setMinImproveRatio(double minImproveRatio);
  22. /// Flags all texels that are interpolated at corners as protected.
  23. void protectCorners(const Shape &shape);
  24. /// Flags all texels that contribute to edges as protected.
  25. template <int N>
  26. void protectEdges(const BitmapConstRef<float, N> &sdf);
  27. /// Flags all texels as protected.
  28. void protectAll();
  29. /// Flags texels that are expected to cause interpolation artifacts based on analysis of the SDF only.
  30. template <int N>
  31. void findErrors(const BitmapConstRef<float, N> &sdf);
  32. /// Flags texels that are expected to cause interpolation artifacts based on analysis of the SDF and comparison with the exact shape distance.
  33. template <template <typename> class ContourCombiner, int N>
  34. void findErrors(const BitmapConstRef<float, N> &sdf, const Shape &shape);
  35. /// Modifies the MSDF so that all texels with the error flag are converted to single-channel.
  36. template <int N>
  37. void apply(const BitmapRef<float, N> &sdf) const;
  38. /// Returns the stencil in its current state (see Flags).
  39. BitmapConstRef<byte, 1> getStencil() const;
  40. private:
  41. BitmapRef<byte, 1> stencil;
  42. SDFTransformation transformation;
  43. double minDeviationRatio;
  44. double minImproveRatio;
  45. };
  46. }