use crate::error::{DecodeError, DecodeErrorKind};
use crate::types::Format;
pub const MAX_PIXELS: usize = 256 * 1024 * 1024;
pub fn check_dimensions(width: usize, height: usize, format: Format) -> Result<(), DecodeError> {
let pixels = width.checked_mul(height).ok_or_else(|| {
DecodeError::new(
DecodeErrorKind::ImageTooLarge,
Some(format),
format!("width*height overflow ({}x{})", width, height),
)
})?;
if pixels > MAX_PIXELS {
return Err(DecodeError::new(
DecodeErrorKind::ImageTooLarge,
Some(format),
format!(
"declared dimensions {}x{} = {} pixels exceeds MAX_PIXELS = {}",
width, height, pixels, MAX_PIXELS
),
));
}
Ok(())
}