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

Handle static RGB WebP images #5586

Merged
merged 1 commit into from
Jan 7, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 16 additions & 5 deletions crates/egui_extras/src/loaders/webp_loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use egui::{
mutex::Mutex,
ColorImage, FrameDurations, Id,
};
use image::{codecs::webp::WebPDecoder, AnimationDecoder as _, ImageDecoder, Rgba};
use image::{codecs::webp::WebPDecoder, AnimationDecoder as _, ColorType, ImageDecoder, Rgba};
use std::{io::Cursor, mem::size_of, sync::Arc, time::Duration};

#[derive(Clone)]
Expand Down Expand Up @@ -48,6 +48,17 @@ impl WebP {
frame_durations: FrameDurations::new(durations),
}))
} else {
// color_type() of WebPDecoder only returns Rgb8/Rgba8 variants of ColorType
let create_image = match decoder.color_type() {
ColorType::Rgb8 => ColorImage::from_rgb,
ColorType::Rgba8 => ColorImage::from_rgba_unmultiplied,
unreachable => {
return Err(format!(
"Unreachable WebP color type, expected Rgb8/Rgba8, got {unreachable:?}"
))
}
};

let (width, height) = decoder.dimensions();
let size = decoder.total_bytes() as usize;

Expand All @@ -56,10 +67,10 @@ impl WebP {
.read_image(&mut data)
.map_err(|error| format!("WebP image read failure ({error})"))?;

let image =
ColorImage::from_rgba_unmultiplied([width as usize, height as usize], &data);

Ok(Self::Static(Arc::new(image)))
Ok(Self::Static(Arc::new(create_image(
[width as usize, height as usize],
&data,
))))
}
}

Expand Down
Loading