2024-05-10 01:35:24 +02:00
|
|
|
use crate::{BitVec, PIXEL_HEIGHT, PIXEL_WIDTH};
|
2024-05-09 23:30:18 +02:00
|
|
|
|
2024-05-12 01:30:55 +02:00
|
|
|
/// A grid of pixels stored in packed bytes.
|
2024-05-17 18:36:18 +02:00
|
|
|
#[derive(Debug, Clone, PartialEq)]
|
2024-05-09 23:30:18 +02:00
|
|
|
pub struct PixelGrid {
|
2024-05-17 18:43:39 +02:00
|
|
|
width: usize,
|
|
|
|
height: usize,
|
2024-05-09 23:30:18 +02:00
|
|
|
bit_vec: BitVec,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl PixelGrid {
|
2024-05-12 01:30:55 +02:00
|
|
|
/// Creates a new pixel grid with the specified dimensions.
|
|
|
|
///
|
|
|
|
/// # Arguments
|
|
|
|
///
|
|
|
|
/// * `width`: size in pixels in x-direction
|
|
|
|
/// * `height`: size in pixels in y-direction
|
|
|
|
///
|
|
|
|
/// returns: PixelGrid initialized to all pixels off
|
|
|
|
///
|
|
|
|
/// # Panics
|
|
|
|
///
|
|
|
|
/// - when the width is not dividable by 8
|
2024-05-10 01:35:24 +02:00
|
|
|
pub fn new(width: usize, height: usize) -> Self {
|
2024-05-09 23:30:18 +02:00
|
|
|
assert_eq!(width % 8, 0);
|
|
|
|
Self {
|
|
|
|
width,
|
|
|
|
height,
|
|
|
|
bit_vec: BitVec::new(width * height),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-12 01:30:55 +02:00
|
|
|
/// Creates a new pixel grid with the size of the whole screen.
|
2024-05-10 01:35:24 +02:00
|
|
|
pub fn max_sized() -> Self {
|
|
|
|
Self::new(PIXEL_WIDTH as usize, PIXEL_HEIGHT as usize)
|
|
|
|
}
|
|
|
|
|
2024-05-12 01:30:55 +02:00
|
|
|
/// Loads a pixel grid with the specified dimensions from the provided data.
|
|
|
|
///
|
|
|
|
/// # Arguments
|
|
|
|
///
|
|
|
|
/// * `width`: size in pixels in x-direction
|
|
|
|
/// * `height`: size in pixels in y-direction
|
|
|
|
///
|
|
|
|
/// returns: PixelGrid that contains a copy of the provided data
|
|
|
|
///
|
|
|
|
/// # Panics
|
|
|
|
///
|
|
|
|
/// - when the dimensions and data size do not match exactly.
|
|
|
|
/// - when the width is not dividable by 8
|
2024-05-10 16:14:53 +02:00
|
|
|
pub fn load(width: usize, height: usize, data: &[u8]) -> Self {
|
|
|
|
assert_eq!(width % 8, 0);
|
|
|
|
assert_eq!(data.len(), height * width / 8);
|
|
|
|
Self {
|
|
|
|
width,
|
|
|
|
height,
|
2024-05-12 01:30:55 +02:00
|
|
|
bit_vec: BitVec::from(data),
|
2024-05-10 16:14:53 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-12 01:30:55 +02:00
|
|
|
/// Sets the byte value at the specified position
|
2024-05-09 23:30:18 +02:00
|
|
|
pub fn set(&mut self, x: usize, y: usize, value: bool) -> bool {
|
|
|
|
self.bit_vec.set(x + y * self.width, value)
|
|
|
|
}
|
|
|
|
|
2024-05-12 01:30:55 +02:00
|
|
|
/// Get the current value at the specified position
|
|
|
|
///
|
|
|
|
/// returns: current pixel value
|
2024-05-09 23:30:18 +02:00
|
|
|
pub fn get(&self, x: usize, y: usize) -> bool {
|
|
|
|
self.bit_vec.get(x + y * self.width)
|
|
|
|
}
|
2024-05-10 12:24:07 +02:00
|
|
|
|
2024-05-12 01:30:55 +02:00
|
|
|
/// Sets all pixels in the grid to the specified value
|
2024-05-10 12:24:07 +02:00
|
|
|
pub fn fill(&mut self, value: bool) {
|
|
|
|
self.bit_vec.fill(value);
|
|
|
|
}
|
2024-05-13 01:26:44 +02:00
|
|
|
|
2024-05-15 20:34:51 +02:00
|
|
|
pub fn mut_data_ref(&mut self) -> &mut [u8] {
|
|
|
|
self.bit_vec.mut_data_ref()
|
2024-05-13 01:26:44 +02:00
|
|
|
}
|
2024-05-17 18:43:39 +02:00
|
|
|
|
|
|
|
/// the size in x-direction in pixels
|
|
|
|
pub fn width(&self) -> usize {
|
|
|
|
self.width
|
|
|
|
}
|
|
|
|
|
|
|
|
/// the height in y-direction in pixels
|
|
|
|
pub fn height(&self) -> usize {
|
|
|
|
self.height
|
|
|
|
}
|
2024-05-09 23:30:18 +02:00
|
|
|
}
|
|
|
|
|
2024-05-15 20:34:51 +02:00
|
|
|
impl From<PixelGrid> for Vec<u8> {
|
|
|
|
/// Turns a `PixelGrid` into the underlying `Vec<u8>`.
|
|
|
|
fn from(value: PixelGrid) -> Self {
|
|
|
|
value.bit_vec.into()
|
2024-05-09 23:30:18 +02:00
|
|
|
}
|
|
|
|
}
|
2024-05-12 17:15:30 +02:00
|
|
|
|
2024-05-16 23:03:39 +02:00
|
|
|
#[cfg(feature = "c_api")]
|
2024-05-16 23:18:43 +02:00
|
|
|
pub mod c_api {
|
2024-05-15 20:34:51 +02:00
|
|
|
use crate::c_slice::CByteSlice;
|
2024-05-12 17:15:30 +02:00
|
|
|
use crate::PixelGrid;
|
|
|
|
|
|
|
|
/// Creates a new `PixelGrid` instance.
|
|
|
|
/// The returned instance has to be freed with `pixel_grid_dealloc`.
|
2024-05-12 18:28:53 +02:00
|
|
|
#[no_mangle]
|
2024-05-16 23:18:43 +02:00
|
|
|
pub unsafe extern "C" fn sp2_pixel_grid_new(
|
|
|
|
width: usize,
|
|
|
|
height: usize,
|
|
|
|
) -> *mut PixelGrid {
|
2024-05-12 17:15:30 +02:00
|
|
|
Box::into_raw(Box::new(PixelGrid::new(width, height)))
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Loads a `PixelGrid` with the specified dimensions from the provided data.
|
|
|
|
/// The returned instance has to be freed with `pixel_grid_dealloc`.
|
2024-05-12 18:28:53 +02:00
|
|
|
#[no_mangle]
|
2024-05-16 23:18:43 +02:00
|
|
|
pub unsafe extern "C" fn sp2_pixel_grid_load(
|
|
|
|
width: usize,
|
|
|
|
height: usize,
|
|
|
|
data: *const u8,
|
|
|
|
data_length: usize,
|
|
|
|
) -> *mut PixelGrid {
|
2024-05-12 17:15:30 +02:00
|
|
|
let data = std::slice::from_raw_parts(data, data_length);
|
|
|
|
Box::into_raw(Box::new(PixelGrid::load(width, height, data)))
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Clones a `PixelGrid`.
|
|
|
|
/// The returned instance has to be freed with `pixel_grid_dealloc`.
|
2024-05-12 18:28:53 +02:00
|
|
|
#[no_mangle]
|
2024-05-16 23:18:43 +02:00
|
|
|
pub unsafe extern "C" fn sp2_pixel_grid_clone(
|
|
|
|
this: *const PixelGrid,
|
|
|
|
) -> *mut PixelGrid {
|
2024-05-12 17:15:30 +02:00
|
|
|
Box::into_raw(Box::new((*this).clone()))
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Deallocates a `PixelGrid`.
|
|
|
|
///
|
|
|
|
/// Note: do not call this if the grid has been consumed in another way, e.g. to create a command.
|
2024-05-12 18:28:53 +02:00
|
|
|
#[no_mangle]
|
|
|
|
pub unsafe extern "C" fn sp2_pixel_grid_dealloc(this: *mut PixelGrid) {
|
2024-05-12 17:15:30 +02:00
|
|
|
_ = Box::from_raw(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Get the current value at the specified position
|
2024-05-12 18:28:53 +02:00
|
|
|
#[no_mangle]
|
2024-05-16 23:18:43 +02:00
|
|
|
pub unsafe extern "C" fn sp2_pixel_grid_get(
|
|
|
|
this: *const PixelGrid,
|
|
|
|
x: usize,
|
|
|
|
y: usize,
|
|
|
|
) -> bool {
|
2024-05-12 17:15:30 +02:00
|
|
|
(*this).get(x, y)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Sets the current value at the specified position
|
2024-05-12 18:28:53 +02:00
|
|
|
#[no_mangle]
|
2024-05-16 23:18:43 +02:00
|
|
|
pub unsafe extern "C" fn sp2_pixel_grid_set(
|
|
|
|
this: *mut PixelGrid,
|
|
|
|
x: usize,
|
|
|
|
y: usize,
|
|
|
|
value: bool,
|
|
|
|
) {
|
2024-05-12 17:15:30 +02:00
|
|
|
(*this).set(x, y, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Fills the whole `PixelGrid` with the specified value
|
2024-05-12 18:28:53 +02:00
|
|
|
#[no_mangle]
|
2024-05-16 23:18:43 +02:00
|
|
|
pub unsafe extern "C" fn sp2_pixel_grid_fill(
|
|
|
|
this: *mut PixelGrid,
|
|
|
|
value: bool,
|
|
|
|
) {
|
2024-05-12 17:15:30 +02:00
|
|
|
(*this).fill(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Gets the width in pixels of the `PixelGrid` instance.
|
2024-05-12 18:28:53 +02:00
|
|
|
#[no_mangle]
|
2024-05-16 23:18:43 +02:00
|
|
|
pub unsafe extern "C" fn sp2_pixel_grid_width(
|
|
|
|
this: *const PixelGrid,
|
|
|
|
) -> usize {
|
2024-05-12 17:15:30 +02:00
|
|
|
(*this).width
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Gets the height in pixels of the `PixelGrid` instance.
|
2024-05-12 18:28:53 +02:00
|
|
|
#[no_mangle]
|
2024-05-16 23:18:43 +02:00
|
|
|
pub unsafe extern "C" fn sp2_pixel_grid_height(
|
|
|
|
this: *const PixelGrid,
|
|
|
|
) -> usize {
|
2024-05-12 17:15:30 +02:00
|
|
|
(*this).height
|
|
|
|
}
|
2024-05-13 01:26:44 +02:00
|
|
|
|
2024-05-15 20:34:51 +02:00
|
|
|
/// Gets an unsafe reference to the data of the `PixelGrid` instance.
|
|
|
|
///
|
|
|
|
/// ## Safety
|
|
|
|
///
|
|
|
|
/// The caller has to make sure to never access the returned memory after the `PixelGrid`
|
|
|
|
/// instance has been consumed or manually deallocated.
|
|
|
|
///
|
|
|
|
/// Reading and writing concurrently to either the original instance or the returned data will
|
|
|
|
/// result in undefined behavior.
|
2024-05-13 01:26:44 +02:00
|
|
|
#[no_mangle]
|
2024-05-16 23:18:43 +02:00
|
|
|
pub unsafe extern "C" fn sp2_pixel_grid_unsafe_data_ref(
|
|
|
|
this: *mut PixelGrid,
|
|
|
|
) -> CByteSlice {
|
2024-05-15 20:34:51 +02:00
|
|
|
let data = (*this).mut_data_ref();
|
|
|
|
CByteSlice {
|
|
|
|
start: data.as_mut_ptr_range().start,
|
|
|
|
length: data.len(),
|
|
|
|
}
|
2024-05-13 01:26:44 +02:00
|
|
|
}
|
2024-05-12 17:15:30 +02:00
|
|
|
}
|
2024-05-17 21:23:56 +02:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use crate::PixelGrid;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn fill() {
|
|
|
|
let mut grid = PixelGrid::new(8, 2);
|
|
|
|
assert_eq!(grid.mut_data_ref(), [0x00, 0x00]);
|
|
|
|
|
|
|
|
grid.fill(true);
|
|
|
|
assert_eq!(grid.mut_data_ref(), [0xFF, 0xFF]);
|
|
|
|
|
|
|
|
grid.fill(false);
|
|
|
|
assert_eq!(grid.mut_data_ref(), [0x00, 0x00]);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn get_set() {
|
|
|
|
let mut grid = PixelGrid::new(8, 2);
|
|
|
|
assert_eq!(grid.get(0, 0), false);
|
|
|
|
assert_eq!(grid.get(1, 1), false);
|
|
|
|
|
|
|
|
grid.set(5, 0, true);
|
|
|
|
grid.set(1, 1, true);
|
|
|
|
assert_eq!(grid.mut_data_ref(), [0x04, 0x40]);
|
|
|
|
|
|
|
|
assert_eq!(grid.get(5, 0), true);
|
|
|
|
assert_eq!(grid.get(1, 1), true);
|
|
|
|
assert_eq!(grid.get(1, 0), false);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn load() {
|
|
|
|
let mut grid = PixelGrid::new(8, 3);
|
|
|
|
for x in 0..grid.width {
|
|
|
|
for y in 0..grid.height {
|
|
|
|
grid.set(x, y, (x + y) % 2 == 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
assert_eq!(grid.mut_data_ref(), [0xAA, 0x55, 0xAA]);
|
|
|
|
|
|
|
|
let data: Vec<u8> = grid.into();
|
|
|
|
|
|
|
|
let mut grid = PixelGrid::load(8, 3, &data);
|
|
|
|
assert_eq!(grid.mut_data_ref(), [0xAA, 0x55, 0xAA]);
|
|
|
|
}
|
|
|
|
}
|