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) } 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 { pub fn set(&mut self, index: usize, value: bool) -> bool {
let (byte_index, bit_mask) = self.get_indexes(index); let (byte_index, bit_mask) = self.get_indexes(index);
@ -30,8 +34,8 @@ impl BitVec {
return self.data[byte_index] & bit_mask != 0; return self.data[byte_index] & bit_mask != 0;
} }
pub fn fill(&mut self, value: bool){ pub fn fill(&mut self, value: bool) {
let byte: u8 = if value {0xFF} else {0x00}; let byte: u8 = if value { 0xFF } else { 0x00 };
self.data.fill(byte); self.data.fill(byte);
} }

View file

@ -22,6 +22,17 @@ impl PixelGrid {
Self::new(PIXEL_WIDTH as usize, PIXEL_HEIGHT as usize) 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 { pub fn set(&mut self, x: usize, y: usize, value: bool) -> bool {
self.bit_vec.set(x + y * self.width, value) self.bit_vec.set(x + y * self.width, value)
} }