Skip to content

Commit 369ca2c

Browse files
committed
Refilter frames
1 parent 000ddd7 commit 369ca2c

File tree

1 file changed

+11
-3
lines changed

1 file changed

+11
-3
lines changed

src/lib.rs

+11-3
Original file line numberDiff line numberDiff line change
@@ -725,7 +725,11 @@ fn recompress_frames(png: &mut PngData, opts: &Options, deadline: Arc<Deadline>)
725725
if !opts.idat_recoding || png.frames.is_empty() {
726726
return;
727727
}
728-
let buffer_size = png.raw.ihdr.raw_data_size();
728+
// Use the same filter chosen for the main image
729+
// No filter means we failed to optimise the main image and we shouldn't bother trying here
730+
let Some(filter) = png.filter else {
731+
return;
732+
};
729733
png.frames
730734
.par_iter_mut()
731735
.with_max_len(1)
@@ -734,9 +738,13 @@ fn recompress_frames(png: &mut PngData, opts: &Options, deadline: Arc<Deadline>)
734738
if deadline.passed() {
735739
return;
736740
}
737-
if let Ok(data) = deflate::inflate(&frame.data, buffer_size).and_then(|data| {
741+
let mut ihdr = png.raw.ihdr.clone();
742+
ihdr.width = frame.width;
743+
ihdr.height = frame.height;
744+
if let Ok(data) = PngImage::new(ihdr, &frame.data).and_then(|image| {
745+
let filtered = image.filter_image(filter, opts.optimize_alpha);
738746
let max_size = AtomicMin::new(Some(frame.data.len() - 1));
739-
opts.deflate.deflate(&data, &max_size)
747+
opts.deflate.deflate(&filtered, &max_size)
740748
}) {
741749
debug!(
742750
"Recompressed fdAT #{:<2}: {} ({} bytes decrease)",

0 commit comments

Comments
 (0)