From 21826bec18a11a51e1dc8e69733f105b63de1171 Mon Sep 17 00:00:00 2001 From: Cody Neiman Date: Fri, 1 Nov 2024 08:33:12 -0400 Subject: [PATCH] Use proper `image` crate URI and MIME support detection (#5324) * [X] I have followed the instructions in the PR template I removed the webp supported URI test given that the webp feature would have to be enabled. I kept that webp is not supported in the other tests. There might need to be an additional warning in the changelog that image support detection is now stricter. --- crates/egui_extras/src/loaders/image_loader.rs | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/crates/egui_extras/src/loaders/image_loader.rs b/crates/egui_extras/src/loaders/image_loader.rs index 14086df9..8c2c4970 100644 --- a/crates/egui_extras/src/loaders/image_loader.rs +++ b/crates/egui_extras/src/loaders/image_loader.rs @@ -4,6 +4,7 @@ use egui::{ mutex::Mutex, ColorImage, }; +use image::ImageFormat; use std::{mem::size_of, path::Path, sync::Arc}; type Entry = Result, String>; @@ -18,18 +19,24 @@ impl ImageCrateLoader { } fn is_supported_uri(uri: &str) -> bool { - // TODO(emilk): use https://github.com/image-rs/image/pull/2038 when new `image` crate is released. let Some(ext) = Path::new(uri).extension().and_then(|ext| ext.to_str()) else { // `true` because if there's no extension, assume that we support it return true; }; - ext != "svg" + // Uses only the enabled image crate features + ImageFormat::all() + .filter(ImageFormat::reading_enabled) + .flat_map(ImageFormat::extensions_str) + .any(|format_ext| ext == *format_ext) } fn is_unsupported_mime(mime: &str) -> bool { - // TODO(emilk): use https://github.com/image-rs/image/pull/2038 when new `image` crate is released. - mime.contains("svg") + // Uses only the enabled image crate features + !ImageFormat::all() + .filter(ImageFormat::reading_enabled) + .map(|fmt| fmt.to_mime_type()) + .any(|format_mime| mime == format_mime) } impl ImageLoader for ImageCrateLoader { @@ -108,7 +115,6 @@ mod tests { assert!(is_supported_uri("https://test.png")); assert!(is_supported_uri("test.jpeg")); assert!(is_supported_uri("http://test.gif")); - assert!(is_supported_uri("test.webp")); assert!(is_supported_uri("file://test")); assert!(!is_supported_uri("test.svg")); }