2
0

rasterization.cpp 3.8 KB

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