2
0

msdf-error-correction.cpp 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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 msdfFastDistanceErrorCorrection(const BitmapRef<float, 3> &sdf, Range pxRange, double minDeviationRatio) {
  81. msdfErrorCorrectionShapeless(sdf, SDFTransformation(Projection(), pxRange), minDeviationRatio, false);
  82. }
  83. void msdfFastDistanceErrorCorrection(const BitmapRef<float, 4> &sdf, Range pxRange, double minDeviationRatio) {
  84. msdfErrorCorrectionShapeless(sdf, SDFTransformation(Projection(), pxRange), minDeviationRatio, false);
  85. }
  86. void msdfFastEdgeErrorCorrection(const BitmapRef<float, 3> &sdf, const SDFTransformation &transformation, double minDeviationRatio) {
  87. msdfErrorCorrectionShapeless(sdf, transformation, minDeviationRatio, true);
  88. }
  89. void msdfFastEdgeErrorCorrection(const BitmapRef<float, 4> &sdf, const SDFTransformation &transformation, double minDeviationRatio) {
  90. msdfErrorCorrectionShapeless(sdf, transformation, minDeviationRatio, true);
  91. }
  92. void msdfFastEdgeErrorCorrection(const BitmapRef<float, 3> &sdf, const Projection &projection, Range range, double minDeviationRatio) {
  93. msdfErrorCorrectionShapeless(sdf, SDFTransformation(projection, range), minDeviationRatio, true);
  94. }
  95. void msdfFastEdgeErrorCorrection(const BitmapRef<float, 4> &sdf, const Projection &projection, Range range, double minDeviationRatio) {
  96. msdfErrorCorrectionShapeless(sdf, SDFTransformation(projection, range), minDeviationRatio, true);
  97. }
  98. void msdfFastEdgeErrorCorrection(const BitmapRef<float, 3> &sdf, Range pxRange, double minDeviationRatio) {
  99. msdfErrorCorrectionShapeless(sdf, SDFTransformation(Projection(), pxRange), minDeviationRatio, true);
  100. }
  101. void msdfFastEdgeErrorCorrection(const BitmapRef<float, 4> &sdf, Range pxRange, double minDeviationRatio) {
  102. msdfErrorCorrectionShapeless(sdf, SDFTransformation(Projection(), pxRange), minDeviationRatio, true);
  103. }
  104. // Legacy version
  105. inline static bool detectClash(const float *a, const float *b, double threshold) {
  106. // Sort channels so that pairs (a0, b0), (a1, b1), (a2, b2) go from biggest to smallest absolute difference
  107. float a0 = a[0], a1 = a[1], a2 = a[2];
  108. float b0 = b[0], b1 = b[1], b2 = b[2];
  109. float tmp;
  110. if (fabsf(b0-a0) < fabsf(b1-a1)) {
  111. tmp = a0, a0 = a1, a1 = tmp;
  112. tmp = b0, b0 = b1, b1 = tmp;
  113. }
  114. if (fabsf(b1-a1) < fabsf(b2-a2)) {
  115. tmp = a1, a1 = a2, a2 = tmp;
  116. tmp = b1, b1 = b2, b2 = tmp;
  117. if (fabsf(b0-a0) < fabsf(b1-a1)) {
  118. tmp = a0, a0 = a1, a1 = tmp;
  119. tmp = b0, b0 = b1, b1 = tmp;
  120. }
  121. }
  122. return (fabsf(b1-a1) >= threshold) &&
  123. !(b0 == b1 && b0 == b2) && // Ignore if other pixel has been equalized
  124. fabsf(a2-.5f) >= fabsf(b2-.5f); // Out of the pair, only flag the pixel farther from a shape edge
  125. }
  126. template <int N>
  127. static void msdfErrorCorrectionInner_legacy(const BitmapRef<float, N> &output, const Vector2 &threshold) {
  128. std::vector<std::pair<int, int> > clashes;
  129. int w = output.width, h = output.height;
  130. for (int y = 0; y < h; ++y)
  131. for (int x = 0; x < w; ++x) {
  132. if (
  133. (x > 0 && detectClash(output(x, y), output(x-1, y), threshold.x)) ||
  134. (x < w-1 && detectClash(output(x, y), output(x+1, y), threshold.x)) ||
  135. (y > 0 && detectClash(output(x, y), output(x, y-1), threshold.y)) ||
  136. (y < h-1 && detectClash(output(x, y), output(x, y+1), threshold.y))
  137. )
  138. clashes.push_back(std::make_pair(x, y));
  139. }
  140. for (std::vector<std::pair<int, int> >::const_iterator clash = clashes.begin(); clash != clashes.end(); ++clash) {
  141. float *pixel = output(clash->first, clash->second);
  142. float med = median(pixel[0], pixel[1], pixel[2]);
  143. pixel[0] = med, pixel[1] = med, pixel[2] = med;
  144. }
  145. #ifndef MSDFGEN_NO_DIAGONAL_CLASH_DETECTION
  146. clashes.clear();
  147. for (int y = 0; y < h; ++y)
  148. for (int x = 0; x < w; ++x) {
  149. if (
  150. (x > 0 && y > 0 && detectClash(output(x, y), output(x-1, y-1), threshold.x+threshold.y)) ||
  151. (x < w-1 && y > 0 && detectClash(output(x, y), output(x+1, y-1), threshold.x+threshold.y)) ||
  152. (x > 0 && y < h-1 && detectClash(output(x, y), output(x-1, y+1), threshold.x+threshold.y)) ||
  153. (x < w-1 && y < h-1 && detectClash(output(x, y), output(x+1, y+1), threshold.x+threshold.y))
  154. )
  155. clashes.push_back(std::make_pair(x, y));
  156. }
  157. for (std::vector<std::pair<int, int> >::const_iterator clash = clashes.begin(); clash != clashes.end(); ++clash) {
  158. float *pixel = output(clash->first, clash->second);
  159. float med = median(pixel[0], pixel[1], pixel[2]);
  160. pixel[0] = med, pixel[1] = med, pixel[2] = med;
  161. }
  162. #endif
  163. }
  164. void msdfErrorCorrection_legacy(const BitmapRef<float, 3> &output, const Vector2 &threshold) {
  165. msdfErrorCorrectionInner_legacy(output, threshold);
  166. }
  167. void msdfErrorCorrection_legacy(const BitmapRef<float, 4> &output, const Vector2 &threshold) {
  168. msdfErrorCorrectionInner_legacy(output, threshold);
  169. }
  170. }