Use proper `image` crate URI and MIME support detection (#5324)

<!--
Please read the "Making a PR" section of
[`CONTRIBUTING.md`](https://github.com/emilk/egui/blob/master/CONTRIBUTING.md)
before opening a Pull Request!

* Keep your PR:s small and focused.
* The PR title is what ends up in the changelog, so make it descriptive!
* If applicable, add a screenshot or gif.
* If it is a non-trivial addition, consider adding a demo for it to
`egui_demo_lib`, or a new example.
* Do NOT open PR:s from your `master` branch, as that makes it hard for
maintainers to test and add commits to your PR.
* Remember to run `cargo fmt` and `cargo clippy`.
* Open the PR as a draft until you have self-reviewed it and run
`./scripts/check.sh`.
* When you have addressed a PR comment, mark it as resolved.

Please be patient! I will review your PR, but my time is limited!
-->

* [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.
This commit is contained in:
Cody Neiman 2024-11-01 08:33:12 -04:00 committed by GitHub
parent 3f5cd74de7
commit 21826bec18
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 11 additions and 5 deletions

View File

@ -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>;
@ -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"));
}