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 cd57847 commit 98a34c0
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 19 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::{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 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 98a34c0

Please sign in to comment.