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 Dec 1, 2023
1 parent 12fa289 commit 29ca80d
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 18 deletions.
10 changes: 3 additions & 7 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::{copy, 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 @@ -31,10 +29,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 @@ -47,7 +43,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 Down
13 changes: 5 additions & 8 deletions src/deflate/zopfli_oxipng.rs
Original file line number Diff line number Diff line change
@@ -1,15 +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>> {
let mut output = Vec::with_capacity(data.len());
let options = zopfli::Options {
iteration_count: iterations.into(),
..Default::default()
};
match zopfli::compress(options, zopfli::Format::Zlib, data, &mut output) {
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));
match zopfli::compress(options, &zopfli::Format::Zlib, data, &mut output) {
Ok(_) => (),
Err(_) => return Err(PngError::new("Failed to compress in zopfli")),
};
Expand Down
5 changes: 2 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -658,9 +658,8 @@ fn parse_opts_into_struct(

if matches.get_flag("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 29ca80d

Please sign in to comment.