From bd51945b4a9c09628c652ee22cc292d568f01f11 Mon Sep 17 00:00:00 2001 From: Andrew Date: Mon, 24 Oct 2022 20:01:41 +1300 Subject: [PATCH] Fix building without zopfli --- src/deflate/mod.rs | 25 +++++-------------------- src/deflate/zopfli_oxipng.rs | 18 ++++++++++++++++++ src/main.rs | 21 +++++++++------------ 3 files changed, 32 insertions(+), 32 deletions(-) create mode 100644 src/deflate/zopfli_oxipng.rs diff --git a/src/deflate/mod.rs b/src/deflate/mod.rs index 2f91ad22..ac17577c 100644 --- a/src/deflate/mod.rs +++ b/src/deflate/mod.rs @@ -1,31 +1,16 @@ -use crate::error::PngError; -use crate::PngResult; use indexmap::IndexSet; -#[cfg(feature = "zopfli")] -use std::num::NonZeroU8; - mod deflater; pub use deflater::crc32; pub use deflater::deflate; pub use deflater::inflate; #[cfg(feature = "zopfli")] -pub fn zopfli_deflate(data: &[u8], iterations: NonZeroU8) -> PngResult> { - 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) { - Ok(_) => (), - Err(_) => return Err(PngError::new("Failed to compress in zopfli")), - }; - output.shrink_to_fit(); - Ok(output) -} +use std::num::NonZeroU8; +#[cfg(feature = "zopfli")] +mod zopfli_oxipng; +#[cfg(feature = "zopfli")] +pub use zopfli_oxipng::deflate as zopfli_deflate; #[derive(Clone, Debug, PartialEq, Eq)] /// DEFLATE algorithms supported by oxipng diff --git a/src/deflate/zopfli_oxipng.rs b/src/deflate/zopfli_oxipng.rs new file mode 100644 index 00000000..b59b93df --- /dev/null +++ b/src/deflate/zopfli_oxipng.rs @@ -0,0 +1,18 @@ +use crate::{PngError, PngResult}; +use std::num::NonZeroU8; + +pub fn deflate(data: &[u8], iterations: NonZeroU8) -> PngResult> { + 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) { + Ok(_) => (), + Err(_) => return Err(PngError::new("Failed to compress in zopfli")), + }; + output.shrink_to_fit(); + Ok(output) +} diff --git a/src/main.rs b/src/main.rs index d950dd1d..6969f2f1 100644 --- a/src/main.rs +++ b/src/main.rs @@ -22,6 +22,7 @@ use oxipng::Headers; use oxipng::Options; use oxipng::{InFile, OutFile}; use std::fs::DirBuilder; +#[cfg(feature = "zopfli")] use std::num::NonZeroU8; use std::path::PathBuf; use std::process::exit; @@ -513,24 +514,20 @@ fn parse_opts_into_struct( } if matches.is_present("zopfli") { - opts.deflate = Deflaters::Zopfli { - iterations: NonZeroU8::new(15).unwrap(), - }; + if explicit_level > Some(3) { + warn!("Level 4 and above are equivalent to level 3 for zopfli"); + } + #[cfg(feature = "zopfli")] + if let Some(iterations) = NonZeroU8::new(15) { + opts.deflate = Deflaters::Zopfli { iterations }; + } } else if let Deflaters::Libdeflater { compression } = &mut opts.deflate { if let Some(x) = matches.value_of("compression") { *compression = parse_numeric_range_opts(x, 1, 12).unwrap(); } } - if explicit_level > Some(3) { - match opts.deflate { - Deflaters::Libdeflater { .. } => {} - _ => { - warn!("Level 4 and above are equivalent to level 3 for zopfli"); - } - } - } - + #[cfg(feature = "parallel")] if let Some(x) = matches.value_of("threads") { let threads = x.parse::().unwrap();