Skip to main content

vzense_rust/util/
buffer_text.rs

1//! Draw ASCII text to a `u8` image buffer.
2
3pub fn no_ir_info(buffer: &mut [u8]) {
4    draw_string(buffer, 640, 60, 200, 3, 159, "IR NOT ACTIVATED FOR DCAM560");
5}
6
7pub fn no_depth_info(buffer: &mut [u8]) {
8    draw_string(
9        buffer,
10        640,
11        40,
12        200,
13        3,
14        255,
15        "DEPTH NOT ACTIVATED FOR DCAM560",
16    );
17}
18
19/// Draw ASCII text to a `u8` image buffer. `width` is the image width, (`x,y`) the start position.
20pub fn draw_string(
21    buffer: &mut [u8],
22    width: usize,
23    x: usize,
24    y: usize,
25    scale: usize,
26    brightness: u8,
27    text: &str,
28) {
29    let mut current_x = x;
30    for c in text.chars() {
31        draw_char(buffer, width, current_x, y, scale, brightness, c);
32        // Move to the next character position, considering the scale
33        current_x += (5 * scale) + scale; // (width of char + 1-pixel space) * scale
34    }
35}
36
37fn draw_char(
38    buffer: &mut [u8],
39    width: usize,
40    x: usize,
41    y: usize,
42    scale: usize,
43    brightness: u8,
44    character: char,
45) {
46    if !character.is_ascii() {
47        return;
48    }
49
50    let font_index = character as usize - 32;
51    if font_index >= FONT.len() {
52        return;
53    }
54
55    let font_char = FONT[font_index];
56
57    for row in 0..7 {
58        for (col, char) in font_char.iter().enumerate() {
59            // Check if the pixel is "on" in the font definition
60            if (char >> row) & 0x01 == 1 {
61                // Now, draw a scale x scale block of pixels
62                for sub_row in 0..scale {
63                    for sub_col in 0..scale {
64                        let pixel_x = x + (col * scale) + sub_col;
65                        let pixel_y = y + (row * scale) + sub_row;
66
67                        // Calculate the index in the 1D buffer
68                        let pixel_index = pixel_y * width + pixel_x;
69
70                        if pixel_index < buffer.len() {
71                            buffer[pixel_index] = brightness;
72                        }
73                    }
74                }
75            }
76        }
77    }
78}
79
80// ascii characters for 5x7 font
81const FONT: [[u8; 5]; 95] = [
82    // 32:   (Space)
83    [0x00, 0x00, 0x00, 0x00, 0x00],
84    // 33: !
85    [0x00, 0x00, 0x5f, 0x00, 0x00],
86    // 34: "
87    [0x00, 0x03, 0x00, 0x03, 0x00],
88    // 35: #
89    [0x14, 0x7e, 0x14, 0x7e, 0x14],
90    // 36: $
91    [0x24, 0x2a, 0x7f, 0x2a, 0x12],
92    // 37: %
93    [0x23, 0x13, 0x08, 0x64, 0x64],
94    // 38: &
95    [0x36, 0x49, 0x56, 0x20, 0x50],
96    // 39: '
97    [0x00, 0x03, 0x03, 0x00, 0x00],
98    // 40: (
99    [0x00, 0x1c, 0x22, 0x41, 0x00],
100    // 41: )
101    [0x00, 0x41, 0x22, 0x1c, 0x00],
102    // 42: *
103    [0x08, 0x2a, 0x7f, 0x2a, 0x08],
104    // 43: +
105    [0x08, 0x08, 0x3e, 0x08, 0x08],
106    // 44: ,
107    [0x00, 0x50, 0x30, 0x00, 0x00],
108    // 45: -
109    [0x08, 0x08, 0x08, 0x08, 0x08],
110    // 46: .
111    [0x00, 0x60, 0x60, 0x00, 0x00],
112    // 47: /
113    [0x20, 0x10, 0x08, 0x04, 0x02],
114    // 48: 0
115    [0x3e, 0x51, 0x49, 0x45, 0x3e],
116    // 49: 1
117    [0x00, 0x42, 0x7f, 0x40, 0x00],
118    // 50: 2
119    [0x62, 0x51, 0x49, 0x49, 0x46],
120    // 51: 3
121    [0x22, 0x41, 0x49, 0x49, 0x36],
122    // 52: 4
123    [0x18, 0x14, 0x12, 0x7f, 0x10],
124    // 53: 5
125    [0x27, 0x45, 0x45, 0x45, 0x39],
126    // 54: 6
127    [0x3c, 0x4a, 0x49, 0x49, 0x30],
128    // 55: 7
129    [0x01, 0x71, 0x09, 0x05, 0x03],
130    // 56: 8
131    [0x36, 0x49, 0x49, 0x49, 0x36],
132    // 57: 9
133    [0x06, 0x49, 0x49, 0x29, 0x1e],
134    // 58: :
135    [0x00, 0x36, 0x36, 0x00, 0x00],
136    // 59: ;
137    [0x00, 0x60, 0x36, 0x00, 0x00],
138    // 60: <
139    [0x08, 0x14, 0x22, 0x41, 0x00],
140    // 61: =
141    [0x14, 0x14, 0x14, 0x14, 0x14],
142    // 62: >
143    [0x00, 0x41, 0x22, 0x14, 0x08],
144    // 63: ?
145    [0x02, 0x01, 0x51, 0x09, 0x06],
146    // 64: @
147    [0x3e, 0x41, 0x5d, 0x49, 0x4e],
148    // 65: A
149    [0x7e, 0x11, 0x11, 0x11, 0x7e],
150    // 66: B
151    [0x7f, 0x49, 0x49, 0x49, 0x36],
152    // 67: C
153    [0x3e, 0x41, 0x41, 0x41, 0x22],
154    // 68: D
155    [0x7f, 0x41, 0x41, 0x41, 0x3e],
156    // 69: E
157    [0x7f, 0x49, 0x49, 0x49, 0x41],
158    // 70: F
159    [0x7f, 0x09, 0x09, 0x01, 0x01],
160    // 71: G
161    [0x3e, 0x41, 0x49, 0x49, 0x7a],
162    // 72: H
163    [0x7f, 0x08, 0x08, 0x08, 0x7f],
164    // 73: I
165    [0x00, 0x41, 0x7f, 0x41, 0x00],
166    // 74: J
167    [0x20, 0x40, 0x41, 0x3f, 0x01],
168    // 75: K
169    [0x7f, 0x08, 0x14, 0x22, 0x41],
170    // 76: L
171    [0x7f, 0x40, 0x40, 0x40, 0x40],
172    // 77: M
173    [0x7f, 0x02, 0x04, 0x02, 0x7f],
174    // 78: N
175    [0x7f, 0x02, 0x04, 0x08, 0x7f],
176    // 79: O
177    [0x3e, 0x41, 0x41, 0x41, 0x3e],
178    // 80: P
179    [0x7f, 0x09, 0x09, 0x09, 0x06],
180    // 81: Q
181    [0x3e, 0x41, 0x51, 0x21, 0x5e],
182    // 82: R
183    [0x7f, 0x09, 0x19, 0x29, 0x46],
184    // 83: S
185    [0x46, 0x49, 0x49, 0x49, 0x31],
186    // 84: T
187    [0x01, 0x01, 0x7f, 0x01, 0x01],
188    // 85: U
189    [0x3f, 0x40, 0x40, 0x40, 0x3f],
190    // 86: V
191    [0x1f, 0x20, 0x40, 0x20, 0x1f],
192    // 87: W
193    [0x3f, 0x40, 0x38, 0x40, 0x3f],
194    // 88: X
195    [0x61, 0x14, 0x08, 0x14, 0x61],
196    // 89: Y
197    [0x01, 0x02, 0x7c, 0x02, 0x01],
198    // 90: Z
199    [0x41, 0x61, 0x51, 0x49, 0x45],
200    // 91: [
201    [0x00, 0x7f, 0x41, 0x41, 0x00],
202    // 92: \
203    [0x02, 0x04, 0x08, 0x10, 0x20],
204    // 93: ]
205    [0x00, 0x41, 0x41, 0x7f, 0x00],
206    // 94: ^
207    [0x04, 0x02, 0x01, 0x02, 0x04],
208    // 95: _
209    [0x40, 0x40, 0x40, 0x40, 0x40],
210    // 96: `
211    [0x00, 0x01, 0x02, 0x04, 0x00],
212    // 97: a
213    [0x20, 0x54, 0x54, 0x54, 0x78],
214    // 98: b
215    [0x7f, 0x44, 0x48, 0x48, 0x30],
216    // 99: c
217    [0x38, 0x44, 0x44, 0x44, 0x20],
218    // 100: d
219    [0x30, 0x48, 0x48, 0x44, 0x7f],
220    // 101: e
221    [0x38, 0x54, 0x54, 0x54, 0x18],
222    // 102: f
223    [0x04, 0x04, 0x7e, 0x05, 0x05],
224    // 103: g
225    [0x48, 0x54, 0x54, 0x54, 0x38],
226    // 104: h
227    [0x7f, 0x08, 0x08, 0x08, 0x70],
228    // 105: i
229    [0x00, 0x44, 0x7d, 0x40, 0x00],
230    // 106: j
231    [0x20, 0x40, 0x44, 0x3d, 0x00],
232    // 107: k
233    [0x7f, 0x08, 0x14, 0x22, 0x41],
234    // 108: l
235    [0x00, 0x7f, 0x40, 0x40, 0x00],
236    // 109: m
237    [0x7c, 0x02, 0x04, 0x02, 0x7c],
238    // 110: n
239    [0x7c, 0x08, 0x08, 0x08, 0x70],
240    // 111: o
241    [0x38, 0x44, 0x44, 0x44, 0x38],
242    // 112: p
243    [0x7c, 0x14, 0x14, 0x14, 0x08],
244    // 113: q
245    [0x08, 0x14, 0x14, 0x14, 0x7c],
246    // 114: r
247    [0x7c, 0x08, 0x04, 0x04, 0x04],
248    // 115: s
249    [0x48, 0x54, 0x54, 0x54, 0x20],
250    // 116: t
251    [0x04, 0x04, 0x3e, 0x04, 0x04],
252    // 117: u
253    [0x3c, 0x40, 0x40, 0x20, 0x7c],
254    // 118: v
255    [0x1c, 0x20, 0x40, 0x20, 0x1c],
256    // 119: w
257    [0x3c, 0x40, 0x38, 0x40, 0x3c],
258    // 120: x
259    [0x44, 0x28, 0x10, 0x28, 0x44],
260    // 121: y
261    [0x44, 0x44, 0x44, 0x44, 0x28],
262    // 122: z
263    [0x44, 0x64, 0x54, 0x4c, 0x44],
264    // 123: {
265    [0x00, 0x08, 0x3e, 0x41, 0x00],
266    // 124: |
267    [0x00, 0x00, 0x7f, 0x00, 0x00],
268    // 125: }
269    [0x00, 0x41, 0x3e, 0x08, 0x00],
270    // 126: ~
271    [0x08, 0x10, 0x10, 0x08, 0x00],
272];