Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use proper image crate URI and MIME support detection #5324

Merged
merged 2 commits into from
Nov 1, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 11 additions & 5 deletions crates/egui_extras/src/loaders/image_loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use egui::{
mutex::Mutex,
ColorImage,
};
use image::ImageFormat;
use std::{mem::size_of, path::Path, sync::Arc};

type Entry = Result<Arc<ColorImage>, String>;
Expand All @@ -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 {
Expand Down Expand Up @@ -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"));
}
Expand Down
Loading