msdf-error-correction.cpp 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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 SDFTransformation &transformation, 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, transformation);
  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 SDFTransformation &transformation, double minDeviationRatio, bool protectAll) {
  48. Bitmap<byte, 1> stencilBuffer(sdf.width, sdf.height);
  49. MSDFErrorCorrection ec(stencilBuffer, transformation);
  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 SDFTransformation &transformation, const MSDFGeneratorConfig &config) {
  57. msdfErrorCorrectionInner(sdf, shape, transformation, config);
  58. }
  59. void msdfErrorCorrection(const BitmapRef<float, 4> &sdf, const Shape &shape, const SDFTransformation &transformation, const MSDFGeneratorConfig &config) {
  60. msdfErrorCorrectionInner(sdf, shape, transformation, config);
  61. }
  62. void msdfErrorCorrection(const BitmapRef<float, 3> &sdf, const Shape &shape, const Projection &projection, Range range, const MSDFGeneratorConfig &config) {
  63. msdfErrorCorrectionInner(sdf, shape, SDFTransformation(projection, range), config);
  64. }
  65. void msdfErrorCorrection(const BitmapRef<float, 4> &sdf, const Shape &shape, const Projection &projection, Range range, const MSDFGeneratorConfig &config) {
  66. msdfErrorCorrectionInner(sdf, shape, SDFTransformation(projection, range), config);
  67. }
  68. void msdfFastDistanceErrorCorrection(const BitmapRef<float, 3> &sdf, const SDFTransformation &transformation, double minDeviationRatio) {
  69. msdfErrorCorrectionShapeless(sdf, transformation, minDeviationRatio, false);
  70. }
  71. void msdfFastDistanceErrorCorrection(const BitmapRef<float, 4> &sdf, const SDFTransformation &transformation, double minDeviationRatio) {
  72. msdfErrorCorrectionShapeless(sdf, transformation, minDeviationRatio, false);
  73. }
  74. void msdfFastDistanceErrorCorrection(const BitmapRef<float, 3> &sdf, const Projection &projection, Range range, double minDeviationRatio) {
  75. msdfErrorCorrectionShapeless(sdf, SDFTransformation(projection, range), minDeviationRatio, false);
  76. }
  77. void msdfFastDistanceErrorCorrection(const BitmapRef<float, 4> &sdf, const Projection &projection, Range range, double minDeviationRatio) {
  78. msdfErrorCorrectionShapeless(sdf, SDFTransformation(projection, range), minDeviationRatio, false);
  79. }
  80. void msdfFastEdgeErrorCorrection(const BitmapRef<float, 3> &sdf, const SDFTransformation &transformation, double minDeviationRatio) {
  81. msdfErrorCorrectionShapeless(sdf, transformation, minDeviationRatio, true);
  82. }
  83. void msdfFastEdgeErrorCorrection(const BitmapRef<float, 4> &sdf, const SDFTransformation &transformation, double minDeviationRatio) {
  84. msdfErrorCorrectionShapeless(sdf, transformation, minDeviationRatio, true);
  85. }
  86. void msdfFastEdgeErrorCorrection(const BitmapRef<float, 3> &sdf, const Projection &projection, Range range, double minDeviationRatio) {
  87. msdfErrorCorrectionShapeless(sdf, SDFTransformation(projection, range), minDeviationRatio, true);
  88. }
  89. void msdfFastEdgeErrorCorrection(const BitmapRef<float, 4> &sdf, const Projection &projection, Range range, double minDeviationRatio) {
  90. msdfErrorCorrectionShapeless(sdf, SDFTransformation(projection, range), minDeviationRatio, true);
  91. }
  92. // Legacy version
  93. inline static bool detectClash(const float *a, const float *b, double threshold) {
  94. // Sort channels so that pairs (a0, b0), (a1, b1), (a2, b2) go from biggest to smallest absolute difference
  95. float a0 = a[0], a1 = a[1], a2 = a[2];
  96. float b0 = b[0], b1 = b[1], b2 = b[2];
  97. float tmp;
  98. if (fabsf(b0-a0) < fabsf(b1-a1)) {
  99. tmp = a0, a0 = a1, a1 = tmp;
  100. tmp = b0, b0 = b1, b1 = tmp;
  101. }
  102. if (fabsf(b1-a1) < fabsf(b2-a2)) {
  103. tmp = a1, a1 = a2, a2 = tmp;
  104. tmp = b1, b1 = b2, b2 = tmp;
  105. if (fabsf(b0-a0) < fabsf(b1-a1)) {
  106. tmp = a0, a0 = a1, a1 = tmp;
  107. tmp = b0, b0 = b1, b1 = tmp;
  108. }
  109. }
  110. return (fabsf(b1-a1) >= threshold) &&
  111. !(b0 == b1 && b0 == b2) && // Ignore if other pixel has been equalized
  112. fabsf(a2-.5f) >= fabsf(b2-.5f); // Out of the pair, only flag the pixel farther from a shape edge
  113. }
  114. template <int N>
  115. static void msdfErrorCorrectionInner_legacy(const BitmapRef<float, N> &output, const Vector2 &threshold) {
  116. std::vector<std::pair<int, int> > clashes;
  117. int w = output.width, h = output.height;
  118. for (int y = 0; y < h; ++y)
  119. for (int x = 0; x < w; ++x) {
  120. if (
  121. (x > 0 && detectClash(output(x, y), output(x-1, y), threshold.x)) ||
  122. (x < w-1 && detectClash(output(x, y), output(x+1, y), threshold.x)) ||
  123. (y > 0 && detectClash(output(x, y), output(x, y-1), threshold.y)) ||
  124. (y < h-1 && detectClash(output(x, y), output(x, y+1), threshold.y))
  125. )
  126. clashes.push_back(std::make_pair(x, y));
  127. }
  128. for (std::vector<std::pair<int, int> >::const_iterator clash = clashes.begin(); clash != clashes.end(); ++clash) {
  129. float *pixel = output(clash->first, clash->second);
  130. float med = median(pixel[0], pixel[1], pixel[2]);
  131. pixel[0] = med, pixel[1] = med, pixel[2] = med;
  132. }
  133. #ifndef MSDFGEN_NO_DIAGONAL_CLASH_DETECTION
  134. clashes.clear();
  135. for (int y = 0; y < h; ++y)
  136. for (int x = 0; x < w; ++x) {
  137. if (
  138. (x > 0 && y > 0 && detectClash(output(x, y), output(x-1, y-1), threshold.x+threshold.y)) ||
  139. (x < w-1 && y > 0 && detectClash(output(x, y), output(x+1, y-1), threshold.x+threshold.y)) ||
  140. (x > 0 && y < h-1 && detectClash(output(x, y), output(x-1, y+1), threshold.x+threshold.y)) ||
  141. (x < w-1 && y < h-1 && detectClash(output(x, y), output(x+1, y+1), threshold.x+threshold.y))
  142. )
  143. clashes.push_back(std::make_pair(x, y));
  144. }
  145. for (std::vector<std::pair<int, int> >::const_iterator clash = clashes.begin(); clash != clashes.end(); ++clash) {
  146. float *pixel = output(clash->first, clash->second);
  147. float med = median(pixel[0], pixel[1], pixel[2]);
  148. pixel[0] = med, pixel[1] = med, pixel[2] = med;
  149. }
  150. #endif
  151. }
  152. void msdfErrorCorrection_legacy(const BitmapRef<float, 3> &output, const Vector2 &threshold) {
  153. msdfErrorCorrectionInner_legacy(output, threshold);
  154. }
  155. void msdfErrorCorrection_legacy(const BitmapRef<float, 4> &output, const Vector2 &threshold) {
  156. msdfErrorCorrectionInner_legacy(output, threshold);
  157. }
  158. }