Skip to content

Commit

Permalink
Disable some reductions at low levels
Browse files Browse the repository at this point in the history
  • Loading branch information
andrews05 committed May 31, 2023
1 parent 5004027 commit 2d3de7b
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions src/reduction/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::evaluate::Evaluator;
use crate::png::PngImage;
use crate::Deadline;
use crate::Deflaters;
use crate::Options;
use std::sync::Arc;

Expand All @@ -22,6 +23,13 @@ pub(crate) fn perform_reductions(
let mut reduction_occurred = false;
let mut evaluation_added = false;

// At low compression levels, skip some transformations which are less likely to be effective
// This currently affects optimization presets 0-2
let cheap = match opts.deflate {
Deflaters::Libdeflater { compression } => compression < 12 && opts.fast_evaluation,
_ => 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) {
Expand Down Expand Up @@ -97,7 +105,7 @@ pub(crate) fn perform_reductions(

// Attempt to convert from indexed to channels
// This may give a better result due to dropping the PLTE chunk
if opts.color_type_reduction && !deadline.passed() {
if !cheap && opts.color_type_reduction && !deadline.passed() {
if let Some(reduced) = indexed_to_channels(&png, opts.grayscale_reduction) {
// This result should not be passed on to subsequent reductions
eval.try_image(Arc::new(reduced));
Expand Down Expand Up @@ -125,7 +133,7 @@ pub(crate) fn perform_reductions(
}

// Attempt to sort the palette using an alternative method
if opts.palette_reduction && !deadline.passed() {
if !cheap && opts.palette_reduction && !deadline.passed() {
if let Some(reduced) = sorted_palette_battiato(&png) {
eval.try_image(Arc::new(reduced));
evaluation_added = true;
Expand Down

0 comments on commit 2d3de7b

Please sign in to comment.