This update brings several parameterization and usage flexibility improvements on Zopfli, allowing users to choose to limit execution time by a number of iterations without improvement, and exposing a more advanced `ZlibEncoder` struct to tune compression block sizes and DEFLATE block types. Some minor microoptimizations were also made. For now, I don't expect this PR to substantially affect how OxiPNG compresses images using its Zopfli mode, but the additional parameter customization may come in handy for future work improving how Zopfli is used in OxiPNG.
16 lines
533 B
Rust
16 lines
533 B
Rust
use crate::{PngError, PngResult};
|
|
use std::num::NonZeroU8;
|
|
|
|
pub fn deflate(data: &[u8], iterations: NonZeroU8) -> PngResult<Vec<u8>> {
|
|
let mut output = Vec::with_capacity(data.len());
|
|
let options = zopfli::Options {
|
|
iteration_count: iterations.into(),
|
|
..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)
|
|
}
|