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

Make flate2 a feature flag #563

Closed
wants to merge 1 commit into from
Closed
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: 5 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,13 @@ rand = "0.8.4"
term = "1.0.1"

[features]
unstable = ["crc32fast/nightly"]
flate2 = [] # Expose flate2 specific functionality
unstable = ["crc32fast/nightly"] # Enable nightly-only features
benchmarks = []

[package.metadata.docs.rs]
features = ["flate2"]

[lints.rust]
unexpected_cfgs = { level = "warn", check-cfg = ['cfg(fuzzing)'] }

Expand Down
15 changes: 12 additions & 3 deletions src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -378,10 +378,19 @@ pub enum DeflateCompression {
/// while still providing a decent compression ratio.
FdeflateUltraFast,

/// Compression level between 0 to 9, where higher values mean better compression at the cost of
/// speed.
///
/// The precise meaning of each level depends the internal impementation and may change in the
/// future. If you want to ensure that the `flate2`` backend is used, you can use the
/// [`Flate2``] variant instead.
Level(u8),

/// Uses [flate2](https://crates.io/crates/flate2) crate with the specified [compression level](flate2::Compression::new).
///
/// Flate2 has several backends that make different trade-offs.
/// See the flate2 documentation for the available backends for more information.
#[cfg(feature = "flate2")]
Flate2(u8),
// Other variants can be added in the future
}
Expand All @@ -398,16 +407,16 @@ impl DeflateCompression {
Compression::NoCompression => Self::NoCompression,
Compression::Fastest => Self::FdeflateUltraFast,
Compression::Fast => Self::FdeflateUltraFast,
Compression::Balanced => Self::Flate2(flate2::Compression::default().level() as u8),
Compression::High => Self::Flate2(flate2::Compression::best().level() as u8),
Compression::Balanced => Self::Level(flate2::Compression::default().level() as u8),
Compression::High => Self::Level(flate2::Compression::best().level() as u8),
}
}

pub(crate) fn closest_flate2_level(&self) -> flate2::Compression {
match self {
DeflateCompression::NoCompression => flate2::Compression::none(),
DeflateCompression::FdeflateUltraFast => flate2::Compression::new(1),
DeflateCompression::Flate2(level) => flate2::Compression::new(u32::from(*level)),
DeflateCompression::Level(level) => flate2::Compression::new(u32::from(*level)),
}
}
}
Expand Down
20 changes: 16 additions & 4 deletions src/encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -782,7 +782,15 @@ impl<W: Write> Writer<W> {
let bpp = self.info.bpp_in_prediction();
let filter_method = self.options.filter;

let zlib_encoded = match self.options.compression {
#[allow(unused_mut)]
let mut compression = self.options.compression;

#[cfg(feature = "flate2")]
if let DeflateCompression::Flate2(level) = compression {
compression = DeflateCompression::Level(level);
}

let zlib_encoded = match compression {
DeflateCompression::NoCompression => {
let mut compressor =
fdeflate::StoredOnlyCompressor::new(std::io::Cursor::new(Vec::new()))?;
Expand Down Expand Up @@ -823,7 +831,7 @@ impl<W: Write> Writer<W> {
compressed
}
}
DeflateCompression::Flate2(level) => {
DeflateCompression::Level(level) => {
let mut current = vec![0; in_len];

let mut zlib =
Expand All @@ -837,6 +845,10 @@ impl<W: Write> Writer<W> {
}
zlib.finish()?
}
#[cfg(feature = "flate2")]
DeflateCompression::Flate2(level) => {
unreachable!("Flate2 is handled by the Level variant")
}
};

match self.info.frame_control {
Expand Down Expand Up @@ -1772,7 +1784,7 @@ mod tests {
let mut buf = vec![0; reader.output_buffer_size()];
let info = reader.next_frame(&mut buf).unwrap();
use DeflateCompression::*;
for compression in [NoCompression, FdeflateUltraFast, Flate2(4)] {
for compression in [NoCompression, FdeflateUltraFast, Level(4)] {
// Encode decoded image
let mut out = Vec::new();
{
Expand Down Expand Up @@ -1830,7 +1842,7 @@ mod tests {
let mut buf = vec![0; reader.output_buffer_size()];
let info = reader.next_frame(&mut buf).unwrap();
use DeflateCompression::*;
for compression in [NoCompression, FdeflateUltraFast, Flate2(4)] {
for compression in [NoCompression, FdeflateUltraFast, Level(4)] {
// Encode decoded image
let mut out = Vec::new();
{
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
//! ```
//!

#![cfg_attr(docsrs, feature(doc_auto_cfg))]
#![cfg_attr(feature = "unstable", feature(portable_simd))]
#![forbid(unsafe_code)]

Expand Down
Loading