msdf-error-correction.cpp 7.1 KB

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