Error if fdAT invalid

This commit is contained in:
Andrew 2025-01-17 16:58:36 +13:00 committed by Alejandro González
parent cca23b5479
commit 14b8b0e93a

View file

@ -369,7 +369,7 @@ fn optimize_png(
} }
postprocess_chunks(png, &opts, &raw.ihdr); postprocess_chunks(png, &opts, &raw.ihdr);
recompress_frames(png, &opts, deadline); recompress_frames(png, &opts, deadline)?;
let output = png.output(); let output = png.output();
@ -721,31 +721,30 @@ fn postprocess_chunks(png: &mut PngData, opts: &Options, orig_ihdr: &IhdrData) {
} }
/// Recompress the additional frames of an APNG /// Recompress the additional frames of an APNG
fn recompress_frames(png: &mut PngData, opts: &Options, deadline: Arc<Deadline>) { fn recompress_frames(png: &mut PngData, opts: &Options, deadline: Arc<Deadline>) -> PngResult<()> {
if !opts.idat_recoding || png.frames.is_empty() { if !opts.idat_recoding || png.frames.is_empty() {
return; return Ok(());
} }
// Use the same filter chosen for the main image // Use the same filter chosen for the main image
// No filter means we failed to optimise the main image and we shouldn't bother trying here // No filter means we failed to optimise the main image and we shouldn't bother trying here
let Some(filter) = png.filter else { let Some(filter) = png.filter else {
return; return Ok(());
}; };
png.frames png.frames
.par_iter_mut() .par_iter_mut()
.with_max_len(1) .with_max_len(1)
.enumerate() .enumerate()
.for_each(|(i, frame)| { .try_for_each(|(i, frame)| {
if deadline.passed() { if deadline.passed() {
return; return Ok(());
} }
let mut ihdr = png.raw.ihdr.clone(); let mut ihdr = png.raw.ihdr.clone();
ihdr.width = frame.width; ihdr.width = frame.width;
ihdr.height = frame.height; ihdr.height = frame.height;
if let Ok(data) = PngImage::new(ihdr, &frame.data).and_then(|image| { let image = PngImage::new(ihdr, &frame.data)?;
let filtered = image.filter_image(filter, opts.optimize_alpha); let filtered = image.filter_image(filter, opts.optimize_alpha);
let max_size = AtomicMin::new(Some(frame.data.len() - 1)); let max_size = AtomicMin::new(Some(frame.data.len() - 1));
opts.deflate.deflate(&filtered, &max_size) if let Ok(data) = opts.deflate.deflate(&filtered, &max_size) {
}) {
debug!( debug!(
"Recompressed fdAT #{:<2}: {} ({} bytes decrease)", "Recompressed fdAT #{:<2}: {} ({} bytes decrease)",
i, i,
@ -754,7 +753,8 @@ fn recompress_frames(png: &mut PngData, opts: &Options, deadline: Arc<Deadline>)
); );
frame.data = data; frame.data = data;
} }
}); Ok(())
})
} }
/// Check if an image was already optimized prior to oxipng's operations /// Check if an image was already optimized prior to oxipng's operations