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

Texture loading in egui #1110

Merged
merged 15 commits into from Jan 15, 2022
Merged

Texture loading in egui #1110

merged 15 commits into from Jan 15, 2022

Conversation

emilk
Copy link
Owner

@emilk emilk commented Jan 15, 2022

This continues #999 by moving texture allocation from epi into egui proper.

With this PR you can convert an image into a texture using egui::Context::load_texture. This returns an egui::TextureHandle which can be used with e.g. ui.image(…). The TextureHandle uses ref-counting, and when the last TextureHandle is dropped, the texture is freed. This makes it far more ergonomic and convenient to show images in egui.

Here is an example of how to use it: https://github.com/emilk/egui/blob/master/eframe/examples/image.rs

Backend integrations need to adapt by handling the new epaint::TexturesDelta each frame, a struct that specifies what textures were loaded and freed each frame.

@emilk emilk changed the title Texture refactor Texture loading in egui Jan 15, 2022
@emilk emilk merged commit 66d80e2 into master Jan 15, 2022
@emilk emilk deleted the texture-refactor branch January 15, 2022 12:59
@yuribudilov
Copy link

Hello

I can not get eframe example (image.rs) to compile, there is no egui::ColorImage found and a few other errors.
Using latest eframe/egui/epi 0.16.0 in Cargo.toml.

Also, image:: did not compile so I included image crate (0.24.0) in my Cargo.toml, I dont know if this is correct.

Sorry, I am totally new to egui/eframe/epi and the examples in eframe are just fragments of code, not complete cargo projects.

fn load_image(image_data: &[u8]) -> Result<egui::ColorImage, image::ImageError> {
    use image::GenericImageView as _;
    let image = image::load_from_memory(image_data)?;
    let size = [image.width() as _, image.height() as _];
    let image_buffer = image.to_rgba8();
    let pixels = image_buffer.as_flat_samples();
    Ok(egui::ColorImage::from_rgba_unmultiplied(
        size,
        pixels.as_slice(),
    ))
}


thank for for any assistance

@Born2Go
Copy link

Born2Go commented Feb 21, 2022

I struggled with this too, in my case I managed to make it work using eframe::epi::Image
I think this struct have been renamed/moved and the examples not updated yet

@emilk
Copy link
Owner Author

emilk commented Feb 21, 2022

Soon 0.17.0 is released and this will become a lot simpler by using egui_extras::RetainedImage (#1282)

andersk added a commit to andersk/egui that referenced this pull request May 18, 2023
TextureId::Epaint was renamed to TextureId::Managed during the
development of emilk#1110.  Update the documentation to match.

Signed-off-by: Anders Kaseorg <andersk@mit.edu>
emilk pushed a commit that referenced this pull request May 22, 2023
TextureId::Epaint was renamed to TextureId::Managed during the
development of #1110.  Update the documentation to match.

Signed-off-by: Anders Kaseorg <andersk@mit.edu>
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.

None yet

3 participants