rusteguieframe

Render an image from a pixel buffer in egui


I am trying to add an image to my window from a pixel buffer in memory. (right now I am using cpu memory because I'm on a macbook anyway).

I don't want the image to be cached because this will be the single frame of a video (but I'm trying to make it work with a single image first, for the same reason I'll start with an hardcoded pixel buffer to begin with).

I will cut the eframe/egui bolierplate ang go to the main part inside the update fn:

egui_extras::install_image_loaders(ctx);

...

ui.heading("Video player");
let pixels = vec![0; 10_000]; // 100x100 8-bit rgb image (all black)
let img = egui::Image::from_bytes("bytes://foo", pixels);
ui.add(img);

I have added egui_extras of course. Now when I run the app I can see the window with the heading but instead of the black rectangle this warning is shown:

enter image description here

Some of my questions regarding the API remain unanswered also due to the lack of documentation and examples:

  1. I am creating an image from a pixel buffer but am not specifying size of the image nor pixel format, which one is the default?
  2. What is the uri parameter of egui::Image::from_bytes() for? I have filled it with a dummy String but its purpose is obscure to me. (There is no description in the docs)
  3. What is causing the error? I have added the required crate and called the setup method install_image_loaders(ctx)

Solution

  • You can use a ColorImage and then supply this to a Texture. An example can be found in this noise-functions-demo source, which has this form (edited down to the essence):

        let pixels: Vec<egui::Color32> = ...;
        // loop to fill out pixels {
        // let color: egui::Color32 = ...;
        // pixels[i] = color;
    
        texture.set(
                egui::ColorImage {
                    size: ...,
                    pixels: pixels,
                },
                egui::TextureOptions::NEAREST,
        );
    
        let size = texture.size_vec2();
        let sized_texture = egui::load::SizedTexture::new(texture, size);
        ui.add(egui::Image::new(sized_texture).fit_to_exact_size(size));