From 165954c2916337fd902e4b719da8ea65ddc756fd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Wed, 2 Dec 2026 15:13:03 +0300 Subject: [PATCH] S13: TIFF compression is an enum, not u64 consts; the raw tag value is only kept for the CCITT message --- crates/ec-image/src/tiff.rs | 39 +++++++++++++++++++------------------ 1 file changed, 20 insertions(+), 19 deletions(-) diff --git a/crates/ec-image/src/tiff.rs b/crates/ec-image/src/tiff.rs index 23ace55a..a1cd4904 100644 --- a/crates/ec-image/src/tiff.rs +++ b/crates/ec-image/src/tiff.rs @@ -39,11 +39,13 @@ const TAG_TILE_COUNTS: u16 = 325; const TAG_EXTRA_SAMPLES: u16 = 338; const TAG_SAMPLE_FORMAT: u16 = 339; -const COMPRESSION_NONE: u64 = 1; -const COMPRESSION_LZW: u64 = 5; -const COMPRESSION_DEFLATE: u64 = 8; -const COMPRESSION_PACKBITS: u64 = 32773; -const COMPRESSION_DEFLATE_OLD: u64 = 32946; +#[derive(Clone, Copy, PartialEq, Eq)] +enum Compression { + None, + Lzw, + PackBits, + Deflate, +} /// The endianness the file declared, and the bytes it declared it about. struct Reader<'a> { @@ -153,7 +155,7 @@ struct Layout { bits: Vec, samples: usize, photometric: u64, - compression: u64, + compression: Compression, planar: u64, predictor: u64, /// Strip or tile data offsets, in the order the file lists them. @@ -203,16 +205,15 @@ fn layout(data: &[u8]) -> Result { } let entries = directory(&reader, reader.u32_at(4)? as usize)?; - let compression = first(&entries, TAG_COMPRESSION, COMPRESSION_NONE); - match compression { - COMPRESSION_NONE - | COMPRESSION_LZW - | COMPRESSION_DEFLATE - | COMPRESSION_PACKBITS - | COMPRESSION_DEFLATE_OLD => {} + let raw = first(&entries, TAG_COMPRESSION, 1); + let compression = match raw { + 1 => Compression::None, + 5 => Compression::Lzw, + 32773 => Compression::PackBits, + 8 | 32946 => Compression::Deflate, 2..=4 => { return Err(Error::unsupported( - format!("TIFF: CCITT compression {compression}"), + format!("TIFF: CCITT compression {raw}"), "the fax coders are a separate format with their own \ two-dimensional coding modes", )); @@ -225,7 +226,7 @@ fn layout(data: &[u8]) -> Result { )); } other => return Err(Error::corrupt(format!("TIFF: compression {other}"))), - } + }; let samples = first(&entries, TAG_SAMPLES, 1) as usize; if samples == 0 || samples > 4 { @@ -482,10 +483,10 @@ fn segment(layout: &Layout, data: &[u8], index: usize, expect: usize) -> Result< .get(at..at.saturating_add(len)) .ok_or_else(|| Error::corrupt("TIFF: a strip reaching past the end of the file"))?; Ok(match layout.compression { - COMPRESSION_NONE => body.to_vec(), - COMPRESSION_LZW => lzw(body, expect)?, - COMPRESSION_PACKBITS => packbits(body, expect), - _ => ec_inflate::inflate_zlib(body, expect.saturating_mul(2).max(1024)) + Compression::None => body.to_vec(), + Compression::Lzw => lzw(body, expect)?, + Compression::PackBits => packbits(body, expect), + Compression::Deflate => ec_inflate::inflate_zlib(body, expect.saturating_mul(2).max(1024)) .map_err(|e| Error::corrupt(format!("TIFF: a Deflate strip: {e}")))?, }) }