202 lines
4.9 KiB
Rust
202 lines
4.9 KiB
Rust
use crate::byte_slice::ByteSlice;
|
|
use crate::{heap_drop, heap_move, heap_move_nonnull, heap_remove, SPBitVec};
|
|
use servicepoint::{Bitmap, DataRef, Grid};
|
|
use std::ptr::NonNull;
|
|
|
|
/// Creates a new [Bitmap] with the specified dimensions.
|
|
///
|
|
/// # Arguments
|
|
///
|
|
/// - `width`: size in pixels in x-direction
|
|
/// - `height`: size in pixels in y-direction
|
|
///
|
|
/// returns: [Bitmap] initialized to all pixels off, or NULL in case of an error.
|
|
///
|
|
/// # Errors
|
|
///
|
|
/// In the following cases, this function will return NULL:
|
|
///
|
|
/// - when the width is not dividable by 8
|
|
///
|
|
/// # Examples
|
|
///
|
|
/// ```C
|
|
/// Cp437Grid grid = sp_bitmap_new(8, 3);
|
|
/// sp_bitmap_fill(grid, true);
|
|
/// sp_bitmap_set(grid, 0, 0, false);
|
|
/// sp_bitmap_free(grid);
|
|
/// ```
|
|
#[no_mangle]
|
|
pub unsafe extern "C" fn sp_bitmap_new(
|
|
width: usize,
|
|
height: usize,
|
|
) -> *mut Bitmap {
|
|
if let Some(bitmap) = Bitmap::new(width, height) {
|
|
heap_move(bitmap)
|
|
} else {
|
|
std::ptr::null_mut()
|
|
}
|
|
}
|
|
|
|
/// Creates a new [Bitmap] with a size matching the screen.
|
|
///
|
|
/// returns: [Bitmap] initialized to all pixels off.
|
|
#[no_mangle]
|
|
pub unsafe extern "C" fn sp_bitmap_new_max_sized() -> NonNull<Bitmap> {
|
|
heap_move_nonnull(Bitmap::max_sized())
|
|
}
|
|
|
|
/// Loads a [Bitmap] with the specified dimensions from the provided data.
|
|
///
|
|
/// # Arguments
|
|
///
|
|
/// - `width`: size in pixels in x-direction
|
|
/// - `height`: size in pixels in y-direction
|
|
///
|
|
/// returns: [Bitmap] that contains a copy of the provided data, or NULL in case of an error.
|
|
#[no_mangle]
|
|
pub unsafe extern "C" fn sp_bitmap_load(
|
|
width: usize,
|
|
height: usize,
|
|
data: ByteSlice,
|
|
) -> *mut Bitmap {
|
|
let data = unsafe { data.as_slice() };
|
|
if let Ok(bitmap) = Bitmap::load(width, height, data) {
|
|
heap_move(bitmap)
|
|
} else {
|
|
std::ptr::null_mut()
|
|
}
|
|
}
|
|
|
|
/// Tries to convert the BitVec to a Bitmap.
|
|
///
|
|
/// The provided BitVec gets consumed.
|
|
///
|
|
/// Returns NULL in case of error.
|
|
#[no_mangle]
|
|
pub unsafe extern "C" fn sp_bitmap_from_bitvec(
|
|
width: usize,
|
|
bitvec: NonNull<SPBitVec>,
|
|
) -> *mut Bitmap {
|
|
let bitvec = unsafe { heap_remove(bitvec) };
|
|
if let Ok(bitmap) = Bitmap::from_bitvec(width, bitvec.0) {
|
|
heap_move(bitmap)
|
|
} else {
|
|
std::ptr::null_mut()
|
|
}
|
|
}
|
|
|
|
/// Clones a [Bitmap].
|
|
#[no_mangle]
|
|
pub unsafe extern "C" fn sp_bitmap_clone(
|
|
bitmap: NonNull<Bitmap>,
|
|
) -> NonNull<Bitmap> {
|
|
heap_move_nonnull(unsafe { bitmap.as_ref().clone() })
|
|
}
|
|
|
|
/// Deallocates a [Bitmap].
|
|
#[no_mangle]
|
|
pub unsafe extern "C" fn sp_bitmap_free(bitmap: NonNull<Bitmap>) {
|
|
unsafe { heap_drop(bitmap) }
|
|
}
|
|
|
|
/// Gets the current value at the specified position in the [Bitmap].
|
|
///
|
|
/// # Arguments
|
|
///
|
|
/// - `bitmap`: instance to read from
|
|
/// - `x` and `y`: position of the cell to read
|
|
///
|
|
/// # Panics
|
|
///
|
|
/// - when accessing `x` or `y` out of bounds
|
|
#[no_mangle]
|
|
pub unsafe extern "C" fn sp_bitmap_get(
|
|
bitmap: NonNull<Bitmap>,
|
|
x: usize,
|
|
y: usize,
|
|
) -> bool {
|
|
unsafe { bitmap.as_ref().get(x, y) }
|
|
}
|
|
|
|
/// Sets the value of the specified position in the [Bitmap].
|
|
///
|
|
/// # Arguments
|
|
///
|
|
/// - `bitmap`: instance to write to
|
|
/// - `x` and `y`: position of the cell
|
|
/// - `value`: the value to write to the cell
|
|
///
|
|
/// # Panics
|
|
///
|
|
/// - when accessing `x` or `y` out of bounds
|
|
#[no_mangle]
|
|
pub unsafe extern "C" fn sp_bitmap_set(
|
|
bitmap: NonNull<Bitmap>,
|
|
x: usize,
|
|
y: usize,
|
|
value: bool,
|
|
) {
|
|
unsafe { (*bitmap.as_ptr()).set(x, y, value) };
|
|
}
|
|
|
|
/// Sets the state of all pixels in the [Bitmap].
|
|
///
|
|
/// # Arguments
|
|
///
|
|
/// - `bitmap`: instance to write to
|
|
/// - `value`: the value to set all pixels to
|
|
#[no_mangle]
|
|
pub unsafe extern "C" fn sp_bitmap_fill(bitmap: NonNull<Bitmap>, value: bool) {
|
|
unsafe { (*bitmap.as_ptr()).fill(value) };
|
|
}
|
|
|
|
/// Gets the width in pixels of the [Bitmap] instance.
|
|
///
|
|
/// # Arguments
|
|
///
|
|
/// - `bitmap`: instance to read from
|
|
///
|
|
/// # Panics
|
|
///
|
|
/// - when `bitmap` is NULL
|
|
///
|
|
/// # Safety
|
|
///
|
|
/// The caller has to make sure that:
|
|
///
|
|
/// - `bitmap` points to a valid [Bitmap]
|
|
#[no_mangle]
|
|
pub unsafe extern "C" fn sp_bitmap_width(bitmap: NonNull<Bitmap>) -> usize {
|
|
unsafe { bitmap.as_ref().width() }
|
|
}
|
|
|
|
/// Gets the height in pixels of the [Bitmap] instance.
|
|
///
|
|
/// # Arguments
|
|
///
|
|
/// - `bitmap`: instance to read from
|
|
#[no_mangle]
|
|
pub unsafe extern "C" fn sp_bitmap_height(bitmap: NonNull<Bitmap>) -> usize {
|
|
unsafe { bitmap.as_ref().height() }
|
|
}
|
|
|
|
/// Gets an unsafe reference to the data of the [Bitmap] instance.
|
|
///
|
|
/// The returned memory is valid for the lifetime of the bitmap.
|
|
#[no_mangle]
|
|
pub unsafe extern "C" fn sp_bitmap_unsafe_data_ref(
|
|
mut bitmap: NonNull<Bitmap>,
|
|
) -> ByteSlice {
|
|
unsafe { ByteSlice::from_slice(bitmap.as_mut().data_ref_mut()) }
|
|
}
|
|
|
|
/// Consumes the Bitmap and returns the contained BitVec
|
|
#[no_mangle]
|
|
pub unsafe extern "C" fn sp_bitmap_into_bitvec(
|
|
bitmap: NonNull<Bitmap>,
|
|
) -> NonNull<SPBitVec> {
|
|
let bitmap = unsafe { heap_remove(bitmap) };
|
|
heap_move_nonnull(SPBitVec(bitmap.into()))
|
|
}
|