From bf8287a0a663048aeff2aaf079202b96cc9e0721 Mon Sep 17 00:00:00 2001 From: cc Date: Wed, 20 Aug 2025 20:31:50 -0700 Subject: Add dilation funcitonality to compressed representation --- src/label_formats/label_format.rs | 109 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 109 insertions(+) (limited to 'src/label_formats/label_format.rs') diff --git a/src/label_formats/label_format.rs b/src/label_formats/label_format.rs index 0f33932..6407097 100644 --- a/src/label_formats/label_format.rs +++ b/src/label_formats/label_format.rs @@ -3,3 +3,112 @@ pub struct LabelFormat { pub width: usize, pub height: usize, } + +impl LabelFormat { + pub fn get_left(&self, x: usize, y: usize) -> Option { + if x > 0 { + return Some(self.buffer[(x-1) + y * self.width]); + } + return None; + } + + pub fn get_right(&self, x: usize, y: usize) -> Option { + if (x+1) < self.width { + return Some(self.buffer[(x+1) + y * self.width]); + } + return None; + } + + pub fn get_up(&self, x: usize, y: usize) -> Option { + if y > 0 { + return Some(self.buffer[x + (y-1) * self.width]); + } + return None; + } + + pub fn get_down(&self, x: usize, y: usize) -> Option { + if (y+1) < self.height { + return Some(self.buffer[x + (y+1) * self.width]); + } + return None; + } + + pub fn display(&self, include_space: bool) { + for y in 0..self.height { + for x in 0..self.width { + print!("{:04X}", self.buffer[x + y*self.width]); + if include_space { + print!(" "); + } + } + println!(); + } + } + + pub fn dilate(&self) -> LabelFormat { + let width = self.width; + let height = self.height; + let mut output_buffer = vec![0u16; width*height]; + for y in 0..height { + for x in 0..width { + let index = x + y * width; + let current_color = self.buffer[index]; + if current_color != 0 { + output_buffer[index] = current_color; + continue; + } + if let Some(color) = self.get_up(x,y) { + if color != 0 { + output_buffer[index] = color; + continue; + } + } + if let Some(color) = self.get_right(x,y) { + if color != 0 { + output_buffer[index] = color; + continue; + } + } + if let Some(color) = self.get_down(x,y) { + if color != 0 { + output_buffer[index] = color; + continue; + } + } + if let Some(color) = self.get_left(x,y) { + if color != 0 { + output_buffer[index] = color; + continue; + } + } + } + } + LabelFormat { + buffer: output_buffer, + width, + height, + } + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn dilation_test() { + const DIM: usize = 6; + let mut test_data = LabelFormat { + buffer: vec![0u16; DIM*DIM], + width: DIM, + height: DIM, + }; + + test_data.buffer[2+3*DIM] = 1; + test_data.buffer[3+3*DIM] = 1; + test_data.display(true); + println!(); + let dilated = test_data.dilate(); + dilated.display(true); + } +} -- cgit v1.2.1