mirror of
https://github.com/djibux/BoilR.git
synced 2026-09-01 14:03:42 +02:00
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:
+25
-49
@@ -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<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(),
|
||||
))
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user