rename PixelGrid to Bitmap

This commit is contained in:
Vinzenz Schroeter 2024-10-14 22:42:45 +02:00
parent f64ce6e57e
commit bd0ecd77d2
23 changed files with 1037 additions and 1042 deletions

View file

@ -21,8 +21,8 @@ int main(void) {
if (connection == NULL)
return 1;
SPPixelGrid *pixels = sp_pixel_grid_new(SP_PIXEL_WIDTH, SP_PIXEL_HEIGHT);
sp_pixel_grid_fill(pixels, true);
SPBitmap *pixels = sp_bitmap_new(SP_PIXEL_WIDTH, SP_PIXEL_HEIGHT);
sp_bitmap_fill(pixels, true);
SPCommand *command = sp_command_bitmap_linear_win(0, 0, pixels, Uncompressed);
while (sp_connection_send_command(connection, sp_command_clone(command)));

View file

@ -83,6 +83,20 @@ typedef uint16_t SPCompressionCode;
*/
typedef struct SPBitVec SPBitVec;
/**
* A grid of pixels.
*
* # 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);
* ```
*/
typedef struct SPBitmap SPBitmap;
/**
* A grid containing brightness values.
*
@ -152,20 +166,6 @@ typedef struct SPCp437Grid SPCp437Grid;
*/
typedef struct SPPacket SPPacket;
/**
* 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_free(grid);
* ```
*/
typedef struct SPPixelGrid SPPixelGrid;
/**
* Represents a span of memory (`&mut [u8]` ) as a struct usable by C code.
*
@ -403,6 +403,219 @@ void sp_bit_vec_set(struct SPBitVec *bit_vec, size_t index, bool value);
*/
struct SPByteSlice sp_bit_vec_unsafe_data_ref(struct SPBitVec *bit_vec);
/**
* Clones a [SPBitmap].
*
* Will never return NULL.
*
* # Panics
*
* - when `bitmap` is NULL
*
* # Safety
*
* The caller has to make sure that:
*
* - `bitmap` points to a valid [SPBitmap]
* - `bitmap` is not written to concurrently
* - the returned instance is freed in some way, either by using a consuming function or
* by explicitly calling `sp_bitmap_free`.
*/
struct SPBitmap *sp_bitmap_clone(const struct SPBitmap *bitmap);
/**
* Sets the state of all pixels in the [SPBitmap].
*
* # Arguments
*
* - `bitmap`: instance to write to
* - `value`: the value to set all pixels to
*
* # Panics
*
* - when `bitmap` is NULL
*
* # Safety
*
* The caller has to make sure that:
*
* - `bitmap` points to a valid [SPBitmap]
* - `bitmap` is not written to or read from concurrently
*/
void sp_bitmap_fill(struct SPBitmap *bitmap, bool value);
/**
* Deallocates a [SPBitmap].
*
* # Panics
*
* - when `bitmap` is NULL
*
* # Safety
*
* The caller has to make sure that:
*
* - `bitmap` points to a valid [SPBitmap]
* - `bitmap` is not used concurrently or after bitmap call
* - `bitmap` was not passed to another consuming function, e.g. to create a [SPCommand]
*/
void sp_bitmap_free(struct SPBitmap *bitmap);
/**
* Gets the current value at the specified position in the [SPBitmap].
*
* # Arguments
*
* - `bitmap`: instance to read from
* - `x` and `y`: position of the cell to read
*
* # Panics
*
* - when `bitmap` is NULL
* - when accessing `x` or `y` out of bounds
*
* # Safety
*
* The caller has to make sure that:
*
* - `bitmap` points to a valid [SPBitmap]
* - `bitmap` is not written to concurrently
*/
bool sp_bitmap_get(const struct SPBitmap *bitmap, size_t x, size_t y);
/**
* Gets the height in pixels of the [SPBitmap] 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 [SPBitmap]
*/
size_t sp_bitmap_height(const struct SPBitmap *bitmap);
/**
* Loads a [SPBitmap] with the specified dimensions from the provided data.
*
* # Arguments
*
* - `width`: size in pixels in x-direction
* - `height`: size in pixels in y-direction
*
* returns: [SPBitmap] that contains a copy of the provided data. Will never return NULL.
*
* # Panics
*
* - when `data` is NULL
* - 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_bitmap_free`.
*/
struct SPBitmap *sp_bitmap_load(size_t width,
size_t height,
const uint8_t *data,
size_t data_length);
/**
* Creates a new [SPBitmap] with the specified dimensions.
*
* # Arguments
*
* - `width`: size in pixels in x-direction
* - `height`: size in pixels in y-direction
*
* returns: [SPBitmap] initialized to all pixels off. Will never return NULL.
*
* # 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_bitmap_free`.
*/
struct SPBitmap *sp_bitmap_new(size_t width,
size_t height);
/**
* Sets the value of the specified position in the [SPBitmap].
*
* # Arguments
*
* - `bitmap`: instance to write to
* - `x` and `y`: position of the cell
* - `value`: the value to write to the cell
*
* returns: old value of the cell
*
* # Panics
*
* - when `bitmap` is NULL
* - when accessing `x` or `y` out of bounds
*
* # Safety
*
* The caller has to make sure that:
*
* - `bitmap` points to a valid [SPBitmap]
* - `bitmap` is not written to or read from concurrently
*/
void sp_bitmap_set(struct SPBitmap *bitmap, size_t x, size_t y, bool value);
/**
* Gets an unsafe reference to the data of the [SPBitmap] instance.
*
* # Panics
*
* - when `bitmap` is NULL
*
* # Safety
*
* The caller has to make sure that:
*
* - `bitmap` points to a valid [SPBitmap]
* - the returned memory range is never accessed after the passed [SPBitmap] has been freed
* - the returned memory range is never accessed concurrently, either via the [SPBitmap] or directly
*/
struct SPByteSlice sp_bitmap_unsafe_data_ref(struct SPBitmap *bitmap);
/**
* Gets the width in pixels of the [SPBitmap] 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 [SPBitmap]
*/
size_t sp_bitmap_width(const struct SPBitmap *bitmap);
/**
* Clones a [SPBrightnessGrid].
*
@ -726,28 +939,28 @@ struct SPCommand *sp_command_bitmap_linear_or(size_t offset,
/**
* Sets a window of pixels to the specified values.
*
* The passed [SPPixelGrid] gets consumed.
* The passed [SPBitmap] gets consumed.
*
* Returns: a new [Command::BitmapLinearWin] instance. Will never return NULL.
*
* # Panics
*
* - when `pixel_grid` is null
* - when `bitmap` is null
* - when `compression_code` is not a valid value
*
* # Safety
*
* The caller has to make sure that:
*
* - `pixel_grid` points to a valid instance of [SPPixelGrid]
* - `pixel_grid` is not used concurrently or after this call
* - `bitmap` points to a valid instance of [SPBitmap]
* - `bitmap` is not used concurrently or after this call
* - `compression` matches one of the allowed enum values
* - the returned [SPCommand] instance is freed in some way, either by using a consuming function or
* by explicitly calling `sp_command_free`.
*/
struct SPCommand *sp_command_bitmap_linear_win(size_t x,
size_t y,
struct SPPixelGrid *pixel_grid,
struct SPBitmap *bitmap,
SPCompressionCode compression_code);
/**
@ -1330,224 +1543,6 @@ struct SPPacket *sp_packet_from_command(struct SPCommand *command);
struct SPPacket *sp_packet_try_load(const uint8_t *data,
size_t length);
/**
* Clones a [SPPixelGrid].
*
* Will never return NULL.
*
* # Panics
*
* - when `pixel_grid` is NULL
*
* # Safety
*
* The caller has to make sure that:
*
* - `pixel_grid` points to a valid [SPPixelGrid]
* - `pixel_grid` 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_free`.
*/
struct SPPixelGrid *sp_pixel_grid_clone(const struct SPPixelGrid *pixel_grid);
/**
* Sets the state of all pixels in the [SPPixelGrid].
*
* # Arguments
*
* - `pixel_grid`: instance to write to
* - `value`: the value to set all pixels to
*
* # Panics
*
* - when `pixel_grid` is NULL
*
* # Safety
*
* The caller has to make sure that:
*
* - `pixel_grid` points to a valid [SPPixelGrid]
* - `pixel_grid` is not written to or read from concurrently
*/
void sp_pixel_grid_fill(struct SPPixelGrid *pixel_grid, bool value);
/**
* Deallocates a [SPPixelGrid].
*
* # Panics
*
* - when `pixel_grid` is NULL
*
* # Safety
*
* The caller has to make sure that:
*
* - `pixel_grid` points to a valid [SPPixelGrid]
* - `pixel_grid` is not used concurrently or after pixel_grid call
* - `pixel_grid` was not passed to another consuming function, e.g. to create a [SPCommand]
*/
void sp_pixel_grid_free(struct SPPixelGrid *pixel_grid);
/**
* Gets the current value at the specified position in the [SPPixelGrid].
*
* # Arguments
*
* - `pixel_grid`: instance to read from
* - `x` and `y`: position of the cell to read
*
* # Panics
*
* - when `pixel_grid` is NULL
* - when accessing `x` or `y` out of bounds
*
* # Safety
*
* The caller has to make sure that:
*
* - `pixel_grid` points to a valid [SPPixelGrid]
* - `pixel_grid` is not written to concurrently
*/
bool sp_pixel_grid_get(const struct SPPixelGrid *pixel_grid,
size_t x,
size_t y);
/**
* Gets the height in pixels of the [SPPixelGrid] instance.
*
* # Arguments
*
* - `pixel_grid`: instance to read from
*
* # Panics
*
* - when `pixel_grid` is NULL
*
* # Safety
*
* The caller has to make sure that:
*
* - `pixel_grid` points to a valid [SPPixelGrid]
*/
size_t sp_pixel_grid_height(const struct SPPixelGrid *pixel_grid);
/**
* Loads a [SPPixelGrid] with the specified dimensions from the provided data.
*
* # Arguments
*
* - `width`: size in pixels in x-direction
* - `height`: size in pixels in y-direction
*
* returns: [SPPixelGrid] that contains a copy of the provided data. Will never return NULL.
*
* # Panics
*
* - when `data` is NULL
* - 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_free`.
*/
struct SPPixelGrid *sp_pixel_grid_load(size_t width,
size_t height,
const uint8_t *data,
size_t data_length);
/**
* Creates a new [SPPixelGrid] with the specified dimensions.
*
* # Arguments
*
* - `width`: size in pixels in x-direction
* - `height`: size in pixels in y-direction
*
* returns: [SPPixelGrid] initialized to all pixels off. Will never return NULL.
*
* # 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_free`.
*/
struct SPPixelGrid *sp_pixel_grid_new(size_t width,
size_t height);
/**
* Sets the value of the specified position in the [SPPixelGrid].
*
* # Arguments
*
* - `pixel_grid`: instance to write to
* - `x` and `y`: position of the cell
* - `value`: the value to write to the cell
*
* returns: old value of the cell
*
* # Panics
*
* - when `pixel_grid` is NULL
* - when accessing `x` or `y` out of bounds
*
* # Safety
*
* The caller has to make sure that:
*
* - `pixel_grid` points to a valid [SPPixelGrid]
* - `pixel_grid` is not written to or read from concurrently
*/
void sp_pixel_grid_set(struct SPPixelGrid *pixel_grid,
size_t x,
size_t y,
bool value);
/**
* Gets an unsafe reference to the data of the [SPPixelGrid] instance.
*
* # Panics
*
* - when `pixel_grid` is NULL
*
* # Safety
*
* The caller has to make sure that:
*
* - `pixel_grid` 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
*/
struct SPByteSlice sp_pixel_grid_unsafe_data_ref(struct SPPixelGrid *pixel_grid);
/**
* Gets the width in pixels of the [SPPixelGrid] instance.
*
* # Arguments
*
* - `pixel_grid`: instance to read from
*
* # Panics
*
* - when `pixel_grid` is NULL
*
* # Safety
*
* The caller has to make sure that:
*
* - `pixel_grid` points to a valid [SPPixelGrid]
*/
size_t sp_pixel_grid_width(const struct SPPixelGrid *pixel_grid);
#ifdef __cplusplus
} // extern "C"
#endif // __cplusplus

View file

@ -6,8 +6,8 @@ int main(void) {
if (connection == NULL)
return 1;
SPPixelGrid *pixels = sp_pixel_grid_new(SP_PIXEL_WIDTH, SP_PIXEL_HEIGHT);
sp_pixel_grid_fill(pixels, true);
SPBitmap *pixels = sp_bitmap_new(SP_PIXEL_WIDTH, SP_PIXEL_HEIGHT);
sp_bitmap_fill(pixels, true);
SPCommand *command = sp_command_bitmap_linear_win(0, 0, pixels, SP_COMPRESSION_CODE_UNCOMPRESSED);
while (sp_connection_send_command(connection, sp_command_clone(command)));

View file

@ -0,0 +1,290 @@
//! C functions for interacting with [SPBitmap]s
//!
//! prefix `sp_bitmap_`
use servicepoint::{DataRef, Grid};
use crate::byte_slice::SPByteSlice;
/// A grid of pixels.
///
/// # 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);
/// ```
pub struct SPBitmap(pub(crate) servicepoint::Bitmap);
/// Creates a new [SPBitmap] with the specified dimensions.
///
/// # Arguments
///
/// - `width`: size in pixels in x-direction
/// - `height`: size in pixels in y-direction
///
/// returns: [SPBitmap] initialized to all pixels off. Will never return NULL.
///
/// # 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_bitmap_free`.
#[no_mangle]
pub unsafe extern "C" fn sp_bitmap_new(
width: usize,
height: usize,
) -> *mut SPBitmap {
let result = Box::into_raw(Box::new(SPBitmap(servicepoint::Bitmap::new(
width, height,
))));
assert!(!result.is_null());
result
}
/// Loads a [SPBitmap] with the specified dimensions from the provided data.
///
/// # Arguments
///
/// - `width`: size in pixels in x-direction
/// - `height`: size in pixels in y-direction
///
/// returns: [SPBitmap] that contains a copy of the provided data. Will never return NULL.
///
/// # Panics
///
/// - when `data` is NULL
/// - 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_bitmap_free`.
#[no_mangle]
pub unsafe extern "C" fn sp_bitmap_load(
width: usize,
height: usize,
data: *const u8,
data_length: usize,
) -> *mut SPBitmap {
assert!(!data.is_null());
let data = std::slice::from_raw_parts(data, data_length);
let result = Box::into_raw(Box::new(SPBitmap(servicepoint::Bitmap::load(
width, height, data,
))));
assert!(!result.is_null());
result
}
/// Clones a [SPBitmap].
///
/// Will never return NULL.
///
/// # Panics
///
/// - when `bitmap` is NULL
///
/// # Safety
///
/// The caller has to make sure that:
///
/// - `bitmap` points to a valid [SPBitmap]
/// - `bitmap` is not written to concurrently
/// - the returned instance is freed in some way, either by using a consuming function or
/// by explicitly calling `sp_bitmap_free`.
#[no_mangle]
pub unsafe extern "C" fn sp_bitmap_clone(
bitmap: *const SPBitmap,
) -> *mut SPBitmap {
assert!(!bitmap.is_null());
let result = Box::into_raw(Box::new(SPBitmap((*bitmap).0.clone())));
assert!(!result.is_null());
result
}
/// Deallocates a [SPBitmap].
///
/// # Panics
///
/// - when `bitmap` is NULL
///
/// # Safety
///
/// The caller has to make sure that:
///
/// - `bitmap` points to a valid [SPBitmap]
/// - `bitmap` is not used concurrently or after bitmap call
/// - `bitmap` was not passed to another consuming function, e.g. to create a [SPCommand]
#[no_mangle]
pub unsafe extern "C" fn sp_bitmap_free(bitmap: *mut SPBitmap) {
assert!(!bitmap.is_null());
_ = Box::from_raw(bitmap);
}
/// Gets the current value at the specified position in the [SPBitmap].
///
/// # Arguments
///
/// - `bitmap`: instance to read from
/// - `x` and `y`: position of the cell to read
///
/// # Panics
///
/// - when `bitmap` is NULL
/// - when accessing `x` or `y` out of bounds
///
/// # Safety
///
/// The caller has to make sure that:
///
/// - `bitmap` points to a valid [SPBitmap]
/// - `bitmap` is not written to concurrently
#[no_mangle]
pub unsafe extern "C" fn sp_bitmap_get(
bitmap: *const SPBitmap,
x: usize,
y: usize,
) -> bool {
assert!(!bitmap.is_null());
(*bitmap).0.get(x, y)
}
/// Sets the value of the specified position in the [SPBitmap].
///
/// # Arguments
///
/// - `bitmap`: instance to write to
/// - `x` and `y`: position of the cell
/// - `value`: the value to write to the cell
///
/// returns: old value of the cell
///
/// # Panics
///
/// - when `bitmap` is NULL
/// - when accessing `x` or `y` out of bounds
///
/// # Safety
///
/// The caller has to make sure that:
///
/// - `bitmap` points to a valid [SPBitmap]
/// - `bitmap` is not written to or read from concurrently
#[no_mangle]
pub unsafe extern "C" fn sp_bitmap_set(
bitmap: *mut SPBitmap,
x: usize,
y: usize,
value: bool,
) {
assert!(!bitmap.is_null());
(*bitmap).0.set(x, y, value);
}
/// Sets the state of all pixels in the [SPBitmap].
///
/// # Arguments
///
/// - `bitmap`: instance to write to
/// - `value`: the value to set all pixels to
///
/// # Panics
///
/// - when `bitmap` is NULL
///
/// # Safety
///
/// The caller has to make sure that:
///
/// - `bitmap` points to a valid [SPBitmap]
/// - `bitmap` is not written to or read from concurrently
#[no_mangle]
pub unsafe extern "C" fn sp_bitmap_fill(
bitmap: *mut SPBitmap,
value: bool,
) {
assert!(!bitmap.is_null());
(*bitmap).0.fill(value);
}
/// Gets the width in pixels of the [SPBitmap] 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 [SPBitmap]
#[no_mangle]
pub unsafe extern "C" fn sp_bitmap_width(
bitmap: *const SPBitmap,
) -> usize {
assert!(!bitmap.is_null());
(*bitmap).0.width()
}
/// Gets the height in pixels of the [SPBitmap] 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 [SPBitmap]
#[no_mangle]
pub unsafe extern "C" fn sp_bitmap_height(
bitmap: *const SPBitmap,
) -> usize {
assert!(!bitmap.is_null());
(*bitmap).0.height()
}
/// Gets an unsafe reference to the data of the [SPBitmap] instance.
///
/// # Panics
///
/// - when `bitmap` is NULL
///
/// # Safety
///
/// The caller has to make sure that:
///
/// - `bitmap` points to a valid [SPBitmap]
/// - the returned memory range is never accessed after the passed [SPBitmap] has been freed
/// - the returned memory range is never accessed concurrently, either via the [SPBitmap] or directly
#[no_mangle]
pub unsafe extern "C" fn sp_bitmap_unsafe_data_ref(
bitmap: *mut SPBitmap,
) -> SPByteSlice {
assert!(!bitmap.is_null());
let data = (*bitmap).0.data_ref_mut();
SPByteSlice {
start: data.as_mut_ptr_range().start,
length: data.len(),
}
}

View file

@ -8,7 +8,7 @@ use servicepoint::{Brightness, Origin};
use crate::{
SPBitVec, SPBrightnessGrid, SPCompressionCode, SPCp437Grid, SPPacket,
SPPixelGrid,
SPBitmap,
};
/// A low-level display command.
@ -413,21 +413,21 @@ pub unsafe extern "C" fn sp_command_cp437_data(
/// Sets a window of pixels to the specified values.
///
/// The passed [SPPixelGrid] gets consumed.
/// The passed [SPBitmap] gets consumed.
///
/// Returns: a new [Command::BitmapLinearWin] instance. Will never return NULL.
///
/// # Panics
///
/// - when `pixel_grid` is null
/// - when `bitmap` is null
/// - when `compression_code` is not a valid value
///
/// # Safety
///
/// The caller has to make sure that:
///
/// - `pixel_grid` points to a valid instance of [SPPixelGrid]
/// - `pixel_grid` is not used concurrently or after this call
/// - `bitmap` points to a valid instance of [SPBitmap]
/// - `bitmap` is not used concurrently or after this call
/// - `compression` matches one of the allowed enum values
/// - the returned [SPCommand] instance is freed in some way, either by using a consuming function or
/// by explicitly calling `sp_command_free`.
@ -435,11 +435,11 @@ pub unsafe extern "C" fn sp_command_cp437_data(
pub unsafe extern "C" fn sp_command_bitmap_linear_win(
x: usize,
y: usize,
pixel_grid: *mut SPPixelGrid,
bitmap: *mut SPBitmap,
compression_code: SPCompressionCode,
) -> *mut SPCommand {
assert!(!pixel_grid.is_null());
let byte_grid = (*Box::from_raw(pixel_grid)).0;
assert!(!bitmap.is_null());
let byte_grid = (*Box::from_raw(bitmap)).0;
let result = Box::into_raw(Box::new(SPCommand(servicepoint::Command::BitmapLinearWin(
Origin::new(x, y),
byte_grid,

View file

@ -13,8 +13,8 @@
//! if (connection == NULL)
//! return 1;
//!
//! SPPixelGrid *pixels = sp_pixel_grid_new(SP_PIXEL_WIDTH, SP_PIXEL_HEIGHT);
//! sp_pixel_grid_fill(pixels, true);
//! SPBitmap *pixels = sp_bitmap_new(SP_PIXEL_WIDTH, SP_PIXEL_HEIGHT);
//! sp_bitmap_fill(pixels, true);
//!
//! SPCommand *command = sp_command_bitmap_linear_win(0, 0, pixels, Uncompressed);
//! while (sp_connection_send_command(connection, sp_command_clone(command)));
@ -33,7 +33,7 @@ pub use crate::connection::*;
pub use crate::constants::*;
pub use crate::cp437_grid::*;
pub use crate::packet::*;
pub use crate::pixel_grid::*;
pub use crate::bitmap::*;
mod bit_vec;
mod brightness_grid;
@ -43,4 +43,4 @@ mod connection;
mod constants;
mod cp437_grid;
mod packet;
mod pixel_grid;
mod bitmap;

View file

@ -1,290 +0,0 @@
//! C functions for interacting with [SPPixelGrid]s
//!
//! prefix `sp_pixel_grid_`
use servicepoint::{DataRef, Grid};
use crate::byte_slice::SPByteSlice;
/// 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_free(grid);
/// ```
pub struct SPPixelGrid(pub(crate) servicepoint::PixelGrid);
/// Creates a new [SPPixelGrid] with the specified dimensions.
///
/// # Arguments
///
/// - `width`: size in pixels in x-direction
/// - `height`: size in pixels in y-direction
///
/// returns: [SPPixelGrid] initialized to all pixels off. Will never return NULL.
///
/// # 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_free`.
#[no_mangle]
pub unsafe extern "C" fn sp_pixel_grid_new(
width: usize,
height: usize,
) -> *mut SPPixelGrid {
let result = Box::into_raw(Box::new(SPPixelGrid(servicepoint::PixelGrid::new(
width, height,
))));
assert!(!result.is_null());
result
}
/// Loads a [SPPixelGrid] with the specified dimensions from the provided data.
///
/// # Arguments
///
/// - `width`: size in pixels in x-direction
/// - `height`: size in pixels in y-direction
///
/// returns: [SPPixelGrid] that contains a copy of the provided data. Will never return NULL.
///
/// # Panics
///
/// - when `data` is NULL
/// - 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_free`.
#[no_mangle]
pub unsafe extern "C" fn sp_pixel_grid_load(
width: usize,
height: usize,
data: *const u8,
data_length: usize,
) -> *mut SPPixelGrid {
assert!(!data.is_null());
let data = std::slice::from_raw_parts(data, data_length);
let result = Box::into_raw(Box::new(SPPixelGrid(servicepoint::PixelGrid::load(
width, height, data,
))));
assert!(!result.is_null());
result
}
/// Clones a [SPPixelGrid].
///
/// Will never return NULL.
///
/// # Panics
///
/// - when `pixel_grid` is NULL
///
/// # Safety
///
/// The caller has to make sure that:
///
/// - `pixel_grid` points to a valid [SPPixelGrid]
/// - `pixel_grid` 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_free`.
#[no_mangle]
pub unsafe extern "C" fn sp_pixel_grid_clone(
pixel_grid: *const SPPixelGrid,
) -> *mut SPPixelGrid {
assert!(!pixel_grid.is_null());
let result = Box::into_raw(Box::new(SPPixelGrid((*pixel_grid).0.clone())));
assert!(!result.is_null());
result
}
/// Deallocates a [SPPixelGrid].
///
/// # Panics
///
/// - when `pixel_grid` is NULL
///
/// # Safety
///
/// The caller has to make sure that:
///
/// - `pixel_grid` points to a valid [SPPixelGrid]
/// - `pixel_grid` is not used concurrently or after pixel_grid call
/// - `pixel_grid` was not passed to another consuming function, e.g. to create a [SPCommand]
#[no_mangle]
pub unsafe extern "C" fn sp_pixel_grid_free(pixel_grid: *mut SPPixelGrid) {
assert!(!pixel_grid.is_null());
_ = Box::from_raw(pixel_grid);
}
/// Gets the current value at the specified position in the [SPPixelGrid].
///
/// # Arguments
///
/// - `pixel_grid`: instance to read from
/// - `x` and `y`: position of the cell to read
///
/// # Panics
///
/// - when `pixel_grid` is NULL
/// - when accessing `x` or `y` out of bounds
///
/// # Safety
///
/// The caller has to make sure that:
///
/// - `pixel_grid` points to a valid [SPPixelGrid]
/// - `pixel_grid` is not written to concurrently
#[no_mangle]
pub unsafe extern "C" fn sp_pixel_grid_get(
pixel_grid: *const SPPixelGrid,
x: usize,
y: usize,
) -> bool {
assert!(!pixel_grid.is_null());
(*pixel_grid).0.get(x, y)
}
/// Sets the value of the specified position in the [SPPixelGrid].
///
/// # Arguments
///
/// - `pixel_grid`: instance to write to
/// - `x` and `y`: position of the cell
/// - `value`: the value to write to the cell
///
/// returns: old value of the cell
///
/// # Panics
///
/// - when `pixel_grid` is NULL
/// - when accessing `x` or `y` out of bounds
///
/// # Safety
///
/// The caller has to make sure that:
///
/// - `pixel_grid` points to a valid [SPPixelGrid]
/// - `pixel_grid` is not written to or read from concurrently
#[no_mangle]
pub unsafe extern "C" fn sp_pixel_grid_set(
pixel_grid: *mut SPPixelGrid,
x: usize,
y: usize,
value: bool,
) {
assert!(!pixel_grid.is_null());
(*pixel_grid).0.set(x, y, value);
}
/// Sets the state of all pixels in the [SPPixelGrid].
///
/// # Arguments
///
/// - `pixel_grid`: instance to write to
/// - `value`: the value to set all pixels to
///
/// # Panics
///
/// - when `pixel_grid` is NULL
///
/// # Safety
///
/// The caller has to make sure that:
///
/// - `pixel_grid` points to a valid [SPPixelGrid]
/// - `pixel_grid` is not written to or read from concurrently
#[no_mangle]
pub unsafe extern "C" fn sp_pixel_grid_fill(
pixel_grid: *mut SPPixelGrid,
value: bool,
) {
assert!(!pixel_grid.is_null());
(*pixel_grid).0.fill(value);
}
/// Gets the width in pixels of the [SPPixelGrid] instance.
///
/// # Arguments
///
/// - `pixel_grid`: instance to read from
///
/// # Panics
///
/// - when `pixel_grid` is NULL
///
/// # Safety
///
/// The caller has to make sure that:
///
/// - `pixel_grid` points to a valid [SPPixelGrid]
#[no_mangle]
pub unsafe extern "C" fn sp_pixel_grid_width(
pixel_grid: *const SPPixelGrid,
) -> usize {
assert!(!pixel_grid.is_null());
(*pixel_grid).0.width()
}
/// Gets the height in pixels of the [SPPixelGrid] instance.
///
/// # Arguments
///
/// - `pixel_grid`: instance to read from
///
/// # Panics
///
/// - when `pixel_grid` is NULL
///
/// # Safety
///
/// The caller has to make sure that:
///
/// - `pixel_grid` points to a valid [SPPixelGrid]
#[no_mangle]
pub unsafe extern "C" fn sp_pixel_grid_height(
pixel_grid: *const SPPixelGrid,
) -> usize {
assert!(!pixel_grid.is_null());
(*pixel_grid).0.height()
}
/// Gets an unsafe reference to the data of the [SPPixelGrid] instance.
///
/// # Panics
///
/// - when `pixel_grid` is NULL
///
/// # Safety
///
/// The caller has to make sure that:
///
/// - `pixel_grid` 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(
pixel_grid: *mut SPPixelGrid,
) -> SPByteSlice {
assert!(!pixel_grid.is_null());
let data = (*pixel_grid).0.data_ref_mut();
SPByteSlice {
start: data.as_mut_ptr_range().start,
length: data.len(),
}
}