Return error if a pixel-gif is larger than the anime-display dimensions

This commit is contained in:
Luke D. Jones
2022-07-20 20:52:03 +12:00
parent a71a40b509
commit 7a8bede92f
5 changed files with 36 additions and 2 deletions

View File

@@ -20,6 +20,8 @@ pub enum AnimeError {
UnsupportedDevice,
InvalidBrightness(f32),
DataBufferLength,
PixelGifWidth(usize),
PixelGifHeight(usize),
}
impl fmt::Display for AnimeError {
@@ -49,6 +51,13 @@ impl fmt::Display for AnimeError {
"Image brightness must be between 0.0 and 1.0 (inclusive), was {}",
bright
),
AnimeError::PixelGifWidth(n) => {
write!(f, "The gif used for pixel-perfect gif is is wider than {n}")
}
AnimeError::PixelGifHeight(n) => write!(
f,
"The gif used for pixel-perfect gif is is taller than {n}"
),
}
}
}

View File

@@ -3,6 +3,7 @@ use serde_derive::{Deserialize, Serialize};
use std::convert::TryFrom;
use std::{fs::File, path::Path, time::Duration};
use crate::error::AnimeError;
use crate::{error::Result, AnimeDataBuffer, AnimeDiagonal, AnimeImage, AnimeType, Pixel};
#[derive(Debug, Clone, Deserialize, Serialize)]
@@ -117,8 +118,17 @@ impl AnimeGif {
// should be t but not in some gifs? What, ASUS, what?
continue;
}
matrix.get_mut()[y + frame.top as usize][x + frame.left as usize] =
(px[0] as f32 * brightness) as u8;
let tmp = matrix.get_mut();
let y = y + frame.top as usize;
if y >= tmp.len() {
return Err(AnimeError::PixelGifHeight(tmp.len()));
}
let x = x + frame.left as usize;
if x >= tmp[y].len() {
return Err(AnimeError::PixelGifWidth(tmp[y].len()));
}
matrix.get_mut()[y][x] = (px[0] as f32 * brightness) as u8;
}
}