stevenkhan commited on
Commit
9fcbfc7
·
verified ·
1 Parent(s): c1a4f78

Upload spectral-font/src/msdf.rs

Browse files
Files changed (1) hide show
  1. spectral-font/src/msdf.rs +57 -0
spectral-font/src/msdf.rs ADDED
@@ -0,0 +1,57 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ pub fn dilate(bitmap: &[u8], width: usize, height: usize, radius: u8) -> Vec<u8> {
2
+ if radius == 0 || bitmap.is_empty() { return bitmap.to_vec(); }
3
+ let r = radius as isize;
4
+ let mut out = vec![0u8; width * height];
5
+ for y in 0..height {
6
+ for x in 0..width {
7
+ let mut max_val = bitmap[y * width + x];
8
+ for dy in -r..=r {
9
+ for dx in -r..=r {
10
+ let ny = (y as isize + dy).clamp(0, height as isize - 1) as usize;
11
+ let nx = (x as isize + dx).clamp(0, width as isize - 1) as usize;
12
+ max_val = max_val.max(bitmap[ny * width + nx]);
13
+ }
14
+ }
15
+ out[y * width + x] = max_val;
16
+ }
17
+ }
18
+ out
19
+ }
20
+
21
+ pub fn thicken_light(bitmap: &[u8], width: usize, height: usize, strength: f32) -> Vec<u8> {
22
+ if strength <= 0.0 || bitmap.is_empty() { return bitmap.to_vec(); }
23
+ let mut out = bitmap.to_vec();
24
+ let threshold = 64u8;
25
+ let strength_byte = (strength * 255.0).clamp(0.0, 255.0) as u8;
26
+ for y in 0..height {
27
+ for x in 0..width {
28
+ let idx = y * width + x;
29
+ if bitmap[idx] >= threshold { continue; }
30
+ let mut neighbor_max = 0u8;
31
+ for dy in -1..=1isize {
32
+ for dx in -1..=1isize {
33
+ if dy == 0 && dx == 0 { continue; }
34
+ let ny = (y as isize + dy).clamp(0, height as isize - 1) as usize;
35
+ let nx = (x as isize + dx).clamp(0, width as isize - 1) as usize;
36
+ neighbor_max = neighbor_max.max(bitmap[ny * width + nx]);
37
+ }
38
+ }
39
+ if neighbor_max > threshold {
40
+ out[idx] = out[idx].saturating_add(strength_byte);
41
+ }
42
+ }
43
+ }
44
+ out
45
+ }
46
+
47
+ pub fn r8_to_rgb(r8: &[u8]) -> Vec<u8> {
48
+ let mut rgb = Vec::with_capacity(r8.len() * 3);
49
+ for &v in r8 { rgb.push(v); rgb.push(v); rgb.push(v); }
50
+ rgb
51
+ }
52
+
53
+ pub fn r8_to_rgba(r8: &[u8]) -> Vec<u8> {
54
+ let mut rgba = Vec::with_capacity(r8.len() * 4);
55
+ for &v in r8 { rgba.push(v); rgba.push(v); rgba.push(v); rgba.push(v); }
56
+ rgba
57
+ }