2
0

msdfgen.cpp 16 KB

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