Skip to content

Commit

Permalink
Check if image is valid before loading (#204)
Browse files Browse the repository at this point in the history
  • Loading branch information
PhilipK authored Aug 12, 2022
1 parent 6489e1a commit e5dae59
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions src/ui/defines.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,17 @@ pub mod ui_images {
let size = [image.width() as _, image.height() as _];
let image_buffer = image.to_rgba8();
let pixels = image_buffer.as_flat_samples();

Ok(ColorImage::from_rgba_unmultiplied(size, pixels.as_slice()))
let rgba = 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",
),
))
}
}
}

0 comments on commit e5dae59

Please sign in to comment.