msdf-edge-artifact-patcher.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. #include "msdf-edge-artifact-patcher.h"
  2. #include <vector>
  3. #include <utility>
  4. #include "arithmetics.hpp"
  5. #include "equation-solver.h"
  6. #include "bitmap-interpolation.hpp"
  7. #include "edge-selectors.h"
  8. #include "contour-combiners.h"
  9. #include "ShapeDistanceFinder.h"
  10. namespace msdfgen {
  11. static bool isHotspot(float am, float bm, float xm) {
  12. return (am > .5f && bm > .5f && xm < .5f) || (am < .5f && bm < .5f && xm > .5f);
  13. // A much more aggressive version for the entire distance field (not just edges): return median(am, bm, xm) != xm;
  14. }
  15. static int findLinearChannelHotspots(double t[1], const float *a, const float *b, float dA, float dB) {
  16. int found = 0;
  17. double x = (double) dA/(dA-dB);
  18. if (x > 0 && x < 1) {
  19. float am = median(a[0], a[1], a[2]);
  20. float bm = median(b[0], b[1], b[2]);
  21. float xm = median(
  22. mix(a[0], b[0], x),
  23. mix(a[1], b[1], x),
  24. mix(a[2], b[2], x)
  25. );
  26. if (isHotspot(am, bm, xm))
  27. t[found++] = x;
  28. }
  29. return found;
  30. }
  31. static int findDiagonalChannelHotspots(double t[2], const float *a, const float *b, const float *c, const float *d, float dA, float dB, float dC, float dD) {
  32. int found = 0;
  33. double x[2];
  34. int solutions = solveQuadratic(x, (dD-dC)-(dB-dA), dC+dB-2*dA, dA);
  35. for (int i = 0; i < solutions; ++i)
  36. if (x[i] > 0 && x[i] < 1) {
  37. float am = median(a[0], a[1], a[2]);
  38. float bm = median(b[0], b[1], b[2]);
  39. float xm = median(
  40. mix(mix(a[0], b[0], x[i]), mix(c[0], d[0], x[i]), x[i]),
  41. mix(mix(a[1], b[1], x[i]), mix(c[1], d[1], x[i]), x[i]),
  42. mix(mix(a[2], b[2], x[i]), mix(c[2], d[2], x[i]), x[i])
  43. );
  44. if (isHotspot(am, bm, xm))
  45. t[found++] = x[i];
  46. }
  47. return found;
  48. }
  49. static int findLinearHotspots(double t[3], const float *a, const float *b) {
  50. int found = 0;
  51. found += findLinearChannelHotspots(t+found, a, b, a[1]-a[0], b[1]-b[0]);
  52. found += findLinearChannelHotspots(t+found, a, b, a[2]-a[1], b[2]-b[1]);
  53. found += findLinearChannelHotspots(t+found, a, b, a[0]-a[2], b[0]-b[2]);
  54. return found;
  55. }
  56. static int findDiagonalHotspots(double t[6], const float *a, const float *b, const float *c, const float *d) {
  57. int found = 0;
  58. found += findDiagonalChannelHotspots(t+found, a, b, c, d, a[1]-a[0], b[1]-b[0], c[1]-c[0], d[1]-d[0]);
  59. found += findDiagonalChannelHotspots(t+found, a, b, c, d, a[2]-a[1], b[2]-b[1], c[2]-c[1], d[2]-d[1]);
  60. found += findDiagonalChannelHotspots(t+found, a, b, c, d, a[0]-a[2], b[0]-b[2], c[0]-c[2], d[0]-d[2]);
  61. return found;
  62. }
  63. template <int N>
  64. void findHotspots(std::vector<Point2> &hotspots, const BitmapConstRef<float, N> &sdf) {
  65. // All hotspots intersect either the horizontal, vertical, or diagonal line that connects neighboring texels
  66. // Horizontal:
  67. for (int y = 0; y < sdf.height; ++y) {
  68. const float *left = sdf(0, y);
  69. const float *right = sdf(1, y);
  70. for (int x = 0; x < sdf.width-1; ++x) {
  71. double t[3];
  72. int found = findLinearHotspots(t, left, right);
  73. for (int i = 0; i < found; ++i)
  74. hotspots.push_back(Point2(x+.5+t[i], y+.5));
  75. left += N, right += N;
  76. }
  77. }
  78. // Vertical:
  79. for (int y = 0; y < sdf.height-1; ++y) {
  80. const float *bottom = sdf(0, y);
  81. const float *top = sdf(0, y+1);
  82. for (int x = 0; x < sdf.width; ++x) {
  83. double t[3];
  84. int found = findLinearHotspots(t, bottom, top);
  85. for (int i = 0; i < found; ++i)
  86. hotspots.push_back(Point2(x+.5, y+.5+t[i]));
  87. bottom += N, top += N;
  88. }
  89. }
  90. // Diagonal:
  91. for (int y = 0; y < sdf.height-1; ++y) {
  92. const float *lb = sdf(0, y);
  93. const float *rb = sdf(1, y);
  94. const float *lt = sdf(0, y+1);
  95. const float *rt = sdf(1, y+1);
  96. for (int x = 0; x < sdf.width-1; ++x) {
  97. double t[6];
  98. int found = 0;
  99. found = findDiagonalHotspots(t, lb, rb, lt, rt);
  100. for (int i = 0; i < found; ++i)
  101. hotspots.push_back(Point2(x+.5+t[i], y+.5+t[i]));
  102. found = findDiagonalHotspots(t, lt, rt, lb, rb);
  103. for (int i = 0; i < found; ++i)
  104. hotspots.push_back(Point2(x+.5+t[i], y+1.5-t[i]));
  105. lb += N, rb += N, lt += N, rt += N;
  106. }
  107. }
  108. }
  109. template <template <typename> class ContourCombiner, int N>
  110. static void msdfPatchEdgeArtifactsInner(const BitmapRef<float, N> &sdf, const Shape &shape, double range, const Vector2 &scale, const Vector2 &translate) {
  111. ShapeDistanceFinder<ContourCombiner<PseudoDistanceSelector> > distanceFinder(shape);
  112. std::vector<Point2> hotspots;
  113. findHotspots(hotspots, BitmapConstRef<float, N>(sdf));
  114. std::vector<std::pair<int, int> > artifacts;
  115. artifacts.reserve(hotspots.size());
  116. for (std::vector<Point2>::const_iterator hotspot = hotspots.begin(); hotspot != hotspots.end(); ++hotspot) {
  117. Point2 pos = *hotspot/scale-translate;
  118. double actualDistance = distanceFinder.distance(pos);
  119. float sd = float(actualDistance/range+.5);
  120. // Store hotspot's closest texel's current color
  121. float *subject = sdf((int) hotspot->x, (int) hotspot->y);
  122. float texel[N];
  123. memcpy(texel, subject, N*sizeof(float));
  124. // Sample signed distance at hotspot
  125. float msd[N];
  126. interpolate(msd, BitmapConstRef<float, N>(sdf), *hotspot);
  127. float oldSsd = median(msd[0], msd[1], msd[2]);
  128. // Flatten hotspot's closest texel
  129. float med = median(subject[0], subject[1], subject[2]);
  130. subject[0] = med, subject[1] = med, subject[2] = med;
  131. // Sample signed distance at hotspot after flattening
  132. interpolate(msd, BitmapConstRef<float, N>(sdf), *hotspot);
  133. float newSsd = median(msd[0], msd[1], msd[2]);
  134. // Revert modified texel
  135. memcpy(subject, texel, N*sizeof(float));
  136. // Consider hotspot an artifact if flattening improved the sample
  137. if (fabsf(newSsd-sd) < fabsf(oldSsd-sd))
  138. artifacts.push_back(std::make_pair((int) hotspot->x, (int) hotspot->y));
  139. }
  140. for (std::vector<std::pair<int, int> >::const_iterator artifact = artifacts.begin(); artifact != artifacts.end(); ++artifact) {
  141. float *pixel = sdf(artifact->first, artifact->second);
  142. float med = median(pixel[0], pixel[1], pixel[2]);
  143. pixel[0] = med, pixel[1] = med, pixel[2] = med;
  144. }
  145. }
  146. void msdfPatchEdgeArtifacts(const BitmapRef<float, 3> &sdf, const Shape &shape, double range, const Vector2 &scale, const Vector2 &translate, bool overlapSupport) {
  147. if (overlapSupport)
  148. msdfPatchEdgeArtifactsInner<OverlappingContourCombiner>(sdf, shape, range, scale, translate);
  149. else
  150. msdfPatchEdgeArtifactsInner<SimpleContourCombiner>(sdf, shape, range, scale, translate);
  151. }
  152. void msdfPatchEdgeArtifacts(const BitmapRef<float, 4> &sdf, const Shape &shape, double range, const Vector2 &scale, const Vector2 &translate, bool overlapSupport) {
  153. if (overlapSupport)
  154. msdfPatchEdgeArtifactsInner<OverlappingContourCombiner>(sdf, shape, range, scale, translate);
  155. else
  156. msdfPatchEdgeArtifactsInner<SimpleContourCombiner>(sdf, shape, range, scale, translate);
  157. }
  158. }