diff --git a/Cargo.lock b/Cargo.lock index 840bb59..4e99433 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1340,8 +1340,7 @@ dependencies = [ [[package]] name = "image" version = "0.24.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7e30ca2ecf7666107ff827a8e481de6a132a9b687ed3bb20bb1c144a36c00964" +source = "git+https://github.com/PhilipK/image?rev=cb66840dd42785c7283206cc9805240501695a61#cb66840dd42785c7283206cc9805240501695a61" dependencies = [ "bytemuck", "byteorder", diff --git a/Cargo.toml b/Cargo.toml index f88f0cf..e0aac75 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -18,6 +18,7 @@ steam_shortcuts_util = "^1.1.8" steamgriddb_api = "^0.3.1" sysinfo = "^0.25.2" + [dependencies.dashmap] features = ["serde"] version = "^5.3.4" @@ -32,8 +33,9 @@ version = "^0.19.0" version = "^0.3.23" [dependencies.image] -features = ["png"] -version = "^0.24.3" +features = ["png","webp","jpeg"] +git = "https://github.com/PhilipK/image" +rev = "cb66840dd42785c7283206cc9805240501695a61" [dependencies.reqwest] default_features = false diff --git a/src/testdata/hollow.webp b/src/testdata/hollow.webp new file mode 100644 index 0000000..fb0bf5d Binary files /dev/null and b/src/testdata/hollow.webp differ diff --git a/src/ui/defines.rs b/src/ui/defines.rs index 800b454..7d2a2c8 100644 --- a/src/ui/defines.rs +++ b/src/ui/defines.rs @@ -11,10 +11,6 @@ pub mod ui_colors { } pub mod ui_images { - use std::{ - path::Path, - thread::{self, Thread}, - }; use eframe::IconData; use egui::{ColorImage, ImageData}; @@ -46,53 +42,27 @@ pub mod ui_images { rgba: pixels.as_slice().to_vec(), } } - pub fn load_image_from_path(path: &Path) -> Option { - if path.exists() { - if let Ok(data) = std::fs::read(path) { - 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 - } - fn load_image_from_memory(image_data: &[u8]) -> Result { + pub fn load_image_from_path(path: &std::path::Path) -> Result { + let image = image::io::Reader::open(path)?.decode()?; + let size = [image.width() as _, image.height() as _]; + let image_buffer = image.to_rgba8(); + let pixels = image_buffer.as_flat_samples(); + Ok(egui::ColorImage::from_rgba_unmultiplied( + size, + pixels.as_slice(), + )) + } + + pub fn load_image_from_memory(image_data: &[u8]) -> Result { let image = image::load_from_memory(image_data)?; let size = [image.width() as _, image.height() as _]; let image_buffer = image.to_rgba8(); let pixels = image_buffer.as_flat_samples(); - 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", - ), - )) - } - }) + Ok(ColorImage::from_rgba_unmultiplied( + size, + pixels.as_slice(), + )) } } @@ -104,18 +74,24 @@ mod tests { #[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()); + assert!(res.is_err()); } #[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()); + assert!(res.is_ok()); } #[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()); + assert!(res.is_ok()); + } + + #[test] + pub fn test_image_load_animated_webp() { + let res = load_image_from_path(std::path::Path::new("src/testdata/hollow.webp")); + assert!(res.is_err()); } } diff --git a/src/ui/ui_image_download.rs b/src/ui/ui_image_download.rs index e5a6a7f..a04c541 100644 --- a/src/ui/ui_image_download.rs +++ b/src/ui/ui_image_download.rs @@ -263,7 +263,7 @@ impl MyEguiApp { TextureState::Downloaded => { //Need to load let image_data = load_image_from_path(&image.thumbnail_path); - if let Some(image_data) = image_data { + if let Ok(image_data) = image_data { let handle = ui.ctx().load_texture( &image_key, image_data, @@ -481,7 +481,7 @@ impl MyEguiApp { let loaded = state.image_handles.contains_key(&key); if !loaded && path.exists() { let image = load_image_from_path(&path); - if let Some(image) = image { + if let Ok(image) = image { let texture = ui .ctx() .load_texture(&key, image, egui::TextureFilter::Linear); @@ -607,7 +607,7 @@ impl MyEguiApp { for image_type in ImageType::all() { let (path, key) = shortcut.key(image_type, Path::new(&user.steam_user_data_folder)); let image = load_image_from_path(&path); - if let Some(image) = image { + if let Ok(image) = image { let texture = ui .ctx() .load_texture(&key, image, egui::TextureFilter::Linear);