create grids without copying data

This commit is contained in:
Vinzenz Schroeter 2025-02-08 15:06:22 +01:00
parent 753a66136e
commit 8cb7a9bbfb
4 changed files with 37 additions and 6 deletions

View file

@ -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:

View file

@ -26,7 +26,7 @@ impl BrightnessGrid {
}
impl From<BrightnessGrid> for Vec<u8> {
fn from(value: ValueGrid<Brightness>) -> Self {
fn from(value: BrightnessGrid) -> Self {
value
.iter()
.map(|brightness| (*brightness).into())
@ -35,7 +35,7 @@ impl From<BrightnessGrid> for Vec<u8> {
}
impl From<&BrightnessGrid> for ByteGrid {
fn from(value: &ValueGrid<Brightness>) -> Self {
fn from(value: &BrightnessGrid) -> Self {
let u8s = value
.iter()
.map(|brightness| (*brightness).into())

View file

@ -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)),

View file

@ -79,6 +79,21 @@ impl<T: Value> ValueGrid<T> {
}
}
/// 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<T>) -> 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].