Fix image_loader for animated image types (#5688)

Hi, after upgrading to 0.31.0 all of my beautiful static webp images
started failing to load. I use the image_loader to load those via the
`image` crate.

I noticed that with 0.31.0 there are additions to how animated image
types are handled with frames and such. And with those changes the frame
index is attached to the uri at the end. This was problematic for the
image_loader, because it wasn't updated to handle that frame tag at the
end of the uri, so when looking up the bytes, it would fail to match the
uri in the bytes cache (the bytes were being saved without the frame
index, but attempting to be fetched _with_ the frame index).

This fixes the image_loader for me with webp & gif. They don't load the
animations, but I think that is because I don't have the custom
image_loader set up so I'm not worried about that for myself. I'm not
sure if that part is problematic in general, or if its just the way I
have my features set up.

You can recreate the issue on master by swapping out the dependency
features in the `images` example like this:
```
# egui_extras = { workspace = true, features = ["default", "all_loaders"] }
# env_logger = { version = "0.10", default-features = false, features = [
#   "auto-color",
#   "humantime",
# ] }
# image = { workspace = true, features = ["jpeg", "png"] }
egui_extras = { workspace = true, features = ["image", "svg"] }
env_logger = { version = "0.10", default-features = false, features = [
  "auto-color",
  "humantime",
] }
image = { workspace = true, features = ["jpeg", "png", "webp", "gif"] }
```

* [x] I have followed the instructions in the PR template

---------

Co-authored-by: lucasmerlin <lucasmeurer96@gmail.com>
This commit is contained in:
Braden Steffaniak 2025-02-18 11:30:50 -05:00 committed by GitHub
parent a8e98d3f9b
commit 770c976ed7
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 6 additions and 0 deletions

View File

@ -1,5 +1,6 @@
use ahash::HashMap;
use egui::{
decode_animated_image_uri,
load::{BytesPoll, ImageLoadResult, ImageLoader, ImagePoll, LoadError, SizeHint},
mutex::Mutex,
ColorImage,
@ -58,6 +59,11 @@ impl ImageLoader for ImageCrateLoader {
// 2. Mime from `BytesPoll::Ready`
// 3. image::guess_format (used internally by image::load_from_memory)
// TODO(lucasmerlin): Egui currently changes all URIs for webp and gif files to include
// the frame index (#0), which breaks if the animated image loader is disabled.
// We work around this by removing the frame index from the URI here
let uri = decode_animated_image_uri(uri).map_or(uri, |(uri, _frame_index)| uri);
// (1)
if uri.starts_with("file://") && !is_supported_uri(uri) {
return Err(LoadError::NotSupported);