#![cfg(feature = "image")]
use once_cell::sync::Lazy;
use std::collections::HashMap;
use std::sync::{Arc, Mutex};
use vello::peniko::{Blob, Extend, Format, Image as PenikoImage};
static IMAGE_CACHE: Lazy<Mutex<HashMap<String, Arc<PenikoImage>>>> =
Lazy::new(|| Mutex::new(HashMap::new()));
pub struct ImageManager;
impl ImageManager {
pub fn get_image(path: &str) -> Option<Arc<PenikoImage>> {
let mut cache = IMAGE_CACHE.lock().unwrap();
if let Some(img) = cache.get(path) {
return Some(img.clone());
}
match image::open(path) {
Ok(img) => {
let rgba = img.to_rgba8();
let (width, height) = rgba.dimensions();
let data = Arc::new(rgba.into_raw());
let peniko_img = Arc::new(PenikoImage {
data: Blob::new(data),
format: Format::Rgba8,
width,
height,
extend: Extend::Pad,
});
cache.insert(path.to_string(), peniko_img.clone());
return Some(peniko_img);
}
Err(e) => {
eprintln!("Error: Failed to load image at '{}': {}", path, e);
}
}
None
}
}