2024-05-28 22:23:21 +02:00
|
|
|
//! C functions for interacting with `PixelGrid`s
|
|
|
|
//!
|
|
|
|
//! prefix `sp_pixel_grid_`
|
|
|
|
|
2024-09-05 21:15:53 +02:00
|
|
|
use servicepoint::{DataRef, Grid};
|
2024-05-26 13:15:11 +02:00
|
|
|
|
2024-09-07 12:23:32 +02:00
|
|
|
use crate::byte_slice::SPByteSlice;
|
2024-09-05 21:15:53 +02:00
|
|
|
|
|
|
|
/// A grid of pixels.
|
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```C
|
|
|
|
/// Cp437Grid grid = sp_pixel_grid_new(8, 3);
|
|
|
|
/// sp_pixel_grid_fill(grid, true);
|
|
|
|
/// sp_pixel_grid_set(grid, 0, 0, false);
|
|
|
|
/// sp_pixel_grid_dealloc(grid);
|
|
|
|
/// ```
|
|
|
|
pub struct SPPixelGrid(pub(crate) servicepoint::PixelGrid);
|
2024-05-26 13:15:11 +02:00
|
|
|
|
2024-05-28 21:25:59 +02:00
|
|
|
/// Creates a new `PixelGrid` with the specified dimensions.
|
|
|
|
///
|
|
|
|
/// # Arguments
|
|
|
|
///
|
2024-08-29 19:39:18 +02:00
|
|
|
/// - `width`: size in pixels in x-direction
|
|
|
|
/// - `height`: size in pixels in y-direction
|
2024-05-28 21:25:59 +02:00
|
|
|
///
|
|
|
|
/// returns: `PixelGrid` initialized to all pixels off
|
|
|
|
///
|
|
|
|
/// # Panics
|
|
|
|
///
|
|
|
|
/// - when the width is not dividable by 8
|
|
|
|
///
|
|
|
|
/// # Safety
|
|
|
|
///
|
|
|
|
/// The caller has to make sure that:
|
|
|
|
///
|
|
|
|
/// - the returned instance is freed in some way, either by using a consuming function or
|
|
|
|
/// by explicitly calling `sp_pixel_grid_dealloc`.
|
2024-05-26 13:15:11 +02:00
|
|
|
#[no_mangle]
|
|
|
|
pub unsafe extern "C" fn sp_pixel_grid_new(
|
|
|
|
width: usize,
|
|
|
|
height: usize,
|
2024-09-05 21:15:53 +02:00
|
|
|
) -> *mut SPPixelGrid {
|
|
|
|
Box::into_raw(Box::new(SPPixelGrid(servicepoint::PixelGrid::new(
|
|
|
|
width, height,
|
|
|
|
))))
|
2024-05-26 13:15:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Loads a `PixelGrid` with the specified dimensions from the provided data.
|
2024-05-28 21:25:59 +02:00
|
|
|
///
|
|
|
|
/// # Arguments
|
|
|
|
///
|
2024-08-29 19:39:18 +02:00
|
|
|
/// - `width`: size in pixels in x-direction
|
|
|
|
/// - `height`: size in pixels in y-direction
|
2024-05-28 21:25:59 +02:00
|
|
|
///
|
|
|
|
/// 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
|
|
|
|
///
|
|
|
|
/// # Safety
|
|
|
|
///
|
|
|
|
/// The caller has to make sure that:
|
|
|
|
///
|
|
|
|
/// - `data` points to a valid memory location of at least `data_length` bytes in size.
|
|
|
|
/// - the returned instance is freed in some way, either by using a consuming function or
|
|
|
|
/// by explicitly calling `sp_pixel_grid_dealloc`.
|
2024-05-26 13:15:11 +02:00
|
|
|
#[no_mangle]
|
|
|
|
pub unsafe extern "C" fn sp_pixel_grid_load(
|
|
|
|
width: usize,
|
|
|
|
height: usize,
|
|
|
|
data: *const u8,
|
|
|
|
data_length: usize,
|
2024-09-05 21:15:53 +02:00
|
|
|
) -> *mut SPPixelGrid {
|
2024-05-26 13:15:11 +02:00
|
|
|
let data = std::slice::from_raw_parts(data, data_length);
|
2024-09-05 21:15:53 +02:00
|
|
|
Box::into_raw(Box::new(SPPixelGrid(servicepoint::PixelGrid::load(
|
|
|
|
width, height, data,
|
|
|
|
))))
|
2024-05-26 13:15:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Clones a `PixelGrid`.
|
2024-05-28 21:25:59 +02:00
|
|
|
///
|
|
|
|
/// # Safety
|
|
|
|
///
|
|
|
|
/// The caller has to make sure that:
|
|
|
|
///
|
|
|
|
/// - `this` points to a valid `PixelGrid`
|
|
|
|
/// - `this` is not written to concurrently
|
|
|
|
/// - the returned instance is freed in some way, either by using a consuming function or
|
|
|
|
/// by explicitly calling `sp_pixel_grid_dealloc`.
|
2024-05-26 13:15:11 +02:00
|
|
|
#[no_mangle]
|
|
|
|
pub unsafe extern "C" fn sp_pixel_grid_clone(
|
2024-09-05 21:15:53 +02:00
|
|
|
this: *const SPPixelGrid,
|
|
|
|
) -> *mut SPPixelGrid {
|
|
|
|
Box::into_raw(Box::new(SPPixelGrid((*this).0.clone())))
|
2024-05-26 13:15:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Deallocates a `PixelGrid`.
|
|
|
|
///
|
2024-05-28 21:25:59 +02:00
|
|
|
/// # Safety
|
|
|
|
///
|
|
|
|
/// The caller has to make sure that:
|
|
|
|
///
|
|
|
|
/// - `this` points to a valid `PixelGrid`
|
|
|
|
/// - `this` is not used concurrently or after this call
|
|
|
|
/// - `this` was not passed to another consuming function, e.g. to create a `Command`
|
2024-05-26 13:15:11 +02:00
|
|
|
#[no_mangle]
|
2024-09-05 21:15:53 +02:00
|
|
|
pub unsafe extern "C" fn sp_pixel_grid_dealloc(this: *mut SPPixelGrid) {
|
2024-05-26 13:15:11 +02:00
|
|
|
_ = Box::from_raw(this);
|
|
|
|
}
|
|
|
|
|
2024-05-28 21:25:59 +02:00
|
|
|
/// Gets the current value at the specified position in the `PixelGrid`.
|
|
|
|
///
|
|
|
|
/// # Arguments
|
|
|
|
///
|
2024-08-29 19:39:18 +02:00
|
|
|
/// - `this`: instance to read from
|
|
|
|
/// - `x` and `y`: position of the cell to read
|
2024-05-28 21:25:59 +02:00
|
|
|
///
|
|
|
|
/// # Panics
|
|
|
|
///
|
|
|
|
/// When accessing `x` or `y` out of bounds.
|
|
|
|
///
|
|
|
|
/// # Safety
|
|
|
|
///
|
|
|
|
/// The caller has to make sure that:
|
|
|
|
///
|
|
|
|
/// - `this` points to a valid `PixelGrid`
|
|
|
|
/// - `this` is not written to concurrently
|
2024-05-26 13:15:11 +02:00
|
|
|
#[no_mangle]
|
|
|
|
pub unsafe extern "C" fn sp_pixel_grid_get(
|
2024-09-05 21:15:53 +02:00
|
|
|
this: *const SPPixelGrid,
|
2024-05-26 13:15:11 +02:00
|
|
|
x: usize,
|
|
|
|
y: usize,
|
|
|
|
) -> bool {
|
2024-09-05 21:15:53 +02:00
|
|
|
(*this).0.get(x, y)
|
2024-05-26 13:15:11 +02:00
|
|
|
}
|
|
|
|
|
2024-05-28 21:25:59 +02:00
|
|
|
/// Sets the value of the specified position in the `PixelGrid`.
|
|
|
|
///
|
|
|
|
/// # Arguments
|
|
|
|
///
|
2024-08-29 19:39:18 +02:00
|
|
|
/// - `this`: instance to write to
|
|
|
|
/// - `x` and `y`: position of the cell
|
|
|
|
/// - `value`: the value to write to the cell
|
2024-05-28 21:25:59 +02:00
|
|
|
///
|
|
|
|
/// returns: old value of the cell
|
|
|
|
///
|
|
|
|
/// # Panics
|
|
|
|
///
|
|
|
|
/// When accessing `x` or `y` out of bounds.
|
|
|
|
///
|
|
|
|
/// # Safety
|
|
|
|
///
|
|
|
|
/// The caller has to make sure that:
|
|
|
|
///
|
|
|
|
/// - `this` points to a valid `PixelGrid`
|
|
|
|
/// - `this` is not written to or read from concurrently
|
2024-05-26 13:15:11 +02:00
|
|
|
#[no_mangle]
|
|
|
|
pub unsafe extern "C" fn sp_pixel_grid_set(
|
2024-09-05 21:15:53 +02:00
|
|
|
this: *mut SPPixelGrid,
|
2024-05-26 13:15:11 +02:00
|
|
|
x: usize,
|
|
|
|
y: usize,
|
|
|
|
value: bool,
|
|
|
|
) {
|
2024-09-05 21:15:53 +02:00
|
|
|
(*this).0.set(x, y, value);
|
2024-05-26 13:15:11 +02:00
|
|
|
}
|
|
|
|
|
2024-05-28 21:25:59 +02:00
|
|
|
/// Sets the state of all pixels in the `PixelGrid`.
|
|
|
|
///
|
|
|
|
/// # Arguments
|
|
|
|
///
|
2024-08-29 19:39:18 +02:00
|
|
|
/// - `this`: instance to write to
|
|
|
|
/// - `value`: the value to set all pixels to
|
2024-05-28 21:25:59 +02:00
|
|
|
///
|
|
|
|
/// # Safety
|
|
|
|
///
|
|
|
|
/// The caller has to make sure that:
|
|
|
|
///
|
|
|
|
/// - `this` points to a valid `PixelGrid`
|
|
|
|
/// - `this` is not written to or read from concurrently
|
2024-05-26 13:15:11 +02:00
|
|
|
#[no_mangle]
|
2024-09-05 21:15:53 +02:00
|
|
|
pub unsafe extern "C" fn sp_pixel_grid_fill(
|
|
|
|
this: *mut SPPixelGrid,
|
|
|
|
value: bool,
|
|
|
|
) {
|
|
|
|
(*this).0.fill(value);
|
2024-05-26 13:15:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Gets the width in pixels of the `PixelGrid` instance.
|
2024-05-28 21:25:59 +02:00
|
|
|
///
|
|
|
|
/// # Arguments
|
|
|
|
///
|
2024-08-29 19:39:18 +02:00
|
|
|
/// - `this`: instance to read from
|
2024-05-28 21:25:59 +02:00
|
|
|
///
|
|
|
|
/// # Safety
|
|
|
|
///
|
|
|
|
/// The caller has to make sure that:
|
|
|
|
///
|
|
|
|
/// - `this` points to a valid `PixelGrid`
|
2024-05-26 13:15:11 +02:00
|
|
|
#[no_mangle]
|
2024-09-05 21:15:53 +02:00
|
|
|
pub unsafe extern "C" fn sp_pixel_grid_width(
|
|
|
|
this: *const SPPixelGrid,
|
|
|
|
) -> usize {
|
|
|
|
(*this).0.width()
|
2024-05-26 13:15:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Gets the height in pixels of the `PixelGrid` instance.
|
2024-05-28 21:25:59 +02:00
|
|
|
///
|
|
|
|
/// # Arguments
|
|
|
|
///
|
2024-08-29 19:39:18 +02:00
|
|
|
/// - `this`: instance to read from
|
2024-05-28 21:25:59 +02:00
|
|
|
///
|
|
|
|
/// # Safety
|
|
|
|
///
|
|
|
|
/// The caller has to make sure that:
|
|
|
|
///
|
|
|
|
/// - `this` points to a valid `PixelGrid`
|
2024-05-26 13:15:11 +02:00
|
|
|
#[no_mangle]
|
2024-09-05 21:15:53 +02:00
|
|
|
pub unsafe extern "C" fn sp_pixel_grid_height(
|
|
|
|
this: *const SPPixelGrid,
|
|
|
|
) -> usize {
|
|
|
|
(*this).0.height()
|
2024-05-26 13:15:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Gets an unsafe reference to the data of the `PixelGrid` instance.
|
|
|
|
///
|
|
|
|
/// ## Safety
|
|
|
|
///
|
2024-05-28 21:25:59 +02:00
|
|
|
/// The caller has to make sure that:
|
2024-05-26 13:15:11 +02:00
|
|
|
///
|
2024-05-28 21:25:59 +02:00
|
|
|
/// - `this` points to a valid `PixelGrid`
|
|
|
|
/// - the returned memory range is never accessed after the passed `PixelGrid` has been freed
|
|
|
|
/// - the returned memory range is never accessed concurrently, either via the `PixelGrid` or directly
|
2024-05-26 13:15:11 +02:00
|
|
|
#[no_mangle]
|
|
|
|
pub unsafe extern "C" fn sp_pixel_grid_unsafe_data_ref(
|
2024-09-05 21:15:53 +02:00
|
|
|
this: *mut SPPixelGrid,
|
|
|
|
) -> SPByteSlice {
|
|
|
|
let data = (*this).0.data_ref_mut();
|
|
|
|
SPByteSlice {
|
2024-05-26 13:15:11 +02:00
|
|
|
start: data.as_mut_ptr_range().start,
|
|
|
|
length: data.len(),
|
|
|
|
}
|
|
|
|
}
|