MSDFErrorCorrection.h 2.3 KB

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