Skip to content

Commit

Permalink
Provide BufferedZopfliDeflater and allow user to pass in a custom Def…
Browse files Browse the repository at this point in the history
…later (shssoichiro#530)

* Add .whitesource configuration file

* Experimental: allow Zopfli to use any size BufWriter

* Allow user to specify the output buffer size as well

* Allow user to specify maximum block splits

* Reformat and fix warnings

* Use deflater on iCCP chunk as well

* Bug fix: need to implement Zlib format

* Make functions const when possible

* Switch to using zopfli::Options in prep for zopfli-rs/zopfli#21

* Switch to using zopfli::Options in prep for zopfli-rs/zopfli#21

* Cargo fmt

* Fix compilation

* Fix tests

* Fix more lints

* Fix more lints

* Fix compilation more

---------

Co-authored-by: mend-bolt-for-github[bot] <42819689+mend-bolt-for-github[bot]@users.noreply.github.com>
Co-authored-by: Chris Hennick <[email protected]>
Co-authored-by: Chris Hennick <[email protected]>
  • Loading branch information
4 people committed Dec 1, 2023
1 parent 2249391 commit 75200fc
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 6 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ required-features = ["zopfli"]
[dependencies]
zopfli = {git = "https://github.com/Pr0methean/zopfli.git", default-features = false, features = ["zlib"] }
simd-adler32 = { version = "0.3.5", optional = true, default-features = false }
simd-adler32 = { version = "0.3.5", optional = true, default-features = false }
rgb = "0.8.36"
indexmap = "2.0.0"
libdeflater = "0.14.0"
Expand Down Expand Up @@ -67,7 +68,7 @@ version = "0.24.6"
rustc_version = "0.4.0"

[features]
zopfli = ["zopfli/zlib", "simd-adler32"]
zopfli = ["zopfli/std", "zopfli/zlib", "simd-adler32"]
binary = ["clap", "wild", "stderrlog"]
default = ["binary", "filetime", "parallel", "zopfli"]
parallel = ["rayon", "indexmap/rayon", "crossbeam-channel"]
Expand Down
1 change: 0 additions & 1 deletion benches/zopfli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ extern crate test;

use oxipng::internal_tests::*;
use oxipng::*;
use std::num::NonZeroU8;
use std::path::PathBuf;
use test::Bencher;

Expand Down
7 changes: 6 additions & 1 deletion src/deflate/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,18 @@ pub use deflater::inflate;
use std::io::{copy, BufWriter, Cursor, Write};
use std::{fmt, fmt::Display, io};

#[cfg(feature = "zopfli")]
use std::io::{self, copy, BufWriter, Cursor, Write};

#[cfg(feature = "zopfli")]
use zopfli::{DeflateEncoder, Options};
#[cfg(feature = "zopfli")]
mod zopfli_oxipng;
#[cfg(feature = "zopfli")]
use simd_adler32::Adler32;
#[cfg(feature = "zopfli")]
use simd_adler32::Adler32;
#[cfg(feature = "zopfli")]
pub use zopfli_oxipng::deflate as zopfli_deflate;

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
Expand Down Expand Up @@ -113,7 +118,7 @@ impl Deflater for BufferedZopfliDeflater {
Ok(out.into_inner())
})();
let out = out.map_err(|e| PngError::new(&e.to_string()))?;
if max_size.get().is_some_and(|max| max < out.len()) {
if max_size.get().map(|max| max < out.len()).unwrap_or(false) {
Err(PngError::DeflatedDataTooLong(out.len()))
} else {
Ok(out)
Expand Down
17 changes: 14 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -588,7 +588,13 @@ fn optimize_png(
} else {
Some(png.estimated_output_size())
};
if let Some(new_png) = optimize_raw(raw.clone(), &opts, deadline.clone(), max_size, &opts.deflate) {
if let Some(new_png) = optimize_raw(
raw.clone(),
&opts,
deadline.clone(),
max_size,
&opts.deflate,
) {
png.raw = new_png.raw;
png.idat_data = new_png.idat_data;
}
Expand Down Expand Up @@ -861,8 +867,13 @@ fn report_format(prefix: &str, png: &PngImage) {
}

/// Perform cleanup of certain chunks from the `PngData` object, after optimization has been completed
fn postprocess_chunks<T>(png: &mut PngData, opts: &Options, orig_ihdr: &IhdrData, deadline: Arc<Deadline>, deflater: &T)
where
fn postprocess_chunks<T>(
png: &mut PngData,
opts: &Options,
deadline: Arc<Deadline>,
orig_ihdr: &IhdrData,
deflater: &T,
) where
T: Deflater,
{
if let Some(iccp_idx) = png.aux_chunks.iter().position(|c| &c.name == b"iCCP") {
Expand Down

0 comments on commit 75200fc

Please sign in to comment.