Use stronger compression in eval (#509)

This commit is contained in:
andrews05 2023-07-09 10:54:58 +12:00 committed by GitHub
parent 2a59419bdf
commit 39b00910ae
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 17 additions and 8 deletions

View file

@ -646,8 +646,18 @@ fn optimize_raw<T: Deflater>(
max_size: Option<usize>, max_size: Option<usize>,
deflater: &T, deflater: &T,
) -> Option<PngData> { ) -> Option<PngData> {
// Must use normal (lazy) compression, as faster ones (greedy) are not representative // Libdeflate has four algorithms: 1-4 = 'greedy', 5-7 = 'lazy', 8-9 = 'lazy2', 10-12 = 'near-optimal'
let eval_compression = 5; // 5 is the minimumm required for a decent evaluation result
// 7 is not noticeably slower than 5 and improves evaluation of filters in 'fast' mode (o2 and lower)
// 8 is a little slower but not noticeably when used only for reductions (o3 and higher)
// 9 is not appreciably better than 8
// 10 and higher are quite slow - good for filters but only good for reductions if matching the main zc level
let eval_compression = match opts.deflate {
Deflaters::Libdeflater { compression } => {
if opts.fast_evaluation { 7 } else { 8 }.min(compression)
}
_ => 8,
};
// None and Bigrams work well together, especially for alpha reductions // None and Bigrams work well together, especially for alpha reductions
let eval_filters = indexset! {RowFilter::None, RowFilter::Bigrams}; let eval_filters = indexset! {RowFilter::None, RowFilter::Bigrams};
// This will collect all versions of images and pick one that compresses best // This will collect all versions of images and pick one that compresses best

View file

@ -180,7 +180,7 @@ fn main() {
) )
.arg( .arg(
Arg::new("fast") Arg::new("fast")
.help("Use fast filter evaluation") .help("Use fast filter evaluation (helpful when you have more filters enabled than CPU cores)")
.long("fast") .long("fast")
.action(ArgAction::SetTrue), .action(ArgAction::SetTrue),
) )
@ -242,7 +242,7 @@ fn main() {
) )
.arg( .arg(
Arg::new("zopfli") Arg::new("zopfli")
.help("Use the slower but better compressing Zopfli algorithm") .help("Use the slow but stronger Zopfli compressor (recommended use is with all filters and `--fast` enabled)")
.short('Z') .short('Z')
.long("zopfli") .long("zopfli")
.action(ArgAction::SetTrue), .action(ArgAction::SetTrue),

Binary file not shown.

Before

Width:  |  Height:  |  Size: 16 KiB

After

Width:  |  Height:  |  Size: 2.7 KiB

View file

@ -11,7 +11,6 @@ use std::ops::Deref;
use std::path::Path; use std::path::Path;
use std::path::PathBuf; use std::path::PathBuf;
const GRAYSCALE: u8 = 0;
const RGB: u8 = 2; const RGB: u8 = 2;
const INDEXED: u8 = 3; const INDEXED: u8 = 3;
const RGBA: u8 = 6; const RGBA: u8 = 6;
@ -597,7 +596,7 @@ fn fix_errors() {
} }
}; };
assert_eq!(png.raw.ihdr.color_type.png_header_code(), GRAYSCALE); assert_eq!(png.raw.ihdr.color_type.png_header_code(), INDEXED);
assert_eq!(png.raw.ihdr.bit_depth, BitDepth::Eight); assert_eq!(png.raw.ihdr.bit_depth, BitDepth::Eight);
// Cannot check if pixels are equal because image crate cannot read corrupt (input) PNGs // Cannot check if pixels are equal because image crate cannot read corrupt (input) PNGs

View file

@ -177,8 +177,8 @@ fn issue_52_05() {
None, None,
RGBA, RGBA,
BitDepth::Eight, BitDepth::Eight,
INDEXED, RGB,
BitDepth::One, BitDepth::Eight,
); );
} }