Skip to main content

openreadout_codecs/
lib.rs

1//! Pure-Rust decoders used by the format readers. No C toolchain is needed, which keeps
2//! the musl static builds simple. Every function returns owned bytes in the caller's
3//! expected layout; nothing here knows about file formats.
4//!
5//! Every decoder is bounded: output never grows past the caller's expected size (plus one
6//! byte to detect overlong streams), and size hints are only trusted up to
7//! [`MAX_PREALLOC`] for up-front allocation.
8//!
9//! Decoding failures are reported as [`CodecError`] values, never by exiting the process.
10//!
11//! # Cargo features
12//!
13//! All enabled by default. A disabled codec's functions still exist and return
14//! [`CodecError::NotCompiled`].
15//!
16//! | feature | codecs |
17//! | --- | --- |
18//! | `zstd` | [`zstd_decode`], [`zstd1_decode`] (Zstandard, via `ruzstd`) |
19//! | `jpeg` | [`jpeg_decode`], [`jpeg_decode_tiff`] and their `_limited` forms (baseline, progressive, lossless) |
20//! | `jpegxr` | [`jpegxr_decode`] (JPEG XR, as in CZI files; `openreadout-jpegxr`, a safe port of jxrlib) |
21//! | `jpeg2000` | [`jpeg2000_decode`] and [`jpeg2000_decode_limited`] (`rust-j2k`, fallback `dicom-toolkit-jpeg2000`) |
22//! | `lzw` | [`lzw_decode`] (TIFF LZW) |
23//! | `deflate` | [`zlib_decode`] |
24//! | `lz4` | [`lz4_block_decode`], [`lz4_sized_decode`], [`hdf5_lz4_decode`] and LZ4 inside Blosc |
25//!
26//! [`chunked_decode`] (CZI chunked compression) uses the `zstd` and `lz4` features.
27//!
28//! [`unshuffle_hilo`], [`packbits_decode`], [`lzf_decode`], [`blosc_decode`], [`blosclz_decode`] and
29//! [`lzma2_decode`] (raw LZMA2 chunks) need no feature.
30//!
31//! # Example
32//!
33//! ```
34//! use openreadout_codecs::{packbits_decode, unshuffle_hilo};
35//!
36//! // PackBits: a literal run of 2 bytes, then the byte 0xAA repeated 3 times.
37//! let raw = packbits_decode(&[0x01, 0x10, 0x20, 0xFE, 0xAA], 5)?;
38//! assert_eq!(raw, [0x10, 0x20, 0xAA, 0xAA, 0xAA]);
39//!
40//! // "HiLo": all low bytes of 16-bit samples, then all high bytes.
41//! assert_eq!(unshuffle_hilo(&[0x01, 0x02, 0xA0, 0xB0]), [0x01, 0xA0, 0x02, 0xB0]);
42//! # Ok::<(), openreadout_codecs::CodecError>(())
43//! ```
44#![forbid(unsafe_code)]
45#![warn(missing_docs)]
46
47mod blosc;
48mod chunked;
49#[cfg(feature = "jpeg2000")]
50mod j2k;
51#[cfg(feature = "jpeg")]
52mod jpeg12;
53#[cfg(feature = "jpegxl")]
54mod jxl;
55#[cfg(feature = "lerc")]
56mod lerc;
57mod lzma;
58#[cfg(feature = "webp")]
59mod webp;
60
61pub use blosc::{
62    BloscCompressor, BloscHeader, blosc_decode, blosc_header, blosclz_decode, hdf5_lz4_decode,
63    lz4_block_decode, lz4_sized_decode, snappy_decode,
64};
65pub use chunked::chunked_decode;
66pub use lzma::lzma2_decode;
67
68/// Codec failure.
69#[derive(Debug, thiserror::Error)]
70#[non_exhaustive]
71pub enum CodecError {
72    /// The data is not a valid stream for this codec.
73    #[error("{codec}: {detail}")]
74    Decode {
75        /// Codec name.
76        codec: &'static str,
77        /// What is wrong with the stream.
78        detail: String,
79    },
80    /// The codec was left out at build time.
81    #[error("{codec} support was not compiled in (feature `{feature}`)")]
82    NotCompiled {
83        /// Codec name.
84        codec: &'static str,
85        /// Cargo feature that enables it.
86        feature: &'static str,
87    },
88    /// A valid stream that uses a feature this decoder does not implement.
89    #[error("{codec}: {detail} is not supported")]
90    Unsupported {
91        /// Codec name.
92        codec: &'static str,
93        /// The feature.
94        detail: String,
95    },
96    /// The stream decoded, but not to the expected number of bytes.
97    #[error("decoded {codec} data is {got} bytes, expected {expected}")]
98    SizeMismatch {
99        /// Codec name.
100        codec: &'static str,
101        /// Bytes decoded.
102        got: usize,
103        /// Bytes the caller expected.
104        expected: usize,
105    },
106}
107
108/// Result alias of this crate.
109pub type Result<T> = std::result::Result<T, CodecError>;
110
111/// Largest buffer a decoder pre-allocates from a caller-supplied size hint (64 MiB). Output
112/// beyond this grows on demand, so a lying size field cannot force a huge up-front allocation.
113pub const MAX_PREALLOC: usize = 64 << 20;
114
115/// Output cap for streams whose decoded size is unknown (`expected_len == 0`): 1 GiB.
116pub const MAX_UNSIZED_OUTPUT: usize = 1 << 30;
117
118/// How many bytes a decoder may produce: one more than expected (so "too long" is
119/// detectable without decoding a bomb to the end), or the unsized cap.
120fn output_limit(expected_len: usize) -> usize {
121    if expected_len == 0 {
122        MAX_UNSIZED_OUTPUT
123    } else {
124        expected_len.saturating_add(1)
125    }
126}
127
128/// Decompress a plain zstd frame. `expected_len` is a sanity bound (0 = unknown); decoding
129/// stops one byte past it, so a decompression bomb costs at most `expected_len + 1` bytes.
130pub fn zstd_decode(data: &[u8], expected_len: usize) -> Result<Vec<u8>> {
131    #[cfg(feature = "zstd")]
132    {
133        use std::io::Read;
134        // Fast path when the size is known: decode straight into a buffer of that size (no
135        // `Read` adapter). Any failure, including trailing bytes after the frame or a size
136        // other than expected, falls through to the streaming decoder below, which produces
137        // the same results and errors.
138        if expected_len != 0 && expected_len <= MAX_PREALLOC {
139            let mut out = vec![0u8; expected_len];
140            let mut fd = ruzstd::decoding::FrameDecoder::new();
141            if let Ok(n) = fd.decode_all(data, &mut out)
142                && n == expected_len
143            {
144                return Ok(out);
145            }
146        }
147        let dec =
148            ruzstd::decoding::StreamingDecoder::new(data).map_err(|e| CodecError::Decode {
149                codec: "zstd",
150                detail: e.to_string(),
151            })?;
152        let limit = output_limit(expected_len);
153        let mut out = Vec::with_capacity(expected_len.min(MAX_PREALLOC));
154        dec.take(limit as u64)
155            .read_to_end(&mut out)
156            .map_err(|e| CodecError::Decode {
157                codec: "zstd",
158                detail: e.to_string(),
159            })?;
160        if (expected_len != 0 && out.len() != expected_len) || out.len() >= MAX_UNSIZED_OUTPUT {
161            return Err(CodecError::SizeMismatch {
162                codec: "zstd",
163                got: out.len(),
164                expected: expected_len,
165            });
166        }
167        Ok(out)
168    }
169    #[cfg(not(feature = "zstd"))]
170    {
171        let _ = (data, expected_len);
172        Err(CodecError::NotCompiled {
173            codec: "zstd",
174            feature: "zstd",
175        })
176    }
177}
178
179/// Parsed "zstd1" prefix used by CZI compression id 6: `header_size`, `chunk_kind`, flags.
180#[derive(Debug, Clone, Copy, PartialEq, Eq)]
181pub struct Zstd1Header {
182    /// Size of the header in bytes (byte 0).
183    pub header_size: u8,
184    /// Kind of the first header chunk (byte 1).
185    pub chunk_kind: u8,
186    /// Bit 0 of the flags byte: samples were byte-shuffled into low-byte and high-byte halves.
187    pub hilo: bool,
188}
189
190/// Parse the zstd1 header; returns the header and the offset of the zstd frame.
191pub fn zstd1_header(data: &[u8]) -> Result<(Zstd1Header, usize)> {
192    if data.len() < 2 {
193        return Err(CodecError::Decode {
194            codec: "zstd1",
195            detail: "buffer shorter than the header".into(),
196        });
197    }
198    let header_size = data[0] as usize;
199    if header_size < 1 || header_size > data.len() {
200        return Err(CodecError::Decode {
201            codec: "zstd1",
202            detail: format!("implausible header_size {header_size}"),
203        });
204    }
205    let chunk_kind = data.get(1).copied().unwrap_or(0);
206    let flags = if header_size >= 3 { data[2] } else { 0 };
207    Ok((
208        Zstd1Header {
209            header_size: header_size as u8,
210            chunk_kind,
211            hilo: flags & 1 != 0,
212        },
213        header_size,
214    ))
215}
216
217/// Decode a zstd1 payload into interleaved samples of `bytes_per_sample` bytes.
218pub fn zstd1_decode(data: &[u8], expected_len: usize, bytes_per_sample: usize) -> Result<Vec<u8>> {
219    let (h, off) = zstd1_header(data)?;
220    let raw = zstd_decode(data.get(off..).unwrap_or_default(), expected_len)?;
221    if h.hilo && bytes_per_sample == 2 {
222        Ok(unshuffle_hilo(&raw))
223    } else {
224        Ok(raw)
225    }
226}
227
228/// Undo the `HiLo` byte shuffle: input is all low bytes followed by all high bytes of
229/// 16-bit little-endian samples; output is the interleaved samples.
230pub fn unshuffle_hilo(shuffled: &[u8]) -> Vec<u8> {
231    let n = shuffled.len() / 2;
232    let (lo, rest) = shuffled.split_at(n);
233    let hi = &rest[..n];
234    let mut out = vec![0u8; shuffled.len()];
235    // Zipped fixed-size chunks: no per-byte capacity or bounds checks in the loop.
236    for (pair, (&l, &h)) in out.as_chunks_mut::<2>().0.iter_mut().zip(lo.iter().zip(hi)) {
237        *pair = [l, h];
238    }
239    if shuffled.len() % 2 == 1 {
240        out[shuffled.len() - 1] = shuffled[shuffled.len() - 1];
241    }
242    out
243}
244
245/// A decoded raster: interleaved samples, no row padding.
246#[derive(Debug, Clone)]
247pub struct Raster {
248    /// Width in pixels.
249    pub width: u32,
250    /// Height in pixels.
251    pub height: u32,
252    /// Samples per pixel (1 for grayscale, 3 for colour).
253    pub channels: u32,
254    /// Bits per sample.
255    pub bits_per_sample: u32,
256    /// True for floating-point samples.
257    pub float: bool,
258    /// True when the codestream declares B,G,R sample order (the caller may want R,G,B).
259    pub bgr: bool,
260    /// The samples, interleaved, `width × channels × bits_per_sample / 8` bytes per row.
261    pub data: Vec<u8>,
262}
263
264/// Decode a JPEG XR codestream (as embedded in CZI subblocks).
265pub fn jpegxr_decode(data: &[u8]) -> Result<Raster> {
266    jpegxr_decode_expect(data, None)
267}
268
269/// Decode a JPEG XR codestream, refusing before any pixel allocation when the codestream's
270/// own dimensions differ from `expect` (width, height), e.g. the geometry a CZI directory
271/// entry declares. Without an expectation the decoded size is capped at
272/// [`MAX_UNSIZED_OUTPUT`].
273///
274/// Backed by `openreadout-jpegxr`, our `forbid(unsafe_code)` port of the decoder path of
275/// Microsoft's jxrlib (bit-exact with it). Features that decoder does not implement (alpha
276/// planes, CMYK, packed formats, ...) are [`CodecError::Unsupported`].
277pub fn jpegxr_decode_expect(data: &[u8], expect: Option<(u32, u32)>) -> Result<Raster> {
278    #[cfg(feature = "jpegxr")]
279    {
280        use openreadout_jpegxr as jxr;
281        let map = |e: jxr::Error| match e {
282            jxr::Error::Unsupported(m) => CodecError::Unsupported {
283                codec: "jpeg-xr",
284                detail: m,
285            },
286            other => CodecError::Decode {
287                codec: "jpeg-xr",
288                detail: other.to_string(),
289            },
290        };
291        let info = jxr::probe(data).map_err(map)?;
292        if let Some((ew, eh)) = expect
293            && (ew, eh) != (info.width, info.height)
294        {
295            return Err(CodecError::Decode {
296                codec: "jpeg-xr",
297                detail: format!(
298                    "codestream is {}x{} but {ew}x{eh} was declared",
299                    info.width, info.height
300                ),
301            });
302        }
303        let limit = match expect {
304            // The declared geometry at the widest sample layout (4 x 32-bit per pixel).
305            Some((w, h)) => {
306                (u64::from(w) * u64::from(h) * 16).min(MAX_UNSIZED_OUTPUT as u64) as usize
307            }
308            None => MAX_UNSIZED_OUTPUT,
309        };
310        let img = jxr::decode(data, limit).map_err(map)?;
311        let (bits, float) = match img.sample {
312            jxr::SampleType::U8 => (8, false),
313            jxr::SampleType::U16 => (16, false),
314            jxr::SampleType::F32 => (32, true),
315            other => {
316                return Err(CodecError::Unsupported {
317                    codec: "jpeg-xr",
318                    detail: format!("{other:?} samples (signed fixed point or half float)"),
319                });
320            }
321        };
322        Ok(Raster {
323            width: img.width,
324            height: img.height,
325            channels: img.channels,
326            bits_per_sample: bits,
327            float,
328            bgr: img.bgr,
329            data: img.data,
330        })
331    }
332    #[cfg(not(feature = "jpegxr"))]
333    {
334        let _ = (data, expect);
335        Err(CodecError::NotCompiled {
336            codec: "jpeg-xr",
337            feature: "jpegxr",
338        })
339    }
340}
341
342/// `io::Write` sink that refuses to grow past `limit` bytes (decompression-bomb guard).
343#[cfg(feature = "lzw")]
344struct LimitedWriter {
345    buf: Vec<u8>,
346    limit: usize,
347}
348
349#[cfg(feature = "lzw")]
350impl std::io::Write for LimitedWriter {
351    fn write(&mut self, data: &[u8]) -> std::io::Result<usize> {
352        let room = self.limit - self.buf.len();
353        if room == 0 && !data.is_empty() {
354            return Err(std::io::Error::other(
355                "decoded data exceeds the declared size",
356            ));
357        }
358        let n = data.len().min(room);
359        self.buf.extend_from_slice(&data[..n]);
360        Ok(n)
361    }
362    fn flush(&mut self) -> std::io::Result<()> {
363        Ok(())
364    }
365}
366
367/// Decode a JPEG stream (ITU-T T.81): baseline and progressive 8-bit, sequential 12-bit
368/// (extended process, Huffman coding, no chroma subsampling), lossless (SOF3) up to 16 bit.
369/// Returns gray (`channels == 1`) or R,G,B (`channels == 3`) samples; samples above 8 bits
370/// come back as little-endian u16. Progressive or subsampled 12-bit streams and CMYK are
371/// rejected with an error.
372pub fn jpeg_decode(data: &[u8]) -> Result<Raster> {
373    jpeg_decode_with(data, None, MAX_UNSIZED_OUTPUT, 0)
374}
375
376/// [`jpeg_decode`], refusing (before decoding anything) a frame whose decoded size would
377/// exceed `max_bytes`. Callers that know the tile or plane size pass a bound derived from it,
378/// so a few-byte stream declaring 60000 x 15000 pixels costs nothing.
379pub fn jpeg_decode_limited(data: &[u8], max_bytes: usize) -> Result<Raster> {
380    jpeg_decode_with(data, None, max_bytes.min(MAX_UNSIZED_OUTPUT), 0)
381}
382
383/// [`jpeg_decode_limited`] at a reduced size: `shift` 1, 2 or 3 decodes at 1/2, 1/4 or 1/8 of
384/// the width and height (rounded up) with jpeg-decoder's reduced inverse DCT, as slide readers
385/// do for levels the file does not store. 8-bit streams only.
386pub fn jpeg_decode_reduced(data: &[u8], shift: u32, max_bytes: usize) -> Result<Raster> {
387    jpeg_decode_with(data, None, max_bytes.min(MAX_UNSIZED_OUTPUT), shift)
388}
389
390/// `color`: `None` keeps the decoder's default (Adobe/JFIF markers, else YCbCr for three
391/// components); `Some` forces the transform. `max_bytes`: largest decoded frame accepted.
392fn jpeg_decode_with(
393    data: &[u8],
394    color: Option<JpegColor>,
395    max_bytes: usize,
396    shift: u32,
397) -> Result<Raster> {
398    #[cfg(feature = "jpeg")]
399    {
400        if shift > 3 {
401            return Err(CodecError::Decode {
402                codec: "jpeg",
403                detail: format!("reduction 1/{} (at most 1/8)", 1u64 << shift.min(63)),
404            });
405        }
406        // jpeg-decoder stops at 8-bit DCT samples; 12-bit sequential streams have their own
407        // decoder (`jpeg12`).
408        if jpeg12::frame_precision(data) == Some(12) {
409            if shift > 0 {
410                return Err(CodecError::Decode {
411                    codec: "jpeg",
412                    detail: "reduced-size decoding of a 12-bit stream".into(),
413                });
414            }
415            return jpeg12::decode(data, color, max_bytes);
416        }
417        let mut dec = jpeg_decoder::Decoder::new(data);
418        // A frame header can declare 65535 x 65535 x 4 samples; cap what the decoder allocates.
419        dec.set_max_decoding_buffer_size(MAX_UNSIZED_OUTPUT);
420        dec.read_info().map_err(|e| CodecError::Decode {
421            codec: "jpeg",
422            detail: e.to_string(),
423        })?;
424        // That cap applies to the output only: the decoder sizes its per-component planes
425        // from the frame header before checking it (a 342-byte stream made it allocate 4 GB).
426        if let Some(i) = dec.info() {
427            let bytes =
428                u64::from(i.width) * u64::from(i.height) * i.pixel_format.pixel_bytes() as u64;
429            if bytes > max_bytes as u64 {
430                return Err(CodecError::Decode {
431                    codec: "jpeg",
432                    detail: format!(
433                        "frame header declares {} x {} pixels ({bytes} bytes decoded)",
434                        i.width, i.height
435                    ),
436                });
437            }
438        }
439        if shift > 0
440            && let Some(i) = dec.info()
441        {
442            let (w, h) = (i.width.div_ceil(1 << shift), i.height.div_ceil(1 << shift));
443            dec.scale(w, h).map_err(|e| CodecError::Decode {
444                codec: "jpeg",
445                detail: e.to_string(),
446            })?;
447        }
448        if let Some(color) = color {
449            // Only three-component streams have a colour transform to choose.
450            if dec
451                .info()
452                .is_some_and(|i| i.pixel_format == jpeg_decoder::PixelFormat::RGB24)
453            {
454                dec.set_color_transform(match color {
455                    // "RGB" in jpeg-decoder: the components already are R, G, B (no conversion).
456                    JpegColor::AsCoded => jpeg_decoder::ColorTransform::RGB,
457                    JpegColor::ToRgb => jpeg_decoder::ColorTransform::YCbCr,
458                });
459            }
460        }
461        let px = dec.decode().map_err(|e| CodecError::Decode {
462            codec: "jpeg",
463            detail: e.to_string(),
464        })?;
465        let info = dec.info().ok_or_else(|| CodecError::Decode {
466            codec: "jpeg",
467            detail: "stream has no frame header".into(),
468        })?;
469        let (channels, bits_per_sample, data) = match info.pixel_format {
470            jpeg_decoder::PixelFormat::L8 => (1, 8, px),
471            jpeg_decoder::PixelFormat::RGB24 => (3, 8, px),
472            // jpeg-decoder hands back native-endian u16; normalize to little-endian.
473            jpeg_decoder::PixelFormat::L16 => (
474                1,
475                16,
476                px.as_chunks::<2>()
477                    .0
478                    .iter()
479                    .flat_map(|c| u16::from_ne_bytes(*c).to_le_bytes())
480                    .collect(),
481            ),
482            jpeg_decoder::PixelFormat::CMYK32 => {
483                return Err(CodecError::Decode {
484                    codec: "jpeg",
485                    detail: "CMYK JPEG streams are not supported".into(),
486                });
487            }
488        };
489        let (width, height) = (u32::from(info.width), u32::from(info.height));
490        let need =
491            width as usize * height as usize * channels as usize * (bits_per_sample as usize / 8);
492        if data.len() < need {
493            return Err(CodecError::SizeMismatch {
494                codec: "jpeg",
495                got: data.len(),
496                expected: need,
497            });
498        }
499        Ok(Raster {
500            width,
501            height,
502            channels,
503            bits_per_sample,
504            float: false,
505            bgr: false,
506            data,
507        })
508    }
509    #[cfg(not(feature = "jpeg"))]
510    {
511        let _ = (data, color, max_bytes, shift);
512        Err(CodecError::NotCompiled {
513            codec: "jpeg",
514            feature: "jpeg",
515        })
516    }
517}
518
519/// Decode a JPEG 2000 codestream (raw J2K `FF 4F FF 51…` or a JP2 file) at its native bit depth.
520/// Samples are interleaved; precisions above 8 bits come back as little-endian u16. Used for the
521/// frames of legacy (NIS-Elements 2.x) ND2 files, VSI/ETS tiles and Aperio SVS tiles. Backed by
522/// `rust-j2k` (pure Rust, no `unsafe`, OpenJPEG-style mid-point reconstruction), with
523/// `dicom-toolkit-jpeg2000` for codestreams it does not support (see `j2k.rs`). The caller
524/// converts colour: components come back as coded (after the codestream's own RCT/ICT).
525pub fn jpeg2000_decode(data: &[u8]) -> Result<Raster> {
526    jpeg2000_decode_limited(data, MAX_UNSIZED_OUTPUT)
527}
528
529/// [`jpeg2000_decode`], refusing (before decoding) an image that would decode to more than
530/// `max_bytes` (counted at 2 bytes per sample).
531pub fn jpeg2000_decode_limited(data: &[u8], max_bytes: usize) -> Result<Raster> {
532    #[cfg(feature = "jpeg2000")]
533    {
534        j2k::decode(data, max_bytes)
535    }
536    #[cfg(not(feature = "jpeg2000"))]
537    {
538        let _ = (data, max_bytes);
539        Err(CodecError::NotCompiled {
540            codec: "jpeg2000",
541            feature: "jpeg2000",
542        })
543    }
544}
545
546/// Decode a WebP file (lossy VP8 or lossless VP8L; TIFF compression 50001) to 8-bit R,G,B, or
547/// R,G,B,A when the file has alpha, refusing (before decoding) an image larger than
548/// `max_bytes`. Backed by `image-webp` (pure Rust, no `unsafe`).
549pub fn webp_decode_limited(data: &[u8], max_bytes: usize) -> Result<Raster> {
550    #[cfg(feature = "webp")]
551    {
552        webp::decode(data, max_bytes)
553    }
554    #[cfg(not(feature = "webp"))]
555    {
556        let _ = (data, max_bytes);
557        Err(CodecError::NotCompiled {
558            codec: "webp",
559            feature: "webp",
560        })
561    }
562}
563
564/// Decode a JPEG XL codestream or container (TIFF compression 50002/52546): the first frame,
565/// colour channels then extra channels (alpha, ...), interleaved; u8 up to 8 bits per sample,
566/// little-endian u16 up to 16, f32 for float images. Refuses (before rendering) an image larger
567/// than `max_bytes`. Backed by `jxl-oxide`.
568pub fn jpegxl_decode_limited(data: &[u8], max_bytes: usize) -> Result<Raster> {
569    #[cfg(feature = "jpegxl")]
570    {
571        jxl::decode(data, max_bytes)
572    }
573    #[cfg(not(feature = "jpegxl"))]
574    {
575        let _ = (data, max_bytes);
576        Err(CodecError::NotCompiled {
577            codec: "jpeg-xl",
578            feature: "jpegxl",
579        })
580    }
581}
582
583/// Decode a LERC2 blob (TIFF compression 34887, after any deflate/zstd wrapping is removed) to
584/// interleaved little-endian samples of the blob's type (`channels` = depth x bands); pixels
585/// its mask marks invalid are 0. Refuses (before decoding) an image larger than `max_bytes`.
586/// Backed by `lerc-rs`.
587pub fn lerc_decode_limited(data: &[u8], max_bytes: usize) -> Result<Raster> {
588    #[cfg(feature = "lerc")]
589    {
590        lerc::decode(data, max_bytes)
591    }
592    #[cfg(not(feature = "lerc"))]
593    {
594        let _ = (data, max_bytes);
595        Err(CodecError::NotCompiled {
596            codec: "lerc",
597            feature: "lerc",
598        })
599    }
600}
601
602/// TIFF-flavoured LZW (MSB-first, 8-bit symbols), as used by CZI compression id 2.
603pub fn lzw_decode(data: &[u8], expected_len: usize) -> Result<Vec<u8>> {
604    #[cfg(feature = "lzw")]
605    {
606        let mut dec = weezl::decode::Decoder::with_tiff_size_switch(weezl::BitOrder::Msb, 8);
607        let mut out = LimitedWriter {
608            buf: Vec::with_capacity(expected_len.min(MAX_PREALLOC)),
609            limit: output_limit(expected_len),
610        };
611        // Reuse a small per-thread staging buffer across TIFF strips. The bounded writer
612        // still stops decompression bombs, without zeroing weezl's default 16 MiB per strip.
613        thread_local! {
614            static BUFFER: std::cell::RefCell<Vec<u8>> =
615                std::cell::RefCell::new(vec![0; 64 << 10]);
616        }
617        BUFFER
618            .with_borrow_mut(|buffer| {
619                let mut stream = dec.into_stream(&mut out);
620                stream.set_buffer(buffer);
621                stream.decode_all(data).status
622            })
623            .or_else(|e| {
624                // A strip that ends without the end-of-information code (libtiff: "Strip not
625                // terminated with EOI code") keeps what it decoded, as libtiff and tifffile do;
626                // a short result is caught by the caller's length check.
627                if e.kind() == std::io::ErrorKind::UnexpectedEof && !out.buf.is_empty() {
628                    Ok(())
629                } else {
630                    Err(e)
631                }
632            })
633            .map_err(|e| CodecError::Decode {
634                codec: "lzw",
635                detail: e.to_string(),
636            })?;
637        let out = out.buf;
638        if (expected_len != 0 && out.len() != expected_len) || out.len() >= MAX_UNSIZED_OUTPUT {
639            return Err(CodecError::SizeMismatch {
640                codec: "lzw",
641                got: out.len(),
642                expected: expected_len,
643            });
644        }
645        Ok(out)
646    }
647    #[cfg(not(feature = "lzw"))]
648    {
649        let _ = (data, expected_len);
650        Err(CodecError::NotCompiled {
651            codec: "lzw",
652            feature: "lzw",
653        })
654    }
655}
656
657/// zlib/deflate (used by some ND2 chunks and by OME-TIFF). With a nonzero `expected_len`
658/// at most that many bytes are inflated (callers only use that prefix).
659pub fn zlib_decode(data: &[u8], expected_len: usize) -> Result<Vec<u8>> {
660    #[cfg(feature = "deflate")]
661    {
662        use std::io::Read;
663        let limit = if expected_len == 0 {
664            MAX_UNSIZED_OUTPUT
665        } else {
666            expected_len
667        };
668        let mut out = Vec::with_capacity(expected_len.min(MAX_PREALLOC));
669        flate2::read::ZlibDecoder::new(data)
670            .take(limit as u64)
671            .read_to_end(&mut out)
672            .map_err(|e| CodecError::Decode {
673                codec: "zlib",
674                detail: e.to_string(),
675            })?;
676        if expected_len == 0 && out.len() >= MAX_UNSIZED_OUTPUT {
677            return Err(CodecError::SizeMismatch {
678                codec: "zlib",
679                got: out.len(),
680                expected: expected_len,
681            });
682        }
683        Ok(out)
684    }
685    #[cfg(not(feature = "deflate"))]
686    {
687        let _ = (data, expected_len);
688        Err(CodecError::NotCompiled {
689            codec: "zlib",
690            feature: "deflate",
691        })
692    }
693}
694
695/// How [`jpeg_decode_tiff`] treats the colour components of the codestream.
696#[derive(Debug, Clone, Copy, PartialEq, Eq)]
697#[non_exhaustive]
698pub enum JpegColor {
699    /// Return the components exactly as coded (no colour conversion). Used when the
700    /// container says the components already are R, G, B (e.g. TIFF photometric RGB).
701    AsCoded,
702    /// Convert three YCbCr-coded components to R, G, B (e.g. TIFF photometric YCbCr).
703    ToRgb,
704}
705
706/// What a JPEG stream's markers say about its colour coding, read up to the first scan
707/// (`SOS`) without decoding anything. See [`jpeg_markers`].
708#[derive(Debug, Clone, Default, PartialEq, Eq)]
709#[non_exhaustive]
710pub struct JpegMarkers {
711    /// An `APP0` segment starting `JFIF\0` is present (JFIF implies Y, Cb, Cr components).
712    pub jfif: bool,
713    /// The transform byte of an `APP14` `Adobe` segment: 0 = components as coded (RGB or
714    /// CMYK), 1 = YCbCr, 2 = YCCK.
715    pub adobe_transform: Option<u8>,
716    /// Component identifiers of the frame header (`SOF`), in coding order.
717    pub component_ids: Vec<u8>,
718    /// Horizontal and vertical sampling factors per component (1 = full resolution).
719    pub sampling: Vec<(u8, u8)>,
720    /// The coding process of the frame: the low nibble of its `SOF` marker (0 baseline,
721    /// 1 extended sequential, 2 progressive, 3 lossless, 9 and up arithmetic coding).
722    pub process: u8,
723    /// Sample precision of the frame in bits (8 or 12 for DCT, 2 to 16 for lossless).
724    pub precision: u8,
725}
726
727impl JpegMarkers {
728    /// The colour coding of a three-component stream by the rules libjpeg applies when the
729    /// container says nothing (JFIF, then Adobe, then component ids `R`,`G`,`B`, else
730    /// YCbCr). `None` for streams that are not three-component.
731    #[must_use]
732    pub fn coded_color(&self) -> Option<JpegColor> {
733        if self.component_ids.len() != 3 {
734            return None;
735        }
736        Some(if self.jfif {
737            JpegColor::ToRgb
738        } else if let Some(t) = self.adobe_transform {
739            if t == 0 {
740                JpegColor::AsCoded
741            } else {
742                JpegColor::ToRgb
743            }
744        } else if self.component_ids == *b"RGB" {
745            JpegColor::AsCoded
746        } else {
747            JpegColor::ToRgb
748        })
749    }
750}
751
752/// Scan the marker segments of a JPEG stream (`tables` first when given, as for a TIFF
753/// `JPEGTables` tag) up to the first `SOS`. Only segment lengths are followed; nothing is
754/// decoded. Fails when the stream does not start with `SOI` or a segment runs past the end
755/// before a frame header was seen.
756pub fn jpeg_markers(data: &[u8], tables: Option<&[u8]>) -> Result<JpegMarkers> {
757    let mut m = JpegMarkers::default();
758    let mut frame_seen = false;
759    for s in tables.into_iter().chain(std::iter::once(data)) {
760        if s.len() < 2 || s[..2] != [0xFF, 0xD8] {
761            return Err(CodecError::Decode {
762                codec: "jpeg",
763                detail: "stream lacks an SOI marker".into(),
764            });
765        }
766        let mut i = 2usize;
767        while let Some(&b) = s.get(i) {
768            if b != 0xFF {
769                break;
770            }
771            let Some(&mk) = s.get(i + 1) else { break };
772            if mk == 0xFF {
773                i += 1; // fill byte
774                continue;
775            }
776            if mk == 0xD8 || mk == 0x01 || (0xD0..=0xD7).contains(&mk) {
777                i += 2;
778                continue;
779            }
780            if mk == 0xD9 || mk == 0xDA {
781                break;
782            }
783            let Some(len) = s
784                .get(i + 2..i + 4)
785                .map(|l| usize::from(u16::from_be_bytes([l[0], l[1]])))
786            else {
787                break;
788            };
789            let Some(seg) = len.checked_sub(2).and_then(|n| s.get(i + 4..i + 4 + n)) else {
790                break;
791            };
792            match mk {
793                0xE0 if seg.starts_with(b"JFIF\0") => m.jfif = true,
794                0xEE if seg.starts_with(b"Adobe") && seg.len() >= 12 => {
795                    m.adobe_transform = Some(seg[11]);
796                }
797                0xC0..=0xCF if !matches!(mk, 0xC4 | 0xC8 | 0xCC) => {
798                    m.process = mk & 0x0F;
799                    m.precision = seg.first().copied().unwrap_or(0);
800                    if let Some(&n) = seg.get(5) {
801                        let comps = seg.get(6..6 + 3 * usize::from(n)).unwrap_or_default();
802                        m.component_ids = comps.as_chunks::<3>().0.iter().map(|c| c[0]).collect();
803                        m.sampling = comps
804                            .as_chunks::<3>()
805                            .0
806                            .iter()
807                            .map(|c| (c[1] >> 4, c[1] & 0x0F))
808                            .collect();
809                        frame_seen = true;
810                    }
811                }
812                _ => {}
813            }
814            i += 2 + len;
815        }
816    }
817    if !frame_seen {
818        return Err(CodecError::Decode {
819            codec: "jpeg",
820            detail: "no frame header (SOF) before the first scan".into(),
821        });
822    }
823    Ok(m)
824}
825
826/// Decode a JPEG chunk of a TIFF (compression 7). `tables` is the abbreviated
827/// table-specification stream of the `JPEGTables` tag (`SOI`, DQT/DHT segments, `EOI`), spliced
828/// in front of the chunk; `color` says whether the components are already R, G, B (TIFF
829/// photometric RGB, decoded as coded) or YCbCr (photometric YCbCr, converted to R, G, B).
830/// Same decoder and output conventions as [`jpeg_decode`].
831pub fn jpeg_decode_tiff(data: &[u8], tables: Option<&[u8]>, color: JpegColor) -> Result<Raster> {
832    jpeg_decode_tiff_limited(data, tables, color, MAX_UNSIZED_OUTPUT)
833}
834
835/// [`jpeg_decode_tiff`] with the frame-size bound of [`jpeg_decode_limited`].
836pub fn jpeg_decode_tiff_limited(
837    data: &[u8],
838    tables: Option<&[u8]>,
839    color: JpegColor,
840    max_bytes: usize,
841) -> Result<Raster> {
842    let stream: std::borrow::Cow<'_, [u8]> = match tables {
843        Some(t) if t.len() >= 4 && data.len() >= 2 => {
844            if t[..2] != [0xFF, 0xD8] || data[..2] != [0xFF, 0xD8] {
845                return Err(CodecError::Decode {
846                    codec: "jpeg",
847                    detail: "JPEG tables or image stream lack an SOI marker".into(),
848                });
849            }
850            let t_end = if t[t.len() - 2..] == [0xFF, 0xD9] {
851                t.len() - 2
852            } else {
853                t.len()
854            };
855            let mut v = Vec::with_capacity(t_end + data.len());
856            v.extend_from_slice(&t[..t_end]);
857            v.extend_from_slice(&data[2..]);
858            std::borrow::Cow::Owned(v)
859        }
860        _ => std::borrow::Cow::Borrowed(data),
861    };
862    jpeg_decode_with(&stream, Some(color), max_bytes.min(MAX_UNSIZED_OUTPUT), 0)
863}
864
865/// Apple/TIFF PackBits run-length decoding (TIFF compression 32773). Stops when
866/// `expected_len` bytes have been produced (0 = decode the whole input).
867pub fn packbits_decode(data: &[u8], expected_len: usize) -> Result<Vec<u8>> {
868    let mut out = Vec::with_capacity(expected_len.min(MAX_PREALLOC));
869    let mut i = 0usize;
870    while i < data.len() && (expected_len == 0 || out.len() < expected_len) {
871        let n = data[i] as i8;
872        i += 1;
873        if n >= 0 {
874            let count = n as usize + 1;
875            let end = i
876                .checked_add(count)
877                .filter(|&e| e <= data.len())
878                .ok_or_else(|| CodecError::Decode {
879                    codec: "packbits",
880                    detail: format!("literal run of {count} bytes runs past the input"),
881                })?;
882            out.extend_from_slice(&data[i..end]);
883            i = end;
884        } else if n != -128 {
885            let count = (1 - i16::from(n)) as usize;
886            let b = *data.get(i).ok_or_else(|| CodecError::Decode {
887                codec: "packbits",
888                detail: "repeat run without a value byte".into(),
889            })?;
890            i += 1;
891            out.extend(std::iter::repeat_n(b, count));
892        }
893    }
894    if expected_len != 0 {
895        out.truncate(expected_len);
896    }
897    Ok(out)
898}
899
900/// LZF decompression (the public liblzf scheme, as in Agilent `MSProfile.bin` and early
901/// Bruker TDF frames): a control byte below 32 copies that many plus one literal bytes;
902/// otherwise its top three bits are a back-reference length (an extra length byte follows
903/// when all three are set) and its low five bits, with the next byte, the distance back.
904/// `max_len` caps the output (0 = [`MAX_UNSIZED_OUTPUT`]); a stream that would produce more
905/// is an error.
906pub fn lzf_decode(data: &[u8], max_len: usize) -> Result<Vec<u8>> {
907    let limit = if max_len == 0 {
908        MAX_UNSIZED_OUTPUT
909    } else {
910        max_len
911    };
912    let bad = |detail: &str| CodecError::Decode {
913        codec: "lzf",
914        detail: detail.into(),
915    };
916    let mut out: Vec<u8> = Vec::with_capacity(limit.min(MAX_PREALLOC).min(data.len() * 4));
917    let mut i = 0usize;
918    while i < data.len() {
919        let c = usize::from(data[i]);
920        i += 1;
921        if c < 32 {
922            let n = c + 1;
923            let lit = data
924                .get(i..i + n)
925                .ok_or_else(|| bad("literal run past the end of the input"))?;
926            if out.len() + n > limit {
927                return Err(bad("output exceeds the expected size"));
928            }
929            out.extend_from_slice(lit);
930            i += n;
931        } else {
932            let mut len = c >> 5;
933            if len == 7 {
934                len += usize::from(*data.get(i).ok_or_else(|| bad("length byte missing"))?);
935                i += 1;
936            }
937            let lo = usize::from(*data.get(i).ok_or_else(|| bad("distance byte missing"))?);
938            i += 1;
939            let back = ((c & 0x1f) << 8) + lo + 1;
940            let from = out
941                .len()
942                .checked_sub(back)
943                .ok_or_else(|| bad("back-reference before the start of the output"))?;
944            let n = len + 2;
945            if out.len() + n > limit {
946                return Err(bad("output exceeds the expected size"));
947            }
948            // Overlapping copies repeat bytes written by this very reference.
949            for k in 0..n {
950                let v = out[from + k];
951                out.push(v);
952            }
953        }
954    }
955    Ok(out)
956}
957
958#[cfg(test)]
959mod tests {
960    use super::*;
961
962    #[test]
963    fn lzf_literals_and_overlapping_references() {
964        // literal "ab", then a back-reference of length 4 at distance 2 ("abab"), then "c"
965        let s = [0x01, b'a', b'b', 0x40, 0x01, 0x00, b'c'];
966        assert_eq!(lzf_decode(&s, 0).unwrap(), b"abababc");
967        assert!(lzf_decode(&s, 3).is_err(), "output cap");
968        assert!(lzf_decode(&[0x05, b'a'], 0).is_err(), "short literal");
969        assert!(
970            lzf_decode(&[0x20, 0x05], 0).is_err(),
971            "reference before start"
972        );
973        assert!(lzf_decode(&[0xE0], 0).is_err(), "missing length byte");
974    }
975
976    /// SOI, optional APP0 JFIF / APP14 Adobe, SOF0 with the given component ids (sampling of
977    /// the first 2x2, others 1x1), SOS.
978    fn jpeg_head(jfif: bool, adobe: Option<u8>, ids: &[u8]) -> Vec<u8> {
979        let mut v = vec![0xFF, 0xD8];
980        if jfif {
981            v.extend_from_slice(&[0xFF, 0xE0, 0, 16]);
982            v.extend_from_slice(b"JFIF\0\x01\x01\0\0\x01\0\x01\0\0");
983        }
984        if let Some(t) = adobe {
985            v.extend_from_slice(&[0xFF, 0xEE, 0, 14]);
986            v.extend_from_slice(b"Adobe\0\x64\0\0\0\0");
987            v.push(t);
988        }
989        let n = u8::try_from(ids.len()).unwrap();
990        let len = 8 + 3 * u16::from(n);
991        v.extend_from_slice(&[0xFF, 0xC0]);
992        v.extend_from_slice(&len.to_be_bytes());
993        v.extend_from_slice(&[8, 0, 16, 0, 16, n]);
994        for (k, id) in ids.iter().enumerate() {
995            v.extend_from_slice(&[*id, if k == 0 { 0x22 } else { 0x11 }, 0]);
996        }
997        v.extend_from_slice(&[0xFF, 0xDA, 0, 8, 1, 1, 0, 0, 63, 0]);
998        v
999    }
1000
1001    #[test]
1002    fn jpeg_markers_and_libjpeg_colour_rules() {
1003        let m = jpeg_markers(&jpeg_head(true, None, &[1, 2, 3]), None).unwrap();
1004        assert!(m.jfif);
1005        assert_eq!(m.component_ids, [1, 2, 3]);
1006        assert_eq!(m.sampling, [(2, 2), (1, 1), (1, 1)]);
1007        assert_eq!(m.coded_color(), Some(JpegColor::ToRgb));
1008        // JFIF wins over R,G,B ids (libjpeg 6b)
1009        let m = jpeg_markers(&jpeg_head(true, None, b"RGB"), None).unwrap();
1010        assert_eq!(m.coded_color(), Some(JpegColor::ToRgb));
1011        // Adobe transform 0 / 1 without JFIF
1012        let m = jpeg_markers(&jpeg_head(false, Some(0), &[1, 2, 3]), None).unwrap();
1013        assert_eq!(
1014            (m.adobe_transform, m.coded_color()),
1015            (Some(0), Some(JpegColor::AsCoded))
1016        );
1017        let m = jpeg_markers(&jpeg_head(false, Some(1), b"RGB"), None).unwrap();
1018        assert_eq!(m.coded_color(), Some(JpegColor::ToRgb));
1019        // ids only
1020        let m = jpeg_markers(&jpeg_head(false, None, b"RGB"), None).unwrap();
1021        assert_eq!(m.coded_color(), Some(JpegColor::AsCoded));
1022        let m = jpeg_markers(&jpeg_head(false, None, &[0, 1, 2]), None).unwrap();
1023        assert_eq!(m.coded_color(), Some(JpegColor::ToRgb));
1024        let m = jpeg_markers(&jpeg_head(false, None, &[1]), None).unwrap();
1025        assert_eq!(m.coded_color(), None);
1026        // tables stream (JPEGTables) scanned first; the frame header is in the chunk
1027        let tables = [0xFF, 0xD8, 0xFF, 0xD9];
1028        let m = jpeg_markers(&jpeg_head(true, None, &[1, 2, 3]), Some(&tables)).unwrap();
1029        assert!(m.jfif);
1030        // malformed: no SOI, truncated segment, no frame header
1031        assert!(jpeg_markers(&[0, 1, 2], None).is_err());
1032        let mut t = jpeg_head(true, None, &[1, 2, 3]);
1033        t.truncate(12);
1034        assert!(jpeg_markers(&t, None).is_err());
1035        assert!(jpeg_markers(&[0xFF, 0xD8, 0xFF, 0xDA, 0, 2], None).is_err());
1036        assert!(jpeg_markers(&[0xFF, 0xD8, 0xFF, 0xC0, 0, 1], None).is_err());
1037    }
1038
1039    /// A lossless WebP whose Huffman code counts overflow image-webp's u16 arithmetic: an error,
1040    /// not a panic, in every build (the workspace turns off overflow checks for image-webp).
1041    #[cfg(feature = "webp")]
1042    #[test]
1043    fn webp_huffman_overflow_is_an_error() {
1044        let p = std::path::Path::new(env!("CARGO_MANIFEST_DIR"))
1045            .join("../../fuzz/known-upstream/image-webp-0.2.4-huffman-overflow.webp");
1046        let Ok(d) = std::fs::read(p) else {
1047            return;
1048        };
1049        assert!(webp_decode_limited(&d, 1 << 20).is_err());
1050    }
1051
1052    /// OpenJPEG-encoded fixtures (imagecodecs 2026.8.16) and OpenJPEG's own decodes of them:
1053    /// lossless 14-bit grey (exact), a JP2-wrapped lossless RGB (exact) and an irreversible
1054    /// 9/7 + ICT RGB (within one grey level).
1055    #[cfg(feature = "jpeg2000")]
1056    #[test]
1057    fn jpeg2000_matches_openjpeg() {
1058        let dir = std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join("tests/fixtures/j2k");
1059        for (name, channels, bits, tol) in [
1060            ("gray16-lossless", 1, 16, 0),
1061            ("rgb8-lossless-jp2", 3, 8, 0),
1062            ("rgb8-lossy-ict", 3, 8, 1),
1063        ] {
1064            let cs = std::fs::read(dir.join(format!("{name}.j2k"))).unwrap();
1065            let want = std::fs::read(dir.join(format!("{name}.openjpeg.raw"))).unwrap();
1066            let r = jpeg2000_decode(&cs).unwrap();
1067            assert_eq!(
1068                (r.width, r.height, r.channels, r.bits_per_sample),
1069                (37, 21, channels, bits),
1070                "{name}"
1071            );
1072            assert_eq!(r.data.len(), want.len(), "{name}");
1073            if bits == 16 {
1074                assert_eq!(r.data, want, "{name}");
1075            } else {
1076                let worst = r.data.iter().zip(&want).map(|(a, b)| a.abs_diff(*b)).max();
1077                assert!(worst <= Some(tol), "{name}: off by {worst:?}");
1078            }
1079            assert!(
1080                jpeg2000_decode(&cs[..cs.len() / 3]).is_err(),
1081                "{name}: truncated"
1082            );
1083            assert!(jpeg2000_decode_limited(&cs, 100).is_err(), "{name}: limit");
1084        }
1085        assert!(jpeg2000_decode(b"not a codestream").is_err());
1086    }
1087
1088    #[test]
1089    fn hilo_roundtrip() {
1090        // samples 0x0201, 0x0403 -> lo bytes [01,03], hi bytes [02,04]
1091        assert_eq!(
1092            unshuffle_hilo(&[0x01, 0x03, 0x02, 0x04]),
1093            vec![0x01, 0x02, 0x03, 0x04]
1094        );
1095    }
1096
1097    /// 4x4 lossless (SOF3) 16-bit JPEG of `i * 1000`, written by libjpeg-turbo via imagecodecs.
1098    #[cfg(feature = "jpeg")]
1099    #[test]
1100    fn jpeg_lossless_16bit_is_exact() {
1101        let data = [
1102            0xff, 0xd8, 0xff, 0xe0, 0x00, 0x10, 0x4a, 0x46, 0x49, 0x46, 0x00, 0x01, 0x01, 0x00,
1103            0x00, 0x01, 0x00, 0x01, 0x00, 0x00, 0xff, 0xc3, 0x00, 0x0b, 0x10, 0x00, 0x04, 0x00,
1104            0x04, 0x01, 0x01, 0x11, 0x00, 0xff, 0xc4, 0x00, 0x16, 0x00, 0x01, 0x01, 0x01, 0x00,
1105            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0a, 0x0c,
1106            0x10, 0xff, 0xda, 0x00, 0x08, 0x01, 0x01, 0x00, 0x01, 0x00, 0x00, 0xcf, 0xa1, 0xf4,
1107            0x3e, 0x8b, 0xe8, 0x1f, 0x43, 0xe8, 0x7d, 0x17, 0xd0, 0x3e, 0x87, 0xd0, 0xfa, 0x2f,
1108            0xa0, 0x7d, 0x0f, 0xa1, 0xf4, 0x7f, 0xff, 0xd9,
1109        ];
1110        let r = jpeg_decode(&data).unwrap();
1111        assert_eq!(
1112            (r.width, r.height, r.channels, r.bits_per_sample),
1113            (4, 4, 1, 16)
1114        );
1115        let want: Vec<u8> = (0..16u16).flat_map(|i| (i * 1000).to_le_bytes()).collect();
1116        assert_eq!(r.data, want);
1117    }
1118
1119    #[cfg(feature = "jpeg")]
1120    #[test]
1121    fn jpeg_rejects_garbage_cleanly() {
1122        assert!(jpeg_decode(&[0xFF, 0xD8, 0xFF, 0x00, 0x01]).is_err());
1123        assert!(jpeg_decode(&[]).is_err());
1124    }
1125
1126    #[test]
1127    fn packbits_spec_example() {
1128        // The example from the TIFF 6.0 specification, section 9.
1129        let packed = [
1130            0xFE, 0xAA, 0x02, 0x80, 0x00, 0x2A, 0xFD, 0xAA, 0x03, 0x80, 0x00, 0x2A, 0x22, 0xF7,
1131            0xAA,
1132        ];
1133        let expected = [
1134            0xAA, 0xAA, 0xAA, 0x80, 0x00, 0x2A, 0xAA, 0xAA, 0xAA, 0xAA, 0x80, 0x00, 0x2A, 0x22,
1135            0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA,
1136        ];
1137        assert_eq!(packbits_decode(&packed, 0).unwrap(), expected);
1138        assert_eq!(packbits_decode(&packed, 5).unwrap(), expected[..5]);
1139        assert!(packbits_decode(&[0x05, 0x01], 0).is_err());
1140    }
1141
1142    #[cfg(feature = "jpeg")]
1143    #[test]
1144    fn jpeg_tiff_rejects_garbage() {
1145        assert!(jpeg_decode_tiff(&[0xFF, 0xD8, 0x00, 0x01], None, JpegColor::AsCoded).is_err());
1146        assert!(
1147            jpeg_decode_tiff(
1148                &[0xFF, 0xD8, 0x00, 0x01],
1149                Some(&[0, 0, 0, 0]),
1150                JpegColor::ToRgb
1151            )
1152            .is_err()
1153        );
1154    }
1155
1156    #[test]
1157    fn zstd1_header_parses() {
1158        let (h, off) = zstd1_header(&[3, 1, 1, 0x28, 0xB5, 0x2F, 0xFD]).unwrap();
1159        assert_eq!(off, 3);
1160        assert!(h.hilo);
1161        assert_eq!(h.chunk_kind, 1);
1162    }
1163
1164    #[cfg(feature = "deflate")]
1165    #[test]
1166    fn zlib_output_is_capped_at_expected_len() {
1167        use std::io::Write;
1168        let mut enc = flate2::write::ZlibEncoder::new(Vec::new(), flate2::Compression::default());
1169        enc.write_all(&vec![0u8; 1 << 20]).unwrap();
1170        let bomb = enc.finish().unwrap();
1171        assert_eq!(zlib_decode(&bomb, 100).unwrap().len(), 100);
1172    }
1173
1174    #[cfg(feature = "lzw")]
1175    #[test]
1176    fn lzw_reused_buffer_preserves_output_and_enforces_limit() {
1177        let raw: Vec<u8> = (0..200_000).map(|i| (i % 251) as u8).collect();
1178        let packed = weezl::encode::Encoder::with_tiff_size_switch(weezl::BitOrder::Msb, 8)
1179            .encode(&raw)
1180            .unwrap();
1181        // Multiple staging-buffer fills, an overlong stream, then reuse after that error.
1182        assert_eq!(lzw_decode(&packed, raw.len()).unwrap(), raw);
1183        assert!(lzw_decode(&packed, 100).is_err());
1184        assert!(lzw_decode(&packed, raw.len() - 1).is_err());
1185        assert!(lzw_decode(&packed, raw.len() + 1).is_err());
1186        assert_eq!(lzw_decode(&packed, 0).unwrap(), raw);
1187        assert!(lzw_decode(&packed[..packed.len() / 2], raw.len()).is_err());
1188        assert_eq!(lzw_decode(&packed, raw.len()).unwrap(), raw);
1189        // A stream without its end-of-information code keeps what it decoded (libtiff and
1190        // tifffile accept such strips; the Harmony corpus has one); the length check still
1191        // rejects it when a size is expected.
1192        let head = lzw_decode(&packed[..packed.len() / 2], 0).unwrap();
1193        assert!(!head.is_empty() && raw.starts_with(&head));
1194    }
1195
1196    #[cfg(feature = "zstd")]
1197    #[test]
1198    fn zstd_fast_path_and_fallback_enforce_size() {
1199        let raw = vec![42; 4096];
1200        let packed = ruzstd::encoding::compress_to_vec(
1201            &raw[..],
1202            ruzstd::encoding::CompressionLevel::Fastest,
1203        );
1204        assert_eq!(zstd_decode(&packed, raw.len()).unwrap(), raw);
1205        assert_eq!(zstd_decode(&packed, 0).unwrap(), raw);
1206        assert!(zstd_decode(&packed, 100).is_err());
1207        assert!(zstd_decode(&packed, raw.len() + 1).is_err());
1208        // A bogus hint must not cause a hint-sized allocation in the fast path.
1209        assert!(zstd_decode(&packed, usize::MAX).is_err());
1210    }
1211
1212    #[test]
1213    fn garbage_is_a_clean_error() {
1214        let junk = [0xFFu8; 64];
1215        assert!(zstd_decode(&junk, 16).is_err());
1216        assert!(zstd1_decode(&junk, 16, 2).is_err());
1217        assert!(zlib_decode(&junk, 16).is_err());
1218        assert!(zstd1_decode(&[], 16, 2).is_err());
1219    }
1220}