summaryrefslogtreecommitdiff
path: root/src/label_formats/label_format.rs
blob: afbb2ad37313fe12ffb1cf9a04bff2fac928e617 (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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
pub struct LabelFormat {
    pub buffer: Vec<u16>,
    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 idilate(&mut self) {
        let dilated = self.dilate();
        self.buffer = dilated.buffer;
    }

    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,
        }
    }

    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
    }

    pub fn dump(&self, filename: &str) -> Result<(), std::io::Error> {
        crate::binfile::dump_u16_vec(filename, self.buffer.clone())
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn label_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!();
        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!();
    }
}