Debian Codegraph @

thirdparty/msdfgen/core/msdf-error-correction.cpp

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