rasterization.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. #include "rasterization.h"
  2. #include <vector>
  3. #include "arithmetics.hpp"
  4. #include "Scanline.h"
  5. namespace msdfgen {
  6. static bool interpretFillRule(int intersections, FillRule fillRule) {
  7. switch (fillRule) {
  8. case FILL_NONZERO:
  9. return intersections != 0;
  10. case FILL_ODD:
  11. return intersections&1;
  12. case FILL_POSITIVE:
  13. return intersections > 0;
  14. case FILL_NEGATIVE:
  15. return intersections < 0;
  16. }
  17. return false;
  18. }
  19. void rasterize(const BitmapRef<float, 1> &output, const Shape &shape, const Vector2 &scale, const Vector2 &translate, FillRule fillRule) {
  20. Point2 p;
  21. Scanline scanline;
  22. for (int y = 0; y < output.height; ++y) {
  23. int row = shape.inverseYAxis ? output.height-y-1 : y;
  24. p.y = (y+.5)/scale.y-translate.y;
  25. shape.scanline(scanline, p.y);
  26. for (int x = 0; x < output.width; ++x) {
  27. p.x = (x+.5)/scale.x-translate.x;
  28. int intersections = scanline.sumIntersections(p.x);
  29. bool fill = interpretFillRule(intersections, fillRule);
  30. *output(x, row) = (float) fill;
  31. }
  32. }
  33. }
  34. void distanceSignCorrection(const BitmapRef<float, 1> &sdf, const Shape &shape, const Vector2 &scale, const Vector2 &translate, FillRule fillRule) {
  35. Point2 p;
  36. Scanline scanline;
  37. for (int y = 0; y < sdf.height; ++y) {
  38. int row = shape.inverseYAxis ? sdf.height-y-1 : y;
  39. p.y = (y+.5)/scale.y-translate.y;
  40. shape.scanline(scanline, p.y);
  41. for (int x = 0; x < sdf.width; ++x) {
  42. p.x = (x+.5)/scale.x-translate.x;
  43. int intersections = scanline.sumIntersections(p.x);
  44. bool fill = interpretFillRule(intersections, fillRule);
  45. float &sd = *sdf(x, row);
  46. if ((sd > .5f) != fill)
  47. sd = 1.f-sd;
  48. }
  49. }
  50. }
  51. void distanceSignCorrection(const BitmapRef<float, 3> &sdf, const Shape &shape, const Vector2 &scale, const Vector2 &translate, FillRule fillRule) {
  52. int w = sdf.width, h = sdf.height;
  53. if (!(w*h))
  54. return;
  55. Point2 p;
  56. Scanline scanline;
  57. bool ambiguous = false;
  58. std::vector<char> matchMap;
  59. matchMap.resize(w*h);
  60. char *match = &matchMap[0];
  61. for (int y = 0; y < h; ++y) {
  62. int row = shape.inverseYAxis ? h-y-1 : y;
  63. p.y = (y+.5)/scale.y-translate.y;
  64. shape.scanline(scanline, p.y);
  65. for (int x = 0; x < w; ++x) {
  66. p.x = (x+.5)/scale.x-translate.x;
  67. int intersections = scanline.sumIntersections(p.x);
  68. bool fill = interpretFillRule(intersections, fillRule);
  69. float *msd = sdf(x, row);
  70. float sd = median(msd[0], msd[1], msd[2]);
  71. if (sd == .5f)
  72. ambiguous = true;
  73. else if ((sd > .5f) != fill) {
  74. msd[0] = 1.f-msd[0];
  75. msd[1] = 1.f-msd[1];
  76. msd[2] = 1.f-msd[2];
  77. *match = -1;
  78. } else
  79. *match = 1;
  80. ++match;
  81. }
  82. }
  83. // This step is necessary to avoid artifacts when whole shape is inverted
  84. if (ambiguous) {
  85. match = &matchMap[0];
  86. for (int y = 0; y < h; ++y) {
  87. int row = shape.inverseYAxis ? h-y-1 : y;
  88. for (int x = 0; x < w; ++x) {
  89. if (!*match) {
  90. int neighborMatch = 0;
  91. if (x > 0) neighborMatch += *(match-1);
  92. if (x < w-1) neighborMatch += *(match+1);
  93. if (y > 0) neighborMatch += *(match-w);
  94. if (y < h-1) neighborMatch += *(match+w);
  95. if (neighborMatch < 0) {
  96. float *msd = sdf(x, row);
  97. msd[0] = 1.f-msd[0];
  98. msd[1] = 1.f-msd[1];
  99. msd[2] = 1.f-msd[2];
  100. }
  101. }
  102. ++match;
  103. }
  104. }
  105. }
  106. }
  107. }