Make libdeflater and zopfli optional
This commit is contained in:
parent
a6fa079e64
commit
754133abbd
3 changed files with 40 additions and 4 deletions
|
|
@ -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]
|
||||
|
|
|
|||
|
|
@ -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,
|
||||
}
|
||||
|
|
|
|||
30
src/lib.rs
30
src/lib.rs
|
|
@ -219,6 +219,10 @@ impl Options {
|
|||
// on an `Options` struct generated by the `default` method.
|
||||
fn apply_preset_0(mut self) -> Self {
|
||||
self.idat_recoding = false;
|
||||
#[cfg_attr(
|
||||
not(any(feature = "zopfli", feature = "libdeflater")),
|
||||
allow(irrefutable_let_patterns)
|
||||
)]
|
||||
if let Deflaters::Zlib { compression, .. } = &mut self.deflate {
|
||||
compression.clear();
|
||||
compression.insert(3);
|
||||
|
|
@ -228,6 +232,10 @@ impl Options {
|
|||
|
||||
fn apply_preset_1(mut self) -> Self {
|
||||
self.filter.clear();
|
||||
#[cfg_attr(
|
||||
not(any(feature = "zopfli", feature = "libdeflater")),
|
||||
allow(irrefutable_let_patterns)
|
||||
)]
|
||||
if let Deflaters::Zlib { strategies, .. } = &mut self.deflate {
|
||||
strategies.clear();
|
||||
}
|
||||
|
|
@ -251,6 +259,10 @@ impl Options {
|
|||
}
|
||||
|
||||
fn apply_preset_5(mut self) -> Self {
|
||||
#[cfg_attr(
|
||||
not(any(feature = "zopfli", feature = "libdeflater")),
|
||||
allow(irrefutable_let_patterns)
|
||||
)]
|
||||
if let Deflaters::Zlib { compression, .. } = &mut self.deflate {
|
||||
compression.clear();
|
||||
for i in 3..9 {
|
||||
|
|
@ -261,6 +273,10 @@ impl Options {
|
|||
}
|
||||
|
||||
fn apply_preset_6(mut self) -> Self {
|
||||
#[cfg_attr(
|
||||
not(any(feature = "zopfli", feature = "libdeflater")),
|
||||
allow(irrefutable_let_patterns)
|
||||
)]
|
||||
if let Deflaters::Zlib { compression, .. } = &mut self.deflate {
|
||||
compression.clear();
|
||||
for i in 1..3 {
|
||||
|
|
@ -463,6 +479,10 @@ fn optimize_png(
|
|||
info!(" File size = {} bytes", file_original_size);
|
||||
|
||||
let mut filter = opts.filter.clone();
|
||||
#[cfg_attr(
|
||||
not(any(feature = "zopfli", feature = "libdeflater")),
|
||||
allow(unreachable_patterns)
|
||||
)]
|
||||
let mut strategies = match &opts.deflate {
|
||||
Deflaters::Zlib { strategies, .. } => Some(strategies.clone()),
|
||||
_ => None,
|
||||
|
|
@ -504,6 +524,10 @@ fn optimize_png(
|
|||
|
||||
if opts.idat_recoding || reduction_occurred {
|
||||
// Go through selected permutations and determine the best
|
||||
#[cfg_attr(
|
||||
not(any(feature = "zopfli", feature = "libdeflater")),
|
||||
allow(irrefutable_let_patterns)
|
||||
)]
|
||||
let combinations = if let Deflaters::Zlib { compression, .. } = &opts.deflate {
|
||||
filter.len() * compression.len() * strategies.as_ref().unwrap().len()
|
||||
} else {
|
||||
|
|
@ -512,6 +536,10 @@ fn optimize_png(
|
|||
let mut results: Vec<TrialOptions> = Vec::with_capacity(combinations);
|
||||
|
||||
for f in &filter {
|
||||
#[cfg_attr(
|
||||
not(any(feature = "zopfli", feature = "libdeflater")),
|
||||
allow(irrefutable_let_patterns)
|
||||
)]
|
||||
if let Deflaters::Zlib { compression, .. } = &opts.deflate {
|
||||
for zc in compression {
|
||||
for zs in strategies.as_ref().unwrap() {
|
||||
|
|
@ -569,7 +597,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),
|
||||
};
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue