aboutsummaryrefslogtreecommitdiff
path: root/src/large_label_format.rs
blob: 0a5c458508d70cfd2c014ae12afdc4e2255c2b39 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
use crate::{LabelFormat,flood_u32};

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_u32(&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())
    }
}