diff --git a/crates/servicepoint/src/bitmap.rs b/crates/servicepoint/src/bitmap.rs index 079410f..afa42f2 100644 --- a/crates/servicepoint/src/bitmap.rs +++ b/crates/servicepoint/src/bitmap.rs @@ -72,8 +72,8 @@ impl Bitmap { /// - when the width is not dividable by 8 #[must_use] pub fn load(width: usize, height: usize, data: &[u8]) -> Self { - assert_eq!(width % 8, 0); - assert_eq!(data.len(), height * width / 8); + assert_eq!(width % 8, 0, "width must be a multiple of 8, but is {width}"); + assert_eq!(data.len(), height * width / 8, "data length must match dimensions, with 8 pixels per byte."); Self { width, height, @@ -81,6 +81,23 @@ impl Bitmap { } } + /// Creates a [Bitmap] with the specified width from the provided [BitVec] without copying it. + /// + /// returns: [Bitmap] that contains the provided data. + /// + /// # Panics + /// + /// - when the bitvec size is not dividable by the provided width + /// - when the width is not dividable by 8 + #[must_use] + pub fn from_bitvec(width: usize, bit_vec: BitVec) -> Self { + assert_eq!(width % 8, 0, "width must be a multiple of 8, but is {width}"); + let len = bit_vec.len(); + let height = len / width; + assert_eq!(0, len % width, "dimension mismatch - len {len} is not dividable by {width}"); + Self { width, height, bit_vec } + } + /// Iterate over all cells in [Bitmap]. /// /// Order is equivalent to the following loop: diff --git a/crates/servicepoint/src/brightness_grid.rs b/crates/servicepoint/src/brightness_grid.rs index 122df35..5727e39 100644 --- a/crates/servicepoint/src/brightness_grid.rs +++ b/crates/servicepoint/src/brightness_grid.rs @@ -26,7 +26,7 @@ impl BrightnessGrid { } impl From for Vec { - fn from(value: ValueGrid) -> Self { + fn from(value: BrightnessGrid) -> Self { value .iter() .map(|brightness| (*brightness).into()) @@ -35,7 +35,7 @@ impl From for Vec { } impl From<&BrightnessGrid> for ByteGrid { - fn from(value: &ValueGrid) -> Self { + fn from(value: &BrightnessGrid) -> Self { let u8s = value .iter() .map(|brightness| (*brightness).into()) diff --git a/crates/servicepoint/src/command.rs b/crates/servicepoint/src/command.rs index 397ada6..4efda1f 100644 --- a/crates/servicepoint/src/command.rs +++ b/crates/servicepoint/src/command.rs @@ -1,6 +1,5 @@ use crate::command_code::CommandCode; use crate::compression::into_decompressed; -use crate::value_grid::ValueGrid; use crate::*; /// Type alias for documenting the meaning of the u16 in enum values @@ -441,7 +440,7 @@ impl Command { payload, } = packet; - let grid = ValueGrid::load(*width as usize, *height as usize, payload); + let grid = ByteGrid::load(*width as usize, *height as usize, payload); let grid = match BrightnessGrid::try_from(grid) { Ok(grid) => grid, Err(val) => return Err(TryFromPacketError::InvalidBrightness(val)), diff --git a/crates/servicepoint/src/value_grid.rs b/crates/servicepoint/src/value_grid.rs index a3400ae..5c39f94 100644 --- a/crates/servicepoint/src/value_grid.rs +++ b/crates/servicepoint/src/value_grid.rs @@ -79,6 +79,21 @@ impl ValueGrid { } } + /// Creates a [ValueGrid] with the specified width from the provided data without copying it. + /// + /// returns: [ValueGrid] that contains the provided data. + /// + /// # Panics + /// + /// - when the data size is not dividable by the width. + #[must_use] + pub fn from_vec(width: usize, data: Vec) -> Self { + let len = data.len(); + let height = len / width; + assert_eq!(0, len % width, "dimension mismatch - len {len} is not dividable by {width}"); + Self { data, width, height } + } + /// Loads a [ValueGrid] with the specified width from the provided data, wrapping to as many rows as needed. /// /// returns: [ValueGrid] that contains a copy of the provided data or [TryLoadValueGridError].