2024-05-15 20:34:51 +02:00
|
|
|
/// A 2D grid of bytes
|
2024-05-11 14:41:09 +02:00
|
|
|
#[derive(Debug, Clone)]
|
2024-05-11 12:43:17 +02:00
|
|
|
pub struct ByteGrid {
|
2024-05-15 20:34:51 +02:00
|
|
|
/// Size in the x-axis
|
2024-05-11 12:43:17 +02:00
|
|
|
pub width: usize,
|
2024-05-15 20:34:51 +02:00
|
|
|
/// Size in the y-axis
|
2024-05-11 12:43:17 +02:00
|
|
|
pub height: usize,
|
|
|
|
data: Vec<u8>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ByteGrid {
|
2024-05-12 01:30:55 +02:00
|
|
|
/// Creates a new byte grid with the specified dimensions.
|
|
|
|
///
|
|
|
|
/// returns: ByteGrid initialized to 0.
|
2024-05-11 12:43:17 +02:00
|
|
|
pub fn new(width: usize, height: usize) -> Self {
|
|
|
|
Self {
|
|
|
|
data: vec![0; width * height],
|
|
|
|
width,
|
|
|
|
height,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-12 01:30:55 +02:00
|
|
|
/// Loads a byte grid with the specified dimensions from the provided data.
|
|
|
|
///
|
|
|
|
/// returns: ByteGrid that contains a copy of the provided data
|
|
|
|
///
|
|
|
|
/// # Panics
|
|
|
|
///
|
|
|
|
/// - when the dimensions and data size do not match exactly.
|
2024-05-11 12:43:17 +02:00
|
|
|
pub fn load(width: usize, height: usize, data: &[u8]) -> Self {
|
|
|
|
assert_eq!(width * height, data.len());
|
|
|
|
Self {
|
|
|
|
data: Vec::from(data),
|
|
|
|
width,
|
|
|
|
height,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-12 01:30:55 +02:00
|
|
|
/// Get the current value at the specified position
|
|
|
|
///
|
|
|
|
/// returns: current byte value
|
2024-05-11 12:43:17 +02:00
|
|
|
pub fn get(&self, x: usize, y: usize) -> u8 {
|
|
|
|
self.data[x + y * self.width]
|
|
|
|
}
|
|
|
|
|
2024-05-12 01:30:55 +02:00
|
|
|
/// Sets the byte value at the specified position
|
2024-05-11 12:43:17 +02:00
|
|
|
pub fn set(&mut self, x: usize, y: usize, value: u8) {
|
|
|
|
self.data[x + y * self.width] = value;
|
|
|
|
}
|
|
|
|
|
2024-05-12 01:30:55 +02:00
|
|
|
/// Sets all bytes in the grid to the specified value
|
2024-05-11 23:28:08 +02:00
|
|
|
pub fn fill(&mut self, value: u8) {
|
2024-05-11 12:43:17 +02:00
|
|
|
self.data.fill(value)
|
|
|
|
}
|
2024-05-15 20:34:51 +02:00
|
|
|
|
|
|
|
/// Get the underlying byte rows
|
|
|
|
pub fn mut_data_ref(&mut self) -> &mut [u8] {
|
|
|
|
self.data.as_mut_slice()
|
|
|
|
}
|
2024-05-11 12:43:17 +02:00
|
|
|
}
|
|
|
|
|
2024-05-15 20:34:51 +02:00
|
|
|
impl From<ByteGrid> for Vec<u8> {
|
|
|
|
/// Turn into the underlying `Vec<u8>` containing the rows of bytes.
|
|
|
|
fn from(value: ByteGrid) -> Self {
|
|
|
|
value.data
|
2024-05-11 12:43:17 +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::{ByteGrid, CByteSlice};
|
2024-05-12 17:15:30 +02:00
|
|
|
|
|
|
|
/// Creates a new `ByteGrid` instance.
|
|
|
|
/// The returned instance has to be freed with `byte_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_byte_grid_new(
|
|
|
|
width: usize,
|
|
|
|
height: usize,
|
|
|
|
) -> *mut ByteGrid {
|
2024-05-12 17:15:30 +02:00
|
|
|
Box::into_raw(Box::new(ByteGrid::new(width, height)))
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Loads a `ByteGrid` with the specified dimensions from the provided data.
|
|
|
|
/// The returned instance has to be freed with `byte_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_byte_grid_load(
|
|
|
|
width: usize,
|
|
|
|
height: usize,
|
|
|
|
data: *const u8,
|
|
|
|
data_length: usize,
|
|
|
|
) -> *mut ByteGrid {
|
2024-05-12 17:15:30 +02:00
|
|
|
let data = std::slice::from_raw_parts(data, data_length);
|
|
|
|
Box::into_raw(Box::new(ByteGrid::load(width, height, data)))
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Clones a `ByteGrid`.
|
|
|
|
/// The returned instance has to be freed with `byte_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_byte_grid_clone(
|
|
|
|
this: *const ByteGrid,
|
|
|
|
) -> *mut ByteGrid {
|
2024-05-12 17:15:30 +02:00
|
|
|
Box::into_raw(Box::new((*this).clone()))
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Deallocates a `ByteGrid`.
|
|
|
|
///
|
|
|
|
/// 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_byte_grid_dealloc(this: *mut ByteGrid) {
|
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_byte_grid_get(
|
|
|
|
this: *const ByteGrid,
|
|
|
|
x: usize,
|
|
|
|
y: usize,
|
|
|
|
) -> u8 {
|
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_byte_grid_set(
|
|
|
|
this: *mut ByteGrid,
|
|
|
|
x: usize,
|
|
|
|
y: usize,
|
|
|
|
value: u8,
|
|
|
|
) {
|
2024-05-12 17:15:30 +02:00
|
|
|
(*this).set(x, y, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Fills the whole `ByteGrid` 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_byte_grid_fill(
|
|
|
|
this: *mut ByteGrid,
|
|
|
|
value: u8,
|
|
|
|
) {
|
2024-05-12 17:15:30 +02:00
|
|
|
(*this).fill(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Gets the width in pixels of the `ByteGrid` 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_byte_grid_width(
|
|
|
|
this: *const ByteGrid,
|
|
|
|
) -> usize {
|
2024-05-12 17:15:30 +02:00
|
|
|
(*this).width
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Gets the height in pixels of the `ByteGrid` 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_byte_grid_height(
|
|
|
|
this: *const ByteGrid,
|
|
|
|
) -> usize {
|
2024-05-12 17:15:30 +02:00
|
|
|
(*this).height
|
|
|
|
}
|
2024-05-15 20:34:51 +02:00
|
|
|
|
|
|
|
/// Gets an unsafe reference to the data of the `ByteGrid` instance.
|
|
|
|
///
|
|
|
|
/// ## Safety
|
|
|
|
///
|
|
|
|
/// The caller has to make sure to never access the returned memory after the `ByteGrid`
|
|
|
|
/// 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.
|
|
|
|
#[no_mangle]
|
2024-05-16 23:18:43 +02:00
|
|
|
pub unsafe extern "C" fn sp2_byte_grid_unsafe_data_ref(
|
|
|
|
this: *mut ByteGrid,
|
|
|
|
) -> 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-16 23:18:43 +02:00
|
|
|
}
|
2024-05-17 17:55:29 +02:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use crate::ByteGrid;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn fill() {
|
|
|
|
let mut grid = ByteGrid::new(2, 2);
|
|
|
|
assert_eq!(grid.data, [0x00, 0x00, 0x00, 0x00]);
|
|
|
|
|
|
|
|
grid.fill(42);
|
|
|
|
assert_eq!(grid.data, [42; 4]);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn get_set() {
|
|
|
|
let mut grid = ByteGrid::new(2, 2);
|
|
|
|
assert_eq!(grid.get(0, 0), 0);
|
|
|
|
assert_eq!(grid.get(1, 1), 0);
|
|
|
|
|
|
|
|
grid.set(0, 0, 42);
|
|
|
|
grid.set(1, 0, 23);
|
|
|
|
assert_eq!(grid.data, [42, 23, 0, 0]);
|
|
|
|
|
|
|
|
assert_eq!(grid.get(0, 0), 42);
|
|
|
|
assert_eq!(grid.get(1, 0), 23);
|
|
|
|
assert_eq!(grid.get(1, 1), 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn load() {
|
|
|
|
let mut grid = ByteGrid::new(2, 3);
|
|
|
|
for x in 0..grid.width {
|
|
|
|
for y in 0..grid.height {
|
|
|
|
grid.set(x, y, (x + y) as u8);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
assert_eq!(grid.data, [0, 1, 1, 2, 2, 3]);
|
|
|
|
|
|
|
|
let data: Vec<u8> = grid.into();
|
|
|
|
|
|
|
|
let grid = ByteGrid::load(2, 3, &*data);
|
|
|
|
assert_eq!(grid.data, [0, 1, 1, 2, 2, 3]);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn mut_data_ref() {
|
|
|
|
let mut vec = ByteGrid::new(2, 2);
|
|
|
|
|
|
|
|
let data_ref = vec.mut_data_ref();
|
|
|
|
data_ref.copy_from_slice(&[1, 2, 3, 4]);
|
|
|
|
|
|
|
|
assert_eq!(vec.data, [1, 2, 3, 4]);
|
|
|
|
assert_eq!(vec.get(1, 0), 2)
|
|
|
|
}
|
|
|
|
}
|