Simplify tracking of reduction_occurred

This commit is contained in:
Andrew 2023-06-02 18:17:57 +12:00
parent 0879f678d5
commit cec90c4686
3 changed files with 9 additions and 31 deletions

View file

@ -25,7 +25,6 @@ pub struct Candidate {
pub idat_data: Vec<u8>, pub idat_data: Vec<u8>,
pub filtered: Vec<u8>, pub filtered: Vec<u8>,
pub filter: RowFilter, pub filter: RowFilter,
pub is_reduction: bool,
// first wins tie-breaker // first wins tie-breaker
nth: usize, nth: usize,
} }
@ -95,11 +94,6 @@ impl Evaluator {
self.eval_best_candidate.into_inner() 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<PngImage>) {
self.try_image_inner(image, false)
}
/// Set best size, if known in advance /// Set best size, if known in advance
pub fn set_best_size(&self, size: usize) { pub fn set_best_size(&self, size: usize) {
self.best_candidate_size.set_min(size); self.best_candidate_size.set_min(size);
@ -107,10 +101,6 @@ impl Evaluator {
/// Check if the image is smaller than others /// Check if the image is smaller than others
pub fn try_image(&self, image: Arc<PngImage>) { pub fn try_image(&self, image: Arc<PngImage>) {
self.try_image_inner(image, true)
}
fn try_image_inner(&self, image: Arc<PngImage>, is_reduction: bool) {
let nth = self.nth.fetch_add(1, SeqCst); let nth = self.nth.fetch_add(1, SeqCst);
// These clones are only cheap refcounts // These clones are only cheap refcounts
let deadline = self.deadline.clone(); let deadline = self.deadline.clone();
@ -150,7 +140,6 @@ impl Evaluator {
idat_data, idat_data,
filtered, filtered,
filter, filter,
is_reduction,
nth, nth,
}; };

View file

@ -615,7 +615,7 @@ fn optimize_png(
/// Perform optimization on the input image data using the options provided /// Perform optimization on the input image data using the options provided
fn optimize_raw( fn optimize_raw(
mut png: Arc<PngImage>, image: Arc<PngImage>,
opts: &Options, opts: &Options,
deadline: Arc<Deadline>, deadline: Arc<Deadline>,
max_size: Option<usize>, max_size: Option<usize>,
@ -631,16 +631,14 @@ fn optimize_raw(
eval_compression, eval_compression,
false, false,
); );
let (baseline, mut reduction_occurred) = let mut png = perform_reductions(image.clone(), opts, &deadline, &eval);
perform_reductions(png.clone(), opts, &deadline, &eval);
png = baseline;
let mut eval_result = eval.get_best_candidate(); let mut eval_result = eval.get_best_candidate();
if let Some(ref result) = eval_result { if let Some(ref result) = eval_result {
if result.is_reduction { png = result.image.clone();
png = result.image.clone();
reduction_occurred = true;
}
} }
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 { if reduction_occurred {
report_format("Reducing image to ", &png); report_format("Reducing image to ", &png);

View file

@ -18,15 +18,13 @@ pub(crate) fn perform_reductions(
opts: &Options, opts: &Options,
deadline: &Deadline, deadline: &Deadline,
eval: &Evaluator, eval: &Evaluator,
) -> (Arc<PngImage>, bool) { ) -> Arc<PngImage> {
let mut reduction_occurred = false;
let mut evaluation_added = false; let mut evaluation_added = false;
// Interlacing must be processed first in order to evaluate the rest correctly // Interlacing must be processed first in order to evaluate the rest correctly
if let Some(interlacing) = opts.interlace { if let Some(interlacing) = opts.interlace {
if let Some(reduced) = png.change_interlacing(interlacing) { if let Some(reduced) = png.change_interlacing(interlacing) {
png = Arc::new(reduced); png = Arc::new(reduced);
reduction_occurred = true;
} }
} }
@ -35,7 +33,6 @@ pub(crate) fn perform_reductions(
if opts.optimize_alpha && !deadline.passed() { if opts.optimize_alpha && !deadline.passed() {
if let Some(reduced) = cleaned_alpha_channel(&png) { if let Some(reduced) = cleaned_alpha_channel(&png) {
png = Arc::new(reduced); 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 opts.bit_depth_reduction && !deadline.passed() {
if let Some(reduced) = reduced_bit_depth_16_to_8(&png, opts.scale_16) { if let Some(reduced) = reduced_bit_depth_16_to_8(&png, opts.scale_16) {
png = Arc::new(reduced); 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 opts.color_type_reduction && opts.grayscale_reduction && !deadline.passed() {
if let Some(reduced) = reduced_rgb_to_grayscale(&png) { if let Some(reduced) = reduced_rgb_to_grayscale(&png) {
png = Arc::new(reduced); png = Arc::new(reduced);
reduction_occurred = true;
} }
} }
@ -62,7 +57,6 @@ pub(crate) fn perform_reductions(
if opts.bit_depth_reduction && !deadline.passed() { if opts.bit_depth_reduction && !deadline.passed() {
if let Some(reduced) = expanded_bit_depth_to_8(&png) { if let Some(reduced) = expanded_bit_depth_to_8(&png) {
png = Arc::new(reduced); png = Arc::new(reduced);
reduction_occurred = true;
} }
} }
@ -71,7 +65,6 @@ pub(crate) fn perform_reductions(
if opts.palette_reduction && !deadline.passed() { if opts.palette_reduction && !deadline.passed() {
if let Some(reduced) = reduced_palette(&png, opts.optimize_alpha) { if let Some(reduced) = reduced_palette(&png, opts.optimize_alpha) {
png = Arc::new(reduced); png = Arc::new(reduced);
reduction_occurred = true;
} }
} }
@ -90,7 +83,6 @@ pub(crate) fn perform_reductions(
evaluation_added = true; evaluation_added = true;
} else { } else {
baseline = png.clone(); baseline = png.clone();
reduction_occurred = true;
} }
} }
} }
@ -127,7 +119,6 @@ pub(crate) fn perform_reductions(
evaluation_added = true; evaluation_added = true;
} else { } else {
baseline = new.clone(); baseline = new.clone();
reduction_occurred = true;
} }
indexed = Some(new); indexed = Some(new);
} }
@ -146,7 +137,7 @@ pub(crate) fn perform_reductions(
} }
if evaluation_added { if evaluation_added {
eval.set_baseline(baseline.clone()); eval.try_image(baseline.clone());
} }
(baseline, reduction_occurred) baseline
} }