Skip to content

Commit

Permalink
Add option for Zopfli iteration count (#640)
Browse files Browse the repository at this point in the history
This PR extends the `--zopfli` argument with an optional iteration
count. In my case, I have a bunch of very small images (a few kB or
less), and I often like to use hundreds of iterations to squeeze off the
last several bytes. (I know that this crate isn't intended for
brute-force optimization, but I've found that some of its
transformations and filter strategies can be more creative than
`zopflipng`.) But this is also useful in the opposite direction, for
allowing Zopfli compression on large images where 15 iterations would be
prohibitive.
  • Loading branch information
LegionMammal978 authored Aug 7, 2024
1 parent 1bb7109 commit 0f24120
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
11 changes: 11 additions & 0 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,17 @@ Recommended use is with '-o max' and '--fast'.")
.long("zopfli")
.action(ArgAction::SetTrue),
)
.arg(
Arg::new("iterations")
.help("Number of Zopfli iterations")
.long_help("\
Set the number of iterations to use for Zopfli compression. Using fewer iterations may \
speed up compression for large files. This option requires '--zopfli' to be set.")
.long("zi")
.default_value("15")
.value_parser(1..=255)
.requires("zopfli"),
)
.arg(
Arg::new("timeout")
.help("Maximum amount of time to spend on optimizations")
Expand Down
12 changes: 7 additions & 5 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -324,12 +324,14 @@ fn parse_opts_into_struct(
opts.strip = StripChunks::Safe;
}

#[cfg(feature = "zopfli")]
if matches.get_flag("zopfli") {
#[cfg(feature = "zopfli")]
if let Some(iterations) = NonZeroU8::new(15) {
opts.deflate = Deflaters::Zopfli { iterations };
}
} else if let (Deflaters::Libdeflater { compression }, Some(x)) =
let iterations = *matches.get_one::<i64>("iterations").unwrap();
opts.deflate = Deflaters::Zopfli {
iterations: NonZeroU8::new(iterations as u8).unwrap(),
};
}
if let (Deflaters::Libdeflater { compression }, Some(x)) =
(&mut opts.deflate, matches.get_one::<i64>("compression"))
{
*compression = *x as u8;
Expand Down

0 comments on commit 0f24120

Please sign in to comment.