pub mod buffer_text;
pub mod color_map;
pub mod touch_detector;
use std::time::Instant;
pub fn new_fixed_vec<T: Clone>(size: usize, init: T) -> Vec<T> {
let mut v = Vec::<T>::with_capacity(size);
v.resize(size, init);
v
}
pub struct Counter {
count: u64,
now: Instant,
info_interval: u64,
}
impl Counter {
pub fn new(info_interval: u64) -> Self {
Self {
count: 0,
now: Instant::now(),
info_interval,
}
}
pub fn fps_frame_count_info(&mut self) -> Option<String> {
self.count += 1;
if self.count.is_multiple_of(self.info_interval) {
let elapsed = self.now.elapsed().as_secs_f64();
self.now = Instant::now();
return Some(format!(
"{:>9.1} fps {:>9} frames",
self.info_interval as f64 / elapsed,
self.count
));
}
None
}
}
const 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]) {
let scale = (255 * UPSCALE) / (max - min) as u32;
for (norm, input) in std::iter::zip(norm, input.chunks_exact(2)) {
let value = u16::from_le_bytes([input[0], input[1]]);
let clamped = (value.clamp(min, max) - min) as u32;
*norm = ((clamped * scale + BIAS) >> 16) as u8;
}
}