diff --git a/src/deflate/mod.rs b/src/deflate/mod.rs index a0244177..0ebf40fe 100644 --- a/src/deflate/mod.rs +++ b/src/deflate/mod.rs @@ -15,6 +15,8 @@ mod zopfli_oxipng; use simd_adler32::Adler32; #[cfg(feature = "zopfli")] pub use zopfli_oxipng::deflate as zopfli_deflate; +#[cfg(feature = "zopfli")] +use zopfli::Options as ZopfliOptions; #[derive(Clone, Copy, Debug, PartialEq, Eq)] /// DEFLATE algorithms supported by oxipng @@ -41,7 +43,7 @@ impl Deflater for Deflaters { let compressed = match self { Self::Libdeflater { compression } => deflate(data, *compression, max_size)?, #[cfg(feature = "zopfli")] - Self::Zopfli { options } => zopfli_deflate(data, options)?, + Self::Zopfli { options } => zopfli_deflate(data, options.clone())?, }; if let Some(max) = max_size.get() { if compressed.len() > max { @@ -55,34 +57,30 @@ impl Deflater for Deflaters { #[cfg(feature = "zopfli")] #[derive(Copy, Clone, Debug)] pub struct BufferedZopfliDeflater { - iterations: NonZeroU8, input_buffer_size: usize, output_buffer_size: usize, - options: Options, + options: ZopfliOptions, } #[cfg(feature = "zopfli")] impl BufferedZopfliDeflater { pub const fn new( - iterations: NonZeroU8, input_buffer_size: usize, output_buffer_size: usize, - max_block_splits: u16, + options: ZopfliOptions, ) -> Self { BufferedZopfliDeflater { - iterations, input_buffer_size, output_buffer_size, + options } } pub const fn const_default() -> Self { BufferedZopfliDeflater { - // SAFETY: trivially safe. Stopgap solution until const unwrap is stabilized. - iterations: unsafe { NonZeroU8::new_unchecked(15) }, input_buffer_size: 1024 * 1024, output_buffer_size: 64 * 1024, - max_block_splits: 15, + options: ZopfliOptions::const_default() } } } @@ -98,11 +96,6 @@ impl Default for BufferedZopfliDeflater { impl Deflater for BufferedZopfliDeflater { /// Fork of the zlib_compress function in Zopfli. fn deflate(&self, data: &[u8], max_size: &AtomicMin) -> PngResult> { - #[allow(clippy::needless_update)] - let options = Options { - iteration_count: self.iterations, - ..Default::default() // for forward compatibility - }; let mut out = Cursor::new(Vec::with_capacity(self.output_buffer_size)); let cmf = 120; /* CM 8, CINFO 7. See zlib spec.*/ let flevel = 3; @@ -117,8 +110,8 @@ impl Deflater for BufferedZopfliDeflater { zopfli_oxipng::HashingAndCountingRead::new(data, &mut rolling_adler, None); out.write_all(&cmfflg.to_be_bytes())?; let mut buffer = BufWriter::with_capacity( - self.buffer_size, - DeflateEncoder::new(options, Default::default(), &mut out), + self.input_buffer_size, + DeflateEncoder::new(self.options.clone(), Default::default(), &mut out), ); copy(&mut in_data, &mut buffer)?; buffer.into_inner()?.finish()?; diff --git a/src/deflate/zopfli_oxipng.rs b/src/deflate/zopfli_oxipng.rs index 3c6c8d4f..04adbfeb 100644 --- a/src/deflate/zopfli_oxipng.rs +++ b/src/deflate/zopfli_oxipng.rs @@ -2,11 +2,11 @@ use crate::{PngError, PngResult}; use simd_adler32::Adler32; use std::io::{Error, ErrorKind, Read}; -pub fn deflate(data: &[u8], options: &zopfli::Options) -> PngResult> { +pub fn deflate(data: &[u8], options: zopfli::Options) -> PngResult> { use std::cmp::max; let mut output = Vec::with_capacity(max(1024, data.len() / 20)); - match zopfli::compress(options, &zopfli::Format::Zlib, data, &mut output) { + match zopfli::compress(options, zopfli::Format::Zlib, data, &mut output) { Ok(_) => (), Err(_) => return Err(PngError::new("Failed to compress in zopfli")), }; diff --git a/src/lib.rs b/src/lib.rs index 11a05d1b..3b133434 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -890,7 +890,7 @@ fn postprocess_chunks( opts: &Options, deadline: Arc, orig_ihdr: &IhdrData, - deflater: T + deflater: &T )where T: Deflater, { diff --git a/src/main.rs b/src/main.rs index 4936fc66..01106229 100644 --- a/src/main.rs +++ b/src/main.rs @@ -28,8 +28,6 @@ use rayon::prelude::*; use std::ffi::OsString; use std::fs::DirBuilder; use std::io::Write; -#[cfg(feature = "zopfli")] -use std::num::NonZeroU8; use std::path::PathBuf; use std::process::exit; use std::time::Duration; diff --git a/tests/flags.rs b/tests/flags.rs index 029256bd..561e06e3 100644 --- a/tests/flags.rs +++ b/tests/flags.rs @@ -4,13 +4,10 @@ use oxipng::*; #[cfg(feature = "filetime")] use std::cell::RefCell; use std::fs::remove_file; -#[cfg(feature = "zopfli")] -use std::num::NonZeroU8; #[cfg(feature = "filetime")] use std::ops::Deref; use std::path::Path; use std::path::PathBuf; -use oxipng::Deflaters::Zopfli; const GRAYSCALE: u8 = 0; const RGB: u8 = 2;