servicepoint/crates/servicepoint_binding_c/src/pixel_grid.rs

247 lines
6 KiB
Rust
Raw Normal View History

2024-09-07 14:11:15 +02:00
//! C functions for interacting with `SPPixelGrid`s
//!
//! prefix `sp_pixel_grid_`
2024-09-05 21:15:53 +02:00
use servicepoint::{DataRef, Grid};
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);
2024-09-07 14:11:15 +02:00
/// sp_pixel_grid_free(grid);
2024-09-05 21:15:53 +02:00
/// ```
pub struct SPPixelGrid(pub(crate) servicepoint::PixelGrid);
2024-09-07 14:11:15 +02:00
/// Creates a new `SPPixelGrid` with the specified dimensions.
2024-05-28 21:25:59 +02:00
///
/// # Arguments
///
/// - `width`: size in pixels in x-direction
/// - `height`: size in pixels in y-direction
2024-05-28 21:25:59 +02:00
///
2024-09-07 14:11:15 +02:00
/// returns: `SPPixelGrid` initialized to all pixels off
2024-05-28 21:25:59 +02:00
///
/// # 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
2024-09-07 14:11:15 +02:00
/// by explicitly calling `sp_pixel_grid_free`.
#[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-09-07 14:11:15 +02:00
/// Loads a `SPPixelGrid` with the specified dimensions from the provided data.
2024-05-28 21:25:59 +02:00
///
/// # Arguments
///
/// - `width`: size in pixels in x-direction
/// - `height`: size in pixels in y-direction
2024-05-28 21:25:59 +02:00
///
2024-09-07 14:11:15 +02:00
/// returns: `SPPixelGrid` that contains a copy of the provided data
2024-05-28 21:25:59 +02:00
///
/// # 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
2024-09-07 14:11:15 +02:00
/// by explicitly calling `sp_pixel_grid_free`.
#[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 {
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-09-07 14:11:15 +02:00
/// Clones a `SPPixelGrid`.
2024-05-28 21:25:59 +02:00
///
/// # Safety
///
/// The caller has to make sure that:
///
2024-09-07 14:11:15 +02:00
/// - `this` points to a valid `SPPixelGrid`
2024-05-28 21:25:59 +02:00
/// - `this` is not written to concurrently
/// - the returned instance is freed in some way, either by using a consuming function or
2024-09-07 14:11:15 +02:00
/// by explicitly calling `sp_pixel_grid_free`.
#[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-09-07 14:11:15 +02:00
/// Deallocates a `SPPixelGrid`.
///
2024-05-28 21:25:59 +02:00
/// # Safety
///
/// The caller has to make sure that:
///
2024-09-07 14:11:15 +02:00
/// - `this` points to a valid `SPPixelGrid`
2024-05-28 21:25:59 +02:00
/// - `this` is not used concurrently or after this call
2024-09-07 14:11:15 +02:00
/// - `this` was not passed to another consuming function, e.g. to create a `SPCommand`
#[no_mangle]
2024-09-07 14:11:15 +02:00
pub unsafe extern "C" fn sp_pixel_grid_free(this: *mut SPPixelGrid) {
_ = Box::from_raw(this);
}
2024-09-07 14:11:15 +02:00
/// Gets the current value at the specified position in the `SPPixelGrid`.
2024-05-28 21:25:59 +02:00
///
/// # Arguments
///
/// - `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:
///
2024-09-07 14:11:15 +02:00
/// - `this` points to a valid `SPPixelGrid`
2024-05-28 21:25:59 +02:00
/// - `this` is not written to concurrently
#[no_mangle]
pub unsafe extern "C" fn sp_pixel_grid_get(
2024-09-05 21:15:53 +02:00
this: *const SPPixelGrid,
x: usize,
y: usize,
) -> bool {
2024-09-05 21:15:53 +02:00
(*this).0.get(x, y)
}
2024-09-07 14:11:15 +02:00
/// Sets the value of the specified position in the `SPPixelGrid`.
2024-05-28 21:25:59 +02:00
///
/// # Arguments
///
/// - `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:
///
2024-09-07 14:11:15 +02:00
/// - `this` points to a valid `SPPixelGrid`
2024-05-28 21:25:59 +02:00
/// - `this` is not written to or read from concurrently
#[no_mangle]
pub unsafe extern "C" fn sp_pixel_grid_set(
2024-09-05 21:15:53 +02:00
this: *mut SPPixelGrid,
x: usize,
y: usize,
value: bool,
) {
2024-09-05 21:15:53 +02:00
(*this).0.set(x, y, value);
}
2024-09-07 14:11:15 +02:00
/// Sets the state of all pixels in the `SPPixelGrid`.
2024-05-28 21:25:59 +02:00
///
/// # Arguments
///
/// - `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:
///
2024-09-07 14:11:15 +02:00
/// - `this` points to a valid `SPPixelGrid`
2024-05-28 21:25:59 +02:00
/// - `this` is not written to or read from concurrently
#[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-09-07 14:11:15 +02:00
/// Gets the width in pixels of the `SPPixelGrid` instance.
2024-05-28 21:25:59 +02:00
///
/// # Arguments
///
/// - `this`: instance to read from
2024-05-28 21:25:59 +02:00
///
/// # Safety
///
/// The caller has to make sure that:
///
2024-09-07 14:11:15 +02:00
/// - `this` points to a valid `SPPixelGrid`
#[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-09-07 14:11:15 +02:00
/// Gets the height in pixels of the `SPPixelGrid` instance.
2024-05-28 21:25:59 +02:00
///
/// # Arguments
///
/// - `this`: instance to read from
2024-05-28 21:25:59 +02:00
///
/// # Safety
///
/// The caller has to make sure that:
///
2024-09-07 14:11:15 +02:00
/// - `this` points to a valid `SPPixelGrid`
#[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-09-07 14:11:15 +02:00
/// Gets an unsafe reference to the data of the `SPPixelGrid` instance.
///
/// ## Safety
///
2024-05-28 21:25:59 +02:00
/// The caller has to make sure that:
///
2024-09-07 14:11:15 +02:00
/// - `this` points to a valid `SPPixelGrid`
/// - the returned memory range is never accessed after the passed `SPPixelGrid` has been freed
/// - the returned memory range is never accessed concurrently, either via the `SPPixelGrid` or directly
#[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 {
start: data.as_mut_ptr_range().start,
length: data.len(),
}
}