servicepoint-binding-csharp/servicepoint2/src/byte_grid.rs

151 lines
4.7 KiB
Rust
Raw Normal View History

/// A 2D grid of bytes
#[derive(Debug, Clone)]
2024-05-11 12:43:17 +02:00
pub struct ByteGrid {
/// Size in the x-axis
2024-05-11 12:43:17 +02:00
pub width: usize,
/// 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)
}
/// 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
}
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-12 18:28:53 +02:00
#[cfg(feature = "c-api")]
2024-05-12 17:15:30 +02:00
pub mod c_api
{
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]
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]
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]
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]
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]
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]
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-13 00:17:40 +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-13 00:17:40 +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
}
/// 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]
pub unsafe extern "C" fn sp2_byte_grid_unsafe_data_ref(this: *mut ByteGrid) -> CByteSlice {
let data = (*this).mut_data_ref();
CByteSlice {
start: data.as_mut_ptr_range().start,
length: data.len(),
}
}
2024-05-12 17:15:30 +02:00
}