From a330998e4e6277a810902522666e3c094280b5f6 Mon Sep 17 00:00:00 2001 From: Andrew Date: Fri, 28 Oct 2022 08:21:37 +1300 Subject: [PATCH] Don't set baseline if no reductions --- src/lib.rs | 24 ++++++++++++++++++------ src/reduction/alpha.rs | 20 +++++++++++--------- 2 files changed, 29 insertions(+), 15 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 5dd53639..d172f1c1 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -500,11 +500,6 @@ fn optimize_png( // This will collect all versions of images and pick one that compresses best let eval = Evaluator::new(deadline.clone()); - // Usually we want transformations that are smaller than the unmodified original, - // but if we're interlacing, we have to accept a possible file size increase. - if opts.interlace.is_none() { - eval.set_baseline(png.raw.clone()); - } perform_reductions(png.raw.clone(), opts, &deadline, &eval); let reduction_occurred = if let Some(result) = eval.get_best_candidate() { *png = result.image; @@ -663,11 +658,17 @@ fn perform_reductions( deadline: &Deadline, eval: &Evaluator, ) { + // The eval baseline will be set from the original png only if we attempt any reductions + let mut baseline = Some(png.clone()); + let mut reduction_occurred = false; + // must be done first to evaluate rest with the correct interlacing if let Some(interlacing) = opts.interlace { if let Some(reduced) = png.change_interlacing(interlacing) { png = Arc::new(reduced); eval.try_image(png.clone()); + // If we're interlacing, we have to accept a possible file size increase + baseline = None; } if deadline.passed() { return; @@ -679,6 +680,7 @@ fn perform_reductions( png = Arc::new(reduced); eval.try_image(png.clone()); report_reduction(&png); + reduction_occurred = true; } if deadline.passed() { return; @@ -700,6 +702,7 @@ fn perform_reductions( } } report_reduction(&png); + reduction_occurred = true; } if deadline.passed() { return; @@ -711,13 +714,22 @@ fn perform_reductions( png = Arc::new(reduced); eval.try_image(png.clone()); report_reduction(&png); + reduction_occurred = true; } if deadline.passed() { return; } } - try_alpha_reductions(png, &opts.alphas, eval); + if try_alpha_reductions(png, &opts.alphas, eval) { + reduction_occurred = true; + } + + if let Some(baseline) = baseline { + if reduction_occurred { + eval.set_baseline(baseline); + } + } } #[derive(Debug)] diff --git a/src/reduction/alpha.rs b/src/reduction/alpha.rs index 06df0676..b3c5c9a1 100644 --- a/src/reduction/alpha.rs +++ b/src/reduction/alpha.rs @@ -14,16 +14,18 @@ pub(crate) fn try_alpha_reductions( png: Arc, alphas: &IndexSet, eval: &Evaluator, -) { - if alphas.is_empty() { - return; +) -> bool { + match png.ihdr.color_type { + ColorType::RGBA | ColorType::GrayscaleAlpha if !alphas.is_empty() => { + alphas + .par_iter() + .with_max_len(1) + .filter_map(|&alpha| filtered_alpha_channel(&png, alpha)) + .for_each(|image| eval.try_image(Arc::new(image))); + true + } + _ => false, } - - alphas - .par_iter() - .with_max_len(1) - .filter_map(|&alpha| filtered_alpha_channel(&png, alpha)) - .for_each(|image| eval.try_image(Arc::new(image))); } pub fn filtered_alpha_channel(png: &PngImage, optim: AlphaOptim) -> Option {