♻️ Create an ImageStore type

This commit is contained in:
Belén Albeza 2024-12-10 15:41:50 +01:00
parent 967bc75a1c
commit 7b1934dcb6
4 changed files with 48 additions and 26 deletions

View file

@ -0,0 +1,33 @@
use skia_safe as skia;
use std::collections::HashMap;
use uuid::Uuid;
pub type Image = skia::Image;
pub struct ImageStore {
images: HashMap<Uuid, Image>,
}
impl ImageStore {
pub fn new() -> Self {
Self {
images: HashMap::with_capacity(2048),
}
}
pub fn add(&mut self, id: Uuid, image_data: &[u8]) -> Result<(), String> {
let image_data = skia::Data::new_copy(image_data);
let image = Image::from_encoded(image_data).ok_or("Error decoding image data")?;
self.images.insert(id, image);
Ok(())
}
pub fn contains(&mut self, id: &Uuid) -> bool {
self.images.contains_key(id)
}
pub fn get(&self, id: &Uuid) -> Option<&Image> {
self.images.get(id)
}
}