use crate::AvifData as AvifDataRust;
#[allow(bad_style)]
#[repr(C)]
pub struct avif_data_t {
pub primary_data: *const u8,
pub primary_size: usize,
pub alpha_data: *const u8,
pub alpha_size: usize,
pub premultiplied_alpha: u8,
rusty_handle: *mut AvifDataRust,
}
#[no_mangle]
pub unsafe extern "C" fn avif_parse(bytes: *const u8, bytes_len: usize) -> *const avif_data_t {
if bytes.is_null() || bytes_len == 0 {
return std::ptr::null();
}
let mut data = std::slice::from_raw_parts(bytes, bytes_len);
match crate::read_avif(&mut data) {
Ok(data) => Box::into_raw(Box::new(avif_data_t {
primary_data: data.primary_item.as_ptr(),
primary_size: data.primary_item.len(),
alpha_data: data
.alpha_item
.as_ref()
.map_or(std::ptr::null(), |a| a.as_ptr()),
alpha_size: data.alpha_item.as_ref().map_or(0, |a| a.len()),
premultiplied_alpha: u8::from(data.premultiplied_alpha),
rusty_handle: Box::into_raw(Box::new(data)),
})),
Err(_) => std::ptr::null(),
}
}
#[no_mangle]
pub unsafe extern "C" fn avif_data_free(data: *const avif_data_t) {
if data.is_null() {
return;
}
let _ = Box::from_raw((*data).rusty_handle);
let _ = Box::from_raw(data.cast_mut());
}