diff --git a/src/testdata/brokenimage.webp b/src/testdata/brokenimage.webp new file mode 100644 index 0000000..3b478ca --- /dev/null +++ b/src/testdata/brokenimage.webp @@ -0,0 +1,6 @@ +[InternetShortcut] +URL=https://codecombat.com/play/dungeon +IDList= +HotKey=0 +IconFile=C:\Users\Philip\AppData\Local\Mozilla\Firefox\Profiles\hiiy2fz7.default-release\shortcutCache\gEyfJTfj4BiLoP3VBBQI1A==.ico +IconIndex=0 diff --git a/src/testdata/smallpng.png b/src/testdata/smallpng.png new file mode 100644 index 0000000..3a6ceea Binary files /dev/null and b/src/testdata/smallpng.png differ diff --git a/src/testdata/spider.webp b/src/testdata/spider.webp new file mode 100644 index 0000000..79bcd3b Binary files /dev/null and b/src/testdata/spider.webp differ diff --git a/src/ui/defines.rs b/src/ui/defines.rs index ed08735..800b454 100644 --- a/src/ui/defines.rs +++ b/src/ui/defines.rs @@ -11,7 +11,10 @@ pub mod ui_colors { } pub mod ui_images { - use std::path::Path; + use std::{ + path::Path, + thread::{self, Thread}, + }; use eframe::IconData; use egui::{ColorImage, ImageData}; @@ -46,7 +49,11 @@ pub mod ui_images { pub fn load_image_from_path(path: &Path) -> Option { if path.exists() { if let Ok(data) = std::fs::read(path) { - return load_image_from_memory(&data).ok(); + let load_result = load_image_from_memory(&data); + if load_result.is_err() { + eprintln!("Could not load image at path {:?}", path); + } + return load_result.ok(); } } None @@ -57,17 +64,58 @@ pub mod ui_images { let size = [image.width() as _, image.height() as _]; let image_buffer = image.to_rgba8(); let pixels = image_buffer.as_flat_samples(); - let rgba = pixels.as_slice(); - let is_valid = size[0] * size[1] * 4 == rgba.len(); - if is_valid { - Ok(ColorImage::from_rgba_unmultiplied(size, rgba)) - } else { - Err(image::ImageError::Decoding( - image::error::DecodingError::new( - image::error::ImageFormatHint::Unknown, - "Image did not have right amount of pixels", - ), - )) - } + thread::scope(|s| { + let rgba = pixels.as_slice(); + let is_valid = size[0] * size[1] * 4 == rgba.len(); + if is_valid { + //Wrapping this in a thread, since it has a tendency to panic + let thread_handle = s + .spawn(move || ColorImage::from_rgba_unmultiplied(size, rgba)) + .join(); + match thread_handle { + Ok(value) => Ok(value), + Err(e) => { + println!("Error loading image {:?}", e); + Err(image::ImageError::Decoding( + image::error::DecodingError::new( + image::error::ImageFormatHint::Unknown, + "Could not load image, it panicked while trying", + ), + )) + } + } + } else { + Err(image::ImageError::Decoding( + image::error::DecodingError::new( + image::error::ImageFormatHint::Unknown, + "Image did not have right amount of pixels", + ), + )) + } + }) + } + +} + +#[cfg(test)] +mod tests { + use super::ui_images::load_image_from_path; + + #[test] + pub fn test_image_load_that_is_broken() { + let res = load_image_from_path(std::path::Path::new("src/testdata/brokenimage.webp")); + assert!(res.is_none()); + } + + #[test] + pub fn test_image_load_that_works_png() { + let res = load_image_from_path(std::path::Path::new("src/testdata/smallpng.png")); + assert!(res.is_some()); + } + + #[test] + pub fn test_image_load_that_works_webp() { + let res = load_image_from_path(std::path::Path::new("src/testdata/spider.webp")); + assert!(res.is_some()); } }