Fix building without zopfli

This commit is contained in:
Andrew 2022-10-24 20:01:41 +13:00
parent 88cd069c37
commit bd51945b4a
3 changed files with 32 additions and 32 deletions

View file

@ -1,31 +1,16 @@
use crate::error::PngError;
use crate::PngResult;
use indexmap::IndexSet; use indexmap::IndexSet;
#[cfg(feature = "zopfli")]
use std::num::NonZeroU8;
mod deflater; mod deflater;
pub use deflater::crc32; pub use deflater::crc32;
pub use deflater::deflate; pub use deflater::deflate;
pub use deflater::inflate; pub use deflater::inflate;
#[cfg(feature = "zopfli")] #[cfg(feature = "zopfli")]
pub fn zopfli_deflate(data: &[u8], iterations: NonZeroU8) -> PngResult<Vec<u8>> { use std::num::NonZeroU8;
use std::cmp::max; #[cfg(feature = "zopfli")]
mod zopfli_oxipng;
let mut output = Vec::with_capacity(max(1024, data.len() / 20)); #[cfg(feature = "zopfli")]
let options = zopfli::Options { pub use zopfli_oxipng::deflate as zopfli_deflate;
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)
}
#[derive(Clone, Debug, PartialEq, Eq)] #[derive(Clone, Debug, PartialEq, Eq)]
/// DEFLATE algorithms supported by oxipng /// DEFLATE algorithms supported by oxipng

View file

@ -0,0 +1,18 @@
use crate::{PngError, PngResult};
use std::num::NonZeroU8;
pub fn deflate(data: &[u8], iterations: NonZeroU8) -> 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) {
Ok(_) => (),
Err(_) => return Err(PngError::new("Failed to compress in zopfli")),
};
output.shrink_to_fit();
Ok(output)
}

View file

@ -22,6 +22,7 @@ use oxipng::Headers;
use oxipng::Options; use oxipng::Options;
use oxipng::{InFile, OutFile}; use oxipng::{InFile, OutFile};
use std::fs::DirBuilder; use std::fs::DirBuilder;
#[cfg(feature = "zopfli")]
use std::num::NonZeroU8; use std::num::NonZeroU8;
use std::path::PathBuf; use std::path::PathBuf;
use std::process::exit; use std::process::exit;
@ -513,24 +514,20 @@ fn parse_opts_into_struct(
} }
if matches.is_present("zopfli") { if matches.is_present("zopfli") {
opts.deflate = Deflaters::Zopfli { if explicit_level > Some(3) {
iterations: NonZeroU8::new(15).unwrap(), 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 { } else if let Deflaters::Libdeflater { compression } = &mut opts.deflate {
if let Some(x) = matches.value_of("compression") { if let Some(x) = matches.value_of("compression") {
*compression = parse_numeric_range_opts(x, 1, 12).unwrap(); *compression = parse_numeric_range_opts(x, 1, 12).unwrap();
} }
} }
if explicit_level > Some(3) { #[cfg(feature = "parallel")]
match opts.deflate {
Deflaters::Libdeflater { .. } => {}
_ => {
warn!("Level 4 and above are equivalent to level 3 for zopfli");
}
}
}
if let Some(x) = matches.value_of("threads") { if let Some(x) = matches.value_of("threads") {
let threads = x.parse::<usize>().unwrap(); let threads = x.parse::<usize>().unwrap();