Add option for Zopfli iteration count (#640)
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.
This commit is contained in:
parent
1bb7109804
commit
0f24120c9a
2 changed files with 18 additions and 5 deletions
11
src/cli.rs
11
src/cli.rs
|
|
@ -337,6 +337,17 @@ Recommended use is with '-o max' and '--fast'.")
|
||||||
.long("zopfli")
|
.long("zopfli")
|
||||||
.action(ArgAction::SetTrue),
|
.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(
|
||||||
Arg::new("timeout")
|
Arg::new("timeout")
|
||||||
.help("Maximum amount of time to spend on optimizations")
|
.help("Maximum amount of time to spend on optimizations")
|
||||||
|
|
|
||||||
12
src/main.rs
12
src/main.rs
|
|
@ -324,12 +324,14 @@ fn parse_opts_into_struct(
|
||||||
opts.strip = StripChunks::Safe;
|
opts.strip = StripChunks::Safe;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(feature = "zopfli")]
|
||||||
if matches.get_flag("zopfli") {
|
if matches.get_flag("zopfli") {
|
||||||
#[cfg(feature = "zopfli")]
|
let iterations = *matches.get_one::<i64>("iterations").unwrap();
|
||||||
if let Some(iterations) = NonZeroU8::new(15) {
|
opts.deflate = Deflaters::Zopfli {
|
||||||
opts.deflate = Deflaters::Zopfli { iterations };
|
iterations: NonZeroU8::new(iterations as u8).unwrap(),
|
||||||
}
|
};
|
||||||
} else if let (Deflaters::Libdeflater { compression }, Some(x)) =
|
}
|
||||||
|
if let (Deflaters::Libdeflater { compression }, Some(x)) =
|
||||||
(&mut opts.deflate, matches.get_one::<i64>("compression"))
|
(&mut opts.deflate, matches.get_one::<i64>("compression"))
|
||||||
{
|
{
|
||||||
*compression = *x as u8;
|
*compression = *x as u8;
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue