Skip to content

Commit

Permalink
Error if fdAT invalid
Browse files Browse the repository at this point in the history
  • Loading branch information
andrews05 authored and AlexTMjugador committed Jan 25, 2025
1 parent cca23b5 commit 14b8b0e
Showing 1 changed file with 12 additions and 12 deletions.
24 changes: 12 additions & 12 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,7 @@ fn optimize_png(
}

postprocess_chunks(png, &opts, &raw.ihdr);
recompress_frames(png, &opts, deadline);
recompress_frames(png, &opts, deadline)?;

let output = png.output();

Expand Down Expand Up @@ -721,31 +721,30 @@ fn postprocess_chunks(png: &mut PngData, opts: &Options, orig_ihdr: &IhdrData) {
}

/// 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() {
return;
return Ok(());
}
// 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
let Some(filter) = png.filter else {
return;
return Ok(());
};
png.frames
.par_iter_mut()
.with_max_len(1)
.enumerate()
.for_each(|(i, frame)| {
.try_for_each(|(i, frame)| {
if deadline.passed() {
return;
return Ok(());
}
let mut ihdr = png.raw.ihdr.clone();
ihdr.width = frame.width;
ihdr.height = frame.height;
if let Ok(data) = PngImage::new(ihdr, &frame.data).and_then(|image| {
let filtered = image.filter_image(filter, opts.optimize_alpha);
let max_size = AtomicMin::new(Some(frame.data.len() - 1));
opts.deflate.deflate(&filtered, &max_size)
}) {
let image = PngImage::new(ihdr, &frame.data)?;
let filtered = image.filter_image(filter, opts.optimize_alpha);
let max_size = AtomicMin::new(Some(frame.data.len() - 1));
if let Ok(data) = opts.deflate.deflate(&filtered, &max_size) {
debug!(
"Recompressed fdAT #{:<2}: {} ({} bytes decrease)",
i,
Expand All @@ -754,7 +753,8 @@ fn recompress_frames(png: &mut PngData, opts: &Options, deadline: Arc<Deadline>)
);
frame.data = data;
}
});
Ok(())
})
}

/// Check if an image was already optimized prior to oxipng's operations
Expand Down

0 comments on commit 14b8b0e

Please sign in to comment.