use core::fmt;
#[derive(Clone, PartialEq, Eq)]
pub struct BitMatrix {
width: usize,
height: usize,
bits: Vec<bool>,
pub quiet_zone: usize,
}
impl BitMatrix {
pub fn new(width: usize, height: usize, quiet_zone: usize) -> Self {
BitMatrix {
width,
height,
bits: vec![false; width * height],
quiet_zone,
}
}
pub fn width(&self) -> usize {
self.width
}
pub fn height(&self) -> usize {
self.height
}
pub fn get(&self, x: usize, y: usize) -> bool {
if x >= self.width || y >= self.height {
return false;
}
self.bits[y * self.width + x]
}
pub fn set(&mut self, x: usize, y: usize, dark: bool) {
assert!(
x < self.width && y < self.height,
"BitMatrix::set out of bounds"
);
self.bits[y * self.width + x] = dark;
}
}
impl fmt::Debug for BitMatrix {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
writeln!(
f,
"BitMatrix {}x{} (quiet_zone {}):",
self.width, self.height, self.quiet_zone
)?;
for y in 0..self.height {
for x in 0..self.width {
f.write_str(if self.get(x, y) { "██" } else { " " })?;
}
writeln!(f)?;
}
Ok(())
}
}
#[derive(Clone, PartialEq, Eq)]
pub struct LinearPattern {
pub modules: Vec<bool>,
pub quiet_zone: usize,
}
impl fmt::Debug for LinearPattern {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
"LinearPattern[{} modules, quiet_zone {}] ",
self.modules.len(),
self.quiet_zone
)?;
for &b in &self.modules {
f.write_str(if b { "█" } else { " " })?;
}
Ok(())
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Encoding {
Matrix(BitMatrix),
Linear(LinearPattern),
}