Check if image is valid before loading (#204)

This commit is contained in:
Philip Kristoffersen
2022-08-12 18:34:36 +02:00
committed by GitHub
parent 6489e1ad45
commit e5dae5901c
+12 -2
View File
@@ -57,7 +57,17 @@ pub mod ui_images {
let size = [image.width() as _, image.height() as _]; let size = [image.width() as _, image.height() as _];
let image_buffer = image.to_rgba8(); let image_buffer = image.to_rgba8();
let pixels = image_buffer.as_flat_samples(); let pixels = image_buffer.as_flat_samples();
let rgba = pixels.as_slice();
Ok(ColorImage::from_rgba_unmultiplied(size, 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",
),
))
}
} }
} }