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
10namespace msdfgen {
11
12template <int N>
13static void msdfErrorCorrectionInner(const BitmapRef<float, N> &sdf, const Shape &shape, const Projection &projection, double range, 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, projection, range);
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
51template <int N>
52static void msdfErrorCorrectionShapeless(const BitmapRef<float, N> &sdf, const Projection &projection, double range, double minDeviationRatio, bool protectAll) {
53 Bitmap<byte, 1> stencilBuffer(sdf.width, sdf.height);
54 MSDFErrorCorrection ec(stencilBuffer, projection, range);
55 ec.setMinDeviationRatio(minDeviationRatio);
56 if (protectAll)
57 ec.protectAll();
58 ec.findErrors<N>(sdf);
59 ec.apply(sdf);
60}
61
62void msdfErrorCorrection(const BitmapRef<float, 3> &sdf, const Shape &shape, const Projection &projection, double range, const MSDFGeneratorConfig &config) {
63 msdfErrorCorrectionInner(sdf, shape, projection, range, config);
64}
65void msdfErrorCorrection(const BitmapRef<float, 4> &sdf, const Shape &shape, const Projection &projection, double range, const MSDFGeneratorConfig &config) {
66 msdfErrorCorrectionInner(sdf, shape, projection, range, config);
67}
68
69void msdfFastDistanceErrorCorrection(const BitmapRef<float, 3> &sdf, const Projection &projection, double range, double minDeviationRatio) {
70 msdfErrorCorrectionShapeless(sdf, projection, range, minDeviationRatio, false);
71}
72void msdfFastDistanceErrorCorrection(const BitmapRef<float, 4> &sdf, const Projection &projection, double range, double minDeviationRatio) {
73 msdfErrorCorrectionShapeless(sdf, projection, range, minDeviationRatio, false);
74}
75
76void msdfFastEdgeErrorCorrection(const BitmapRef<float, 3> &sdf, const Projection &projection, double range, double minDeviationRatio) {
77 msdfErrorCorrectionShapeless(sdf, projection, range, minDeviationRatio, true);
78}
79void msdfFastEdgeErrorCorrection(const BitmapRef<float, 4> &sdf, const Projection &projection, double range, double minDeviationRatio) {
80 msdfErrorCorrectionShapeless(sdf, projection, range, minDeviationRatio, true);
81}
82
83
84// Legacy version
85
86inline static bool detectClash(const float *a, const float *b, double threshold) {
87 // Sort channels so that pairs (a0, b0), (a1, b1), (a2, b2) go from biggest to smallest absolute difference
88 float a0 = a[0], a1 = a[1], a2 = a[2];
89 float b0 = b[0], b1 = b[1], b2 = b[2];
90 float tmp;
91 if (fabsf(b0-a0) < fabsf(b1-a1)) {
92 tmp = a0, a0 = a1, a1 = tmp;
93 tmp = b0, b0 = b1, b1 = tmp;
94 }
95 if (fabsf(b1-a1) < fabsf(b2-a2)) {
96 tmp = a1, a1 = a2, a2 = tmp;
97 tmp = b1, b1 = b2, b2 = tmp;
98 if (fabsf(b0-a0) < fabsf(b1-a1)) {
99 tmp = a0, a0 = a1, a1 = tmp;
100 tmp = b0, b0 = b1, b1 = tmp;
101 }
102 }
103 return (fabsf(b1-a1) >= threshold) &&
104 !(b0 == b1 && b0 == b2) && // Ignore if other pixel has been equalized
105 fabsf(a2-.5f) >= fabsf(b2-.5f); // Out of the pair, only flag the pixel farther from a shape edge
106}
107
108template <int N>
109static void msdfErrorCorrectionInner_legacy(const BitmapRef<float, N> &output, const Vector2 &threshold) {
110 std::vector<std::pair<int, int> > clashes;
111 int w = output.width, h = output.height;
112 for (int y = 0; y < h; ++y)
113 for (int x = 0; x < w; ++x) {
114 if (
115 (x > 0 && detectClash(output(x, y), output(x-1, y), threshold.x)) ||
116 (x < w-1 && detectClash(output(x, y), output(x+1, y), threshold.x)) ||
117 (y > 0 && detectClash(output(x, y), output(x, y-1), threshold.y)) ||
118 (y < h-1 && detectClash(output(x, y), output(x, y+1), threshold.y))
119 )
120 clashes.push_back(std::make_pair(x, y));
121 }
122 for (std::vector<std::pair<int, int> >::const_iterator clash = clashes.begin(); clash != clashes.end(); ++clash) {
123 float *pixel = output(clash->first, clash->second);
124 float med = median(pixel[0], pixel[1], pixel[2]);
125 pixel[0] = med, pixel[1] = med, pixel[2] = med;
126 }
127#ifndef MSDFGEN_NO_DIAGONAL_CLASH_DETECTION
128 clashes.clear();
129 for (int y = 0; y < h; ++y)
130 for (int x = 0; x < w; ++x) {
131 if (
132 (x > 0 && y > 0 && detectClash(output(x, y), output(x-1, y-1), threshold.x+threshold.y)) ||
133 (x < w-1 && y > 0 && detectClash(output(x, y), output(x+1, y-1), threshold.x+threshold.y)) ||
134 (x > 0 && y < h-1 && detectClash(output(x, y), output(x-1, y+1), threshold.x+threshold.y)) ||
135 (x < w-1 && y < h-1 && detectClash(output(x, y), output(x+1, y+1), threshold.x+threshold.y))
136 )
137 clashes.push_back(std::make_pair(x, y));
138 }
139 for (std::vector<std::pair<int, int> >::const_iterator clash = clashes.begin(); clash != clashes.end(); ++clash) {
140 float *pixel = output(clash->first, clash->second);
141 float med = median(pixel[0], pixel[1], pixel[2]);
142 pixel[0] = med, pixel[1] = med, pixel[2] = med;
143 }
144#endif
145}
146
147void msdfErrorCorrection_legacy(const BitmapRef<float, 3> &output, const Vector2 &threshold) {
148 msdfErrorCorrectionInner_legacy(output, threshold);
149}
150void msdfErrorCorrection_legacy(const BitmapRef<float, 4> &output, const Vector2 &threshold) {
151 msdfErrorCorrectionInner_legacy(output, threshold);
152}
153
154}
155