siglus_scene_vm/assets/
mod.rs1pub mod g00;
2
3use anyhow::{Context, Result, bail};
4use std::path::Path;
5
6#[derive(Clone, Debug)]
8pub struct RgbaImage {
9 pub width: u32,
10 pub height: u32,
11 pub center_x: i32,
15 pub center_y: i32,
16 pub rgba: Vec<u8>,
18}
19
20pub fn load_image_any(path: &Path, g00_frame_index: usize) -> Result<RgbaImage> {
28 let ext = path
29 .extension()
30 .and_then(|s| s.to_str())
31 .unwrap_or("")
32 .to_ascii_lowercase();
33
34 match ext.as_str() {
35 "g00" => {
36 let bytes = crate::resource::read_file_bytes(path)
37 .with_context(|| format!("read {:?}", path))?;
38 let decoded =
39 g00::decode_g00(&bytes).with_context(|| format!("decode g00 {:?}", path))?;
40 if decoded.frames.is_empty() {
41 bail!("g00 has no frames: {:?}", path);
42 }
43 if g00_frame_index >= decoded.frames.len() {
44 bail!(
45 "g00 frame index out of range: {:?} index={} count={}",
46 path,
47 g00_frame_index,
48 decoded.frames.len()
49 );
50 }
51 Ok(decoded.frames[g00_frame_index].clone())
52 }
53 "png" | "jpg" | "jpeg" | "bmp" | "dds" => {
54 #[cfg(all(target_arch = "wasm32", target_os = "unknown"))]
55 let img = {
56 let bytes = crate::resource::read_file_bytes(path)
57 .with_context(|| format!("read image {:?}", path))?;
58 image::load_from_memory(&bytes)
59 .with_context(|| format!("decode image {:?}", path))?
60 };
61 #[cfg(not(all(target_arch = "wasm32", target_os = "unknown")))]
62 let img = image::open(path).with_context(|| format!("decode image {:?}", path))?;
63 let rgba = img.to_rgba8();
64 let (w, h) = rgba.dimensions();
65 Ok(RgbaImage {
66 width: w,
67 height: h,
68 center_x: 0,
69 center_y: 0,
70 rgba: rgba.into_raw(),
71 })
72 }
73 _ => {
74 bail!("unsupported image extension: {:?}", path);
75 }
76 }
77}