From abdbc42639d0acd6fc0db381f7cbe6a37928643f Mon Sep 17 00:00:00 2001 From: cc Date: Wed, 20 Aug 2025 20:09:34 -0700 Subject: Modularize Label Formats --- src/label_formats/label_format.rs | 5 +++ src/label_formats/large_label_format.rs | 66 +++++++++++++++++++++++++++++++++ src/label_formats/mod.rs | 6 +++ 3 files changed, 77 insertions(+) create mode 100644 src/label_formats/label_format.rs create mode 100644 src/label_formats/large_label_format.rs create mode 100644 src/label_formats/mod.rs (limited to 'src/label_formats') diff --git a/src/label_formats/label_format.rs b/src/label_formats/label_format.rs new file mode 100644 index 0000000..0f33932 --- /dev/null +++ b/src/label_formats/label_format.rs @@ -0,0 +1,5 @@ +pub struct LabelFormat { + pub buffer: Vec, + pub width: usize, + pub height: usize, +} diff --git a/src/label_formats/large_label_format.rs b/src/label_formats/large_label_format.rs new file mode 100644 index 0000000..121a4df --- /dev/null +++ b/src/label_formats/large_label_format.rs @@ -0,0 +1,66 @@ +use super::LabelFormat; + +pub struct LargeLabelFormat { + pub buffer: Vec, + 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() + } +} + +fn flood(source: &LargeLabelFormat, destination: &mut Vec, + 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); + } +} + +pub fn compress_large_labels(buffer: LargeLabelFormat) -> LabelFormat { + let mut label: u16 = 1; + let mut output_buffer: Vec = vec![0u16; buffer.buffer.len()]; + for y in 0..buffer.height { + for x in 0..buffer.width { + let index = x + y*buffer.width; + if buffer.buffer[index] == 0 { + continue; + } + if output_buffer[index] == 0 { + let color = buffer.buffer[index]; + flood(&buffer, &mut output_buffer, x, y, color, label); + label += 1; + } + } + } + return LabelFormat { + buffer: output_buffer, + width: buffer.width, + height: buffer.height, + }; +} diff --git a/src/label_formats/mod.rs b/src/label_formats/mod.rs new file mode 100644 index 0000000..7386df8 --- /dev/null +++ b/src/label_formats/mod.rs @@ -0,0 +1,6 @@ +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; -- cgit v1.2.1