msdfgen.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  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 BitmapRef<float, 1> BitmapRefType;
  12. inline static void convert(float *pixels, double distance, double range) {
  13. *pixels = float(distance/range+.5);
  14. }
  15. };
  16. template <>
  17. class DistancePixelConversion<MultiDistance> {
  18. public:
  19. typedef BitmapRef<float, 3> BitmapRefType;
  20. inline static void convert(float *pixels, const MultiDistance &distance, double range) {
  21. pixels[0] = float(distance.r/range+.5);
  22. pixels[1] = float(distance.g/range+.5);
  23. pixels[2] = float(distance.b/range+.5);
  24. }
  25. };
  26. template <>
  27. class DistancePixelConversion<MultiAndTrueDistance> {
  28. public:
  29. typedef BitmapRef<float, 4> BitmapRefType;
  30. inline static void convert(float *pixels, const MultiAndTrueDistance &distance, double range) {
  31. pixels[0] = float(distance.r/range+.5);
  32. pixels[1] = float(distance.g/range+.5);
  33. pixels[2] = float(distance.b/range+.5);
  34. pixels[3] = float(distance.a/range+.5);
  35. }
  36. };
  37. template <class ContourCombiner>
  38. void generateDistanceField(const typename DistancePixelConversion<typename ContourCombiner::DistanceType>::BitmapRefType &output, const Shape &shape, double range, const Vector2 &scale, const Vector2 &translate) {
  39. #ifdef MSDFGEN_USE_OPENMP
  40. #pragma omp parallel
  41. #endif
  42. {
  43. ContourCombiner contourCombiner(shape);
  44. Point2 p;
  45. #ifdef MSDFGEN_USE_OPENMP
  46. #pragma omp for
  47. #endif
  48. for (int y = 0; y < output.height; ++y) {
  49. int row = shape.inverseYAxis ? output.height-y-1 : y;
  50. p.y = (y+.5)/scale.y-translate.y;
  51. for (int x = 0; x < output.width; ++x) {
  52. p.x = (x+.5)/scale.x-translate.x;
  53. contourCombiner.reset(p);
  54. for (std::vector<Contour>::const_iterator contour = shape.contours.begin(); contour != shape.contours.end(); ++contour) {
  55. if (!contour->edges.empty()) {
  56. typename ContourCombiner::EdgeSelectorType edgeSelector(p);
  57. const EdgeSegment *prevEdge = contour->edges.size() >= 2 ? *(contour->edges.end()-2) : *contour->edges.begin();
  58. const EdgeSegment *curEdge = contour->edges.back();
  59. for (std::vector<EdgeHolder>::const_iterator edge = contour->edges.begin(); edge != contour->edges.end(); ++edge) {
  60. const EdgeSegment *nextEdge = *edge;
  61. edgeSelector.addEdge(prevEdge, curEdge, nextEdge);
  62. prevEdge = curEdge;
  63. curEdge = nextEdge;
  64. }
  65. contourCombiner.setContourEdgeSelection(int(contour-shape.contours.begin()), edgeSelector);
  66. }
  67. }
  68. typename ContourCombiner::DistanceType distance = contourCombiner.distance();
  69. DistancePixelConversion<typename ContourCombiner::DistanceType>::convert(output(x, row), distance, range);
  70. }
  71. }
  72. }
  73. }
  74. void generateSDF(const BitmapRef<float, 1> &output, const Shape &shape, double range, const Vector2 &scale, const Vector2 &translate, bool overlapSupport) {
  75. if (overlapSupport)
  76. generateDistanceField<OverlappingContourCombiner<TrueDistanceSelector> >(output, shape, range, scale, translate);
  77. else
  78. generateDistanceField<SimpleContourCombiner<TrueDistanceSelector> >(output, shape, range, scale, translate);
  79. }
  80. void generatePseudoSDF(const BitmapRef<float, 1> &output, const Shape &shape, double range, const Vector2 &scale, const Vector2 &translate, bool overlapSupport) {
  81. if (overlapSupport)
  82. generateDistanceField<OverlappingContourCombiner<PseudoDistanceSelector> >(output, shape, range, scale, translate);
  83. else
  84. generateDistanceField<SimpleContourCombiner<PseudoDistanceSelector> >(output, shape, range, scale, translate);
  85. }
  86. void generateMSDF(const BitmapRef<float, 3> &output, const Shape &shape, double range, const Vector2 &scale, const Vector2 &translate, double edgeThreshold, bool overlapSupport) {
  87. if (overlapSupport)
  88. generateDistanceField<OverlappingContourCombiner<MultiDistanceSelector> >(output, shape, range, scale, translate);
  89. else
  90. generateDistanceField<SimpleContourCombiner<MultiDistanceSelector> >(output, shape, range, scale, translate);
  91. if (edgeThreshold > 0)
  92. msdfErrorCorrection(output, edgeThreshold/(scale*range));
  93. }
  94. void generateMTSDF(const BitmapRef<float, 4> &output, const Shape &shape, double range, const Vector2 &scale, const Vector2 &translate, double edgeThreshold, bool overlapSupport) {
  95. if (overlapSupport)
  96. generateDistanceField<OverlappingContourCombiner<MultiAndTrueDistanceSelector> >(output, shape, range, scale, translate);
  97. else
  98. generateDistanceField<SimpleContourCombiner<MultiAndTrueDistanceSelector> >(output, shape, range, scale, translate);
  99. if (edgeThreshold > 0)
  100. msdfErrorCorrection(output, edgeThreshold/(scale*range));
  101. }
  102. inline static bool detectClash(const float *a, const float *b, double threshold) {
  103. // Sort channels so that pairs (a0, b0), (a1, b1), (a2, b2) go from biggest to smallest absolute difference
  104. float a0 = a[0], a1 = a[1], a2 = a[2];
  105. float b0 = b[0], b1 = b[1], b2 = b[2];
  106. float tmp;
  107. if (fabsf(b0-a0) < fabsf(b1-a1)) {
  108. tmp = a0, a0 = a1, a1 = tmp;
  109. tmp = b0, b0 = b1, b1 = tmp;
  110. }
  111. if (fabsf(b1-a1) < fabsf(b2-a2)) {
  112. tmp = a1, a1 = a2, a2 = tmp;
  113. tmp = b1, b1 = b2, b2 = tmp;
  114. if (fabsf(b0-a0) < fabsf(b1-a1)) {
  115. tmp = a0, a0 = a1, a1 = tmp;
  116. tmp = b0, b0 = b1, b1 = tmp;
  117. }
  118. }
  119. return (fabsf(b1-a1) >= threshold) &&
  120. !(b0 == b1 && b0 == b2) && // Ignore if other pixel has been equalized
  121. fabsf(a2-.5f) >= fabsf(b2-.5f); // Out of the pair, only flag the pixel farther from a shape edge
  122. }
  123. template <int N>
  124. void msdfErrorCorrectionInner(const BitmapRef<float, N> &output, const Vector2 &threshold) {
  125. std::vector<std::pair<int, int> > clashes;
  126. int w = output.width, h = output.height;
  127. for (int y = 0; y < h; ++y)
  128. for (int x = 0; x < w; ++x) {
  129. if (
  130. (x > 0 && detectClash(output(x, y), output(x-1, y), threshold.x)) ||
  131. (x < w-1 && detectClash(output(x, y), output(x+1, y), threshold.x)) ||
  132. (y > 0 && detectClash(output(x, y), output(x, y-1), threshold.y)) ||
  133. (y < h-1 && detectClash(output(x, y), output(x, y+1), 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. float *pixel = output(clash->first, clash->second);
  139. float med = median(pixel[0], pixel[1], pixel[2]);
  140. pixel[0] = med, pixel[1] = med, pixel[2] = med;
  141. }
  142. #ifndef MSDFGEN_NO_DIAGONAL_CLASH_DETECTION
  143. clashes.clear();
  144. for (int y = 0; y < h; ++y)
  145. for (int x = 0; x < w; ++x) {
  146. if (
  147. (x > 0 && y > 0 && detectClash(output(x, y), output(x-1, y-1), threshold.x+threshold.y)) ||
  148. (x < w-1 && y > 0 && detectClash(output(x, y), output(x+1, y-1), threshold.x+threshold.y)) ||
  149. (x > 0 && y < h-1 && detectClash(output(x, y), output(x-1, y+1), threshold.x+threshold.y)) ||
  150. (x < w-1 && y < h-1 && detectClash(output(x, y), output(x+1, y+1), threshold.x+threshold.y))
  151. )
  152. clashes.push_back(std::make_pair(x, y));
  153. }
  154. for (std::vector<std::pair<int, int> >::const_iterator clash = clashes.begin(); clash != clashes.end(); ++clash) {
  155. float *pixel = output(clash->first, clash->second);
  156. float med = median(pixel[0], pixel[1], pixel[2]);
  157. pixel[0] = med, pixel[1] = med, pixel[2] = med;
  158. }
  159. #endif
  160. }
  161. void msdfErrorCorrection(const BitmapRef<float, 3> &output, const Vector2 &threshold) {
  162. msdfErrorCorrectionInner(output, threshold);
  163. }
  164. void msdfErrorCorrection(const BitmapRef<float, 4> &output, const Vector2 &threshold) {
  165. msdfErrorCorrectionInner(output, threshold);
  166. }
  167. // Legacy version
  168. void generateSDF_legacy(const BitmapRef<float, 1> &output, const Shape &shape, double range, const Vector2 &scale, const Vector2 &translate) {
  169. #ifdef MSDFGEN_USE_OPENMP
  170. #pragma omp parallel for
  171. #endif
  172. for (int y = 0; y < output.height; ++y) {
  173. int row = shape.inverseYAxis ? output.height-y-1 : y;
  174. for (int x = 0; x < output.width; ++x) {
  175. double dummy;
  176. Point2 p = Vector2(x+.5, y+.5)/scale-translate;
  177. SignedDistance minDistance;
  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. SignedDistance distance = (*edge)->signedDistance(p, dummy);
  181. if (distance < minDistance)
  182. minDistance = distance;
  183. }
  184. *output(x, row) = float(minDistance.distance/range+.5);
  185. }
  186. }
  187. }
  188. void generatePseudoSDF_legacy(const BitmapRef<float, 1> &output, const Shape &shape, double range, const Vector2 &scale, const Vector2 &translate) {
  189. #ifdef MSDFGEN_USE_OPENMP
  190. #pragma omp parallel for
  191. #endif
  192. for (int y = 0; y < output.height; ++y) {
  193. int row = shape.inverseYAxis ? output.height-y-1 : y;
  194. for (int x = 0; x < output.width; ++x) {
  195. Point2 p = Vector2(x+.5, y+.5)/scale-translate;
  196. SignedDistance minDistance;
  197. const EdgeHolder *nearEdge = NULL;
  198. double nearParam = 0;
  199. for (std::vector<Contour>::const_iterator contour = shape.contours.begin(); contour != shape.contours.end(); ++contour)
  200. for (std::vector<EdgeHolder>::const_iterator edge = contour->edges.begin(); edge != contour->edges.end(); ++edge) {
  201. double param;
  202. SignedDistance distance = (*edge)->signedDistance(p, param);
  203. if (distance < minDistance) {
  204. minDistance = distance;
  205. nearEdge = &*edge;
  206. nearParam = param;
  207. }
  208. }
  209. if (nearEdge)
  210. (*nearEdge)->distanceToPseudoDistance(minDistance, p, nearParam);
  211. *output(x, row) = float(minDistance.distance/range+.5);
  212. }
  213. }
  214. }
  215. void generateMSDF_legacy(const BitmapRef<float, 3> &output, const Shape &shape, double range, const Vector2 &scale, const Vector2 &translate, double edgeThreshold) {
  216. #ifdef MSDFGEN_USE_OPENMP
  217. #pragma omp parallel for
  218. #endif
  219. for (int y = 0; y < output.height; ++y) {
  220. int row = shape.inverseYAxis ? output.height-y-1 : y;
  221. for (int x = 0; x < output.width; ++x) {
  222. Point2 p = Vector2(x+.5, y+.5)/scale-translate;
  223. struct {
  224. SignedDistance minDistance;
  225. const EdgeHolder *nearEdge;
  226. double nearParam;
  227. } r, g, b;
  228. r.nearEdge = g.nearEdge = b.nearEdge = NULL;
  229. r.nearParam = g.nearParam = b.nearParam = 0;
  230. for (std::vector<Contour>::const_iterator contour = shape.contours.begin(); contour != shape.contours.end(); ++contour)
  231. for (std::vector<EdgeHolder>::const_iterator edge = contour->edges.begin(); edge != contour->edges.end(); ++edge) {
  232. double param;
  233. SignedDistance distance = (*edge)->signedDistance(p, param);
  234. if ((*edge)->color&RED && distance < r.minDistance) {
  235. r.minDistance = distance;
  236. r.nearEdge = &*edge;
  237. r.nearParam = param;
  238. }
  239. if ((*edge)->color&GREEN && distance < g.minDistance) {
  240. g.minDistance = distance;
  241. g.nearEdge = &*edge;
  242. g.nearParam = param;
  243. }
  244. if ((*edge)->color&BLUE && distance < b.minDistance) {
  245. b.minDistance = distance;
  246. b.nearEdge = &*edge;
  247. b.nearParam = param;
  248. }
  249. }
  250. if (r.nearEdge)
  251. (*r.nearEdge)->distanceToPseudoDistance(r.minDistance, p, r.nearParam);
  252. if (g.nearEdge)
  253. (*g.nearEdge)->distanceToPseudoDistance(g.minDistance, p, g.nearParam);
  254. if (b.nearEdge)
  255. (*b.nearEdge)->distanceToPseudoDistance(b.minDistance, p, b.nearParam);
  256. output(x, row)[0] = float(r.minDistance.distance/range+.5);
  257. output(x, row)[1] = float(g.minDistance.distance/range+.5);
  258. output(x, row)[2] = float(b.minDistance.distance/range+.5);
  259. }
  260. }
  261. if (edgeThreshold > 0)
  262. msdfErrorCorrection(output, edgeThreshold/(scale*range));
  263. }
  264. void generateMTSDF_legacy(const BitmapRef<float, 4> &output, const Shape &shape, double range, const Vector2 &scale, const Vector2 &translate, double edgeThreshold) {
  265. #ifdef MSDFGEN_USE_OPENMP
  266. #pragma omp parallel for
  267. #endif
  268. for (int y = 0; y < output.height; ++y) {
  269. int row = shape.inverseYAxis ? output.height-y-1 : y;
  270. for (int x = 0; x < output.width; ++x) {
  271. Point2 p = Vector2(x+.5, y+.5)/scale-translate;
  272. SignedDistance minDistance;
  273. struct {
  274. SignedDistance minDistance;
  275. const EdgeHolder *nearEdge;
  276. double nearParam;
  277. } r, g, b;
  278. r.nearEdge = g.nearEdge = b.nearEdge = NULL;
  279. r.nearParam = g.nearParam = b.nearParam = 0;
  280. for (std::vector<Contour>::const_iterator contour = shape.contours.begin(); contour != shape.contours.end(); ++contour)
  281. for (std::vector<EdgeHolder>::const_iterator edge = contour->edges.begin(); edge != contour->edges.end(); ++edge) {
  282. double param;
  283. SignedDistance distance = (*edge)->signedDistance(p, param);
  284. if (distance < minDistance)
  285. minDistance = distance;
  286. if ((*edge)->color&RED && distance < r.minDistance) {
  287. r.minDistance = distance;
  288. r.nearEdge = &*edge;
  289. r.nearParam = param;
  290. }
  291. if ((*edge)->color&GREEN && distance < g.minDistance) {
  292. g.minDistance = distance;
  293. g.nearEdge = &*edge;
  294. g.nearParam = param;
  295. }
  296. if ((*edge)->color&BLUE && distance < b.minDistance) {
  297. b.minDistance = distance;
  298. b.nearEdge = &*edge;
  299. b.nearParam = param;
  300. }
  301. }
  302. if (r.nearEdge)
  303. (*r.nearEdge)->distanceToPseudoDistance(r.minDistance, p, r.nearParam);
  304. if (g.nearEdge)
  305. (*g.nearEdge)->distanceToPseudoDistance(g.minDistance, p, g.nearParam);
  306. if (b.nearEdge)
  307. (*b.nearEdge)->distanceToPseudoDistance(b.minDistance, p, b.nearParam);
  308. output(x, row)[0] = float(r.minDistance.distance/range+.5);
  309. output(x, row)[1] = float(g.minDistance.distance/range+.5);
  310. output(x, row)[2] = float(b.minDistance.distance/range+.5);
  311. output(x, row)[3] = float(minDistance.distance/range+.5);
  312. }
  313. }
  314. if (edgeThreshold > 0)
  315. msdfErrorCorrection(output, edgeThreshold/(scale*range));
  316. }
  317. }