summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorcc <cc@localhost>2025-08-20 20:31:50 -0700
committercc <cc@localhost>2025-08-20 20:31:50 -0700
commitbf8287a0a663048aeff2aaf079202b96cc9e0721 (patch)
tree4762c061784a7c9affa198554df54d76f469cfcf
parentd459729f67e4a83ce361dcf2bc6d5d75f427e23d (diff)
Add dilation funcitonality to compressed representation
-rw-r--r--src/label_formats/label_format.rs109
-rw-r--r--src/label_formats/large_label_format.rs38
-rw-r--r--src/label_formats/mod.rs1
-rw-r--r--src/lib.rs10
4 files changed, 134 insertions, 24 deletions
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<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 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);
+ }
+}
diff --git a/src/label_formats/large_label_format.rs b/src/label_formats/large_label_format.rs
index f717ac5..244c7d3 100644
--- a/src/label_formats/large_label_format.rs
+++ b/src/label_formats/large_label_format.rs
@@ -42,25 +42,27 @@ fn flood(source: &LargeLabelFormat, destination: &mut Vec<u16>,
}
}
-pub fn compress_large_labels(largelabels: LargeLabelFormat) -> LabelFormat {
- let mut label: u16 = 1;
- let mut output_buffer: Vec<u16> = vec![0u16; largelabels.buffer.len()];
- for y in 0..largelabels.height {
- for x in 0..largelabels.width {
- let index = x + y*largelabels.width;
- if largelabels.buffer[index] == 0 {
- continue;
- }
- if output_buffer[index] == 0 {
- let color = largelabels.buffer[index];
- flood(&largelabels, &mut output_buffer, x, y, color, label);
- label += 1;
+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,
+ };
}
- return LabelFormat {
- buffer: output_buffer,
- width: largelabels.width,
- height: largelabels.height,
- };
}
diff --git a/src/label_formats/mod.rs b/src/label_formats/mod.rs
index 7386df8..1e3e4ae 100644
--- a/src/label_formats/mod.rs
+++ b/src/label_formats/mod.rs
@@ -2,5 +2,4 @@ mod large_label_format;
mod label_format;
pub use large_label_format::LargeLabelFormat;
-pub use large_label_format::compress_large_labels;
pub use label_format::LabelFormat;
diff --git a/src/lib.rs b/src/lib.rs
index 3af14d1..c246ed2 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -81,9 +81,9 @@ mod tests {
#[test]
fn tiff_data_dump_test() {
tiff::ignore_warnings();
- if let Some(standard_format) = tiff::to_large_labels(TEST_IMAGE_PATH) {
- println!("{:?}", standard_format);
- let data = standard_format.buffer;
+ if let Some(large_label_format) = tiff::to_large_labels(TEST_IMAGE_PATH) {
+ println!("{:?}", large_label_format);
+ let data = large_label_format.buffer;
binfile::dump_u32_vec(TEST_OUTPUT_PATH, data).expect("File output error");
} else {
assert!(false);
@@ -93,8 +93,8 @@ mod tests {
#[test]
fn tiff_compressed_data_dump_test() {
tiff::ignore_warnings();
- if let Some(standard_format) = tiff::to_large_labels(TEST_IMAGE_PATH) {
- let compressed_format = label_formats::compress_large_labels(standard_format);
+ if let Some(large_label_format) = tiff::to_large_labels(TEST_IMAGE_PATH) {
+ let compressed_format = large_label_format.compress();
let data = compressed_format.buffer;
binfile::dump_u16_vec(TEST_LABEL_OUTPUT_PATH, data).expect("File output error");
} else {