Estella 0.113.0
A fast 2D and 3D game engine powered by WebAssembly and ECS
Loading...
Searching...
No Matches
SdfGenerator.cpp
Go to the documentation of this file.
1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team
14#include "SdfGenerator.hpp"
15
16#include <vector>
17#include <cmath>
18#include <algorithm>
19
20namespace esengine::text {
21namespace {
22
23constexpr f32 kInf = 1e20f;
25constexpr usize kKeptScratch = 1u << 20;
26
27struct Scratch {
28 std::vector<f32> outer, inner, d, z;
29 std::vector<i32> v;
30};
31
44bool edt1d(const f32* f, usize stride, f32* d, i32* v, f32* z, i32 n, f32 limit) {
45 i32 k = -1;
46 for (i32 q = 0; q < n; ++q) {
47 const f32 fq = f[static_cast<usize>(q) * stride];
48 if (fq >= limit) continue;
49 if (fq == 0.0f && q > 0 && q + 1 < n
50 && f[static_cast<usize>(q - 1) * stride] == 0.0f && f[static_cast<usize>(q + 1) * stride] == 0.0f) {
51 continue;
52 }
53 if (k < 0) {
54 k = 0;
55 v[0] = q;
56 z[0] = -kInf;
57 z[1] = kInf;
58 continue;
59 }
60 const f32 sq = fq + static_cast<f32>(q) * q;
61 f32 s;
62 while (true) {
63 const i32 p = v[k];
64 s = (sq - (f[static_cast<usize>(p) * stride] + static_cast<f32>(p) * p)) / (2.0f * (q - p));
65 if (s > z[k]) break;
66 --k;
67 }
68 ++k;
69 v[k] = q;
70 z[k] = s;
71 z[k + 1] = kInf;
72 }
73 if (k < 0) return false;
74 k = 0;
75 for (i32 q = 0; q < n; ++q) {
76 if (f[static_cast<usize>(q) * stride] == 0.0f) {
77 d[q] = 0.0f;
78 continue;
79 }
80 while (z[k + 1] < static_cast<f32>(q)) ++k;
81 const f32 dx = static_cast<f32>(q - v[k]);
82 d[q] = dx * dx + f[static_cast<usize>(v[k]) * stride];
83 }
84 return true;
85}
86
88void edt2d(f32* grid, i32 w, i32 h, Scratch& s, f32 limit) {
89 for (i32 x = 0; x < w; ++x) {
90 if (!edt1d(grid + x, static_cast<usize>(w), s.d.data(), s.v.data(), s.z.data(), h, limit)) continue;
91 for (i32 y = 0; y < h; ++y) grid[static_cast<usize>(y) * w + x] = s.d[static_cast<usize>(y)];
92 }
93 for (i32 y = 0; y < h; ++y) {
94 f32* row = grid + static_cast<usize>(y) * w;
95 if (!edt1d(row, 1, s.d.data(), s.v.data(), s.z.data(), w, limit)) continue;
96 std::copy_n(s.d.data(), w, row);
97 }
98}
99
100} // namespace
101
102void sdfFromAlpha(const u8* alpha, u8* out, u32 width, u32 height, f32 spread) {
103 if (!alpha || !out || width == 0 || height == 0) return;
104
105 const i32 w = static_cast<i32>(width);
106 const i32 h = static_cast<i32>(height);
107 const usize n = static_cast<usize>(w) * static_cast<usize>(h);
108 const usize m = static_cast<usize>(std::max(w, h));
109
110 static thread_local Scratch scratch;
111 Scratch& s = scratch;
112 if (s.outer.size() < n) {
113 s.outer.resize(n);
114 s.inner.resize(n);
115 }
116 if (s.d.size() < m) {
117 s.d.resize(m);
118 s.v.resize(m);
119 s.z.resize(m + 1);
120 }
121 f32* outer = s.outer.data();
122 f32* inner = s.inner.data();
123
124 // Squared distance to the glyph interior (outer) and exterior (inner).
125 // Coverage seeds: a fully covered texel is interior (outer seed 0); a
126 // fully empty one is exterior (inner seed 0); a partial texel sits within
127 // half a texel of the edge, on the side its coverage says.
128 for (usize i = 0; i < n; ++i) {
129 const u8 c = alpha[i];
130 if (c == 255) {
131 outer[i] = 0.0f;
132 inner[i] = kInf;
133 } else if (c == 0) {
134 outer[i] = kInf;
135 inner[i] = 0.0f;
136 } else {
137 const f32 a = static_cast<f32>(c) / 255.0f;
138 const f32 dOut = std::max(0.0f, 0.5f - a); // toward the interior side
139 const f32 dIn = std::max(0.0f, a - 0.5f); // toward the exterior side
140 outer[i] = dOut * dOut;
141 inner[i] = dIn * dIn;
142 }
143 }
144
145 // Past spread + 1 the byte has clamped: wherever one field is that far, the texel is fully on one side and the other is 0.
146 const f32 limit = (spread > 0.0f) ? (spread + 1.0f) * (spread + 1.0f) : kInf;
147 edt2d(outer, w, h, s, limit);
148 edt2d(inner, w, h, s, limit);
149
150 const f32 scale = (spread > 0.0f) ? (127.0f / spread) : 127.0f;
151 for (usize i = 0; i < n; ++i) {
152 // + inside, − outside, ~0 at the (sub-texel) edge. Most texels have one side at exactly 0.
153 const f32 toOut = inner[i] == 0.0f ? 0.0f : std::sqrt(inner[i]);
154 const f32 toIn = outer[i] == 0.0f ? 0.0f : std::sqrt(outer[i]);
155 const f32 v = 128.5f + (toOut - toIn) * scale;
156 out[i] = v <= 0.0f ? u8(0) : v >= 255.0f ? u8(255) : static_cast<u8>(v);
157 }
158
159 if (n > kKeptScratch) {
160 s.outer = {};
161 s.inner = {};
162 s.d = {};
163 s.v = {};
164 s.z = {};
165 }
166}
167
168} // namespace esengine::text
Signed distance field generation from an 8-bit alpha coverage bitmap.
void sdfFromAlpha(const u8 *alpha, u8 *out, u32 width, u32 height, f32 spread)
Signed distance field from an antialiased alpha coverage bitmap.
std::int32_t i32
Signed 32-bit integer.
Definition Types.hpp:54
std::size_t usize
Size type for indexing and sizes.
Definition Types.hpp:64
std::uint32_t u32
Unsigned 32-bit integer.
Definition Types.hpp:45
std::uint8_t u8
Unsigned 8-bit integer.
Definition Types.hpp:41
float f32
32-bit floating point
Definition Types.hpp:59