Do not accept images that have wrong sizes

This commit is contained in:
Jaby 2023-10-12 11:06:11 +02:00 committed by Jaby
parent 56a599f9c5
commit b1c9fd93c9
1 changed files with 171 additions and 147 deletions

View File

@ -62,15 +62,39 @@ fn modify_palette(mut image: IndexedImage, clut_align: ClutAlignment, semi_trans
}
fn encode<T: PSXImageConverter>(image: T, color_depth: ColorType, clut_align: ClutAlignment, output: &mut dyn Write) -> Result<(), Error> {
let width = {
let width = image.width();
match color_depth {
ColorType::Clut4 => width/4,
ColorType::Clut8 => width/2,
ColorType::Full16 => width
let (width, height) = {
fn return_error(clut_type: u32, div: u32, width: u16, height: u16) -> Result<(u16, u16), Error> {
return Err(Error::from_callback(|| {format!("CLUT {} images require a width divideable by {} (found width: {}/{}={}, height: {}/{}={})", clut_type, div,
width, div, (width as f32/div as f32),
height, div, (height as f32/div as f32))}));
}
};
let height = image.height();
let width = image.width();
let height = image.height();
match color_depth {
ColorType::Clut4 => {
if width & 3 == 0 {
Ok((width/4, height))
}
else {
return_error(4, 4, width, height)
}
},
ColorType::Clut8 => {
if width & 1 == 0 {
Ok((width/2, height))
}
else {
return_error(8, 2, width, height)
}
},
ColorType::Full16 => {
Ok((width, height))
}
}
}?;
let palette = image.get_palette();
let (pal_width, pal_height) = {
if let Some(palette) = &palette {