Make libdeflater and zopfli optional (#319)

* Make `libdeflater` and `zopfli` optional
This commit is contained in:
Anhil 2020-11-02 04:18:15 +03:00 committed by GitHub
parent 17cd51259e
commit 70f911fa3e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 17 additions and 4 deletions

View file

@ -33,11 +33,11 @@ bit-vec = "^0.6.2"
byteorder = "^1.0.0"
crc = "^1.2.0"
itertools = "^0.9.0"
zopfli = "^0.4.0"
zopfli = { version = "^0.4.0", optional = true }
miniz_oxide = "0.4"
rgb = "0.8.25"
indexmap = { version = "1.6.0", features = ["rayon"] }
libdeflater = "0.5.0"
libdeflater = { version = "0.5.0", optional = true }
log = "0.4.11"
stderrlog = { version = "0.5.0", optional = true }
@ -71,7 +71,7 @@ binary = [
"wild",
"stderrlog",
]
default = ["binary", "parallel"]
default = ["binary", "parallel", "libdeflater", "zopfli"]
parallel = ["rayon"]
[lib]

View file

@ -3,12 +3,13 @@ use crate::error::PngError;
use crate::Deadline;
use crate::PngResult;
use indexmap::IndexSet;
use std::cmp::max;
#[doc(hidden)]
pub mod miniz_stream;
#[cfg(feature = "libdeflater")]
mod deflater;
#[cfg(feature = "libdeflater")]
pub use deflater::deflate as libdeflater_deflate;
#[cfg(any(target_arch = "x86_64", target_arch = "aarch64"))]
@ -47,7 +48,10 @@ pub fn deflate(
miniz_stream::compress_to_vec_oxipng(data, zc, zw.into(), zs.into(), max_size, deadline)
}
#[cfg(feature = "zopfli")]
pub fn zopfli_deflate(data: &[u8]) -> PngResult<Vec<u8>> {
use std::cmp::max;
let mut output = Vec::with_capacity(max(1024, data.len() / 20));
let options = zopfli::Options::default();
match zopfli::compress(&options, &zopfli::Format::Zlib, data, &mut output) {
@ -79,8 +83,10 @@ pub enum Deflaters {
/// Default: `15`
window: u8,
},
#[cfg(feature = "zopfli")]
/// Use the better but slower Zopfli implementation
Zopfli,
#[cfg(feature = "libdeflater")]
/// Use libdeflater.
Libdeflater,
}

View file

@ -12,6 +12,11 @@
#![warn(clippy::path_buf_push_overwrite)]
#![warn(clippy::range_plus_one)]
#![allow(clippy::cognitive_complexity)]
#![cfg_attr(
not(any(feature = "libdeflater", feature = "zopfli")),
allow(irrefutable_let_patterns),
allow(unreachable_patterns)
)]
#[cfg(feature = "parallel")]
extern crate rayon;
@ -569,7 +574,9 @@ fn optimize_png(
&best_size,
&deadline,
),
#[cfg(feature = "zopfli")]
Deflaters::Zopfli => deflate::zopfli_deflate(filtered),
#[cfg(feature = "libdeflater")]
Deflaters::Libdeflater => deflate::libdeflater_deflate(filtered, &best_size),
};