diff --git a/src/evaluate.rs b/src/evaluate.rs index 8c05fde7..0d187198 100644 --- a/src/evaluate.rs +++ b/src/evaluate.rs @@ -25,7 +25,6 @@ pub struct Candidate { pub idat_data: Vec, pub filtered: Vec, pub filter: RowFilter, - pub is_reduction: bool, // first wins tie-breaker nth: usize, } @@ -95,11 +94,6 @@ impl Evaluator { self.eval_best_candidate.into_inner() } - /// Set baseline image. It will be used only to measure minimum compression level required - pub fn set_baseline(&self, image: Arc) { - self.try_image_inner(image, false) - } - /// Set best size, if known in advance pub fn set_best_size(&self, size: usize) { self.best_candidate_size.set_min(size); @@ -107,10 +101,6 @@ impl Evaluator { /// Check if the image is smaller than others pub fn try_image(&self, image: Arc) { - self.try_image_inner(image, true) - } - - fn try_image_inner(&self, image: Arc, is_reduction: bool) { let nth = self.nth.fetch_add(1, SeqCst); // These clones are only cheap refcounts let deadline = self.deadline.clone(); @@ -150,7 +140,6 @@ impl Evaluator { idat_data, filtered, filter, - is_reduction, nth, }; diff --git a/src/lib.rs b/src/lib.rs index bbb9919e..d870da55 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -615,7 +615,7 @@ fn optimize_png( /// Perform optimization on the input image data using the options provided fn optimize_raw( - mut png: Arc, + image: Arc, opts: &Options, deadline: Arc, max_size: Option, @@ -631,16 +631,14 @@ fn optimize_raw( eval_compression, false, ); - let (baseline, mut reduction_occurred) = - perform_reductions(png.clone(), opts, &deadline, &eval); - png = baseline; + let mut png = perform_reductions(image.clone(), opts, &deadline, &eval); let mut eval_result = eval.get_best_candidate(); if let Some(ref result) = eval_result { - if result.is_reduction { - png = result.image.clone(); - reduction_occurred = true; - } + png = result.image.clone(); } + let reduction_occurred = png.ihdr.color_type != image.ihdr.color_type + || png.ihdr.bit_depth != image.ihdr.bit_depth + || png.ihdr.interlaced != image.ihdr.interlaced; if reduction_occurred { report_format("Reducing image to ", &png); diff --git a/src/reduction/mod.rs b/src/reduction/mod.rs index f53a8f3c..d7fae113 100644 --- a/src/reduction/mod.rs +++ b/src/reduction/mod.rs @@ -18,15 +18,13 @@ pub(crate) fn perform_reductions( opts: &Options, deadline: &Deadline, eval: &Evaluator, -) -> (Arc, bool) { - let mut reduction_occurred = false; +) -> Arc { let mut evaluation_added = false; // Interlacing must be processed first in order to evaluate the rest correctly if let Some(interlacing) = opts.interlace { if let Some(reduced) = png.change_interlacing(interlacing) { png = Arc::new(reduced); - reduction_occurred = true; } } @@ -35,7 +33,6 @@ pub(crate) fn perform_reductions( if opts.optimize_alpha && !deadline.passed() { if let Some(reduced) = cleaned_alpha_channel(&png) { png = Arc::new(reduced); - // This does not count as a reduction } } @@ -44,7 +41,6 @@ pub(crate) fn perform_reductions( if opts.bit_depth_reduction && !deadline.passed() { if let Some(reduced) = reduced_bit_depth_16_to_8(&png, opts.scale_16) { png = Arc::new(reduced); - reduction_occurred = true; } } @@ -53,7 +49,6 @@ pub(crate) fn perform_reductions( if opts.color_type_reduction && opts.grayscale_reduction && !deadline.passed() { if let Some(reduced) = reduced_rgb_to_grayscale(&png) { png = Arc::new(reduced); - reduction_occurred = true; } } @@ -62,7 +57,6 @@ pub(crate) fn perform_reductions( if opts.bit_depth_reduction && !deadline.passed() { if let Some(reduced) = expanded_bit_depth_to_8(&png) { png = Arc::new(reduced); - reduction_occurred = true; } } @@ -71,7 +65,6 @@ pub(crate) fn perform_reductions( if opts.palette_reduction && !deadline.passed() { if let Some(reduced) = reduced_palette(&png, opts.optimize_alpha) { png = Arc::new(reduced); - reduction_occurred = true; } } @@ -90,7 +83,6 @@ pub(crate) fn perform_reductions( evaluation_added = true; } else { baseline = png.clone(); - reduction_occurred = true; } } } @@ -127,7 +119,6 @@ pub(crate) fn perform_reductions( evaluation_added = true; } else { baseline = new.clone(); - reduction_occurred = true; } indexed = Some(new); } @@ -146,7 +137,7 @@ pub(crate) fn perform_reductions( } if evaluation_added { - eval.set_baseline(baseline.clone()); + eval.try_image(baseline.clone()); } - (baseline, reduction_occurred) + baseline }