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
use std::fmt;
use crate::{common::BitMatrix, Exceptions};
#[derive(Debug, PartialEq, Eq, Clone)]
pub struct ByteMatrix {
bytes: Vec<Vec<u8>>,
width: u32,
height: u32,
}
impl ByteMatrix {
pub fn new(width: u32, height: u32) -> Self {
Self {
bytes: vec![vec![0u8; width as usize]; height as usize],
width,
height,
}
}
pub fn getHeight(&self) -> u32 {
self.height
}
pub fn getWidth(&self) -> u32 {
self.width
}
pub fn get(&self, x: u32, y: u32) -> u8 {
self.bytes[y as usize][x as usize]
}
pub fn getArray(&self) -> &Vec<Vec<u8>> {
&self.bytes
}
pub fn set(&mut self, x: u32, y: u32, value: u8) {
self.bytes[y as usize][x as usize] = value;
}
pub fn set_bool(&mut self, x: u32, y: u32, value: bool) {
self.bytes[y as usize][x as usize] = u8::from(value); }
pub fn clear(&mut self, value: u8) {
for row in self.bytes.iter_mut() {
row.fill(value);
}
}
}
impl fmt::Display for ByteMatrix {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let mut result = String::with_capacity(2 * self.width as usize * self.height as usize + 2);
for y in 0..self.height as usize {
let bytesY = &self.bytes[y];
for byte in bytesY.iter().take(self.width as usize) {
match *byte {
0 => result.push_str(" 0"),
1 => result.push_str(" 1"),
_ => result.push_str(" "),
};
}
result.push('\n');
}
write!(f, "{result}")
}
}
impl TryFrom<ByteMatrix> for BitMatrix {
type Error = Exceptions;
fn try_from(value: ByteMatrix) -> Result<Self, Self::Error> {
let mut bit_matrix = BitMatrix::new(value.getWidth(), value.getHeight())?;
for y in 0..value.getHeight() {
for x in 0..value.getWidth() {
if value.get(x, y) > 0 {
bit_matrix.set(x, y);
}
}
}
Ok(bit_matrix)
}
}