Skip to content

Commit 0f24120

Browse files
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.
1 parent 1bb7109 commit 0f24120

File tree

2 files changed

+18
-5
lines changed

2 files changed

+18
-5
lines changed

src/cli.rs

+11
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,17 @@ Recommended use is with '-o max' and '--fast'.")
337337
.long("zopfli")
338338
.action(ArgAction::SetTrue),
339339
)
340+
.arg(
341+
Arg::new("iterations")
342+
.help("Number of Zopfli iterations")
343+
.long_help("\
344+
Set the number of iterations to use for Zopfli compression. Using fewer iterations may \
345+
speed up compression for large files. This option requires '--zopfli' to be set.")
346+
.long("zi")
347+
.default_value("15")
348+
.value_parser(1..=255)
349+
.requires("zopfli"),
350+
)
340351
.arg(
341352
Arg::new("timeout")
342353
.help("Maximum amount of time to spend on optimizations")

src/main.rs

+7-5
Original file line numberDiff line numberDiff line change
@@ -324,12 +324,14 @@ fn parse_opts_into_struct(
324324
opts.strip = StripChunks::Safe;
325325
}
326326

327+
#[cfg(feature = "zopfli")]
327328
if matches.get_flag("zopfli") {
328-
#[cfg(feature = "zopfli")]
329-
if let Some(iterations) = NonZeroU8::new(15) {
330-
opts.deflate = Deflaters::Zopfli { iterations };
331-
}
332-
} else if let (Deflaters::Libdeflater { compression }, Some(x)) =
329+
let iterations = *matches.get_one::<i64>("iterations").unwrap();
330+
opts.deflate = Deflaters::Zopfli {
331+
iterations: NonZeroU8::new(iterations as u8).unwrap(),
332+
};
333+
}
334+
if let (Deflaters::Libdeflater { compression }, Some(x)) =
333335
(&mut opts.deflate, matches.get_one::<i64>("compression"))
334336
{
335337
*compression = *x as u8;

0 commit comments

Comments
 (0)