msdfgen.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. #include "../msdfgen.h"
  2. #include "edge-selectors.h"
  3. #include "contour-combiners.h"
  4. namespace msdfgen {
  5. template <typename DistanceType>
  6. class SquaredDistancePixelConversion;
  7. template <>
  8. class SquaredDistancePixelConversion<double> {
  9. public:
  10. typedef float PixelType;
  11. inline static PixelType convert(double sqDistance, double range) {
  12. return PixelType(sign(sqDistance)*sqrt(fabs(sqDistance))/range+.5);
  13. }
  14. };
  15. template <>
  16. class SquaredDistancePixelConversion<MultiDistance> {
  17. public:
  18. typedef FloatRGB PixelType;
  19. inline static PixelType convert(const MultiDistance &sqDistance, double range) {
  20. PixelType pixel;
  21. pixel.r = float(sign(sqDistance.r)*sqrt(fabs(sqDistance.r))/range+.5);
  22. pixel.g = float(sign(sqDistance.g)*sqrt(fabs(sqDistance.g))/range+.5);
  23. pixel.b = float(sign(sqDistance.b)*sqrt(fabs(sqDistance.b))/range+.5);
  24. return pixel;
  25. }
  26. };
  27. template <class ContourCombiner>
  28. void generateDistanceField(Bitmap<typename SquaredDistancePixelConversion<typename ContourCombiner::DistanceType>::PixelType> &output, const Shape &shape, double range, const Vector2 &scale, const Vector2 &translate) {
  29. int w = output.width(), h = output.height();
  30. #ifdef MSDFGEN_USE_OPENMP
  31. #pragma omp parallel
  32. #endif
  33. {
  34. ContourCombiner contourCombiner(shape);
  35. Point2 p;
  36. #ifdef MSDFGEN_USE_OPENMP
  37. #pragma omp for
  38. #endif
  39. for (int y = 0; y < h; ++y) {
  40. int row = shape.inverseYAxis ? h-y-1 : y;
  41. p.y = (y+.5)/scale.y-translate.y;
  42. for (int x = 0; x < w; ++x) {
  43. p.x = (x+.5)/scale.x-translate.x;
  44. contourCombiner.reset(p);
  45. for (std::vector<Contour>::const_iterator contour = shape.contours.begin(); contour != shape.contours.end(); ++contour) {
  46. if (!contour->edges.empty()) {
  47. ContourCombiner::EdgeSelectorType edgeSelector(p);
  48. const EdgeSegment *prevEdge = contour->edges.size() >= 2 ? *(contour->edges.end()-2) : *contour->edges.begin();
  49. const EdgeSegment *curEdge = contour->edges.back();
  50. for (std::vector<EdgeHolder>::const_iterator edge = contour->edges.begin(); edge != contour->edges.end(); ++edge) {
  51. const EdgeSegment *nextEdge = *edge;
  52. edgeSelector.addEdge(prevEdge, curEdge, nextEdge);
  53. prevEdge = curEdge;
  54. curEdge = nextEdge;
  55. }
  56. contourCombiner.setContourEdgeSelection(int(contour-shape.contours.begin()), edgeSelector);
  57. }
  58. }
  59. ContourCombiner::DistanceType sqDistance = contourCombiner.squaredDistance();
  60. output(x, row) = SquaredDistancePixelConversion<ContourCombiner::DistanceType>::convert(sqDistance, range);
  61. }
  62. }
  63. }
  64. }
  65. void generateSDF(Bitmap<float> &output, const Shape &shape, double range, const Vector2 &scale, const Vector2 &translate, bool overlapSupport) {
  66. if (overlapSupport)
  67. generateDistanceField<OverlappingContourCombiner<TrueDistanceSelector> >(output, shape, range, scale, translate);
  68. else
  69. generateDistanceField<SimpleContourCombiner<TrueDistanceSelector> >(output, shape, range, scale, translate);
  70. }
  71. void generatePseudoSDF(Bitmap<float> &output, const Shape &shape, double range, const Vector2 &scale, const Vector2 &translate, bool overlapSupport) {
  72. if (overlapSupport)
  73. generateDistanceField<OverlappingContourCombiner<PseudoDistanceSelector> >(output, shape, range, scale, translate);
  74. else
  75. generateDistanceField<SimpleContourCombiner<PseudoDistanceSelector> >(output, shape, range, scale, translate);
  76. }
  77. void generateMSDF(Bitmap<FloatRGB> &output, const Shape &shape, double range, const Vector2 &scale, const Vector2 &translate, double edgeThreshold, bool overlapSupport) {
  78. if (overlapSupport)
  79. generateDistanceField<OverlappingContourCombiner<MultiDistanceSelector> >(output, shape, range, scale, translate);
  80. else
  81. generateDistanceField<SimpleContourCombiner<MultiDistanceSelector> >(output, shape, range, scale, translate);
  82. if (edgeThreshold > 0)
  83. msdfErrorCorrection(output, edgeThreshold/(scale*range));
  84. }
  85. inline static bool detectClash(const FloatRGB &a, const FloatRGB &b, double threshold) {
  86. // Sort channels so that pairs (a0, b0), (a1, b1), (a2, b2) go from biggest to smallest absolute difference
  87. float a0 = a.r, a1 = a.g, a2 = a.b;
  88. float b0 = b.r, b1 = b.g, b2 = b.b;
  89. float tmp;
  90. if (fabsf(b0-a0) < fabsf(b1-a1)) {
  91. tmp = a0, a0 = a1, a1 = tmp;
  92. tmp = b0, b0 = b1, b1 = tmp;
  93. }
  94. if (fabsf(b1-a1) < fabsf(b2-a2)) {
  95. tmp = a1, a1 = a2, a2 = tmp;
  96. tmp = b1, b1 = b2, b2 = tmp;
  97. if (fabsf(b0-a0) < fabsf(b1-a1)) {
  98. tmp = a0, a0 = a1, a1 = tmp;
  99. tmp = b0, b0 = b1, b1 = tmp;
  100. }
  101. }
  102. return (fabsf(b1-a1) >= threshold) &&
  103. !(b0 == b1 && b0 == b2) && // Ignore if other pixel has been equalized
  104. fabsf(a2-.5f) >= fabsf(b2-.5f); // Out of the pair, only flag the pixel farther from a shape edge
  105. }
  106. void msdfErrorCorrection(Bitmap<FloatRGB> &output, const Vector2 &threshold) {
  107. std::vector<std::pair<int, int> > clashes;
  108. int w = output.width(), h = output.height();
  109. for (int y = 0; y < h; ++y)
  110. for (int x = 0; x < w; ++x) {
  111. if (
  112. (x > 0 && detectClash(output(x, y), output(x-1, y), threshold.x)) ||
  113. (x < w-1 && detectClash(output(x, y), output(x+1, y), threshold.x)) ||
  114. (y > 0 && detectClash(output(x, y), output(x, y-1), threshold.y)) ||
  115. (y < h-1 && detectClash(output(x, y), output(x, y+1), threshold.y))
  116. )
  117. clashes.push_back(std::make_pair(x, y));
  118. }
  119. for (std::vector<std::pair<int, int> >::const_iterator clash = clashes.begin(); clash != clashes.end(); ++clash) {
  120. FloatRGB &pixel = output(clash->first, clash->second);
  121. float med = median(pixel.r, pixel.g, pixel.b);
  122. pixel.r = med, pixel.g = med, pixel.b = med;
  123. }
  124. #ifndef MSDFGEN_NO_DIAGONAL_CLASH_DETECTION
  125. clashes.clear();
  126. for (int y = 0; y < h; ++y)
  127. for (int x = 0; x < w; ++x) {
  128. if (
  129. (x > 0 && y > 0 && detectClash(output(x, y), output(x-1, y-1), threshold.x+threshold.y)) ||
  130. (x < w-1 && y > 0 && detectClash(output(x, y), output(x+1, y-1), threshold.x+threshold.y)) ||
  131. (x > 0 && y < h-1 && detectClash(output(x, y), output(x-1, y+1), threshold.x+threshold.y)) ||
  132. (x < w-1 && y < h-1 && detectClash(output(x, y), output(x+1, y+1), threshold.x+threshold.y))
  133. )
  134. clashes.push_back(std::make_pair(x, y));
  135. }
  136. for (std::vector<std::pair<int, int> >::const_iterator clash = clashes.begin(); clash != clashes.end(); ++clash) {
  137. FloatRGB &pixel = output(clash->first, clash->second);
  138. float med = median(pixel.r, pixel.g, pixel.b);
  139. pixel.r = med, pixel.g = med, pixel.b = med;
  140. }
  141. #endif
  142. }
  143. // Legacy version
  144. void generateSDF_legacy(Bitmap<float> &output, const Shape &shape, double range, const Vector2 &scale, const Vector2 &translate) {
  145. int w = output.width(), h = output.height();
  146. #ifdef MSDFGEN_USE_OPENMP
  147. #pragma omp parallel for
  148. #endif
  149. for (int y = 0; y < h; ++y) {
  150. int row = shape.inverseYAxis ? h-y-1 : y;
  151. for (int x = 0; x < w; ++x) {
  152. double dummy;
  153. Point2 p = Vector2(x+.5, y+.5)/scale-translate;
  154. SignedDistance minDistance;
  155. for (std::vector<Contour>::const_iterator contour = shape.contours.begin(); contour != shape.contours.end(); ++contour)
  156. for (std::vector<EdgeHolder>::const_iterator edge = contour->edges.begin(); edge != contour->edges.end(); ++edge) {
  157. SignedDistance distance = (*edge)->signedDistance(p, dummy);
  158. if (distance < minDistance)
  159. minDistance = distance;
  160. }
  161. output(x, row) = float(minDistance.distance()/range+.5);
  162. }
  163. }
  164. }
  165. void generatePseudoSDF_legacy(Bitmap<float> &output, const Shape &shape, double range, const Vector2 &scale, const Vector2 &translate) {
  166. int w = output.width(), h = output.height();
  167. #ifdef MSDFGEN_USE_OPENMP
  168. #pragma omp parallel for
  169. #endif
  170. for (int y = 0; y < h; ++y) {
  171. int row = shape.inverseYAxis ? h-y-1 : y;
  172. for (int x = 0; x < w; ++x) {
  173. Point2 p = Vector2(x+.5, y+.5)/scale-translate;
  174. SignedDistance minDistance;
  175. const EdgeHolder *nearEdge = NULL;
  176. double nearParam = 0;
  177. for (std::vector<Contour>::const_iterator contour = shape.contours.begin(); contour != shape.contours.end(); ++contour)
  178. for (std::vector<EdgeHolder>::const_iterator edge = contour->edges.begin(); edge != contour->edges.end(); ++edge) {
  179. double param;
  180. SignedDistance distance = (*edge)->signedDistance(p, param);
  181. if (distance < minDistance) {
  182. minDistance = distance;
  183. nearEdge = &*edge;
  184. nearParam = param;
  185. }
  186. }
  187. if (nearEdge)
  188. (*nearEdge)->distanceToPseudoDistance(minDistance, p, nearParam);
  189. output(x, row) = float(minDistance.distance()/range+.5);
  190. }
  191. }
  192. }
  193. void generateMSDF_legacy(Bitmap<FloatRGB> &output, const Shape &shape, double range, const Vector2 &scale, const Vector2 &translate, double edgeThreshold) {
  194. int w = output.width(), h = output.height();
  195. #ifdef MSDFGEN_USE_OPENMP
  196. #pragma omp parallel for
  197. #endif
  198. for (int y = 0; y < h; ++y) {
  199. int row = shape.inverseYAxis ? h-y-1 : y;
  200. for (int x = 0; x < w; ++x) {
  201. Point2 p = Vector2(x+.5, y+.5)/scale-translate;
  202. struct {
  203. SignedDistance minDistance;
  204. const EdgeHolder *nearEdge;
  205. double nearParam;
  206. } r, g, b;
  207. r.nearEdge = g.nearEdge = b.nearEdge = NULL;
  208. r.nearParam = g.nearParam = b.nearParam = 0;
  209. for (std::vector<Contour>::const_iterator contour = shape.contours.begin(); contour != shape.contours.end(); ++contour)
  210. for (std::vector<EdgeHolder>::const_iterator edge = contour->edges.begin(); edge != contour->edges.end(); ++edge) {
  211. double param;
  212. SignedDistance distance = (*edge)->signedDistance(p, param);
  213. if ((*edge)->color&RED && distance < r.minDistance) {
  214. r.minDistance = distance;
  215. r.nearEdge = &*edge;
  216. r.nearParam = param;
  217. }
  218. if ((*edge)->color&GREEN && distance < g.minDistance) {
  219. g.minDistance = distance;
  220. g.nearEdge = &*edge;
  221. g.nearParam = param;
  222. }
  223. if ((*edge)->color&BLUE && distance < b.minDistance) {
  224. b.minDistance = distance;
  225. b.nearEdge = &*edge;
  226. b.nearParam = param;
  227. }
  228. }
  229. if (r.nearEdge)
  230. (*r.nearEdge)->distanceToPseudoDistance(r.minDistance, p, r.nearParam);
  231. if (g.nearEdge)
  232. (*g.nearEdge)->distanceToPseudoDistance(g.minDistance, p, g.nearParam);
  233. if (b.nearEdge)
  234. (*b.nearEdge)->distanceToPseudoDistance(b.minDistance, p, b.nearParam);
  235. output(x, row).r = float(r.minDistance.distance()/range+.5);
  236. output(x, row).g = float(g.minDistance.distance()/range+.5);
  237. output(x, row).b = float(b.minDistance.distance()/range+.5);
  238. }
  239. }
  240. if (edgeThreshold > 0)
  241. msdfErrorCorrection(output, edgeThreshold/(scale*range));
  242. }
  243. }