Debian Codegraph @

thirdparty/msdfgen/core/rasterization.cpp

raw symbols SCIP
1
2 #include "rasterization.h"
3
4 #include <vector>
5 #include "arithmetics.hpp"
6
7 namespace msdfgen {
8
9 void rasterize(BitmapSection<float, 1> output, const Shape &shape, const Projection &projection, FillRule fillRule) {
10 output.reorient(shape.getYAxisOrientation());
11 Scanline scanline;
12 for (int y = 0; y < output.height; ++y) {
13 shape.scanline(scanline, projection.unprojectY(y+.5));
14 for (int x = 0; x < output.width; ++x)
15 *output(x, y) = (float) scanline.filled(projection.unprojectX(x+.5), fillRule);
16 }
17 }
18
19 void distanceSignCorrection(BitmapSection<float, 1> sdf, const Shape &shape, const Projection &projection, float sdfZeroValue, FillRule fillRule) {
20 sdf.reorient(shape.getYAxisOrientation());
21 float doubleSdfZeroValue = sdfZeroValue+sdfZeroValue;
22 Scanline scanline;
23 for (int y = 0; y < sdf.height; ++y) {
24 shape.scanline(scanline, projection.unprojectY(y+.5));
25 for (int x = 0; x < sdf.width; ++x) {
26 bool fill = scanline.filled(projection.unprojectX(x+.5), fillRule);
27 float &sd = *sdf(x, y);
28 if ((sd > sdfZeroValue) != fill)
29 sd = doubleSdfZeroValue-sd;
30 }
31 }
32 }
33
34 template <int N>
35 static void multiDistanceSignCorrection(BitmapSection<float, N> sdf, const Shape &shape, const Projection &projection, float sdfZeroValue, FillRule fillRule) {
36 int w = sdf.width, h = sdf.height;
37 if (!(w && h))
38 return;
39 sdf.reorient(shape.getYAxisOrientation());
40 float doubleSdfZeroValue = sdfZeroValue+sdfZeroValue;
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 shape.scanline(scanline, projection.unprojectY(y+.5));
48 for (int x = 0; x < w; ++x) {
49 bool fill = scanline.filled(projection.unprojectX(x+.5), fillRule);
50 float *msd = sdf(x, y);
51 float sd = median(msd[0], msd[1], msd[2]);
52 if (sd == sdfZeroValue)
53 ambiguous = true;
54 else if ((sd > sdfZeroValue) != fill) {
55 msd[0] = doubleSdfZeroValue-msd[0];
56 msd[1] = doubleSdfZeroValue-msd[1];
57 msd[2] = doubleSdfZeroValue-msd[2];
58 *match = -1;
59 } else
60 *match = 1;
61 if (N >= 4 && (msd[3] > sdfZeroValue) != fill)
62 msd[3] = doubleSdfZeroValue-msd[3];
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 for (int x = 0; x < w; ++x) {
71 if (!*match) {
72 int neighborMatch = 0;
73 if (x > 0) neighborMatch += *(match-1);
74 if (x < w-1) neighborMatch += *(match+1);
75 if (y > 0) neighborMatch += *(match-w);
76 if (y < h-1) neighborMatch += *(match+w);
77 if (neighborMatch < 0) {
78 float *msd = sdf(x, y);
79 msd[0] = doubleSdfZeroValue-msd[0];
80 msd[1] = doubleSdfZeroValue-msd[1];
81 msd[2] = doubleSdfZeroValue-msd[2];
82 }
83 }
84 ++match;
85 }
86 }
87 }
88 }
89
90 void distanceSignCorrection(BitmapSection<float, 3> sdf, const Shape &shape, const Projection &projection, float sdfZeroValue, FillRule fillRule) {
91 multiDistanceSignCorrection(sdf, shape, projection, sdfZeroValue, fillRule);
92 }
93
94 void distanceSignCorrection(BitmapSection<float, 4> sdf, const Shape &shape, const Projection &projection, float sdfZeroValue, FillRule fillRule) {
95 multiDistanceSignCorrection(sdf, shape, projection, sdfZeroValue, fillRule);
96 }
97
98 // Legacy API
99
100 void rasterize(const BitmapSection<float, 1> &output, const Shape &shape, const Vector2 &scale, const Vector2 &translate, FillRule fillRule) {
101 rasterize(output, shape, Projection(scale, translate), fillRule);
102 }
103
104 void distanceSignCorrection(BitmapSection<float, 1> sdf, const Shape &shape, const Projection &projection, FillRule fillRule) {
105 distanceSignCorrection(sdf, shape, projection, .5f, fillRule);
106 }
107
108 void distanceSignCorrection(BitmapSection<float, 3> sdf, const Shape &shape, const Projection &projection, FillRule fillRule) {
109 distanceSignCorrection(sdf, shape, projection, .5f, fillRule);
110 }
111
112 void distanceSignCorrection(BitmapSection<float, 4> sdf, const Shape &shape, const Projection &projection, FillRule fillRule) {
113 distanceSignCorrection(sdf, shape, projection, .5f, fillRule);
114 }
115
116 void distanceSignCorrection(const BitmapSection<float, 1> &sdf, const Shape &shape, const Vector2 &scale, const Vector2 &translate, FillRule fillRule) {
117 distanceSignCorrection(sdf, shape, Projection(scale, translate), fillRule);
118 }
119
120 void distanceSignCorrection(const BitmapSection<float, 3> &sdf, const Shape &shape, const Vector2 &scale, const Vector2 &translate, FillRule fillRule) {
121 distanceSignCorrection(sdf, shape, Projection(scale, translate), fillRule);
122 }
123
124 void distanceSignCorrection(const BitmapSection<float, 4> &sdf, const Shape &shape, const Vector2 &scale, const Vector2 &translate, FillRule fillRule) {
125 distanceSignCorrection(sdf, shape, Projection(scale, translate), fillRule);
126 }
127
128 }