mirror of
https://github.com/djibux/BoilR.git
synced 2026-09-01 14:03:42 +02:00
Wrap image in a thread to avoid panics (#254)
This commit is contained in:
+11
-4
@@ -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> {
|
||||||
|
|||||||
Reference in New Issue
Block a user