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

257 lines
6.6 KiB
Rust
Raw Normal View History

2024-05-18 10:50:35 +02:00
use crate::Grid;
/// A 2D grid of bytes
#[derive(Debug, Clone, PartialEq)]
2024-05-11 12:43:17 +02:00
pub struct ByteGrid {
width: usize,
height: usize,
2024-05-11 12:43:17 +02:00
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-18 10:50:35 +02:00
/// Get the underlying byte rows
pub fn mut_data_ref(&mut self) -> &mut [u8] {
self.data.as_mut_slice()
}
fn check_indexes(&self, x: usize, y: usize) {
if x >= self.width {
panic!("cannot access byte {x}-{y} because x is outside of bounds 0..{}", self.width)
}
if y >= self.height {
panic!("cannot access byte {x}-{y} because y is outside of bounds 0..{}", self.height)
}
2024-05-11 12:43:17 +02:00
}
2024-05-18 10:50:35 +02:00
}
2024-05-11 12:43:17 +02:00
2024-05-18 10:50:35 +02:00
impl Grid<u8> for ByteGrid {
fn set(&mut self, x: usize, y: usize, value: u8) -> u8 {
2024-05-17 23:48:30 +02:00
self.check_indexes(x, y);
2024-05-18 10:50:35 +02:00
let pos = &mut self.data[x + y * self.width];
let old_val = *pos;
*pos = value;
old_val
2024-05-11 12:43:17 +02:00
}
2024-05-18 10:50:35 +02:00
fn get(&self, x: usize, y: usize) -> u8 {
self.check_indexes(x, y);
self.data[x + y * self.width]
2024-05-11 12:43:17 +02:00
}
2024-05-18 10:50:35 +02:00
fn fill(&mut self, value: u8) {
self.data.fill(value)
}
2024-05-18 10:50:35 +02:00
fn width(&self) -> usize {
self.width
}
2024-05-18 10:50:35 +02:00
fn height(&self) -> usize {
self.height
}
2024-05-11 12:43:17 +02:00
}
2024-05-18 10:50:35 +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 {
use crate::{ByteGrid, CByteSlice};
2024-05-18 10:50:35 +02:00
use crate::grid::Grid;
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
}
/// 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 {
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 {
2024-05-18 10:50:35 +02:00
use crate::{ByteGrid, Grid};
2024-05-17 17:55:29 +02:00
#[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();
2024-05-17 18:44:31 +02:00
let grid = ByteGrid::load(2, 3, &data);
2024-05-17 17:55:29 +02:00
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)
}
2024-05-17 18:44:31 +02:00
}