Skip to content

Commit

Permalink
Support ZSTD compression method for reading (#256)
Browse files Browse the repository at this point in the history
  • Loading branch information
stschake authored Dec 20, 2024
1 parent ff52aea commit a19cf37
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 1 deletion.
4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,14 @@ exclude = ["tests/images/*", "tests/fuzz_images/*"]
weezl = "0.1.0"
jpeg = { package = "jpeg-decoder", version = "0.3.0", default-features = false }
flate2 = "1.0.20"
zstd = { version = "0.13", optional = true }

[dev-dependencies]
criterion = "0.3.1"

[features]
zstd = ["dep:zstd"]

[[bench]]
name = "lzw"
harness = false
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ This table lists photometric interpretations and sample formats which are suppor
| Deflate |||
| PackBits |||
| JPEG || not yet |
| ZSTD || not yet |


## Not yet supported
Expand Down
6 changes: 5 additions & 1 deletion src/decoder/image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,11 @@ impl Image {
CompressionMethod::None => Box::new(reader),
CompressionMethod::LZW => {
Box::new(LZWReader::new(reader, usize::try_from(compressed_length)?))
}
},
#[cfg(feature = "zstd")]
CompressionMethod::ZSTD => {
Box::new(zstd::Decoder::new(reader)?)
},
CompressionMethod::PackBits => Box::new(PackBitsReader::new(reader, compressed_length)),
CompressionMethod::Deflate | CompressionMethod::OldDeflate => {
Box::new(DeflateReader::new(reader))
Expand Down
3 changes: 3 additions & 0 deletions src/tags.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,9 @@ pub enum CompressionMethod(u16) unknown("A custom compression method") {
Deflate = 8,
OldDeflate = 0x80B2,
PackBits = 0x8005,

// Self-assigned by libtiff
ZSTD = 0xC350,
}
}

Expand Down
7 changes: 7 additions & 0 deletions tests/decode_images.rs
Original file line number Diff line number Diff line change
Expand Up @@ -515,3 +515,10 @@ fn test_predictor_3_rgb_f32() {
fn test_predictor_3_gray_f32() {
test_image_sum_f32("predictor-3-gray-f32.tif", ColorType::Gray(32), 20008.275);
}

#[test]
#[cfg(feature = "zstd")]
fn test_zstd_compression() {
// gdal_translate -co COMPRESS=ZSTD -co ZSTD_LEVEL=20 int16.tif int16_zstd.tif
test_image_sum_i16("int16_zstd.tif", ColorType::Gray(16), 354396);
}
Binary file added tests/images/int16_zstd.tif
Binary file not shown.

0 comments on commit a19cf37

Please sign in to comment.