1pub mod buffer_text;
4pub mod color_map;
5pub mod touch_detector;
6
7use std::time::Instant;
8
9pub fn new_fixed_vec<T: Clone>(size: usize, init: T) -> Vec<T> {
11 let mut v = Vec::<T>::with_capacity(size);
12 v.resize(size, init);
13 v
14}
15
16pub struct Counter {
20 count: u64,
21 now: Instant,
22 info_interval: u64,
23}
24impl Counter {
25 pub fn new(info_interval: u64) -> Self {
26 Self {
27 count: 0,
28 now: Instant::now(),
29 info_interval,
30 }
31 }
32
33 pub fn fps_frame_count_info(&mut self) -> Option<String> {
34 self.count += 1;
35 if self.count.is_multiple_of(self.info_interval) {
36 let elapsed = self.now.elapsed().as_secs_f64();
37 self.now = Instant::now();
38 return Some(format!(
39 "{:>9.1} fps {:>9} frames",
40 self.info_interval as f64 / elapsed,
41 self.count
42 ));
43 }
44 None
45 }
46}
47
48const UPSCALE: u32 = 1 << 16; const BIAS: u32 = 1 << 15; pub fn normalize_u16_to_u8_integer_arithmetic(input: &[u8], min: u16, max: u16, norm: &mut [u8]) {
53 let scale = (255 * UPSCALE) / (max - min) as u32;
55
56 for (norm, input) in std::iter::zip(norm, input.chunks_exact(2)) {
57 let value = u16::from_le_bytes([input[0], input[1]]);
58
59 let clamped = (value.clamp(min, max) - min) as u32;
60
61 *norm = ((clamped * scale + BIAS) >> 16) as u8;
63 }
64}