rasterization.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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(Bitmap<float> &output, const Shape &shape, const Vector2 &scale, const Vector2 &translate, FillRule fillRule) {
  20. int w = output.width(), h = output.height();
  21. Point2 p;
  22. Scanline scanline;
  23. for (int y = 0; y < h; ++y) {
  24. int row = shape.inverseYAxis ? h-y-1 : y;
  25. p.y = (y+.5)/scale.y-translate.y;
  26. shape.scanline(scanline, p.y);
  27. for (int x = 0; x < w; ++x) {
  28. p.x = (x+.5)/scale.x-translate.x;
  29. int intersections = scanline.sumIntersections(p.x);
  30. bool fill = interpretFillRule(intersections, fillRule);
  31. output(x, row) = (float) fill;
  32. }
  33. }
  34. }
  35. void distanceSignCorrection(Bitmap<float> &sdf, const Shape &shape, const Vector2 &scale, const Vector2 &translate, FillRule fillRule) {
  36. int w = sdf.width(), h = sdf.height();
  37. Point2 p;
  38. Scanline scanline;
  39. for (int y = 0; y < h; ++y) {
  40. int row = shape.inverseYAxis ? h-y-1 : y;
  41. p.y = (y+.5)/scale.y-translate.y;
  42. shape.scanline(scanline, p.y);
  43. for (int x = 0; x < w; ++x) {
  44. p.x = (x+.5)/scale.x-translate.x;
  45. int intersections = scanline.sumIntersections(p.x);
  46. bool fill = interpretFillRule(intersections, fillRule);
  47. float &sd = sdf(x, row);
  48. if ((sd > .5f) != fill)
  49. sd = 1.f-sd;
  50. }
  51. }
  52. }
  53. void distanceSignCorrection(Bitmap<FloatRGB> &sdf, const Shape &shape, const Vector2 &scale, const Vector2 &translate, FillRule fillRule) {
  54. int w = sdf.width(), h = sdf.height();
  55. if (!(w*h))
  56. return;
  57. Point2 p;
  58. Scanline scanline;
  59. bool ambiguous = false;
  60. std::vector<char> matchMap;
  61. matchMap.resize(w*h);
  62. char *match = &matchMap[0];
  63. for (int y = 0; y < h; ++y) {
  64. int row = shape.inverseYAxis ? h-y-1 : y;
  65. p.y = (y+.5)/scale.y-translate.y;
  66. shape.scanline(scanline, p.y);
  67. for (int x = 0; x < w; ++x) {
  68. p.x = (x+.5)/scale.x-translate.x;
  69. int intersections = scanline.sumIntersections(p.x);
  70. bool fill = interpretFillRule(intersections, fillRule);
  71. FloatRGB &msd = sdf(x, row);
  72. float sd = median(msd.r, msd.g, msd.b);
  73. if (sd == .5f)
  74. ambiguous = true;
  75. else if ((sd > .5f) != fill) {
  76. msd.r = 1.f-msd.r;
  77. msd.g = 1.f-msd.g;
  78. msd.b = 1.f-msd.b;
  79. *match = -1;
  80. } else
  81. *match = 1;
  82. ++match;
  83. }
  84. }
  85. // This step is necessary to avoid artifacts when whole shape is inverted
  86. if (ambiguous) {
  87. match = &matchMap[0];
  88. for (int y = 0; y < h; ++y) {
  89. int row = shape.inverseYAxis ? h-y-1 : y;
  90. for (int x = 0; x < w; ++x) {
  91. if (!*match) {
  92. int neighborMatch = 0;
  93. if (x > 0) neighborMatch += *(match-1);
  94. if (x < w-1) neighborMatch += *(match+1);
  95. if (y > 0) neighborMatch += *(match-w);
  96. if (y < h-1) neighborMatch += *(match+w);
  97. if (neighborMatch < 0) {
  98. FloatRGB &msd = sdf(x, row);
  99. msd.r = 1.f-msd.r;
  100. msd.g = 1.f-msd.g;
  101. msd.b = 1.f-msd.b;
  102. }
  103. }
  104. ++match;
  105. }
  106. }
  107. }
  108. }
  109. }