msdfgen.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. #include "../msdfgen.h"
  2. #include "arithmetics.hpp"
  3. namespace msdfgen {
  4. void generateSDF(Bitmap<float> &output, const Shape &shape, double range, const Vector2 &scale, const Vector2 &translate) {
  5. int w = output.width(), h = output.height();
  6. #ifdef MSDFGEN_USE_OPENMP
  7. #pragma omp parallel for
  8. #endif
  9. for (int y = 0; y < h; ++y) {
  10. int row = shape.inverseYAxis ? h-y-1 : y;
  11. for (int x = 0; x < w; ++x) {
  12. double dummy;
  13. Point2 p = Vector2(x+.5, y+.5)/scale-translate;
  14. SignedDistance minDistance;
  15. for (std::vector<Contour>::const_iterator contour = shape.contours.begin(); contour != shape.contours.end(); ++contour)
  16. for (std::vector<EdgeHolder>::const_iterator edge = contour->edges.begin(); edge != contour->edges.end(); ++edge) {
  17. SignedDistance distance = (*edge)->signedDistance(p, dummy);
  18. if (distance < minDistance)
  19. minDistance = distance;
  20. }
  21. output(x, row) = float(minDistance.distance/range+.5);
  22. }
  23. }
  24. }
  25. void generatePseudoSDF(Bitmap<float> &output, const Shape &shape, double range, const Vector2 &scale, const Vector2 &translate) {
  26. int w = output.width(), h = output.height();
  27. #ifdef MSDFGEN_USE_OPENMP
  28. #pragma omp parallel for
  29. #endif
  30. for (int y = 0; y < h; ++y) {
  31. int row = shape.inverseYAxis ? h-y-1 : y;
  32. for (int x = 0; x < w; ++x) {
  33. Point2 p = Vector2(x+.5, y+.5)/scale-translate;
  34. SignedDistance minDistance;
  35. const EdgeHolder *nearEdge = NULL;
  36. double nearParam = 0;
  37. for (std::vector<Contour>::const_iterator contour = shape.contours.begin(); contour != shape.contours.end(); ++contour)
  38. for (std::vector<EdgeHolder>::const_iterator edge = contour->edges.begin(); edge != contour->edges.end(); ++edge) {
  39. double param;
  40. SignedDistance distance = (*edge)->signedDistance(p, param);
  41. if (distance < minDistance) {
  42. minDistance = distance;
  43. nearEdge = &*edge;
  44. nearParam = param;
  45. }
  46. }
  47. if (nearEdge)
  48. (*nearEdge)->distanceToPseudoDistance(minDistance, p, nearParam);
  49. output(x, row) = float(minDistance.distance/range+.5);
  50. }
  51. }
  52. }
  53. static inline bool pixelClash(const FloatRGB &a, const FloatRGB &b, double threshold) {
  54. // Only consider pair where both are on the inside or both are on the outside
  55. bool aIn = (a.r > .5f)+(a.g > .5f)+(a.b > .5f) >= 2;
  56. bool bIn = (b.r > .5f)+(b.g > .5f)+(b.b > .5f) >= 2;
  57. if (aIn != bIn) return false;
  58. // If the change is 0 <-> 1 or 2 <-> 3 channels and not 1 <-> 1 or 2 <-> 2, it is not a clash
  59. if ((a.r > .5f && a.g > .5f && a.b > .5f) || (a.r < .5f && a.g < .5f && a.b < .5f)
  60. || (b.r > .5f && b.g > .5f && b.b > .5f) || (b.r < .5f && b.g < .5f && b.b < .5f))
  61. return false;
  62. // Find which color is which: _a, _b = the changing channels, _c = the remaining one
  63. float aa, ab, ba, bb, ac, bc;
  64. if ((a.r > .5f) != (b.r > .5f) && (a.r < .5f) != (b.r < .5f)) {
  65. aa = a.r, ba = b.r;
  66. if ((a.g > .5f) != (b.g > .5f) && (a.g < .5f) != (b.g < .5f)) {
  67. ab = a.g, bb = b.g;
  68. ac = a.b, bc = b.b;
  69. } else if ((a.b > .5f) != (b.b > .5f) && (a.b < .5f) != (b.b < .5f)) {
  70. ab = a.b, bb = b.b;
  71. ac = a.g, bc = b.g;
  72. } else
  73. return false; // this should never happen
  74. } else if ((a.g > .5f) != (b.g > .5f) && (a.g < .5f) != (b.g < .5f)
  75. && (a.b > .5f) != (b.b > .5f) && (a.b < .5f) != (b.b < .5f)) {
  76. aa = a.g, ba = b.g;
  77. ab = a.b, bb = b.b;
  78. ac = a.r, bc = b.r;
  79. } else
  80. return false;
  81. // Find if the channels are in fact discontinuous
  82. return (fabsf(aa-ba) >= threshold)
  83. && (fabsf(ab-bb) >= threshold)
  84. && fabsf(ac-.5f) >= fabsf(bc-.5f); // Out of the pair, only flag the pixel farther from a shape edge
  85. }
  86. void msdfErrorCorrection(Bitmap<FloatRGB> &output, const Vector2 &threshold) {
  87. std::vector<std::pair<int, int> > clashes;
  88. int w = output.width(), h = output.height();
  89. for (int y = 0; y < h; ++y)
  90. for (int x = 0; x < w; ++x) {
  91. if ((x > 0 && pixelClash(output(x, y), output(x-1, y), threshold.x))
  92. || (x < w-1 && pixelClash(output(x, y), output(x+1, y), threshold.x))
  93. || (y > 0 && pixelClash(output(x, y), output(x, y-1), threshold.y))
  94. || (y < h-1 && pixelClash(output(x, y), output(x, y+1), threshold.y)))
  95. clashes.push_back(std::make_pair(x, y));
  96. }
  97. for (std::vector<std::pair<int, int> >::const_iterator clash = clashes.begin(); clash != clashes.end(); ++clash) {
  98. FloatRGB &pixel = output(clash->first, clash->second);
  99. float med = median(pixel.r, pixel.g, pixel.b);
  100. pixel.r = med, pixel.g = med, pixel.b = med;
  101. }
  102. }
  103. void generateMSDF(Bitmap<FloatRGB> &output, const Shape &shape, double range, const Vector2 &scale, const Vector2 &translate, double edgeThreshold) {
  104. int w = output.width(), h = output.height();
  105. #ifdef MSDFGEN_USE_OPENMP
  106. #pragma omp parallel for
  107. #endif
  108. for (int y = 0; y < h; ++y) {
  109. int row = shape.inverseYAxis ? h-y-1 : y;
  110. for (int x = 0; x < w; ++x) {
  111. Point2 p = Vector2(x+.5, y+.5)/scale-translate;
  112. struct {
  113. SignedDistance minDistance;
  114. const EdgeHolder *nearEdge;
  115. double nearParam;
  116. } r, g, b;
  117. r.nearEdge = g.nearEdge = b.nearEdge = NULL;
  118. r.nearParam = g.nearParam = b.nearParam = 0;
  119. for (std::vector<Contour>::const_iterator contour = shape.contours.begin(); contour != shape.contours.end(); ++contour)
  120. for (std::vector<EdgeHolder>::const_iterator edge = contour->edges.begin(); edge != contour->edges.end(); ++edge) {
  121. double param;
  122. SignedDistance distance = (*edge)->signedDistance(p, param);
  123. if ((*edge)->color&RED && distance < r.minDistance) {
  124. r.minDistance = distance;
  125. r.nearEdge = &*edge;
  126. r.nearParam = param;
  127. }
  128. if ((*edge)->color&GREEN && distance < g.minDistance) {
  129. g.minDistance = distance;
  130. g.nearEdge = &*edge;
  131. g.nearParam = param;
  132. }
  133. if ((*edge)->color&BLUE && distance < b.minDistance) {
  134. b.minDistance = distance;
  135. b.nearEdge = &*edge;
  136. b.nearParam = param;
  137. }
  138. }
  139. if (r.nearEdge)
  140. (*r.nearEdge)->distanceToPseudoDistance(r.minDistance, p, r.nearParam);
  141. if (g.nearEdge)
  142. (*g.nearEdge)->distanceToPseudoDistance(g.minDistance, p, g.nearParam);
  143. if (b.nearEdge)
  144. (*b.nearEdge)->distanceToPseudoDistance(b.minDistance, p, b.nearParam);
  145. output(x, row).r = float(r.minDistance.distance/range+.5);
  146. output(x, row).g = float(g.minDistance.distance/range+.5);
  147. output(x, row).b = float(b.minDistance.distance/range+.5);
  148. }
  149. }
  150. if (edgeThreshold > 0)
  151. msdfErrorCorrection(output, edgeThreshold/(scale*range));
  152. }
  153. }