rasterization.cpp 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. void distanceSignCorrection(const BitmapRef<float, 3> &sdf, const Shape &shape, const Vector2 &scale, const Vector2 &translate, FillRule fillRule) {
  36. int w = sdf.width, h = sdf.height;
  37. if (!(w*h))
  38. return;
  39. Point2 p;
  40. Scanline scanline;
  41. bool ambiguous = false;
  42. std::vector<char> matchMap;
  43. matchMap.resize(w*h);
  44. char *match = &matchMap[0];
  45. for (int y = 0; y < h; ++y) {
  46. int row = shape.inverseYAxis ? h-y-1 : y;
  47. p.y = (y+.5)/scale.y-translate.y;
  48. shape.scanline(scanline, p.y);
  49. for (int x = 0; x < w; ++x) {
  50. p.x = (x+.5)/scale.x-translate.x;
  51. bool fill = scanline.filled(p.x, fillRule);
  52. float *msd = sdf(x, row);
  53. float sd = median(msd[0], msd[1], msd[2]);
  54. if (sd == .5f)
  55. ambiguous = true;
  56. else if ((sd > .5f) != fill) {
  57. msd[0] = 1.f-msd[0];
  58. msd[1] = 1.f-msd[1];
  59. msd[2] = 1.f-msd[2];
  60. *match = -1;
  61. } else
  62. *match = 1;
  63. ++match;
  64. }
  65. }
  66. // This step is necessary to avoid artifacts when whole shape is inverted
  67. if (ambiguous) {
  68. match = &matchMap[0];
  69. for (int y = 0; y < h; ++y) {
  70. int row = shape.inverseYAxis ? h-y-1 : y;
  71. for (int x = 0; x < w; ++x) {
  72. if (!*match) {
  73. int neighborMatch = 0;
  74. if (x > 0) neighborMatch += *(match-1);
  75. if (x < w-1) neighborMatch += *(match+1);
  76. if (y > 0) neighborMatch += *(match-w);
  77. if (y < h-1) neighborMatch += *(match+w);
  78. if (neighborMatch < 0) {
  79. float *msd = sdf(x, row);
  80. msd[0] = 1.f-msd[0];
  81. msd[1] = 1.f-msd[1];
  82. msd[2] = 1.f-msd[2];
  83. }
  84. }
  85. ++match;
  86. }
  87. }
  88. }
  89. }
  90. }