use crate::{size, DevicePixels, Result, SharedString, Size}; use anyhow::anyhow; use image::{Bgra, ImageBuffer}; use std::{ borrow::Cow, fmt, sync::atomic::{AtomicUsize, Ordering::SeqCst}, }; pub trait AssetSource: 'static + Send + Sync { fn load(&self, path: &SharedString) -> Result>; fn list(&self, path: &SharedString) -> Result>; } impl AssetSource for () { fn load(&self, path: &SharedString) -> Result> { Err(anyhow!( "get called on empty asset provider with \"{}\"", path )) } fn list(&self, _path: &SharedString) -> Result> { Ok(vec![]) } } pub struct ImageData { pub id: usize, data: ImageBuffer, Vec>, } impl ImageData { pub fn from_raw(size: Size, bytes: Vec) -> Self { static NEXT_ID: AtomicUsize = AtomicUsize::new(0); Self { id: NEXT_ID.fetch_add(1, SeqCst), data: ImageBuffer::from_raw(size.width.into(), size.height.into(), bytes).unwrap(), } } pub fn as_bytes(&self) -> &[u8] { &self.data } pub fn into_bytes(self) -> Vec { self.data.into_raw() } pub fn size(&self) -> Size { let (width, height) = self.data.dimensions(); size(width.into(), height.into()) } } impl fmt::Debug for ImageData { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { f.debug_struct("ImageData") .field("id", &self.id) .field("size", &self.data.dimensions()) .finish() } }