aboutsummaryrefslogtreecommitdiff
path: root/src/label_format.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/label_format.rs')
-rw-r--r--src/label_format.rs90
1 files changed, 90 insertions, 0 deletions
diff --git a/src/label_format.rs b/src/label_format.rs
index afbb2ad..1762445 100644
--- a/src/label_format.rs
+++ b/src/label_format.rs
@@ -1,3 +1,5 @@
+use crate::flood_u16;
+
pub struct LabelFormat {
pub buffer: Vec<u16>,
pub width: usize,
@@ -176,6 +178,54 @@ impl LabelFormat {
pub fn dump(&self, filename: &str) -> Result<(), std::io::Error> {
crate::binfile::dump_u16_vec(filename, self.buffer.clone())
}
+
+ pub fn combine(&self, other: &LabelFormat) -> Option<LabelFormat> {
+ let width = self.width;
+ let height = self.height;
+ let rwidth = other.width;
+ let rheight = other.height;
+ if (width != rwidth) || (height != rheight) {
+ return None;
+ }
+ let mut output = LabelFormat {
+ buffer: vec![0u16; width * height],
+ width,
+ height,
+ };
+ for y in 0..height {
+ for x in 0..width {
+ let index = x + y * width;
+ output.buffer[index] = self.buffer[index];
+ if output.buffer[index] == 0 {
+ output.buffer[index] = other.buffer[index];
+ }
+ }
+ }
+ return Some(output);
+ }
+
+ pub fn refresh(&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_u16(self, &mut output_buffer, x, y, color, label);
+ label += 1;
+ }
+ }
+ }
+ return LabelFormat {
+ buffer: output_buffer,
+ width: self.width,
+ height: self.height,
+ };
+ }
}
#[cfg(test)]
@@ -224,4 +274,44 @@ mod tests {
test_data.display(true);
println!();
}
+
+ #[test]
+ fn combination_test() {
+ const DIM: usize = 6;
+ let mut test_data_1 = LabelFormat {
+ buffer: vec![0u16; DIM*DIM],
+ width: DIM,
+ height: DIM,
+ };
+
+ test_data_1.buffer[2+3*DIM] = 1;
+ test_data_1.buffer[3+3*DIM] = 1;
+ test_data_1.buffer[4+3*DIM] = 2;
+ test_data_1.display(true);
+ println!();
+
+ let mut test_data_2 = LabelFormat {
+ buffer: vec![0u16; DIM*DIM],
+ width: DIM,
+ height: DIM,
+ };
+
+ test_data_2.buffer[2+1*DIM] = 2;
+ test_data_2.buffer[3+1*DIM] = 2;
+ test_data_2.buffer[3+2*DIM] = 2;
+ test_data_2.buffer[4+1*DIM] = 3;
+ test_data_2.display(true);
+ println!();
+
+ if let Some(out_data) = test_data_1.combine(&test_data_2) {
+ out_data.display(true);
+ println!();
+
+ let refreshed = out_data.refresh();
+ refreshed.display(true);
+ println!();
+ } else {
+ assert!(false);
+ }
+ }
}