#[derive(Debug, Clone)]
pub struct Bitmap<T, const N: usize> {
pub width: usize,
pub height: usize,
data: Vec<T>,
}
impl<T: Copy + Default, const N: usize> Bitmap<T, N> {
pub fn new(width: usize, height: usize) -> Self {
Bitmap {
width,
height,
data: vec![T::default(); width * height * N],
}
}
}
impl<T, const N: usize> Bitmap<T, N> {
#[inline]
fn offset(&self, x: usize, y: usize) -> usize {
N * (self.width * y + x)
}
#[inline]
pub fn pixel(&self, x: usize, y: usize) -> &[T] {
let o = self.offset(x, y);
&self.data[o..o + N]
}
#[inline]
pub fn pixel_mut(&mut self, x: usize, y: usize) -> &mut [T] {
let o = self.offset(x, y);
&mut self.data[o..o + N]
}
#[inline]
pub fn data(&self) -> &[T] {
&self.data
}
#[inline]
pub fn data_mut(&mut self) -> &mut [T] {
&mut self.data
}
pub fn rows_mut(&mut self) -> impl Iterator<Item = (usize, &mut [T])> {
let stride = self.width * N;
self.data.chunks_mut(stride).enumerate()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn alloc_and_access() {
let mut b: Bitmap<f32, 3> = Bitmap::new(4, 2);
assert_eq!(b.data().len(), 4 * 2 * 3);
b.pixel_mut(1, 1).copy_from_slice(&[0.1, 0.2, 0.3]);
assert_eq!(b.pixel(1, 1), &[0.1, 0.2, 0.3]);
}
}