Skip to content

Commit

Permalink
Switch to using zopfli::Options in prep for zopfli-rs/zopfli#21
Browse files Browse the repository at this point in the history
  • Loading branch information
Pr0methean committed Jun 24, 2023
1 parent b870a1b commit 6bb019b
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 43 deletions.
43 changes: 12 additions & 31 deletions src/deflate/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ pub use deflater::inflate;
use std::io::{BufWriter, copy, Cursor, Write};
use std::{fmt, fmt::Display, io};

#[cfg(feature = "zopfli")]
use std::num::NonZeroU8;
#[cfg(feature = "zopfli")]
use zopfli::{DeflateEncoder, Options};
#[cfg(feature = "zopfli")]
Expand All @@ -29,10 +27,8 @@ pub enum Deflaters {
#[cfg(feature = "zopfli")]
/// Use the better but slower Zopfli implementation
Zopfli {
/// 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,
/// Zopfli compression options
options: Options,
},
}

Expand All @@ -45,7 +41,7 @@ impl Deflater for Deflaters {
let compressed = match self {
Self::Libdeflater { compression } => deflate(data, *compression, max_size)?,
#[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 compressed.len() > max {
Expand All @@ -59,43 +55,34 @@ impl Deflater for Deflaters {
#[cfg(feature = "zopfli")]
#[derive(Copy, Clone, Debug)]
pub struct BufferedZopfliDeflater {
iterations: NonZeroU8,
input_buffer_size: usize,
output_buffer_size: usize,
max_block_splits: u16,
options: Options
}

#[cfg(feature = "zopfli")]
impl BufferedZopfliDeflater {
pub const fn new(
iterations: NonZeroU8,
input_buffer_size: usize,
output_buffer_size: usize,
max_block_splits: u16,
options: Options,
) -> Self {
BufferedZopfliDeflater {
iterations,
input_buffer_size,
output_buffer_size,
max_block_splits,
}
}

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,
}
}
}

#[cfg(feature = "zopfli")]
impl Default for BufferedZopfliDeflater {
fn default() -> Self {
Self::const_default()
BufferedZopfliDeflater {
input_buffer_size: 1024 * 1024,
output_buffer_size: 64 * 1024,
options: Options::default()
}
}
}

Expand All @@ -104,12 +91,6 @@ impl Deflater for BufferedZopfliDeflater {

/// Fork of the zlib_compress function in Zopfli.
fn deflate(&self, data: &[u8], max_size: &AtomicMin) -> PngResult<Vec<u8>> {
#[allow(clippy::needless_update)]
let options = Options {
iteration_count: self.iterations,
maximum_block_splits: self.max_block_splits,
..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;
Expand All @@ -125,7 +106,7 @@ impl Deflater for BufferedZopfliDeflater {
let mut buffer = BufWriter::with_capacity(
self.input_buffer_size,
DeflateEncoder::new(
options,
self.options,
Default::default(),
&mut out,
),
Expand Down
9 changes: 2 additions & 7 deletions src/deflate/zopfli_oxipng.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,12 @@
use std::io::{Error, ErrorKind, Read};
use crate::{PngError, PngResult};
use std::num::NonZeroU8;
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;

let mut output = Vec::with_capacity(max(1024, data.len() / 20));
let options = zopfli::Options {
iteration_count: iterations,
..Default::default()
};
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")),
};
Expand Down
7 changes: 2 additions & 5 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ use oxipng::RowFilter;
use oxipng::StripChunks;
use oxipng::{InFile, OutFile};
use std::fs::DirBuilder;
#[cfg(feature = "zopfli")]
use std::num::NonZeroU8;
use std::path::PathBuf;
use std::process::exit;
use std::time::Duration;
Expand Down Expand Up @@ -552,9 +550,8 @@ fn parse_opts_into_struct(

if matches.is_present("zopfli") {
#[cfg(feature = "zopfli")]
if let Some(iterations) = NonZeroU8::new(15) {
opts.deflate = Deflaters::Zopfli { iterations };
}
let zopfli_opts = zopfli::Options::default();
opts.deflate = Deflaters::Zopfli { options: zopfli_opts };
} else if let Deflaters::Libdeflater { compression } = &mut opts.deflate {
if let Some(x) = matches.get_one::<i64>("compression") {
*compression = *x as u8;
Expand Down

0 comments on commit 6bb019b

Please sign in to comment.