Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/thirdparty/msdfgen/core/msdf-error-correction.cpp
9902 views
1
2
#include "msdf-error-correction.h"
3
4
#include <vector>
5
#include "arithmetics.hpp"
6
#include "Bitmap.h"
7
#include "contour-combiners.h"
8
#include "MSDFErrorCorrection.h"
9
10
namespace msdfgen {
11
12
template <int N>
13
static void msdfErrorCorrectionInner(const BitmapRef<float, N> &sdf, const Shape &shape, const SDFTransformation &transformation, const MSDFGeneratorConfig &config) {
14
if (config.errorCorrection.mode == ErrorCorrectionConfig::DISABLED)
15
return;
16
Bitmap<byte, 1> stencilBuffer;
17
if (!config.errorCorrection.buffer)
18
stencilBuffer = Bitmap<byte, 1>(sdf.width, sdf.height);
19
BitmapRef<byte, 1> stencil;
20
stencil.pixels = config.errorCorrection.buffer ? config.errorCorrection.buffer : (byte *) stencilBuffer;
21
stencil.width = sdf.width, stencil.height = sdf.height;
22
MSDFErrorCorrection ec(stencil, transformation);
23
ec.setMinDeviationRatio(config.errorCorrection.minDeviationRatio);
24
ec.setMinImproveRatio(config.errorCorrection.minImproveRatio);
25
switch (config.errorCorrection.mode) {
26
case ErrorCorrectionConfig::DISABLED:
27
case ErrorCorrectionConfig::INDISCRIMINATE:
28
break;
29
case ErrorCorrectionConfig::EDGE_PRIORITY:
30
ec.protectCorners(shape);
31
ec.protectEdges<N>(sdf);
32
break;
33
case ErrorCorrectionConfig::EDGE_ONLY:
34
ec.protectAll();
35
break;
36
}
37
if (config.errorCorrection.distanceCheckMode == ErrorCorrectionConfig::DO_NOT_CHECK_DISTANCE || (config.errorCorrection.distanceCheckMode == ErrorCorrectionConfig::CHECK_DISTANCE_AT_EDGE && config.errorCorrection.mode != ErrorCorrectionConfig::EDGE_ONLY)) {
38
ec.findErrors<N>(sdf);
39
if (config.errorCorrection.distanceCheckMode == ErrorCorrectionConfig::CHECK_DISTANCE_AT_EDGE)
40
ec.protectAll();
41
}
42
if (config.errorCorrection.distanceCheckMode == ErrorCorrectionConfig::ALWAYS_CHECK_DISTANCE || config.errorCorrection.distanceCheckMode == ErrorCorrectionConfig::CHECK_DISTANCE_AT_EDGE) {
43
if (config.overlapSupport)
44
ec.findErrors<OverlappingContourCombiner, N>(sdf, shape);
45
else
46
ec.findErrors<SimpleContourCombiner, N>(sdf, shape);
47
}
48
ec.apply(sdf);
49
}
50
51
template <int N>
52
static void msdfErrorCorrectionShapeless(const BitmapRef<float, N> &sdf, const SDFTransformation &transformation, double minDeviationRatio, bool protectAll) {
53
Bitmap<byte, 1> stencilBuffer(sdf.width, sdf.height);
54
MSDFErrorCorrection ec(stencilBuffer, transformation);
55
ec.setMinDeviationRatio(minDeviationRatio);
56
if (protectAll)
57
ec.protectAll();
58
ec.findErrors<N>(sdf);
59
ec.apply(sdf);
60
}
61
62
void msdfErrorCorrection(const BitmapRef<float, 3> &sdf, const Shape &shape, const SDFTransformation &transformation, const MSDFGeneratorConfig &config) {
63
msdfErrorCorrectionInner(sdf, shape, transformation, config);
64
}
65
void msdfErrorCorrection(const BitmapRef<float, 4> &sdf, const Shape &shape, const SDFTransformation &transformation, const MSDFGeneratorConfig &config) {
66
msdfErrorCorrectionInner(sdf, shape, transformation, config);
67
}
68
void msdfErrorCorrection(const BitmapRef<float, 3> &sdf, const Shape &shape, const Projection &projection, Range range, const MSDFGeneratorConfig &config) {
69
msdfErrorCorrectionInner(sdf, shape, SDFTransformation(projection, range), config);
70
}
71
void msdfErrorCorrection(const BitmapRef<float, 4> &sdf, const Shape &shape, const Projection &projection, Range range, const MSDFGeneratorConfig &config) {
72
msdfErrorCorrectionInner(sdf, shape, SDFTransformation(projection, range), config);
73
}
74
75
void msdfFastDistanceErrorCorrection(const BitmapRef<float, 3> &sdf, const SDFTransformation &transformation, double minDeviationRatio) {
76
msdfErrorCorrectionShapeless(sdf, transformation, minDeviationRatio, false);
77
}
78
void msdfFastDistanceErrorCorrection(const BitmapRef<float, 4> &sdf, const SDFTransformation &transformation, double minDeviationRatio) {
79
msdfErrorCorrectionShapeless(sdf, transformation, minDeviationRatio, false);
80
}
81
void msdfFastDistanceErrorCorrection(const BitmapRef<float, 3> &sdf, const Projection &projection, Range range, double minDeviationRatio) {
82
msdfErrorCorrectionShapeless(sdf, SDFTransformation(projection, range), minDeviationRatio, false);
83
}
84
void msdfFastDistanceErrorCorrection(const BitmapRef<float, 4> &sdf, const Projection &projection, Range range, double minDeviationRatio) {
85
msdfErrorCorrectionShapeless(sdf, SDFTransformation(projection, range), minDeviationRatio, false);
86
}
87
void msdfFastDistanceErrorCorrection(const BitmapRef<float, 3> &sdf, Range pxRange, double minDeviationRatio) {
88
msdfErrorCorrectionShapeless(sdf, SDFTransformation(Projection(), pxRange), minDeviationRatio, false);
89
}
90
void msdfFastDistanceErrorCorrection(const BitmapRef<float, 4> &sdf, Range pxRange, double minDeviationRatio) {
91
msdfErrorCorrectionShapeless(sdf, SDFTransformation(Projection(), pxRange), minDeviationRatio, false);
92
}
93
94
void msdfFastEdgeErrorCorrection(const BitmapRef<float, 3> &sdf, const SDFTransformation &transformation, double minDeviationRatio) {
95
msdfErrorCorrectionShapeless(sdf, transformation, minDeviationRatio, true);
96
}
97
void msdfFastEdgeErrorCorrection(const BitmapRef<float, 4> &sdf, const SDFTransformation &transformation, double minDeviationRatio) {
98
msdfErrorCorrectionShapeless(sdf, transformation, minDeviationRatio, true);
99
}
100
void msdfFastEdgeErrorCorrection(const BitmapRef<float, 3> &sdf, const Projection &projection, Range range, double minDeviationRatio) {
101
msdfErrorCorrectionShapeless(sdf, SDFTransformation(projection, range), minDeviationRatio, true);
102
}
103
void msdfFastEdgeErrorCorrection(const BitmapRef<float, 4> &sdf, const Projection &projection, Range range, double minDeviationRatio) {
104
msdfErrorCorrectionShapeless(sdf, SDFTransformation(projection, range), minDeviationRatio, true);
105
}
106
void msdfFastEdgeErrorCorrection(const BitmapRef<float, 3> &sdf, Range pxRange, double minDeviationRatio) {
107
msdfErrorCorrectionShapeless(sdf, SDFTransformation(Projection(), pxRange), minDeviationRatio, true);
108
}
109
void msdfFastEdgeErrorCorrection(const BitmapRef<float, 4> &sdf, Range pxRange, double minDeviationRatio) {
110
msdfErrorCorrectionShapeless(sdf, SDFTransformation(Projection(), pxRange), minDeviationRatio, true);
111
}
112
113
114
// Legacy version
115
116
inline static bool detectClash(const float *a, const float *b, double threshold) {
117
// Sort channels so that pairs (a0, b0), (a1, b1), (a2, b2) go from biggest to smallest absolute difference
118
float a0 = a[0], a1 = a[1], a2 = a[2];
119
float b0 = b[0], b1 = b[1], b2 = b[2];
120
float tmp;
121
if (fabsf(b0-a0) < fabsf(b1-a1)) {
122
tmp = a0, a0 = a1, a1 = tmp;
123
tmp = b0, b0 = b1, b1 = tmp;
124
}
125
if (fabsf(b1-a1) < fabsf(b2-a2)) {
126
tmp = a1, a1 = a2, a2 = tmp;
127
tmp = b1, b1 = b2, b2 = tmp;
128
if (fabsf(b0-a0) < fabsf(b1-a1)) {
129
tmp = a0, a0 = a1, a1 = tmp;
130
tmp = b0, b0 = b1, b1 = tmp;
131
}
132
}
133
return (fabsf(b1-a1) >= threshold) &&
134
!(b0 == b1 && b0 == b2) && // Ignore if other pixel has been equalized
135
fabsf(a2-.5f) >= fabsf(b2-.5f); // Out of the pair, only flag the pixel farther from a shape edge
136
}
137
138
template <int N>
139
static void msdfErrorCorrectionInner_legacy(const BitmapRef<float, N> &output, const Vector2 &threshold) {
140
std::vector<std::pair<int, int> > clashes;
141
int w = output.width, h = output.height;
142
for (int y = 0; y < h; ++y)
143
for (int x = 0; x < w; ++x) {
144
if (
145
(x > 0 && detectClash(output(x, y), output(x-1, y), threshold.x)) ||
146
(x < w-1 && detectClash(output(x, y), output(x+1, y), threshold.x)) ||
147
(y > 0 && detectClash(output(x, y), output(x, y-1), threshold.y)) ||
148
(y < h-1 && detectClash(output(x, y), output(x, y+1), threshold.y))
149
)
150
clashes.push_back(std::make_pair(x, y));
151
}
152
for (std::vector<std::pair<int, int> >::const_iterator clash = clashes.begin(); clash != clashes.end(); ++clash) {
153
float *pixel = output(clash->first, clash->second);
154
float med = median(pixel[0], pixel[1], pixel[2]);
155
pixel[0] = med, pixel[1] = med, pixel[2] = med;
156
}
157
#ifndef MSDFGEN_NO_DIAGONAL_CLASH_DETECTION
158
clashes.clear();
159
for (int y = 0; y < h; ++y)
160
for (int x = 0; x < w; ++x) {
161
if (
162
(x > 0 && y > 0 && detectClash(output(x, y), output(x-1, y-1), threshold.x+threshold.y)) ||
163
(x < w-1 && y > 0 && detectClash(output(x, y), output(x+1, y-1), threshold.x+threshold.y)) ||
164
(x > 0 && y < h-1 && detectClash(output(x, y), output(x-1, y+1), threshold.x+threshold.y)) ||
165
(x < w-1 && y < h-1 && detectClash(output(x, y), output(x+1, y+1), threshold.x+threshold.y))
166
)
167
clashes.push_back(std::make_pair(x, y));
168
}
169
for (std::vector<std::pair<int, int> >::const_iterator clash = clashes.begin(); clash != clashes.end(); ++clash) {
170
float *pixel = output(clash->first, clash->second);
171
float med = median(pixel[0], pixel[1], pixel[2]);
172
pixel[0] = med, pixel[1] = med, pixel[2] = med;
173
}
174
#endif
175
}
176
177
void msdfErrorCorrection_legacy(const BitmapRef<float, 3> &output, const Vector2 &threshold) {
178
msdfErrorCorrectionInner_legacy(output, threshold);
179
}
180
void msdfErrorCorrection_legacy(const BitmapRef<float, 4> &output, const Vector2 &threshold) {
181
msdfErrorCorrectionInner_legacy(output, threshold);
182
}
183
184
}
185
186