Wrap image in a thread to avoid panics (#254)

This commit is contained in:
Philip Kristoffersen
2022-10-14 21:37:18 +02:00
committed by GitHub
parent 8293cdcd8d
commit e900c8af3a
+11 -4
View File
@@ -12,6 +12,8 @@ pub mod ui_colors {
pub mod ui_images { pub mod ui_images {
use std::thread::JoinHandle;
use eframe::IconData; use eframe::IconData;
use egui::{ColorImage, ImageData}; use egui::{ColorImage, ImageData};
@@ -43,10 +45,10 @@ pub mod ui_images {
} }
} }
pub fn load_image_from_path( pub fn load_image_from_path(path: &std::path::Path) -> eyre::Result<egui::ColorImage> {
path: &std::path::Path, let path_owned = path.to_owned();
) -> Result<egui::ColorImage, image::ImageError> { let handle: JoinHandle<eyre::Result<egui::ColorImage>> = std::thread::spawn(move || {
let image = image::io::Reader::open(path)? let image = image::io::Reader::open(&path_owned)?
.with_guessed_format()? .with_guessed_format()?
.decode()?; .decode()?;
let size = [image.width() as _, image.height() as _]; let size = [image.width() as _, image.height() as _];
@@ -56,6 +58,11 @@ pub mod ui_images {
size, size,
pixels.as_slice(), pixels.as_slice(),
)) ))
});
match handle.join() {
Ok(thread_result) => thread_result,
Err(_e) => Err(eyre::format_err!("Failed to load image at {:?} ", path)),
}
} }
pub fn load_image_from_memory(image_data: &[u8]) -> Result<ColorImage, image::ImageError> { pub fn load_image_from_memory(image_data: &[u8]) -> Result<ColorImage, image::ImageError> {