Guess image format from file extension

This commit is contained in:
Benoît du Garreau 2021-05-06 17:49:25 +02:00
parent 8ae1d106cf
commit 77b086c242

View File

@ -98,8 +98,13 @@ impl Image {
pub struct ImageLoader;
impl Loader<Image> for ImageLoader {
fn load(content: Cow<[u8]>, _: &str) -> Result<Image, BoxedError> {
let image = image::load_from_memory(&content)?;
fn load(content: Cow<[u8]>, ext: &str) -> Result<Image, BoxedError> {
let format = match ext {
"png" => image::ImageFormat::Png,
"jpg" => image::ImageFormat::Jpeg,
_ => return Err("unknown image format".into()),
};
let image = image::load_from_memory_with_format(&content, format)?;
Ok(Image(Arc::new(image)))
}
}