Skip to content

Commit

Permalink
Use custom version of image-rs (#210)
Browse files Browse the repository at this point in the history
This is done to avoid a panic in the current version image rs
  • Loading branch information
PhilipK authored Aug 23, 2022
1 parent 97f706a commit 43c1493
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 56 deletions.
3 changes: 1 addition & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
Expand Down
Binary file added src/testdata/hollow.webp
Binary file not shown.
74 changes: 25 additions & 49 deletions src/ui/defines.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -46,53 +42,27 @@ pub mod ui_images {
rgba: pixels.as_slice().to_vec(),
}
}
pub fn load_image_from_path(path: &Path) -> Option<ColorImage> {
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<ColorImage, image::ImageError> {
pub fn load_image_from_path(path: &std::path::Path) -> Result<egui::ColorImage, image::ImageError> {
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<ColorImage, image::ImageError> {
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(),
))
}

}
Expand All @@ -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());
}
}
6 changes: 3 additions & 3 deletions src/ui/ui_image_download.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 43c1493

Please sign in to comment.