Implement DEFLATE compression level control properly, with a semver break

This commit is contained in:
Sergey "Shnatsel" Davidoff committed 2026-03-05 17:59:18 +00:00
1 parent 37578add53
commit 0362e6e31d
5 files changed
+15 -27

No files matched your search

+2 -2
View File
@@ -4,7 +4,7 @@ extern crate tiff;
use criterion::{black_box, Criterion, Throughput};
use std::io::Cursor;
use tiff::encoder::colortype::RGB32Float;
use tiff::encoder::{Compression, DeflateLevel, Predictor, TiffEncoder};
use tiff::encoder::{Compression, Predictor, TiffEncoder};
const WIDTH: u32 = 500;
const HEIGHT: u32 = 500;
@@ -14,7 +14,7 @@ fn encode_image_f32_float_predictor_deflate(data: &[f32]) {
TiffEncoder::new(&mut buf)
.unwrap()
.with_predictor(Predictor::FloatingPoint)
.with_compression(Compression::Deflate(DeflateLevel::Fast))
.with_compression(Compression::Deflate(1))
.write_image::<RGB32Float>(WIDTH, HEIGHT, black_box(data))
.unwrap();
}
+8 -17
View File
@@ -7,24 +7,15 @@ pub struct Deflate {
level: FlateCompression,
}
/// The level of compression used by the Deflate algorithm.
/// It allows trading compression ratio for compression speed.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
#[non_exhaustive]
#[derive(Default)]
pub enum DeflateLevel {
/// The fastest possible compression mode.
Fast = 1,
/// The conserative choice between speed and ratio.
#[default]
Balanced = 6,
/// The best compression available with Deflate.
Best = 9,
}
impl Deflate {
/// Create a new deflate compressor with a specific level of compression.
pub fn with_level(level: DeflateLevel) -> Self {
///
/// The valid levels are 1 through 9 inclusive. The default level is 6.
///
/// - `1` stands for light but fast compression
/// - `6` is the recommended balanced default
/// - `9` is maximum compression ratio at the cost of slow encoding
pub fn with_level(level: u8) -> Self {
Self {
level: FlateCompression::new(level as u32),
}
@@ -33,7 +24,7 @@ impl Deflate {
impl Default for Deflate {
fn default() -> Self {
Self::with_level(DeflateLevel::default())
Self::with_level(6)
}
}
+1 -1
View File
@@ -9,7 +9,7 @@ mod packbits;
mod uncompressed;
#[cfg(feature = "deflate")]
pub use self::deflate::{Deflate, DeflateLevel};
pub use self::deflate::Deflate;
#[cfg(feature = "lzw")]
pub use self::lzw::Lzw;
+1 -3
View File
@@ -38,8 +38,6 @@ use self::writer::*;
/// [Predictor::Horizontal] is for integer sample types.
/// [Predictor::FloatingPoint] is for floating-point sample types (f32, f64).
pub type Predictor = crate::tags::Predictor;
#[cfg(feature = "deflate")]
pub type DeflateLevel = compression::DeflateLevel;
#[derive(Clone, Copy, PartialEq, Default)]
pub enum Compression {
@@ -48,7 +46,7 @@ pub enum Compression {
#[cfg(feature = "lzw")]
Lzw,
#[cfg(feature = "deflate")]
Deflate(DeflateLevel),
Deflate(u8),
Packbits,
}
+3 -4
View File
@@ -5,7 +5,6 @@ use tiff::{
decoder::{Decoder, DecodingResult},
encoder::{
colortype::{self, ColorType},
compression::*,
Compression, TiffEncoder, TiffValue,
},
};
@@ -147,9 +146,9 @@ fn encode_decode_with_lzw() {
#[test]
#[cfg(feature = "deflate")]
fn encode_decode_with_deflate() {
encode_decode_with_compression(Compression::Deflate(DeflateLevel::Fast));
encode_decode_with_compression(Compression::Deflate(DeflateLevel::Balanced));
encode_decode_with_compression(Compression::Deflate(DeflateLevel::Best));
encode_decode_with_compression(Compression::Deflate(1));
encode_decode_with_compression(Compression::Deflate(6));
encode_decode_with_compression(Compression::Deflate(9));
}
#[test]