Skip to main content

gltforge_unity/
unity_image.rs

1use crate::unity_gltf::UnityGltf;
2
3/// A glTF image entry, providing the URI needed for Unity to load the texture asset.
4pub struct UnityImage {
5    /// The image name. Falls back to the image index as a string if unnamed.
6    pub name: String,
7
8    /// The URI of the image, if it references an external file.
9    /// `None` for buffer-view-embedded images.
10    pub uri: Option<String>,
11}
12
13// ─── FFI ────────────────────────────────────────────────────────────────────
14
15/// Return the number of images.
16///
17/// # Safety
18/// `ptr` must be a valid, non-null handle.
19#[unsafe(no_mangle)]
20pub unsafe extern "C" fn gltforge_image_count(ptr: *const UnityGltf) -> u32 {
21    unsafe { &*ptr }.images.len() as u32
22}
23
24/// Return a pointer to the UTF-8 name bytes for image `image_idx`.
25/// `out_len` receives the byte length. Returns null if `image_idx` is out of range.
26///
27/// # Safety
28/// `ptr` must be a valid, non-null handle. `out_len` may be null.
29#[unsafe(no_mangle)]
30pub unsafe extern "C" fn gltforge_image_name(
31    ptr: *const UnityGltf,
32    image_idx: u32,
33    out_len: *mut u32,
34) -> *const u8 {
35    unsafe { crate::write_name({ &*ptr }.images.get(&image_idx).map(|i| &i.name), out_len) }
36}
37
38/// Return a pointer to the UTF-8 URI bytes for image `image_idx`.
39/// `out_len` receives the byte length.
40/// Returns null if `image_idx` is out of range or the image is buffer-view-embedded.
41///
42/// # Safety
43/// `ptr` must be a valid, non-null handle. `out_len` may be null.
44#[unsafe(no_mangle)]
45pub unsafe extern "C" fn gltforge_image_uri(
46    ptr: *const UnityGltf,
47    image_idx: u32,
48    out_len: *mut u32,
49) -> *const u8 {
50    unsafe {
51        crate::write_name(
52            { &*ptr }
53                .images
54                .get(&image_idx)
55                .and_then(|i| i.uri.as_ref()),
56            out_len,
57        )
58    }
59}