diff options
author | cc <cc@localhost> | 2025-08-20 22:23:34 -0700 |
---|---|---|
committer | cc <cc@localhost> | 2025-08-20 22:23:49 -0700 |
commit | 00e05ac35b85fdf0e7eb5f5db9db4b0a563fa14d (patch) | |
tree | b3b203070d29821e855be4acfe9cfc95c657d002 /src | |
parent | cf4b76823629e10d79a6a848a6ffaa72d494811d (diff) |
Externalize Label Structure
Diffstat (limited to 'src')
-rw-r--r-- | src/binfile.rs | 27 | ||||
-rw-r--r-- | src/label_formats/label_format.rs | 227 | ||||
-rw-r--r-- | src/label_formats/large_label_format.rs | 45 | ||||
-rw-r--r-- | src/label_formats/mod.rs | 33 | ||||
-rw-r--r-- | src/lib.rs | 4 | ||||
-rw-r--r-- | src/tiff.rs | 2 |
6 files changed, 3 insertions, 335 deletions
diff --git a/src/binfile.rs b/src/binfile.rs deleted file mode 100644 index 5b0b783..0000000 --- a/src/binfile.rs +++ /dev/null @@ -1,27 +0,0 @@ -use std::io::{Error,ErrorKind}; - -pub fn dump_u32_vec(file_name: &str, data: Vec<u32>) -> Result<(), Error> { - use std::fs::File; - use std::io::Write; - if let Ok(mut file) = File::create(file_name) { - for value in data { - file.write_all(&value.to_le_bytes())?; - } - } else { - return Err(Error::new(ErrorKind::Other, "Error creating file")); - } - Ok(()) -} - -pub fn dump_u16_vec(file_name: &str, data: Vec<u16>) -> Result<(), Error> { - use std::fs::File; - use std::io::Write; - if let Ok(mut file) = File::create(file_name) { - for value in data { - file.write_all(&value.to_le_bytes())?; - } - } else { - return Err(Error::new(ErrorKind::Other, "Error creating file")); - } - Ok(()) -} diff --git a/src/label_formats/label_format.rs b/src/label_formats/label_format.rs deleted file mode 100644 index afbb2ad..0000000 --- a/src/label_formats/label_format.rs +++ /dev/null @@ -1,227 +0,0 @@ -pub struct LabelFormat { - pub buffer: Vec<u16>, - pub width: usize, - pub height: usize, -} - -impl LabelFormat { - pub fn get_left(&self, x: usize, y: usize) -> Option<u16> { - if x > 0 { - return Some(self.buffer[(x-1) + y * self.width]); - } - return None; - } - - pub fn get_right(&self, x: usize, y: usize) -> Option<u16> { - 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<u16> { - if y > 0 { - return Some(self.buffer[x + (y-1) * self.width]); - } - return None; - } - - pub fn get_down(&self, x: usize, y: usize) -> Option<u16> { - 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 idilate(&mut self) { - let dilated = self.dilate(); - self.buffer = dilated.buffer; - } - - 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, - } - } - - pub fn ierode(&mut self) { - let eroded = self.erode(); - self.buffer = eroded.buffer; - } - - pub fn erode(&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 { - continue; - } - if let Some(color) = self.get_up(x,y) { - if color == 0 { - continue; - } - } - if let Some(color) = self.get_right(x,y) { - if color == 0 { - continue; - } - } - if let Some(color) = self.get_down(x,y) { - if color == 0 { - continue; - } - } - if let Some(color) = self.get_left(x,y) { - if color == 0 { - continue; - } - } - output_buffer[index] = current_color; - } - } - LabelFormat { - buffer: output_buffer, - width, - height, - } - } - - pub fn icloseup(&mut self, n: usize) { - for _ in 0..n { - self.ierode(); - } - for _ in 0..n { - self.idilate(); - } - } - - pub fn closeup(&self, n: usize) -> LabelFormat { - let mut x = self.erode(); - x.icloseup(n-1); - x.idilate(); - x - } - - pub fn ifill(&mut self, n: usize) { - for _ in 0..n { - self.idilate(); - } - for _ in 0..n { - self.ierode(); - } - } - - pub fn fill(&self, n: usize) -> LabelFormat { - let mut x = self.dilate(); - x.icloseup(n-1); - x.ierode(); - x - } - - pub fn dump(&self, filename: &str) -> Result<(), std::io::Error> { - crate::binfile::dump_u16_vec(filename, self.buffer.clone()) - } -} - -#[cfg(test)] -mod tests { - use super::*; - - #[test] - fn label_morphology_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.buffer[4+3*DIM] = 2; - test_data.display(true); - println!(); - let dilated = test_data.dilate(); - dilated.display(true); - println!(); - let restored = dilated.erode(); - restored.display(true); - } - - #[test] - fn mutable_morphology_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.buffer[4+3*DIM] = 2; - test_data.display(true); - println!(); - test_data.idilate(); - test_data.display(true); - println!(); - test_data.ierode(); - test_data.display(true); - println!(); - } -} diff --git a/src/label_formats/large_label_format.rs b/src/label_formats/large_label_format.rs deleted file mode 100644 index a6128c0..0000000 --- a/src/label_formats/large_label_format.rs +++ /dev/null @@ -1,45 +0,0 @@ -use super::{LabelFormat,flood}; - -pub struct LargeLabelFormat { - pub buffer: Vec<u32>, - pub width: usize, - pub height: usize, -} - -impl std::fmt::Debug for LargeLabelFormat { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - f.debug_struct("LargeLabelFormat") - .field("width", &self.width) - .field("height", &self.height) - .finish() - } -} - -impl LargeLabelFormat { - pub fn compress(&self) -> LabelFormat { - let mut label: u16 = 1; - let mut output_buffer: Vec<u16> = vec![0u16; self.buffer.len()]; - for y in 0..self.height { - for x in 0..self.width { - let index = x + y*self.width; - if self.buffer[index] == 0 { - continue; - } - if output_buffer[index] == 0 { - let color = self.buffer[index]; - flood(&self, &mut output_buffer, x, y, color, label); - label += 1; - } - } - } - return LabelFormat { - buffer: output_buffer, - width: self.width, - height: self.height, - }; - } - - pub fn dump(&self, filename: &str) -> Result<(), std::io::Error> { - crate::binfile::dump_u32_vec(filename, self.buffer.clone()) - } -} diff --git a/src/label_formats/mod.rs b/src/label_formats/mod.rs deleted file mode 100644 index b48486a..0000000 --- a/src/label_formats/mod.rs +++ /dev/null @@ -1,33 +0,0 @@ -mod large_label_format; -mod label_format; - -pub use large_label_format::LargeLabelFormat; -pub use label_format::LabelFormat; - -pub(crate) fn flood(source: &LargeLabelFormat, destination: &mut Vec<u16>, - x: usize, y: usize, - from_color: u32, to_color: u16) { - let width = source.width; - let destination_color = destination[x + y * width]; - if destination_color != 0 { - return; - } - let source_color = source.buffer[x + y * width]; - if source_color != from_color { - return; - } - destination[x + y * width] = to_color; - if x > 0 { - flood(source, destination, x-1, y, from_color, to_color); - } - if (x+1) < width { - flood(source, destination, x+1, y, from_color, to_color); - } - if y > 0 { - flood(source, destination, x, y-1, from_color, to_color); - } - if (y+1) < source.height { - flood(source, destination, x, y+1, from_color, to_color); - } -} - @@ -1,6 +1,6 @@ +extern crate label_toolkit; + pub mod tiff; -pub mod binfile; -pub mod label_formats; pub const TEST_IMAGE_PATH: &str = "../test.tif"; pub const TEST_OUTPUT_PATH: &str = "../test.bin"; diff --git a/src/tiff.rs b/src/tiff.rs index d223961..88acbea 100644 --- a/src/tiff.rs +++ b/src/tiff.rs @@ -1,7 +1,7 @@ use std::os::raw::{c_char, c_uint, c_void, c_longlong, c_long}; use std::ffi::CString; -use crate::label_formats::LargeLabelFormat; +use label_toolkit::LargeLabelFormat; /// # TIFF Internal Structure |