add ability to load PixelGrid and BitVec

This commit is contained in:
Vinzenz Schroeter 2024-05-10 16:14:53 +02:00
parent e6f8afb378
commit a23ca55f60
2 changed files with 17 additions and 2 deletions

View file

@ -10,6 +10,10 @@ impl BitVec {
Self { data: vec!(0; size / 8) }
}
pub fn load(data: &[u8]) -> BitVec {
Self { data: Vec::from(data) }
}
pub fn set(&mut self, index: usize, value: bool) -> bool {
let (byte_index, bit_mask) = self.get_indexes(index);

View file

@ -22,6 +22,17 @@ impl PixelGrid {
Self::new(PIXEL_WIDTH as usize, PIXEL_HEIGHT as usize)
}
pub fn load(width: usize, height: usize, data: &[u8]) -> Self {
assert_eq!(width % 8, 0);
assert_eq!(height % 8, 0);
assert_eq!(data.len(), height * width / 8);
Self {
width,
height,
bit_vec: BitVec::load(data),
}
}
pub fn set(&mut self, x: usize, y: usize, value: bool) -> bool {
self.bit_vec.set(x + y * self.width, value)
}