pixel_widgets/
graphics.rs

1use std::sync::{Arc, Mutex};
2
3use anyhow::*;
4
5use crate::cache::Cache;
6use crate::draw::{ImageData, Patch};
7
8/// Cloneable image loader
9pub struct Graphics {
10    pub(crate) cache: Arc<Mutex<Cache>>,
11}
12
13impl Graphics {
14    /// Loads an image
15    pub fn load_image<B: AsRef<[u8]>>(&self, bytes: B) -> Result<ImageData> {
16        let image = image::load_from_memory(bytes.as_ref())?;
17        let image = self.cache.lock().unwrap().load_image(image.into_rgba8());
18        Ok(image)
19    }
20
21    /// Loads a 9 patch.
22    pub fn load_patch<B: AsRef<[u8]>>(&self, bytes: B) -> Result<Patch> {
23        let image = image::load_from_memory(bytes.as_ref())?;
24        let image = self.cache.lock().unwrap().load_patch(image.into_rgba8());
25        Ok(image)
26    }
27}
28
29impl Clone for Graphics {
30    fn clone(&self) -> Self {
31        Self {
32            cache: self.cache.clone(),
33        }
34    }
35}