1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
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 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: data.premultiplied_alpha as u8,
_rusty_handle: Box::into_raw(Box::new(data)),
})),
Err(_) => std::ptr::null()
}
}
#[no_mangle]
pub unsafe extern 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 as *mut avif_data_t);
}