Switch to using zopfli::Options in prep for https://github.com/zopfli-rs/zopfli/pull/21

This commit is contained in:
Chris Hennick 2023-06-24 11:37:15 -07:00
parent cd57847c15
commit 98a34c0722
No known key found for this signature in database
GPG key ID: 25653935CC8B6C74
3 changed files with 7 additions and 19 deletions

View file

@ -7,8 +7,6 @@ pub use deflater::inflate;
use std::io::{BufWriter, copy, Cursor, Write}; use std::io::{BufWriter, copy, Cursor, Write};
use std::{fmt, fmt::Display, io}; use std::{fmt, fmt::Display, io};
#[cfg(feature = "zopfli")]
use std::num::NonZeroU8;
#[cfg(feature = "zopfli")] #[cfg(feature = "zopfli")]
use zopfli::{DeflateEncoder, Options}; use zopfli::{DeflateEncoder, Options};
#[cfg(feature = "zopfli")] #[cfg(feature = "zopfli")]
@ -29,10 +27,8 @@ pub enum Deflaters {
#[cfg(feature = "zopfli")] #[cfg(feature = "zopfli")]
/// Use the better but slower Zopfli implementation /// Use the better but slower Zopfli implementation
Zopfli { Zopfli {
/// The number of compression iterations to do. 15 iterations are fine /// Zopfli compression options
/// for small files, but bigger files will need to be compressed with options: Options,
/// less iterations, or else they will be too slow.
iterations: NonZeroU8,
}, },
} }
@ -45,7 +41,7 @@ impl Deflater for Deflaters {
let compressed = match self { let compressed = match self {
Self::Libdeflater { compression } => deflate(data, *compression, max_size)?, Self::Libdeflater { compression } => deflate(data, *compression, max_size)?,
#[cfg(feature = "zopfli")] #[cfg(feature = "zopfli")]
Self::Zopfli { iterations } => zopfli_deflate(data, *iterations)?, Self::Zopfli { options } => zopfli_deflate(data, options)?,
}; };
if let Some(max) = max_size.get() { if let Some(max) = max_size.get() {
if compressed.len() > max { if compressed.len() > max {

View file

@ -1,17 +1,12 @@
use std::io::{Error, ErrorKind, Read}; use std::io::{Error, ErrorKind, Read};
use crate::{PngError, PngResult}; use crate::{PngError, PngResult};
use std::num::NonZeroU8;
use simd_adler32::Adler32; use simd_adler32::Adler32;
pub fn deflate(data: &[u8], iterations: NonZeroU8) -> PngResult<Vec<u8>> { pub fn deflate(data: &[u8], options: &zopfli::Options) -> PngResult<Vec<u8>> {
use std::cmp::max; use std::cmp::max;
let mut output = Vec::with_capacity(max(1024, data.len() / 20)); let mut output = Vec::with_capacity(max(1024, data.len() / 20));
let options = zopfli::Options { match zopfli::compress(options, &zopfli::Format::Zlib, data, &mut output) {
iteration_count: iterations,
..Default::default()
};
match zopfli::compress(&options, &zopfli::Format::Zlib, data, &mut output) {
Ok(_) => (), Ok(_) => (),
Err(_) => return Err(PngError::new("Failed to compress in zopfli")), Err(_) => return Err(PngError::new("Failed to compress in zopfli")),
}; };

View file

@ -22,8 +22,6 @@ use oxipng::RowFilter;
use oxipng::StripChunks; use oxipng::StripChunks;
use oxipng::{InFile, OutFile}; use oxipng::{InFile, OutFile};
use std::fs::DirBuilder; use std::fs::DirBuilder;
#[cfg(feature = "zopfli")]
use std::num::NonZeroU8;
use std::path::PathBuf; use std::path::PathBuf;
use std::process::exit; use std::process::exit;
use std::time::Duration; use std::time::Duration;
@ -552,9 +550,8 @@ fn parse_opts_into_struct(
if matches.is_present("zopfli") { if matches.is_present("zopfli") {
#[cfg(feature = "zopfli")] #[cfg(feature = "zopfli")]
if let Some(iterations) = NonZeroU8::new(15) { let zopfli_opts = zopfli::Options::default();
opts.deflate = Deflaters::Zopfli { iterations }; opts.deflate = Deflaters::Zopfli { options: zopfli_opts };
}
} else if let Deflaters::Libdeflater { compression } = &mut opts.deflate { } else if let Deflaters::Libdeflater { compression } = &mut opts.deflate {
if let Some(x) = matches.get_one::<i64>("compression") { if let Some(x) = matches.get_one::<i64>("compression") {
*compression = *x as u8; *compression = *x as u8;