adds a image loader for DynamicImage/rgba8/gif
The macro converts the dynamic_image to rgba8 bytes and the rest is handled by the new image loader(only processes Uris that start with "rgba8://"
let img = image::load_from_memory(include_bytes!("../../testimg.png").as_slice()).unwrap();
let v = include_dynamic_image!("testimg", img);
let img = Image::new(v);
the use case I intended for it is to display gifs. an example for that would be:
Image::new_gif("testgif", gif("testgif"))
use egui::ImageSource;
use image::codecs::gif::GifDecoder;
use image::{AnimationDecoder, EncodableLayout, ImageDecoder};
use std::borrow::Cow;
use std::fs::File;
use std::time::Duration;
pub fn gif(uri: &str) -> Vec<(ImageSource<'static>, Duration)> {
let file_in = File::open("test.gif").unwrap();
let decoder = GifDecoder::new(file_in).unwrap();
let (width, height) = decoder.dimensions();
let mut res = vec![];
for (index, frame) in decoder.into_frames().enumerate() {
let frame = frame.unwrap();
let img = frame.buffer();
let mut bytes = vec![];
bytes.extend_from_slice(&(width as usize).to_le_bytes());
bytes.extend_from_slice(&(height as usize).to_le_bytes());
bytes.extend_from_slice(img.as_bytes());
let delay: Duration = frame.delay().into();
res.push((
ImageSource::Bytes {
uri: Cow::Owned(format!("rgba8://{}-{}", uri, index)),
bytes: bytes.into(),
},
delay,
));
}
res
}
I would like to ask what you would prefer. a custom GifImage component or Image with the ability to display gifs.
I have modified the Image component. It seems to be mostly working. the only issue is that ctx.request_repaint_after(dur); freezes the app. Im not quite sure if it should request the repaint, since there could be multiple gifs which would request too many repaints. it might make sense to run egui in continuous instead of reactive when using gifs. It would be great if you could tell me what's best for performance
@emilk is this fine?