2024-05-11 14:41:09 +02:00
|
|
|
#[derive(Debug, Clone)]
|
2024-05-11 12:43:17 +02:00
|
|
|
pub struct ByteGrid {
|
|
|
|
pub width: usize,
|
|
|
|
pub height: usize,
|
|
|
|
data: Vec<u8>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ByteGrid {
|
|
|
|
pub fn new(width: usize, height: usize) -> Self {
|
|
|
|
Self {
|
|
|
|
data: vec![0; width * height],
|
|
|
|
width,
|
|
|
|
height,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn load(width: usize, height: usize, data: &[u8]) -> Self {
|
|
|
|
assert_eq!(width * height, data.len());
|
|
|
|
Self {
|
|
|
|
data: Vec::from(data),
|
|
|
|
width,
|
|
|
|
height,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get(&self, x: usize, y: usize) -> u8 {
|
|
|
|
self.data[x + y * self.width]
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn set(&mut self, x: usize, y: usize, value: u8) {
|
|
|
|
self.data[x + y * self.width] = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn fill(&mut self, value: u8){
|
|
|
|
self.data.fill(value)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Into<Vec<u8>> for ByteGrid {
|
|
|
|
fn into(self) -> Vec<u8> {
|
|
|
|
self.data
|
|
|
|
}
|
|
|
|
}
|