mirror of
https://github.com/penpot/penpot.git
synced 2025-06-14 17:01:38 +02:00
♻️ Create an ImageStore type
This commit is contained in:
parent
967bc75a1c
commit
7b1934dcb6
4 changed files with 48 additions and 26 deletions
33
render-wasm/src/render/images.rs
Normal file
33
render-wasm/src/render/images.rs
Normal 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)
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue