Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use Flate2 for encoding #350

Merged
merged 5 commits into from
Aug 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "png"
version = "0.17.5"
version = "0.17.6"
license = "MIT OR Apache-2.0"

description = "PNG decoding and encoding library in pure Rust"
Expand All @@ -22,8 +22,8 @@ include = [
[dependencies]
bitflags = "1.0"
crc32fast = "1.2.0"
deflate = "1.0"
miniz_oxide = "0.5.1"
flate2 = "1.0"
miniz_oxide = "0.5.0"

[dev-dependencies]
criterion = "0.3.1"
Expand Down
8 changes: 8 additions & 0 deletions src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,15 @@ pub enum Compression {
/// the encoder can do, but is meant to emulate the `Best` setting in the `Flate2`
/// library.
Best,
#[deprecated(
since = "0.17.6",
note = "use one of the other compression levels instead, such as 'fast'"
)]
Huffman,
#[deprecated(
since = "0.17.6",
note = "use one of the other compression levels instead, such as 'fast'"
)]
Rle,
}

Expand Down
17 changes: 8 additions & 9 deletions src/encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use ops::{Deref, DerefMut};
use std::{borrow, error, fmt, io, mem, ops, result};

use crc32fast::Hasher as Crc32;
use deflate::write::ZlibEncoder;
use flate2::write::ZlibEncoder;

use crate::chunk::{self, ChunkType};
use crate::common::{
Expand Down Expand Up @@ -670,8 +670,7 @@ impl<W: Write> Writer<W> {
let mut prev = prev.as_slice();
let mut current = vec![0; in_len];

let mut zlib =
deflate::write::ZlibEncoder::new(Vec::new(), self.info.compression.to_options());
let mut zlib = ZlibEncoder::new(Vec::new(), self.info.compression.to_options());
let bpp = self.info.bpp_in_prediction();
let filter_method = self.options.filter;
let adaptive_method = self.options.adaptive_filter;
Expand Down Expand Up @@ -2289,13 +2288,13 @@ mod tests {
/// Since this only contains trait impls, there is no need to make this public, they are simply
/// available when the mod is compiled as well.
impl Compression {
fn to_options(self) -> deflate::CompressionOptions {
fn to_options(self) -> flate2::Compression {
match self {
Compression::Default => deflate::CompressionOptions::default(),
Compression::Fast => deflate::CompressionOptions::fast(),
Compression::Best => deflate::CompressionOptions::high(),
Compression::Huffman => deflate::CompressionOptions::huffman_only(),
Compression::Rle => deflate::CompressionOptions::rle(),
Compression::Default => flate2::Compression::default(),
Compression::Fast => flate2::Compression::fast(),
Compression::Best => flate2::Compression::best(),
Compression::Huffman => flate2::Compression::none(),
Compression::Rle => flate2::Compression::none(),
}
}
}
11 changes: 6 additions & 5 deletions src/text_metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,8 @@
#![warn(missing_docs)]

use crate::{chunk, encoder, DecodingError, EncodingError};
use deflate::{write::ZlibEncoder, Compression};
use flate2::write::ZlibEncoder;
use flate2::Compression;
use miniz_oxide::inflate::{decompress_to_vec_zlib, decompress_to_vec_zlib_with_limit};
use std::{convert::TryFrom, io::Write};

Expand Down Expand Up @@ -322,7 +323,7 @@ impl ZTXtChunk {
match &self.text {
OptCompressed::Uncompressed(s) => {
let uncompressed_raw = encode_iso_8859_1(s)?;
let mut encoder = ZlibEncoder::new(Vec::new(), Compression::Fast);
let mut encoder = ZlibEncoder::new(Vec::new(), Compression::fast());
encoder
.write_all(&uncompressed_raw)
.map_err(|_| EncodingError::from(TextEncodingError::CompressionError))?;
Expand Down Expand Up @@ -360,7 +361,7 @@ impl EncodableTextChunk for ZTXtChunk {
OptCompressed::Uncompressed(s) => {
// This code may have a bug. Check for correctness.
let uncompressed_raw = encode_iso_8859_1(s)?;
let mut encoder = ZlibEncoder::new(data, Compression::Fast);
let mut encoder = ZlibEncoder::new(data, Compression::fast());
encoder
.write_all(&uncompressed_raw)
.map_err(|_| EncodingError::from(TextEncodingError::CompressionError))?;
Expand Down Expand Up @@ -496,7 +497,7 @@ impl ITXtChunk {
match &self.text {
OptCompressed::Uncompressed(s) => {
let uncompressed_raw = s.as_bytes();
let mut encoder = ZlibEncoder::new(Vec::new(), Compression::Fast);
let mut encoder = ZlibEncoder::new(Vec::new(), Compression::fast());
encoder
.write_all(uncompressed_raw)
.map_err(|_| EncodingError::from(TextEncodingError::CompressionError))?;
Expand Down Expand Up @@ -558,7 +559,7 @@ impl EncodableTextChunk for ITXtChunk {
}
OptCompressed::Uncompressed(s) => {
let uncompressed_raw = s.as_bytes();
let mut encoder = ZlibEncoder::new(data, Compression::Fast);
let mut encoder = ZlibEncoder::new(data, Compression::fast());
encoder
.write_all(uncompressed_raw)
.map_err(|_| EncodingError::from(TextEncodingError::CompressionError))?;
Expand Down