Merge pull request #351 from Shnatsel/semver-breaking-deflate-level

Implement DEFLATE compression level control properly, with a semver break
This commit is contained in:
Aurelia Molzer authored and GitHub committed 2026-03-05 20:12:27 +00:00
commit 31c49ea6ef
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 criterion::{black_box, Criterion, Throughput};
use std::io::Cursor; use std::io::Cursor;
use tiff::encoder::colortype::RGB32Float; use tiff::encoder::colortype::RGB32Float;
use tiff::encoder::{Compression, DeflateLevel, Predictor, TiffEncoder}; use tiff::encoder::{Compression, Predictor, TiffEncoder};
const WIDTH: u32 = 500; const WIDTH: u32 = 500;
const HEIGHT: u32 = 500; const HEIGHT: u32 = 500;
@@ -14,7 +14,7 @@ fn encode_image_f32_float_predictor_deflate(data: &[f32]) {
TiffEncoder::new(&mut buf) TiffEncoder::new(&mut buf)
.unwrap() .unwrap()
.with_predictor(Predictor::FloatingPoint) .with_predictor(Predictor::FloatingPoint)
.with_compression(Compression::Deflate(DeflateLevel::Fast)) .with_compression(Compression::Deflate(1))
.write_image::<RGB32Float>(WIDTH, HEIGHT, black_box(data)) .write_image::<RGB32Float>(WIDTH, HEIGHT, black_box(data))
.unwrap(); .unwrap();
} }
+8 -17
View File
@@ -7,24 +7,15 @@ pub struct Deflate {
level: FlateCompression, 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 { impl Deflate {
/// Create a new deflate compressor with a specific level of compression. /// 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 { Self {
level: FlateCompression::new(level as u32), level: FlateCompression::new(level as u32),
} }
@@ -45,7 +36,7 @@ impl Deflate {
impl Default for Deflate { impl Default for Deflate {
fn default() -> Self { fn default() -> Self {
Self::with_level(DeflateLevel::default()) Self::with_level(6)
} }
} }
+1 -1
View File
@@ -9,7 +9,7 @@ mod packbits;
mod uncompressed; mod uncompressed;
#[cfg(feature = "deflate")] #[cfg(feature = "deflate")]
pub use self::deflate::{Deflate, DeflateLevel}; pub use self::deflate::Deflate;
#[cfg(feature = "lzw")] #[cfg(feature = "lzw")]
pub use self::lzw::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::Horizontal] is for integer sample types.
/// [Predictor::FloatingPoint] is for floating-point sample types (f32, f64). /// [Predictor::FloatingPoint] is for floating-point sample types (f32, f64).
pub type Predictor = crate::tags::Predictor; pub type Predictor = crate::tags::Predictor;
#[cfg(feature = "deflate")]
pub type DeflateLevel = compression::DeflateLevel;
#[derive(Clone, Copy, PartialEq, Default)] #[derive(Clone, Copy, PartialEq, Default)]
pub enum Compression { pub enum Compression {
@@ -48,7 +46,7 @@ pub enum Compression {
#[cfg(feature = "lzw")] #[cfg(feature = "lzw")]
Lzw, Lzw,
#[cfg(feature = "deflate")] #[cfg(feature = "deflate")]
Deflate(DeflateLevel), Deflate(u8),
Packbits, Packbits,
} }
+3 -4
View File
@@ -5,7 +5,6 @@ use tiff::{
decoder::{Decoder, DecodingResult}, decoder::{Decoder, DecodingResult},
encoder::{ encoder::{
colortype::{self, ColorType}, colortype::{self, ColorType},
compression::*,
Compression, TiffEncoder, TiffValue, Compression, TiffEncoder, TiffValue,
}, },
}; };
@@ -147,9 +146,9 @@ fn encode_decode_with_lzw() {
#[test] #[test]
#[cfg(feature = "deflate")] #[cfg(feature = "deflate")]
fn encode_decode_with_deflate() { fn encode_decode_with_deflate() {
encode_decode_with_compression(Compression::Deflate(DeflateLevel::Fast)); encode_decode_with_compression(Compression::Deflate(1));
encode_decode_with_compression(Compression::Deflate(DeflateLevel::Balanced)); encode_decode_with_compression(Compression::Deflate(6));
encode_decode_with_compression(Compression::Deflate(DeflateLevel::Best)); encode_decode_with_compression(Compression::Deflate(9));
} }
#[test] #[test]