Skip to content

Commit

Permalink
Reduce excess dithering when using lossy LZW
Browse files Browse the repository at this point in the history
  • Loading branch information
kornelski committed Jun 23, 2024
1 parent ac13dc1 commit 1fd6f3f
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,16 @@ impl SettingsExt {
0
}
}

pub(crate) fn dithering_level(&self) -> f32 {
let gifsicle_quality = if cfg!(feature = "gifsicle") { self.giflossy_quality } else { 100 };
debug_assert!(gifsicle_quality <= 100);
// lossy LZW adds its own dithering, so the input could be less nosiy to compensate
// but don't change dithering unless gifsicle quality < 90, and don't completely disable it
let gifsicle_factor = 0.25 + f32::from(gifsicle_quality) * (1./100. * 1./0.9 * 0.75);

(f32::from(self.s.quality) * (1./50. * gifsicle_factor) - 1.).max(0.2)
}
}

impl Default for Settings {
Expand Down Expand Up @@ -485,8 +495,7 @@ impl Writer {
res = liq.quantize(&mut img)?;
}
}

res.set_dithering_level((f32::from(self.settings.s.quality) / 50.0 - 1.).max(0.2))?;
res.set_dithering_level(self.settings.dithering_level())?;

let mut out = Vec::new();
out.try_reserve_exact(width*height).map_err(imagequant::liq_error::from)?;
Expand Down

0 comments on commit 1fd6f3f

Please sign in to comment.