diff --git a/src/bit_vec.rs b/src/bit_vec.rs index 733f239..68ee3b3 100644 --- a/src/bit_vec.rs +++ b/src/bit_vec.rs @@ -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); @@ -30,8 +34,8 @@ impl BitVec { return self.data[byte_index] & bit_mask != 0; } - pub fn fill(&mut self, value: bool){ - let byte: u8 = if value {0xFF} else {0x00}; + pub fn fill(&mut self, value: bool) { + let byte: u8 = if value { 0xFF } else { 0x00 }; self.data.fill(byte); } diff --git a/src/pixel_grid.rs b/src/pixel_grid.rs index 90806c0..8138595 100644 --- a/src/pixel_grid.rs +++ b/src/pixel_grid.rs @@ -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) }