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

adds a image loader for DynamicImage/rgba8/gif #3951

Closed
wants to merge 0 commits into from
Closed

adds a image loader for DynamicImage/rgba8/gif #3951

wants to merge 0 commits into from

Conversation

JustFrederik
Copy link
Contributor

@JustFrederik JustFrederik commented Feb 2, 2024

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);

@JustFrederik
Copy link
Contributor Author

JustFrederik commented Feb 2, 2024

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

@JustFrederik JustFrederik changed the title adds a image loader for DynamicImage adds a image loader for DynamicImage/rgba8 Feb 2, 2024
@JustFrederik JustFrederik changed the title adds a image loader for DynamicImage/rgba8 adds a image loader for DynamicImage/rgba8/gif Feb 2, 2024
crates/egui_extras/src/image.rs Outdated Show resolved Hide resolved
crates/egui_extras/src/image.rs Outdated Show resolved Hide resolved
@JustFrederik JustFrederik requested a review from emilk February 13, 2024 10:48
crates/egui_extras/Cargo.toml Outdated Show resolved Hide resolved
crates/egui_extras/src/image.rs Outdated Show resolved Hide resolved
crates/egui_extras/src/loaders.rs Outdated Show resolved Hide resolved
@JustFrederik JustFrederik requested a review from emilk February 28, 2024 15:59
crates/egui/src/widgets/image.rs Outdated Show resolved Hide resolved
crates/egui/src/widgets/image.rs Outdated Show resolved Hide resolved
crates/egui/src/widgets/image.rs Outdated Show resolved Hide resolved
crates/egui/src/widgets/image.rs Outdated Show resolved Hide resolved
crates/egui/src/widgets/image.rs Outdated Show resolved Hide resolved
crates/egui/src/widgets/image.rs Outdated Show resolved Hide resolved
crates/egui/src/widgets/image.rs Outdated Show resolved Hide resolved
crates/egui/src/widgets/image.rs Outdated Show resolved Hide resolved
crates/egui_extras/src/loaders.rs Outdated Show resolved Hide resolved
@JustFrederik JustFrederik requested a review from emilk March 31, 2024 12:27
crates/egui/src/widgets/image.rs Outdated Show resolved Hide resolved
crates/egui/src/widgets/image.rs Outdated Show resolved Hide resolved
crates/egui/src/widgets/image.rs Outdated Show resolved Hide resolved
crates/egui_extras/src/loaders/raw_image_loader.rs Outdated Show resolved Hide resolved
crates/egui_extras/src/image.rs Outdated Show resolved Hide resolved
crates/egui/src/widgets/image.rs Outdated Show resolved Hide resolved
crates/egui/src/widgets/image.rs Outdated Show resolved Hide resolved
@JustFrederik JustFrederik requested a review from emilk April 22, 2024 08:59
@JustFrederik JustFrederik mentioned this pull request May 18, 2024
@JustFrederik
Copy link
Contributor Author

@emilk is this fine?


impl eframe::App for MyApp {
fn update(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) {
egui::CentralPanel::default().show(ctx, |ui| {
egui::ScrollArea::both().show(ui, |ui| {
ui.add(self.gif.clone());
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would this work too?

Suggested change
ui.add(self.gif.clone());
ui.image(egui_extras::include_gif!("ferris.gif"));

That is, will this work without reloading the gif each frame? This is how we want it to work for it to be consistent with all other image formats

@JustFrederik JustFrederik mentioned this pull request Jun 5, 2024
emilk added a commit that referenced this pull request Jun 19, 2024
* Previous PR: #3951 
* Closes #4489

---------

Co-authored-by: Emil Ernerfeldt <[email protected]>
hacknus pushed a commit to hacknus/egui that referenced this pull request Oct 30, 2024
* Previous PR: emilk#3951 
* Closes emilk#4489

---------

Co-authored-by: Emil Ernerfeldt <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants