Skip to content

Commit 273e454

Browse files
Add option for Zopfli iteration count
1 parent 61deab3 commit 273e454

File tree

6 files changed

+24
-18
lines changed

6 files changed

+24
-18
lines changed

benches/zopfli.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@
33
extern crate oxipng;
44
extern crate test;
55

6-
use std::{num::NonZeroU8, path::PathBuf};
6+
use std::{num::NonZeroU64, path::PathBuf};
77

88
use oxipng::{internal_tests::*, *};
99
use test::Bencher;
1010

1111
// SAFETY: trivially safe. Stopgap solution until const unwrap is stabilized.
12-
const DEFAULT_ZOPFLI_ITERATIONS: NonZeroU8 = unsafe { NonZeroU8::new_unchecked(15) };
12+
const DEFAULT_ZOPFLI_ITERATIONS: NonZeroU64 = unsafe { NonZeroU64::new_unchecked(15) };
1313

1414
#[bench]
1515
fn zopfli_16_bits_strategy_0(b: &mut Bencher) {

src/cli.rs

+7-2
Original file line numberDiff line numberDiff line change
@@ -332,10 +332,15 @@ be processed successfully.")
332332
.help("Use the much slower but stronger Zopfli compressor")
333333
.long_help("\
334334
Use the much slower but stronger Zopfli compressor for main compression trials. \
335-
Recommended use is with '-o max' and '--fast'.")
335+
Recommended use is with '-o max' and '--fast'. The default number of iterations is 15. \
336+
Using fewer iterations may speed up compression for large files.")
336337
.short('Z')
337338
.long("zopfli")
338-
.action(ArgAction::SetTrue),
339+
.num_args(0..=1)
340+
.require_equals(true)
341+
.value_name("iterations")
342+
.default_missing_value("15")
343+
.value_parser(1..),
339344
)
340345
.arg(
341346
Arg::new("timeout")

src/deflate/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
mod deflater;
22
#[cfg(feature = "zopfli")]
3-
use std::num::NonZeroU8;
3+
use std::num::NonZeroU64;
44
use std::{fmt, fmt::Display};
55

66
pub use deflater::{crc32, deflate, inflate};
@@ -25,7 +25,7 @@ pub enum Deflaters {
2525
/// The number of compression iterations to do. 15 iterations are fine
2626
/// for small files, but bigger files will need to be compressed with
2727
/// less iterations, or else they will be too slow.
28-
iterations: NonZeroU8,
28+
iterations: NonZeroU64,
2929
},
3030
}
3131

src/deflate/zopfli_oxipng.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
use std::num::NonZeroU8;
1+
use std::num::NonZeroU64;
22

33
use crate::{PngError, PngResult};
44

5-
pub fn deflate(data: &[u8], iterations: NonZeroU8) -> PngResult<Vec<u8>> {
5+
pub fn deflate(data: &[u8], iterations: NonZeroU64) -> PngResult<Vec<u8>> {
66
let mut output = Vec::with_capacity(data.len());
77
let options = zopfli::Options {
8-
iteration_count: iterations.into(),
8+
iteration_count: iterations,
99
..Default::default()
1010
};
1111
match zopfli::compress(options, zopfli::Format::Zlib, data, &mut output) {

src/main.rs

+8-7
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
mod rayon;
1818

1919
#[cfg(feature = "zopfli")]
20-
use std::num::NonZeroU8;
20+
use std::num::NonZeroU64;
2121
use std::{ffi::OsString, fs::DirBuilder, io::Write, path::PathBuf, process::exit, time::Duration};
2222

2323
use clap::ArgMatches;
@@ -324,12 +324,13 @@ fn parse_opts_into_struct(
324324
opts.strip = StripChunks::Safe;
325325
}
326326

327-
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)) =
327+
#[cfg(feature = "zopfli")]
328+
if let Some(&iterations) = matches.get_one::<i64>("zopfli") {
329+
opts.deflate = Deflaters::Zopfli {
330+
iterations: NonZeroU64::new(iterations as u64).unwrap(),
331+
};
332+
}
333+
if let (Deflaters::Libdeflater { compression }, Some(x)) =
333334
(&mut opts.deflate, matches.get_one::<i64>("compression"))
334335
{
335336
*compression = *x as u8;

tests/flags.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#[cfg(feature = "filetime")]
22
use std::cell::RefCell;
33
#[cfg(feature = "zopfli")]
4-
use std::num::NonZeroU8;
4+
use std::num::NonZeroU64;
55
#[cfg(feature = "filetime")]
66
use std::ops::Deref;
77
use std::{
@@ -681,7 +681,7 @@ fn zopfli_mode() {
681681
let input = PathBuf::from("tests/files/zopfli_mode.png");
682682
let (output, mut opts) = get_opts(&input);
683683
opts.deflate = Deflaters::Zopfli {
684-
iterations: NonZeroU8::new(15).unwrap(),
684+
iterations: NonZeroU64::new(15).unwrap(),
685685
};
686686

687687
test_it_converts(

0 commit comments

Comments
 (0)