S13: TIFF compression is an enum, not u64 consts; the raw tag value is only kept for the CCITT message
Browse files at this commit165954c2916337fd902e4b719da8ea65ddc756fdParent: 4fd4ed41 changed file +20 -19
- crates/ec-image/src/tiff.rs +20 -19
crates/ec-image/src/tiff.rsmodified+20 -19
| @@ -39,11 +39,13 @@ const TAG_TILE_COUNTS: u16 = 325; | ||
| 39 | 39 | const TAG_EXTRA_SAMPLES: u16 = 338; |
| 40 | 40 | const TAG_SAMPLE_FORMAT: u16 = 339; |
| 41 | 41 | |
| 42 | -const COMPRESSION_NONE: u64 = 1; | |
| 43 | -const COMPRESSION_LZW: u64 = 5; | |
| 44 | -const COMPRESSION_DEFLATE: u64 = 8; | |
| 45 | -const COMPRESSION_PACKBITS: u64 = 32773; | |
| 46 | -const COMPRESSION_DEFLATE_OLD: u64 = 32946; | |
| 42 | +#[derive(Clone, Copy, PartialEq, Eq)] | |
| 43 | +enum Compression { | |
| 44 | + None, | |
| 45 | + Lzw, | |
| 46 | + PackBits, | |
| 47 | + Deflate, | |
| 48 | +} | |
| 47 | 49 | |
| 48 | 50 | /// The endianness the file declared, and the bytes it declared it about. |
| 49 | 51 | struct Reader<'a> { |
| @@ -153,7 +155,7 @@ struct Layout { | ||
| 153 | 155 | bits: Vec<u16>, |
| 154 | 156 | samples: usize, |
| 155 | 157 | photometric: u64, |
| 156 | - compression: u64, | |
| 158 | + compression: Compression, | |
| 157 | 159 | planar: u64, |
| 158 | 160 | predictor: u64, |
| 159 | 161 | /// Strip or tile data offsets, in the order the file lists them. |
| @@ -203,16 +205,15 @@ fn layout(data: &[u8]) -> Result<Layout> { | ||
| 203 | 205 | } |
| 204 | 206 | let entries = directory(&reader, reader.u32_at(4)? as usize)?; |
| 205 | 207 | |
| 206 | - let compression = first(&entries, TAG_COMPRESSION, COMPRESSION_NONE); | |
| 207 | - match compression { | |
| 208 | - COMPRESSION_NONE | |
| 209 | - | COMPRESSION_LZW | |
| 210 | - | COMPRESSION_DEFLATE | |
| 211 | - | COMPRESSION_PACKBITS | |
| 212 | - | COMPRESSION_DEFLATE_OLD => {} | |
| 208 | + let raw = first(&entries, TAG_COMPRESSION, 1); | |
| 209 | + let compression = match raw { | |
| 210 | + 1 => Compression::None, | |
| 211 | + 5 => Compression::Lzw, | |
| 212 | + 32773 => Compression::PackBits, | |
| 213 | + 8 | 32946 => Compression::Deflate, | |
| 213 | 214 | 2..=4 => { |
| 214 | 215 | return Err(Error::unsupported( |
| 215 | - format!("TIFF: CCITT compression {compression}"), | |
| 216 | + format!("TIFF: CCITT compression {raw}"), | |
| 216 | 217 | "the fax coders are a separate format with their own \ |
| 217 | 218 | two-dimensional coding modes", |
| 218 | 219 | )); |
| @@ -225,7 +226,7 @@ fn layout(data: &[u8]) -> Result<Layout> { | ||
| 225 | 226 | )); |
| 226 | 227 | } |
| 227 | 228 | other => return Err(Error::corrupt(format!("TIFF: compression {other}"))), |
| 228 | - } | |
| 229 | + }; | |
| 229 | 230 | |
| 230 | 231 | let samples = first(&entries, TAG_SAMPLES, 1) as usize; |
| 231 | 232 | if samples == 0 || samples > 4 { |
| @@ -482,10 +483,10 @@ fn segment(layout: &Layout, data: &[u8], index: usize, expect: usize) -> Result< | ||
| 482 | 483 | .get(at..at.saturating_add(len)) |
| 483 | 484 | .ok_or_else(|| Error::corrupt("TIFF: a strip reaching past the end of the file"))?; |
| 484 | 485 | Ok(match layout.compression { |
| 485 | - COMPRESSION_NONE => body.to_vec(), | |
| 486 | - COMPRESSION_LZW => lzw(body, expect)?, | |
| 487 | - COMPRESSION_PACKBITS => packbits(body, expect), | |
| 488 | - _ => ec_inflate::inflate_zlib(body, expect.saturating_mul(2).max(1024)) | |
| 486 | + Compression::None => body.to_vec(), | |
| 487 | + Compression::Lzw => lzw(body, expect)?, | |
| 488 | + Compression::PackBits => packbits(body, expect), | |
| 489 | + Compression::Deflate => ec_inflate::inflate_zlib(body, expect.saturating_mul(2).max(1024)) | |
| 489 | 490 | .map_err(|e| Error::corrupt(format!("TIFF: a Deflate strip: {e}")))?, |
| 490 | 491 | }) |
| 491 | 492 | } |