From 754133abbd568b04739f5e04d83ac3909fa73a62 Mon Sep 17 00:00:00 2001 From: Anhil <53266103+Anhil-111@users.noreply.github.com> Date: Mon, 28 Sep 2020 19:45:10 +0300 Subject: [PATCH] Make `libdeflater` and `zopfli` optional --- Cargo.toml | 6 +++--- src/deflate/mod.rs | 8 +++++++- src/lib.rs | 30 ++++++++++++++++++++++++++++++ 3 files changed, 40 insertions(+), 4 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 0af94d4d..c195c888 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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] diff --git a/src/deflate/mod.rs b/src/deflate/mod.rs index d64b958b..69879901 100644 --- a/src/deflate/mod.rs +++ b/src/deflate/mod.rs @@ -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> { + 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, } diff --git a/src/lib.rs b/src/lib.rs index d988e471..ea3ce8c5 100644 --- a/src/lib.rs +++ b/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 = 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), };