summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorcc <cc@localhost>2025-08-20 20:33:47 -0700
committercc <cc@localhost>2025-08-20 20:52:59 -0700
commit121c681f3f50154ac667cd40c0f04e3b8613d3bc (patch)
treeda1a6097ca5d6c4a8be75761dd3f05d488b497e9
parentbf8287a0a663048aeff2aaf079202b96cc9e0721 (diff)
Add erosion functionalitypush-sspklqul
Added in-place versions Added closeup and fill functionality
-rw-r--r--src/label_formats/label_format.rs111
1 files changed, 110 insertions, 1 deletions
diff --git a/src/label_formats/label_format.rs b/src/label_formats/label_format.rs
index 6407097..b2373f5 100644
--- a/src/label_formats/label_format.rs
+++ b/src/label_formats/label_format.rs
@@ -45,6 +45,11 @@ impl LabelFormat {
}
}
+ 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;
@@ -89,6 +94,84 @@ impl LabelFormat {
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
+ }
}
#[cfg(test)]
@@ -96,7 +179,7 @@ mod tests {
use super::*;
#[test]
- fn dilation_test() {
+ fn label_morphology_test() {
const DIM: usize = 6;
let mut test_data = LabelFormat {
buffer: vec![0u16; DIM*DIM],
@@ -106,9 +189,35 @@ mod tests {
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!();
}
}