add possibility to get a reference to the raw data of PixelGrid

This commit is contained in:
Vinzenz Schroeter 2024-05-13 01:26:44 +02:00
parent 10e6138756
commit ea7061db7f
7 changed files with 75 additions and 13 deletions

View file

@ -75,6 +75,10 @@ impl BitVec {
self.data.len() * 8
}
pub fn data_ref(&self) -> &[u8] {
&*self.data
}
fn get_indexes(&self, index: usize) -> (usize, u8) {
let byte_index = index / 8;
let bit_in_byte_index = 7 - index % 8;
@ -160,7 +164,7 @@ pub mod c_api {
/// Gets the length of the `BitVec` in bits.
#[no_mangle]
pub unsafe extern "C" fn sp2_bit_vec_len(this: *const BitVec) -> usize{
pub unsafe extern "C" fn sp2_bit_vec_len(this: *const BitVec) -> usize {
(*this).len()
}
}

View file

@ -76,6 +76,10 @@ impl PixelGrid {
pub fn fill(&mut self, value: bool) {
self.bit_vec.fill(value);
}
pub fn data_ref(&self) -> &[u8] {
self.bit_vec.data_ref()
}
}
impl Into<Vec<u8>> for PixelGrid {
@ -148,4 +152,11 @@ pub mod c_api
pub unsafe extern "C" fn sp2_pixel_grid_height(this: *const PixelGrid) -> usize {
(*this).height
}
/// Gets a reference to the data of the `PixelGrid` instance.
#[no_mangle]
pub unsafe extern "C" fn sp2_pixel_grid_data_ref(this: *const PixelGrid) -> *const u8 {
// TODO: also return length
(*this).data_ref().as_ptr_range().start
}
}