Get ImageSource from TextureHandle

Any hint on how to get ImageSource from TextureHandle . The issue came along while compiling a crate with updated dependecies.

    pub fn draw(&mut self, ui: &mut Ui) {
        let texture: &mut TextureHandle = self.tex_handle.get_or_insert_with(|| {
            // Load the texture only once.
            ui.ctx()
                .load_texture("my-image", self.render_image.clone(), TextureOptions::LINEAR)
        });

        self.renderer
            .update_result(&mut self.render_image);

        texture.set(self.render_image.clone(), TextureOptions::LINEAR);
        ui.vertical(|ui| {
            if self.renderer.waiting_for_next_frame {
                let pb = ProgressBar::new(self.renderer.progress as f32)
                    .animate(self.renderer.waiting_for_next_frame)
                    .show_percentage();
                pb.ui(ui);
            }
            ui.image(texture, ui.available_size()); // COMPILATION error here
            //ui.image(texture);
        });
    }

You have to give more details, such as:

  • The crate(s) that you are using
  • The compiler error
1 Like

The following crates have been updated to the recent version

[dependencies]

egui = { version = "0.25.0" }

eframe = { version = "0.25.0" }

rand = "0.8.5"

And since then , I have the compilation error

error[E0277]: the trait bound SizedTexture: From<&mut TextureHandle> is not satisfied
--> src\app\ui.rs:52:22
|
52 | ui.image(texture);
| ----- ^^^^^^^ the trait From<&mut TextureHandle> is not implemented for SizedTexture
| |
| required by a bound introduced by this call
|
= help: the following other types implement trait From<T>:
<SizedTexture as From<&'a TextureHandle>>
<SizedTexture as From<(TextureId, egui::Vec2)>>
= note: required for &mut TextureHandle to implement Into<SizedTexture>
= note: required for ImageSource<'_> to implement From<&mut TextureHandle>
= note: 1 redundant requirement hidden
= note: required for &mut TextureHandle to implement Into<ImageSource<'_>>
note: required by a bound in Ui::image
--> C:\Users\a295697.cargo\registry\src\index.crates.io-6f17d22bba15001f\egui-0.25.0\src\ui.rs:1576:46
|
1576 | pub fn image<'a>(&mut self, source: impl Into<ImageSource<'a>>) -> Response {
| ^^^^^^^^^^^^^^^^^^^^^ required by this bound in Ui::image

Can you try passing the texture handle as a shared reference?

ui.image(&*texture);
1 Like

(Jinx, posting for the links)

Try:

ui.image(&*texture, ui.available_size());

Because

1 Like

It does not help . The recent version does not accept two input arguments, it accepts only one. Probably you could download the crate from here and try recompile using the latest dependencies as I mentioned above.

https://crates.io/crates/raytracer-rs

Ui::image takes a source: impl Into<ImageSource<'a>> argument, which means that source can be anything that implements Into<ImageSource<'a>>, which means "anything that can be converted into an ImageSource".

ImageSource is an enum

pub enum ImageSource<'a> {
    Uri(Cow<'a, str>),
    Texture(SizedTexture),
    Bytes {
        uri: Cow<'static, str>,
        bytes: Bytes,
    },
}

SizedTexture sounds like what we're after, since currently what's being passed is a texture and a size. A SizedTexture can be constructed with SizedTexture::new and implements Into<ImageSource<'a>>[1], so we can pass it to Ui::image as the source argument.

Something like

let texture = SizedTexture::new(texture, ui.available_size());
ui.image(texture);

should hopefully work.

In the future, it's best to list all the relevant crates clearly from the start, it's not obvious from the first post that this is related to egui, raytracer-rs or any other crate in particular.


  1. This is a little convoluted, but impl<T: Into<SizedTexture>> From<T> for ImageSource<'static> essentially makes it so all types that can be converted into a SizedTexture can also be converted into an ImageSource. SizedTexture itself can be "converted" into a SizedTexture, so a SizedTexture can also be converted into an ImageSource thanks to this impl... ↩ī¸Ž

2 Likes