msdfgen.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. #include "../msdfgen.h"
  2. #include <vector>
  3. #include "edge-selectors.h"
  4. #include "contour-combiners.h"
  5. #include "ShapeDistanceFinder.h"
  6. #include "msdf-edge-artifact-patcher.h"
  7. namespace msdfgen {
  8. template <typename DistanceType>
  9. class DistancePixelConversion;
  10. template <>
  11. class DistancePixelConversion<double> {
  12. public:
  13. typedef BitmapRef<float, 1> BitmapRefType;
  14. inline static void convert(float *pixels, double distance, double range) {
  15. *pixels = float(distance/range+.5);
  16. }
  17. };
  18. template <>
  19. class DistancePixelConversion<MultiDistance> {
  20. public:
  21. typedef BitmapRef<float, 3> BitmapRefType;
  22. inline static void convert(float *pixels, const MultiDistance &distance, double range) {
  23. pixels[0] = float(distance.r/range+.5);
  24. pixels[1] = float(distance.g/range+.5);
  25. pixels[2] = float(distance.b/range+.5);
  26. }
  27. };
  28. template <>
  29. class DistancePixelConversion<MultiAndTrueDistance> {
  30. public:
  31. typedef BitmapRef<float, 4> BitmapRefType;
  32. inline static void convert(float *pixels, const MultiAndTrueDistance &distance, double range) {
  33. pixels[0] = float(distance.r/range+.5);
  34. pixels[1] = float(distance.g/range+.5);
  35. pixels[2] = float(distance.b/range+.5);
  36. pixels[3] = float(distance.a/range+.5);
  37. }
  38. };
  39. template <class ContourCombiner>
  40. void generateDistanceField(const typename DistancePixelConversion<typename ContourCombiner::DistanceType>::BitmapRefType &output, const Shape &shape, double range, const Vector2 &scale, const Vector2 &translate) {
  41. #ifdef MSDFGEN_USE_OPENMP
  42. #pragma omp parallel
  43. #endif
  44. {
  45. ShapeDistanceFinder<ContourCombiner> distanceFinder(shape);
  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. typename ContourCombiner::DistanceType distance = distanceFinder.distance(p);
  58. DistancePixelConversion<typename ContourCombiner::DistanceType>::convert(output(x, row), distance, range);
  59. }
  60. rightToLeft = !rightToLeft;
  61. }
  62. }
  63. }
  64. void generateSDF(const BitmapRef<float, 1> &output, const Shape &shape, double range, const Vector2 &scale, const Vector2 &translate, bool overlapSupport) {
  65. if (overlapSupport)
  66. generateDistanceField<OverlappingContourCombiner<TrueDistanceSelector> >(output, shape, range, scale, translate);
  67. else
  68. generateDistanceField<SimpleContourCombiner<TrueDistanceSelector> >(output, shape, range, scale, translate);
  69. }
  70. void generatePseudoSDF(const BitmapRef<float, 1> &output, const Shape &shape, double range, const Vector2 &scale, const Vector2 &translate, bool overlapSupport) {
  71. if (overlapSupport)
  72. generateDistanceField<OverlappingContourCombiner<PseudoDistanceSelector> >(output, shape, range, scale, translate);
  73. else
  74. generateDistanceField<SimpleContourCombiner<PseudoDistanceSelector> >(output, shape, range, scale, translate);
  75. }
  76. void generateMSDF(const BitmapRef<float, 3> &output, const Shape &shape, double range, const Vector2 &scale, const Vector2 &translate, double edgeThreshold, bool overlapSupport) {
  77. if (overlapSupport)
  78. generateDistanceField<OverlappingContourCombiner<MultiDistanceSelector> >(output, shape, range, scale, translate);
  79. else
  80. generateDistanceField<SimpleContourCombiner<MultiDistanceSelector> >(output, shape, range, scale, translate);
  81. if (edgeThreshold > 0)
  82. msdfErrorCorrection(output, edgeThreshold/(scale*range));
  83. msdfPatchEdgeArtifacts(output, shape, range, scale, translate, overlapSupport);
  84. }
  85. void generateMTSDF(const BitmapRef<float, 4> &output, const Shape &shape, double range, const Vector2 &scale, const Vector2 &translate, double edgeThreshold, bool overlapSupport) {
  86. if (overlapSupport)
  87. generateDistanceField<OverlappingContourCombiner<MultiAndTrueDistanceSelector> >(output, shape, range, scale, translate);
  88. else
  89. generateDistanceField<SimpleContourCombiner<MultiAndTrueDistanceSelector> >(output, shape, range, scale, translate);
  90. if (edgeThreshold > 0)
  91. msdfErrorCorrection(output, edgeThreshold/(scale*range));
  92. msdfPatchEdgeArtifacts(output, shape, range, scale, translate, overlapSupport);
  93. }
  94. // Legacy version
  95. void generateSDF_legacy(const BitmapRef<float, 1> &output, const Shape &shape, double range, const Vector2 &scale, const Vector2 &translate) {
  96. #ifdef MSDFGEN_USE_OPENMP
  97. #pragma omp parallel for
  98. #endif
  99. for (int y = 0; y < output.height; ++y) {
  100. int row = shape.inverseYAxis ? output.height-y-1 : y;
  101. for (int x = 0; x < output.width; ++x) {
  102. double dummy;
  103. Point2 p = Vector2(x+.5, y+.5)/scale-translate;
  104. SignedDistance minDistance;
  105. for (std::vector<Contour>::const_iterator contour = shape.contours.begin(); contour != shape.contours.end(); ++contour)
  106. for (std::vector<EdgeHolder>::const_iterator edge = contour->edges.begin(); edge != contour->edges.end(); ++edge) {
  107. SignedDistance distance = (*edge)->signedDistance(p, dummy);
  108. if (distance < minDistance)
  109. minDistance = distance;
  110. }
  111. *output(x, row) = float(minDistance.distance/range+.5);
  112. }
  113. }
  114. }
  115. void generatePseudoSDF_legacy(const BitmapRef<float, 1> &output, const Shape &shape, double range, const Vector2 &scale, const Vector2 &translate) {
  116. #ifdef MSDFGEN_USE_OPENMP
  117. #pragma omp parallel for
  118. #endif
  119. for (int y = 0; y < output.height; ++y) {
  120. int row = shape.inverseYAxis ? output.height-y-1 : y;
  121. for (int x = 0; x < output.width; ++x) {
  122. Point2 p = Vector2(x+.5, y+.5)/scale-translate;
  123. SignedDistance minDistance;
  124. const EdgeHolder *nearEdge = NULL;
  125. double nearParam = 0;
  126. for (std::vector<Contour>::const_iterator contour = shape.contours.begin(); contour != shape.contours.end(); ++contour)
  127. for (std::vector<EdgeHolder>::const_iterator edge = contour->edges.begin(); edge != contour->edges.end(); ++edge) {
  128. double param;
  129. SignedDistance distance = (*edge)->signedDistance(p, param);
  130. if (distance < minDistance) {
  131. minDistance = distance;
  132. nearEdge = &*edge;
  133. nearParam = param;
  134. }
  135. }
  136. if (nearEdge)
  137. (*nearEdge)->distanceToPseudoDistance(minDistance, p, nearParam);
  138. *output(x, row) = float(minDistance.distance/range+.5);
  139. }
  140. }
  141. }
  142. void generateMSDF_legacy(const BitmapRef<float, 3> &output, const Shape &shape, double range, const Vector2 &scale, const Vector2 &translate, double edgeThreshold) {
  143. #ifdef MSDFGEN_USE_OPENMP
  144. #pragma omp parallel for
  145. #endif
  146. for (int y = 0; y < output.height; ++y) {
  147. int row = shape.inverseYAxis ? output.height-y-1 : y;
  148. for (int x = 0; x < output.width; ++x) {
  149. Point2 p = Vector2(x+.5, y+.5)/scale-translate;
  150. struct {
  151. SignedDistance minDistance;
  152. const EdgeHolder *nearEdge;
  153. double nearParam;
  154. } r, g, b;
  155. r.nearEdge = g.nearEdge = b.nearEdge = NULL;
  156. r.nearParam = g.nearParam = b.nearParam = 0;
  157. for (std::vector<Contour>::const_iterator contour = shape.contours.begin(); contour != shape.contours.end(); ++contour)
  158. for (std::vector<EdgeHolder>::const_iterator edge = contour->edges.begin(); edge != contour->edges.end(); ++edge) {
  159. double param;
  160. SignedDistance distance = (*edge)->signedDistance(p, param);
  161. if ((*edge)->color&RED && distance < r.minDistance) {
  162. r.minDistance = distance;
  163. r.nearEdge = &*edge;
  164. r.nearParam = param;
  165. }
  166. if ((*edge)->color&GREEN && distance < g.minDistance) {
  167. g.minDistance = distance;
  168. g.nearEdge = &*edge;
  169. g.nearParam = param;
  170. }
  171. if ((*edge)->color&BLUE && distance < b.minDistance) {
  172. b.minDistance = distance;
  173. b.nearEdge = &*edge;
  174. b.nearParam = param;
  175. }
  176. }
  177. if (r.nearEdge)
  178. (*r.nearEdge)->distanceToPseudoDistance(r.minDistance, p, r.nearParam);
  179. if (g.nearEdge)
  180. (*g.nearEdge)->distanceToPseudoDistance(g.minDistance, p, g.nearParam);
  181. if (b.nearEdge)
  182. (*b.nearEdge)->distanceToPseudoDistance(b.minDistance, p, b.nearParam);
  183. output(x, row)[0] = float(r.minDistance.distance/range+.5);
  184. output(x, row)[1] = float(g.minDistance.distance/range+.5);
  185. output(x, row)[2] = float(b.minDistance.distance/range+.5);
  186. }
  187. }
  188. if (edgeThreshold > 0)
  189. msdfErrorCorrection(output, edgeThreshold/(scale*range));
  190. }
  191. void generateMTSDF_legacy(const BitmapRef<float, 4> &output, const Shape &shape, double range, const Vector2 &scale, const Vector2 &translate, double edgeThreshold) {
  192. #ifdef MSDFGEN_USE_OPENMP
  193. #pragma omp parallel for
  194. #endif
  195. for (int y = 0; y < output.height; ++y) {
  196. int row = shape.inverseYAxis ? output.height-y-1 : y;
  197. for (int x = 0; x < output.width; ++x) {
  198. Point2 p = Vector2(x+.5, y+.5)/scale-translate;
  199. SignedDistance minDistance;
  200. struct {
  201. SignedDistance minDistance;
  202. const EdgeHolder *nearEdge;
  203. double nearParam;
  204. } r, g, b;
  205. r.nearEdge = g.nearEdge = b.nearEdge = NULL;
  206. r.nearParam = g.nearParam = b.nearParam = 0;
  207. for (std::vector<Contour>::const_iterator contour = shape.contours.begin(); contour != shape.contours.end(); ++contour)
  208. for (std::vector<EdgeHolder>::const_iterator edge = contour->edges.begin(); edge != contour->edges.end(); ++edge) {
  209. double param;
  210. SignedDistance distance = (*edge)->signedDistance(p, param);
  211. if (distance < minDistance)
  212. minDistance = distance;
  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)[0] = float(r.minDistance.distance/range+.5);
  236. output(x, row)[1] = float(g.minDistance.distance/range+.5);
  237. output(x, row)[2] = float(b.minDistance.distance/range+.5);
  238. output(x, row)[3] = float(minDistance.distance/range+.5);
  239. }
  240. }
  241. if (edgeThreshold > 0)
  242. msdfErrorCorrection(output, edgeThreshold/(scale*range));
  243. }
  244. }