2
0

msdf-error-correction.cpp 9.4 KB

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