Skip to content

Commit

Permalink
Add option for Zopfli iteration count
Browse files Browse the repository at this point in the history
  • Loading branch information
LegionMammal978 committed Aug 3, 2024
1 parent 61deab3 commit 889642f
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 15 deletions.
4 changes: 2 additions & 2 deletions benches/zopfli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
extern crate oxipng;
extern crate test;

use std::{num::NonZeroU8, path::PathBuf};
use std::{num::NonZeroU64, path::PathBuf};

use oxipng::{internal_tests::*, *};
use test::Bencher;

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

#[bench]
fn zopfli_16_bits_strategy_0(b: &mut Bencher) {
Expand Down
9 changes: 7 additions & 2 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -332,10 +332,15 @@ be processed successfully.")
.help("Use the much slower but stronger Zopfli compressor")
.long_help("\
Use the much slower but stronger Zopfli compressor for main compression trials. \
Recommended use is with '-o max' and '--fast'.")
Recommended use is with '-o max' and '--fast'. The default number of iterations is 15. \
Using fewer iterations may speed up compression for large files.")
.short('Z')
.long("zopfli")
.action(ArgAction::SetTrue),
.num_args(0..=1)
.require_equals(true)
.value_name("iterations")
.default_missing_value("15")
.value_parser(1..),
)
.arg(
Arg::new("timeout")
Expand Down
4 changes: 2 additions & 2 deletions src/deflate/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
mod deflater;
#[cfg(feature = "zopfli")]
use std::num::NonZeroU8;
use std::num::NonZeroU64;
use std::{fmt, fmt::Display};

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

Expand Down
6 changes: 3 additions & 3 deletions src/deflate/zopfli_oxipng.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use std::num::NonZeroU8;
use std::num::NonZeroU64;

use crate::{PngError, PngResult};

pub fn deflate(data: &[u8], iterations: NonZeroU8) -> PngResult<Vec<u8>> {
pub fn deflate(data: &[u8], iterations: NonZeroU64) -> PngResult<Vec<u8>> {
let mut output = Vec::with_capacity(data.len());
let options = zopfli::Options {
iteration_count: iterations.into(),
iteration_count: iterations,
..Default::default()
};
match zopfli::compress(options, zopfli::Format::Zlib, data, &mut output) {
Expand Down
10 changes: 6 additions & 4 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
mod rayon;

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

use clap::ArgMatches;
Expand Down Expand Up @@ -324,10 +324,12 @@ fn parse_opts_into_struct(
opts.strip = StripChunks::Safe;
}

if matches.get_flag("zopfli") {
if let Some(&iterations) = matches.get_one::<i64>("zopfli") {

Check failure

Code scanning / clippy

unused variable: iterations Error

unused variable: iterations

Check failure

Code scanning / clippy

unused variable: iterations Error

unused variable: iterations
#[cfg(feature = "zopfli")]
if let Some(iterations) = NonZeroU8::new(15) {
opts.deflate = Deflaters::Zopfli { iterations };
{
opts.deflate = Deflaters::Zopfli {
iterations: NonZeroU64::new(iterations as u64).unwrap(),
};
}
} else if let (Deflaters::Libdeflater { compression }, Some(x)) =
(&mut opts.deflate, matches.get_one::<i64>("compression"))
Expand Down
4 changes: 2 additions & 2 deletions tests/flags.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#[cfg(feature = "filetime")]
use std::cell::RefCell;
#[cfg(feature = "zopfli")]
use std::num::NonZeroU8;
use std::num::NonZeroU64;
#[cfg(feature = "filetime")]
use std::ops::Deref;
use std::{
Expand Down Expand Up @@ -681,7 +681,7 @@ fn zopfli_mode() {
let input = PathBuf::from("tests/files/zopfli_mode.png");
let (output, mut opts) = get_opts(&input);
opts.deflate = Deflaters::Zopfli {
iterations: NonZeroU8::new(15).unwrap(),
iterations: NonZeroU64::new(15).unwrap(),
};

test_it_converts(
Expand Down

0 comments on commit 889642f

Please sign in to comment.