-
Notifications
You must be signed in to change notification settings - Fork 124
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Revert "Provide BufferedZopfliDeflater and allow user to pass in a cu…
- Loading branch information
1 parent
775e718
commit 31e796c
Showing
14 changed files
with
77 additions
and
242 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,64 +1,18 @@ | ||
use crate::{PngError, PngResult}; | ||
use simd_adler32::Adler32; | ||
use std::io::{Error, ErrorKind, Read}; | ||
use std::num::NonZeroU8; | ||
|
||
pub fn deflate(data: &[u8], options: &zopfli::Options) -> PngResult<Vec<u8>> { | ||
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)); | ||
match zopfli::compress(options, &zopfli::Format::Zlib, data, &mut output) { | ||
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) | ||
} | ||
|
||
/// Forked from zopfli crate | ||
pub trait Hasher { | ||
fn update(&mut self, data: &[u8]); | ||
} | ||
|
||
impl Hasher for &mut Adler32 { | ||
fn update(&mut self, data: &[u8]) { | ||
Adler32::write(self, data) | ||
} | ||
} | ||
|
||
/// A reader that wraps another reader, a hasher and an optional counter, | ||
/// updating the hasher state and incrementing a counter of bytes read so | ||
/// far for each block of data read. | ||
pub struct HashingAndCountingRead<'counter, R: Read, H: Hasher> { | ||
inner: R, | ||
hasher: H, | ||
bytes_read: Option<&'counter mut u32>, | ||
} | ||
|
||
impl<'counter, R: Read, H: Hasher> HashingAndCountingRead<'counter, R, H> { | ||
pub fn new(inner: R, hasher: H, bytes_read: Option<&'counter mut u32>) -> Self { | ||
Self { | ||
inner, | ||
hasher, | ||
bytes_read, | ||
} | ||
} | ||
} | ||
|
||
impl<R: Read, H: Hasher> Read for HashingAndCountingRead<'_, R, H> { | ||
fn read(&mut self, buf: &mut [u8]) -> Result<usize, Error> { | ||
match self.inner.read(buf) { | ||
Ok(bytes_read) => { | ||
self.hasher.update(&buf[..bytes_read]); | ||
|
||
if let Some(total_bytes_read) = &mut self.bytes_read { | ||
**total_bytes_read = total_bytes_read | ||
.checked_add(bytes_read.try_into().map_err(|_| ErrorKind::Other)?) | ||
.ok_or(ErrorKind::Other)?; | ||
} | ||
|
||
Ok(bytes_read) | ||
} | ||
Err(err) => Err(err), | ||
} | ||
} | ||
} |
Oops, something went wrong.