Use custom version of image-rs (#210)

This is done to avoid a panic in the current version image rs
This commit is contained in:
Philip Kristoffersen
2022-08-23 22:16:57 +02:00
committed by GitHub
parent 97f706a2be
commit 43c1493c40
5 changed files with 33 additions and 56 deletions
Generated
+1 -2
View File
@@ -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",
+4 -2
View File
@@ -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
BIN
View File
Binary file not shown.

After

Width:  |  Height:  |  Size: 9.1 KiB

+23 -47
View File
@@ -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,54 +42,28 @@ 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
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(),
))
}
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> {
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",
),
Ok(ColorImage::from_rgba_unmultiplied(
size,
pixels.as_slice(),
))
}
}
} else {
Err(image::ImageError::Decoding(
image::error::DecodingError::new(
image::error::ImageFormatHint::Unknown,
"Image did not have right amount of pixels",
),
))
}
})
}
}
@@ -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());
}
}
+3 -3
View File
@@ -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);