Skip to content

Commit

Permalink
Fix merge
Browse files Browse the repository at this point in the history
  • Loading branch information
Pr0methean committed Dec 1, 2023
1 parent 8eda163 commit 49b0256
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 24 deletions.
25 changes: 9 additions & 16 deletions src/deflate/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ mod zopfli_oxipng;
use simd_adler32::Adler32;
#[cfg(feature = "zopfli")]
pub use zopfli_oxipng::deflate as zopfli_deflate;
#[cfg(feature = "zopfli")]
use zopfli::Options as ZopfliOptions;

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
/// DEFLATE algorithms supported by oxipng
Expand All @@ -41,7 +43,7 @@ impl Deflater for Deflaters {
let compressed = match self {
Self::Libdeflater { compression } => deflate(data, *compression, max_size)?,
#[cfg(feature = "zopfli")]
Self::Zopfli { options } => zopfli_deflate(data, options)?,
Self::Zopfli { options } => zopfli_deflate(data, options.clone())?,
};
if let Some(max) = max_size.get() {
if compressed.len() > max {
Expand All @@ -55,34 +57,30 @@ impl Deflater for Deflaters {
#[cfg(feature = "zopfli")]
#[derive(Copy, Clone, Debug)]
pub struct BufferedZopfliDeflater {
iterations: NonZeroU8,
input_buffer_size: usize,
output_buffer_size: usize,
options: Options,
options: ZopfliOptions,
}

#[cfg(feature = "zopfli")]
impl BufferedZopfliDeflater {
pub const fn new(
iterations: NonZeroU8,
input_buffer_size: usize,
output_buffer_size: usize,
max_block_splits: u16,
options: ZopfliOptions,
) -> Self {
BufferedZopfliDeflater {
iterations,
input_buffer_size,
output_buffer_size,
options
}
}

pub const fn const_default() -> Self {
BufferedZopfliDeflater {
// SAFETY: trivially safe. Stopgap solution until const unwrap is stabilized.
iterations: unsafe { NonZeroU8::new_unchecked(15) },
input_buffer_size: 1024 * 1024,
output_buffer_size: 64 * 1024,
max_block_splits: 15,
options: ZopfliOptions::const_default()
}
}
}
Expand All @@ -98,11 +96,6 @@ impl Default for BufferedZopfliDeflater {
impl Deflater for BufferedZopfliDeflater {
/// Fork of the zlib_compress function in Zopfli.
fn deflate(&self, data: &[u8], max_size: &AtomicMin) -> PngResult<Vec<u8>> {
#[allow(clippy::needless_update)]
let options = Options {
iteration_count: self.iterations,
..Default::default() // for forward compatibility
};
let mut out = Cursor::new(Vec::with_capacity(self.output_buffer_size));
let cmf = 120; /* CM 8, CINFO 7. See zlib spec.*/
let flevel = 3;
Expand All @@ -117,8 +110,8 @@ impl Deflater for BufferedZopfliDeflater {
zopfli_oxipng::HashingAndCountingRead::new(data, &mut rolling_adler, None);
out.write_all(&cmfflg.to_be_bytes())?;
let mut buffer = BufWriter::with_capacity(
self.buffer_size,
DeflateEncoder::new(options, Default::default(), &mut out),
self.input_buffer_size,
DeflateEncoder::new(self.options.clone(), Default::default(), &mut out),
);
copy(&mut in_data, &mut buffer)?;
buffer.into_inner()?.finish()?;
Expand Down
4 changes: 2 additions & 2 deletions src/deflate/zopfli_oxipng.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ use crate::{PngError, PngResult};
use simd_adler32::Adler32;
use std::io::{Error, ErrorKind, Read};

pub fn deflate(data: &[u8], options: &zopfli::Options) -> PngResult<Vec<u8>> {
pub fn deflate(data: &[u8], options: zopfli::Options) -> PngResult<Vec<u8>> {
use std::cmp::max;

let mut output = Vec::with_capacity(max(1024, data.len() / 20));
match zopfli::compress(options, &zopfli::Format::Zlib, data, &mut output) {
match zopfli::compress(options, zopfli::Format::Zlib, data, &mut output) {
Ok(_) => (),
Err(_) => return Err(PngError::new("Failed to compress in zopfli")),
};
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -890,7 +890,7 @@ fn postprocess_chunks<T>(
opts: &Options,
deadline: Arc<Deadline>,
orig_ihdr: &IhdrData,
deflater: T
deflater: &T
)where
T: Deflater,
{
Expand Down
2 changes: 0 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@ use rayon::prelude::*;
use std::ffi::OsString;
use std::fs::DirBuilder;
use std::io::Write;
#[cfg(feature = "zopfli")]
use std::num::NonZeroU8;
use std::path::PathBuf;
use std::process::exit;
use std::time::Duration;
Expand Down
3 changes: 0 additions & 3 deletions tests/flags.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,10 @@ use oxipng::*;
#[cfg(feature = "filetime")]
use std::cell::RefCell;
use std::fs::remove_file;
#[cfg(feature = "zopfli")]
use std::num::NonZeroU8;
#[cfg(feature = "filetime")]
use std::ops::Deref;
use std::path::Path;
use std::path::PathBuf;
use oxipng::Deflaters::Zopfli;

const GRAYSCALE: u8 = 0;
const RGB: u8 = 2;
Expand Down

0 comments on commit 49b0256

Please sign in to comment.