msdfgen.cpp 12 KB

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