msdf-error-correction.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. #include "msdf-error-correction.h"
  2. #include <vector>
  3. #include "arithmetics.hpp"
  4. #include "Bitmap.h"
  5. #include "MSDFErrorCorrection.h"
  6. namespace msdfgen {
  7. template <int N>
  8. static void msdfErrorCorrectionInner(const BitmapRef<float, N> &sdf, const Shape &shape, const Projection &projection, double range, const ErrorCorrectionConfig &config) {
  9. if (config.mode == ErrorCorrectionConfig::DISABLED)
  10. return;
  11. Bitmap<byte, 1> stencilBuffer;
  12. if (!config.buffer)
  13. stencilBuffer = Bitmap<byte, 1>(sdf.width, sdf.height);
  14. BitmapRef<byte, 1> stencil;
  15. stencil.pixels = config.buffer ? config.buffer : (byte *) stencilBuffer;
  16. stencil.width = sdf.width, stencil.height = sdf.height;
  17. MSDFErrorCorrection ec(stencil);
  18. switch (config.mode) {
  19. case ErrorCorrectionConfig::DISABLED:
  20. case ErrorCorrectionConfig::INDISCRIMINATE:
  21. break;
  22. case ErrorCorrectionConfig::EDGE_PRIORITY:
  23. ec.protectCorners(shape, projection);
  24. ec.protectEdges<N>(sdf, projection, range);
  25. break;
  26. case ErrorCorrectionConfig::EDGE_ONLY:
  27. ec.protectAll();
  28. break;
  29. }
  30. ec.findErrors<N>(sdf, projection, range, config.threshold);
  31. ec.apply(sdf);
  32. }
  33. template <int N>
  34. static void msdfErrorCorrectionShapeless(const BitmapRef<float, N> &sdf, const Projection &projection, double range, double threshold, bool protectAll) {
  35. Bitmap<byte, 1> stencilBuffer(sdf.width, sdf.height);
  36. MSDFErrorCorrection ec(stencilBuffer);
  37. if (protectAll)
  38. ec.protectAll();
  39. ec.findErrors<N>(sdf, projection, range, threshold);
  40. ec.apply(sdf);
  41. }
  42. void msdfErrorCorrection(const BitmapRef<float, 3> &sdf, const Shape &shape, const Projection &projection, double range, const ErrorCorrectionConfig &config) {
  43. msdfErrorCorrectionInner(sdf, shape, projection, range, config);
  44. }
  45. void msdfErrorCorrection(const BitmapRef<float, 4> &sdf, const Shape &shape, const Projection &projection, double range, const ErrorCorrectionConfig &config) {
  46. msdfErrorCorrectionInner(sdf, shape, projection, range, config);
  47. }
  48. void msdfDistanceErrorCorrection(const BitmapRef<float, 3> &sdf, const Projection &projection, double range, double threshold) {
  49. msdfErrorCorrectionShapeless(sdf, projection, range, threshold, false);
  50. }
  51. void msdfDistanceErrorCorrection(const BitmapRef<float, 4> &sdf, const Projection &projection, double range, double threshold) {
  52. msdfErrorCorrectionShapeless(sdf, projection, range, threshold, false);
  53. }
  54. void msdfEdgeErrorCorrection(const BitmapRef<float, 3> &sdf, const Projection &projection, double range, double threshold) {
  55. msdfErrorCorrectionShapeless(sdf, projection, range, threshold, true);
  56. }
  57. void msdfEdgeErrorCorrection(const BitmapRef<float, 4> &sdf, const Projection &projection, double range, double threshold) {
  58. msdfErrorCorrectionShapeless(sdf, projection, range, threshold, true);
  59. }
  60. // Legacy version
  61. inline static bool detectClash(const float *a, const float *b, double threshold) {
  62. // Sort channels so that pairs (a0, b0), (a1, b1), (a2, b2) go from biggest to smallest absolute difference
  63. float a0 = a[0], a1 = a[1], a2 = a[2];
  64. float b0 = b[0], b1 = b[1], b2 = b[2];
  65. float tmp;
  66. if (fabsf(b0-a0) < fabsf(b1-a1)) {
  67. tmp = a0, a0 = a1, a1 = tmp;
  68. tmp = b0, b0 = b1, b1 = tmp;
  69. }
  70. if (fabsf(b1-a1) < fabsf(b2-a2)) {
  71. tmp = a1, a1 = a2, a2 = tmp;
  72. tmp = b1, b1 = b2, b2 = tmp;
  73. if (fabsf(b0-a0) < fabsf(b1-a1)) {
  74. tmp = a0, a0 = a1, a1 = tmp;
  75. tmp = b0, b0 = b1, b1 = tmp;
  76. }
  77. }
  78. return (fabsf(b1-a1) >= threshold) &&
  79. !(b0 == b1 && b0 == b2) && // Ignore if other pixel has been equalized
  80. fabsf(a2-.5f) >= fabsf(b2-.5f); // Out of the pair, only flag the pixel farther from a shape edge
  81. }
  82. template <int N>
  83. static void msdfErrorCorrectionInner_legacy(const BitmapRef<float, N> &output, const Vector2 &threshold) {
  84. std::vector<std::pair<int, int> > clashes;
  85. int w = output.width, h = output.height;
  86. for (int y = 0; y < h; ++y)
  87. for (int x = 0; x < w; ++x) {
  88. if (
  89. (x > 0 && detectClash(output(x, y), output(x-1, y), threshold.x)) ||
  90. (x < w-1 && detectClash(output(x, y), output(x+1, y), threshold.x)) ||
  91. (y > 0 && detectClash(output(x, y), output(x, y-1), threshold.y)) ||
  92. (y < h-1 && detectClash(output(x, y), output(x, y+1), threshold.y))
  93. )
  94. clashes.push_back(std::make_pair(x, y));
  95. }
  96. for (std::vector<std::pair<int, int> >::const_iterator clash = clashes.begin(); clash != clashes.end(); ++clash) {
  97. float *pixel = output(clash->first, clash->second);
  98. float med = median(pixel[0], pixel[1], pixel[2]);
  99. pixel[0] = med, pixel[1] = med, pixel[2] = med;
  100. }
  101. #ifndef MSDFGEN_NO_DIAGONAL_CLASH_DETECTION
  102. clashes.clear();
  103. for (int y = 0; y < h; ++y)
  104. for (int x = 0; x < w; ++x) {
  105. if (
  106. (x > 0 && y > 0 && detectClash(output(x, y), output(x-1, y-1), threshold.x+threshold.y)) ||
  107. (x < w-1 && y > 0 && detectClash(output(x, y), output(x+1, y-1), threshold.x+threshold.y)) ||
  108. (x > 0 && y < h-1 && detectClash(output(x, y), output(x-1, y+1), threshold.x+threshold.y)) ||
  109. (x < w-1 && y < h-1 && detectClash(output(x, y), output(x+1, y+1), threshold.x+threshold.y))
  110. )
  111. clashes.push_back(std::make_pair(x, y));
  112. }
  113. for (std::vector<std::pair<int, int> >::const_iterator clash = clashes.begin(); clash != clashes.end(); ++clash) {
  114. float *pixel = output(clash->first, clash->second);
  115. float med = median(pixel[0], pixel[1], pixel[2]);
  116. pixel[0] = med, pixel[1] = med, pixel[2] = med;
  117. }
  118. #endif
  119. }
  120. void msdfErrorCorrection_legacy(const BitmapRef<float, 3> &output, const Vector2 &threshold) {
  121. msdfErrorCorrectionInner_legacy(output, threshold);
  122. }
  123. void msdfErrorCorrection_legacy(const BitmapRef<float, 4> &output, const Vector2 &threshold) {
  124. msdfErrorCorrectionInner_legacy(output, threshold);
  125. }
  126. }