GitCorner

Code

ossartcraftlightcraft🌐
5a24289898fd
//! JPEG: marker parsing (EXIF/XMP/ICC/Adobe/MPF), full decode via `zune-jpeg`, DCT-scaled and
//! CMYK/YCCK decode via `jpeg-decoder`, embedded-preview thumbnails.

use crate::convert::{Buf, Meta, Model, Raw, check_size, finish};
use crate::{DecodeOptions, Decoded, Error, Format, Result, Thumbnail, ThumbnailSource, exif};
use craft_image_codecs::jpeg_sef::{reaches_eoi, repair as without_sef_trailer};

const F: Format = Format::Jpeg;

pub(crate) type Markers = craft_metadata::JpegInspection;

pub(crate) fn parse_markers(bytes: &[u8]) -> Option<Markers> {
    craft_metadata::inspect_jpeg(bytes)
}

pub(crate) fn decode(bytes: &[u8], opts: &DecodeOptions) -> Result<Decoded> {
    decode_with_fallback(bytes, opts, None)
}

const MAX_APP1: u32 = 128;

/// Stored dimensions and EXIF orientation from the markers, without decoding the scan data. The
/// file is refused, as a decode would refuse it, when it has no frame header, an unsupported frame
/// type or component count, or when its markers don't run to an end-of-image marker after the
/// image data (a truncated file). Damage *inside* the entropy-coded data can't be seen this way.
pub(crate) fn header(bytes: &[u8]) -> Result<(u32, u32, u16)> {
    let bytes = &*without_sef_trailer(bytes);
    let m = parse_markers(bytes).ok_or_else(|| Error::Malformed(F, "missing SOI".into()))?;
    if m.components == 0 {
        return Err(Error::Malformed(F, "no frame header".into()));
    }
    check_size(F, m.width as u64, m.height as u64, &DecodeOptions::default())?;
    if !matches!(m.sof, 0xC0..=0xC3) {
        return Err(Error::Unsupported(F, "arithmetic-coded or hierarchical JPEG"));
    }
    if !matches!(m.components, 1 | 3 | 4) {
        return Err(Error::Malformed(F, format!("{} colour components", m.components)));
    }
    if m.first_scan_components == 0 || !reaches_eoi(bytes) {
        return Err(Error::Malformed(F, "truncated: the image data doesn't end with an end-of-image marker".into()));
    }
    let orientation = m.exif.as_deref().map(exif::summarize).unwrap_or_default().orientation.unwrap_or(1);
    Ok((m.width, m.height, orientation))
}

pub(crate) fn decode_with_fallback(bytes: &[u8], opts: &DecodeOptions, fallback: Option<crate::NamedSpace>) -> Result<Decoded> {
    let bytes = &*without_sef_trailer(bytes);
    let m = parse_markers(bytes).ok_or_else(|| Error::Malformed(F, "missing SOI".into()))?;
    if m.components == 0 {
        return Err(Error::Malformed(F, "no frame header".into()));
    }
    check_size(F, m.width as u64, m.height as u64, opts)?;
    // The allocating decoders below parse the markers again, all of them: the count is capped so
    // their quadratic/unbounded handling of APP1 metadata stays linear in file size.
    if m.app1 > MAX_APP1 {
        return Err(Error::Malformed(F, "too many APP1 markers".into()));
    }
    let raw = decode_via_craft_codecs(bytes, opts)?;
    // A raw container may carry the colour declaration of an otherwise untagged preview.
    // Apply it before linearization/resizing; existing JPEG metadata always wins.
    let hint = fallback
        .filter(|_| m.icc.is_none() && !m.icc_invalid && m.exif.is_none())
        .map(|space| crate::SourceSpace::named(space, crate::SpaceOrigin::Container));
    let meta = Meta { icc: m.icc, icc_invalid: m.icc_invalid, exif: m.exif, xmp: m.xmp, hint, ..Default::default() };
    finish(F, raw, meta, (m.width, m.height), opts)
}

fn decode_via_craft_codecs(bytes: &[u8], budget: &DecodeOptions) -> Result<Raw> {
    let ic_opts = craft_image_codecs::DecodeOptions {
        limits: craft_image_codecs::Limits {
            max_width: u32::MAX,
            max_height: u32::MAX,
            max_pixels: budget.max_pixels,
            max_alloc: budget.max_pixels.saturating_mul(8),
        },
        keep_orientation: true,
    };
    let img = craft_image_codecs::decode_as_with(craft_image_codecs::Format::Jpeg, bytes, &ic_opts).map_err(|e| match e {
        craft_image_codecs::CodecError::LimitExceeded(_) => Error::TooLarge(0, 0),
        other => Error::Malformed(F, other.to_string()),
    })?;
    if img.warnings.iter().any(|w| matches!(w, craft_image_codecs::DecodeWarning::Truncated { .. })) {
        return Err(Error::Malformed(F, "truncated: the image data doesn't end with an end-of-image marker".into()));
    }
    let (w, h) = (img.width() as usize, img.height() as usize);
    check_size(F, w as u64, h as u64, budget)?;
    let model = match img.layout() {
        craft_image_codecs::ChannelLayout::Gray | craft_image_codecs::ChannelLayout::GrayA => Model::Gray,
        craft_image_codecs::ChannelLayout::Cmyk | craft_image_codecs::ChannelLayout::CmykA => Model::Cmyk,
        _ => Model::Rgb,
    };
    let (buf, bit_depth) = match img.sample_type() {
        craft_image_codecs::SampleType::U16 => {
            (Buf::U16(img.to_u16_samples().ok_or_else(|| Error::Malformed(F, "expected 16-bit samples".into()))?), 16)
        }
        _ => (Buf::U8(img.as_u8().ok_or_else(|| Error::Malformed(F, "expected 8-bit samples".into()))?.to_vec()), 8),
    };
    Ok(Raw { width: w, height: h, model, alpha: false, premultiplied: false, buf, bit_depth })
}

/// Fast path: an embedded JPEG preview (EXIF IFD1 thumbnail, or the largest MPF preview) whose long
/// edge is ≥ `max_edge`, decoded with DCT scaling and fitted.
pub(crate) fn embedded_thumbnail(bytes: &[u8], max_edge: u32, min_edge: u32) -> Option<Thumbnail> {
    let m = parse_markers(bytes)?;
    let summary = m.exif.as_deref().map(exif::summarize).unwrap_or_default();
    let orientation = summary.orientation.unwrap_or(1);
    let mut candidates: Vec<(&[u8], ThumbnailSource)> = Vec::new();
    if let (Some(ex), Some((o, l))) = (m.exif.as_deref(), summary.thumbnail) {
        candidates.push((&ex[o..o + l], ThumbnailSource::ExifThumbnail));
    }
    for e in mpf_images(&m) {
        if e.kind == MPF_GAIN_MAP {
            continue;
        }
        if let Some(s) = bytes.get(e.offset..e.offset.saturating_add(e.len)) {
            candidates.push((s, ThumbnailSource::MpfPreview));
        }
    }
    // Smallest adequate preview wins.
    let mut best: Option<(u32, &[u8], ThumbnailSource)> = None;
    for (data, src) in candidates {
        let Some(pm) = parse_markers(data) else { continue };
        // a gain map (also when its MPF entry doesn't say so) is greyscale gain data, not a preview
        if pm.is_gain_map() {
            continue;
        }
        let long = pm.width.max(pm.height);
        // Previews must have the main image's aspect ratio (within 2%), else they are letterboxed/cropped.
        let aspect_ok = m.width > 0
            && m.height > 0
            && pm.height > 0
            && ((pm.width as f64 / pm.height as f64) / (m.width as f64 / m.height as f64) - 1.0).abs() < 0.02;
        // Prefer the smallest preview covering `max_edge`; otherwise the largest one ≥ `min_edge`.
        let better = match &best {
            None => true,
            Some(b) if b.0 >= max_edge => long >= max_edge && long < b.0,
            Some(b) => long > b.0,
        };
        if long >= min_edge && aspect_ok && better {
            best = Some((long, data, src));
        }
    }
    let (_, data, source) = best?;
    let d = decode(data, &DecodeOptions::fit(max_edge, max_edge)).ok()?;
    Some(Thumbnail { image: d.to_srgb8(), orientation, source, source_width: m.width, source_height: m.height })
}

/// MP type code of a gain map image (CIPA DC-007 entry attribute, low 24 bits).
pub(crate) const MPF_GAIN_MAP: u32 = 0x05_0000;

/// The MPF-listed images other than the primary.
pub(crate) fn mpf_images(m: &Markers) -> Vec<craft_metadata::mpf::MpfImage> {
    m.mpf.as_ref().map_or_else(Vec::new, |(base, data)| craft_metadata::mpf::images_from_index(data, *base))
}

/// Decode to the file's 8-bit samples as stored (no colour management): 1 channel for greyscale
/// files, else 3 (RGB). For data images such as gain maps, whose values are codes, not colours.
pub(crate) fn decode_codes8(bytes: &[u8]) -> Result<(usize, usize, usize, Vec<u8>)> {
    let m = parse_markers(bytes).ok_or_else(|| Error::Malformed(F, "missing SOI".into()))?;
    if m.components == 0 {
        return Err(Error::Malformed(F, "no frame header".into()));
    }
    check_size(F, m.width as u64, m.height as u64, &DecodeOptions::default())?;
    if m.app1 > MAX_APP1 {
        return Err(Error::Malformed(F, "too many APP1 markers".into()));
    }
    if !matches!(m.components, 1 | 3) || m.precision != 8 {
        return Err(Error::Malformed(F, "expected an 8-bit greyscale or RGB image".into()));
    }
    let raw = decode_via_craft_codecs(bytes, &DecodeOptions::default())?;
    let ch = raw.model.channels();
    match raw.buf {
        Buf::U8(v) if v.len() >= raw.width * raw.height * ch => Ok((raw.width, raw.height, ch, v)),
        _ => Err(Error::Malformed(F, "unexpected sample format".into())),
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn shared_eoi_walk_matches_previous_on_mutated_streams() {
        fn previous(b: &[u8]) -> bool {
            let mut p = 2usize;
            loop {
                let Some(skip) = b.get(p..).and_then(|s| s.iter().position(|&v| v == 0xff)) else { return false };
                p = p.saturating_add(skip);
                let Some(&marker) = b.get(p.saturating_add(1)) else { return false };
                p = match marker {
                    0xd9 => return true,
                    0xff => p.saturating_add(1),
                    0x00 | 0x01 | 0xd0..=0xd8 => p.saturating_add(2),
                    _ => {
                        let Some(len) = b.get(p.saturating_add(2)..p.saturating_add(4)).map(|s| u16::from_be_bytes([s[0], s[1]]) as usize) else {
                            return false;
                        };
                        if len < 2 {
                            return false;
                        }
                        p.saturating_add(2).saturating_add(len)
                    }
                };
            }
        }
        let base = [0xff, 0xd8, 0xff, 0xe1, 0, 4, 1, 2, 0xff, 0xda, 0, 3, 1, 1, 0xff, 0, 3, 0xff, 0xd0, 4, 0xff, 0xd9];
        for end in 0..=base.len() {
            for at in 0..end {
                for value in [0, 1, 0xff, 0xd9] {
                    let mut bytes = base[..end].to_vec();
                    bytes[at] = value;
                    assert_eq!(reaches_eoi(&bytes), previous(&bytes), "end {end}, at {at}, value {value}");
                }
            }
        }
    }

    #[test]
    fn mpf_shared_matches_previous_reader_on_mutated_indices() {
        fn previous(base: usize, data: &[u8]) -> Vec<craft_metadata::mpf::MpfImage> {
            let Some(t) = exif::Tiff::new(data) else { return vec![] };
            let Some(ifd) = t.first_ifd() else { return vec![] };
            let Some((entries, _)) = t.ifd(ifd) else { return vec![] };
            let Some(e) = entries.iter().find(|e| e.tag == 0xb002) else { return vec![] };
            let Some(list) = t.bytes(e) else { return vec![] };
            list.as_chunks::<16>()
                .0
                .iter()
                .enumerate()
                .take(16)
                .skip(1)
                .filter_map(|(_, rec)| {
                    let rd = |i| {
                        let b = [rec[i], rec[i + 1], rec[i + 2], rec[i + 3]];
                        if data.starts_with(b"II") { u32::from_le_bytes(b) } else { u32::from_be_bytes(b) }
                    };
                    let (attr, len, off) = (rd(0), rd(4) as usize, rd(8) as usize);
                    (len > 0 && off > 0).then_some(craft_metadata::mpf::MpfImage { offset: base.saturating_add(off), len, kind: attr & 0x00ff_ffff })
                })
                .collect()
        }
        let file = craft_metadata::mpf::write(&[0xff, 0xd8, 0xff, 0xd9], &[(0x050000, &[0xff, 0xd8, 0xff, 0xd9])]).unwrap();
        let m = parse_markers(&file).unwrap();
        let (base, data) = m.mpf.unwrap();
        for at in 0..data.len() {
            for value in [0u8, 0xff] {
                let mut altered = data.clone();
                altered[at] = value;
                assert_eq!(craft_metadata::mpf::images_from_index(&altered, base), previous(base, &altered), "mutation at {at}, value {value}");
            }
        }
    }

    #[test]
    fn container_fallback_matches_tagged_jpeg_before_resizing_and_keeps_own_metadata() {
        use crate::{ChromaSubsampling, EncodeImage, EncodeMeta, NamedSpace, Samples, SpaceOrigin, encode_jpeg};
        let pixels: Vec<u8> = (0..16 * 16).flat_map(|i| if i % 2 == 0 { [120, 180, 90] } else { [30, 100, 200] }).collect();
        let encode = |icc, exif| {
            encode_jpeg(
                &EncodeImage::new(16, 16, 3, Samples::U8(&pixels)),
                100,
                ChromaSubsampling::S444,
                &EncodeMeta { icc, exif, ..Default::default() },
            )
            .unwrap()
        };
        let untagged = encode(None, None);
        let adobe = crate::icc::write_named(NamedSpace::AdobeRgb);
        let tagged = encode(Some(&adobe), None);
        for opts in [DecodeOptions::default(), DecodeOptions::fit(3, 3)] {
            let with = crate::decode_jpeg_with_fallback(&untagged, opts, NamedSpace::AdobeRgb).unwrap();
            let expected = crate::decode(&tagged, opts).unwrap();
            assert_eq!(with.space.origin, SpaceOrigin::Container);
            let (a, b) = (with.to_working(), expected.to_working());
            for (p, q) in a.data.iter().zip(&b.data) {
                assert!(p.iter().zip(q).all(|(a, b)| (a - b).abs() < 0.0001), "{p:?} != {q:?}");
            }
        }
        let srgb = crate::icc::write_named(NamedSpace::Srgb);
        let exif = crate::exif::minimal_exif(6);
        for bytes in [encode(Some(&srgb), None), encode(None, Some(&exif)), encode(Some(b"bad ICC"), None)] {
            let opts = DecodeOptions::fit(5, 5);
            let with = crate::decode_jpeg_with_fallback(&bytes, opts, NamedSpace::AdobeRgb).unwrap();
            let expected = crate::decode(&bytes, opts).unwrap();
            assert_eq!(with.space, expected.space);
            assert_eq!(with.image, expected.image);
            assert_eq!(with.orientation, expected.orientation);
        }
        assert!(crate::decode_jpeg_with_fallback(b"bad JPEG", DecodeOptions::default(), NamedSpace::AdobeRgb).is_err());
    }

    fn app2_icc(sequence: u8, total: u8, data: &[u8]) -> Vec<u8> {
        let mut payload = b"ICC_PROFILE\0".to_vec();
        payload.extend_from_slice(&[sequence, total]);
        payload.extend_from_slice(data);
        let mut segment = vec![0xff, 0xe2];
        segment.extend_from_slice(&u16::try_from(payload.len() + 2).unwrap().to_be_bytes());
        segment.extend_from_slice(&payload);
        segment
    }

    fn jpeg_with_icc_chunks(chunks: &[Vec<u8>]) -> Vec<u8> {
        use crate::{ChromaSubsampling, EncodeImage, EncodeMeta, Samples, encode_jpeg};
        let pixels = [180, 60, 40, 180, 60, 40, 180, 60, 40, 180, 60, 40];
        let base = encode_jpeg(&EncodeImage::new(2, 2, 3, Samples::U8(&pixels)), 100, ChromaSubsampling::S444, &EncodeMeta::default()).unwrap();
        let mut jpeg = base.get(..2).unwrap_or_default().to_vec();
        for chunk in chunks {
            jpeg.extend_from_slice(chunk);
        }
        jpeg.extend_from_slice(base.get(2..).unwrap_or_default());
        jpeg
    }

    #[test]
    fn jpeg_icc_accepts_reordered_chunks_and_preserves_pixels() {
        use crate::{NamedSpace, SpaceOrigin, icc};
        let profile = icc::write_named(NamedSpace::DisplayP3);
        let (first, second) = profile.split_at(profile.len() / 2);
        let single = jpeg_with_icc_chunks(&[app2_icc(1, 1, &profile)]);
        let reordered = jpeg_with_icc_chunks(&[app2_icc(2, 2, second), app2_icc(1, 2, first)]);

        let one = crate::decode(&single, DecodeOptions::default()).unwrap();
        let two = crate::decode(&reordered, DecodeOptions::default()).unwrap();
        assert_eq!(one.icc.as_deref(), Some(profile.as_slice()));
        assert_eq!(two.icc.as_deref(), Some(profile.as_slice()));
        assert!(!parse_markers(&reordered).unwrap().icc_invalid);
        assert_eq!(one.space, two.space);
        assert_eq!(two.space.origin, SpaceOrigin::IccMatrixTrc);
        let (a, b) = (one.to_working(), two.to_working());
        assert_eq!(a.data.len(), b.data.len());
        for (left, right) in a.data.iter().zip(&b.data) {
            assert!(left.iter().zip(right).all(|(x, y)| (x - y).abs() < 0.00001), "{left:?} != {right:?}");
        }
    }

    #[test]
    fn jpeg_icc_rejects_incoherent_chunks_and_uses_unsupported_fallback() {
        use crate::{NamedSpace, SpaceOrigin, icc};
        let profile = icc::write_named(NamedSpace::DisplayP3);
        let (first, second) = profile.split_at(profile.len() / 2);
        let base = jpeg_with_icc_chunks(&[]);
        let reference = crate::decode(&base, DecodeOptions::default()).unwrap();
        let cases = [
            ("missing", vec![app2_icc(1, 2, &profile)]),
            ("duplicate", vec![app2_icc(1, 2, first), app2_icc(1, 2, second)]),
            ("zero sequence", vec![app2_icc(0, 1, &profile)]),
            ("zero total", vec![app2_icc(1, 0, &profile)]),
            ("out of range", vec![app2_icc(2, 1, &profile)]),
            ("gap", vec![app2_icc(1, 3, first), app2_icc(3, 3, second)]),
            ("inconsistent totals", vec![app2_icc(1, 2, first), app2_icc(2, 3, second)]),
            ("short header", {
                let mut segment = vec![0xff, 0xe2];
                segment.extend_from_slice(&15u16.to_be_bytes());
                segment.extend_from_slice(b"ICC_PROFILE\0\x01");
                vec![segment]
            }),
        ];
        for (name, chunks) in cases {
            let jpeg = jpeg_with_icc_chunks(&chunks);
            let markers = parse_markers(&jpeg).unwrap();
            let decoded = crate::decode(&jpeg, DecodeOptions::default()).unwrap();
            assert_eq!(decoded.space.origin, SpaceOrigin::IccUnsupported, "{name}: invalid profile must not be applied");
            assert!(markers.icc.is_none(), "{name}: invalid ICC must not be assembled");
            assert!(markers.icc_invalid, "{name}: invalid ICC presence must be retained");
            assert_eq!(decoded.icc, None, "{name}: invalid ICC bytes must not escape decode");
            assert_eq!(decoded.space.origin, SpaceOrigin::IccUnsupported, "{name}");
            assert_eq!(decoded.space.named, Some(NamedSpace::Srgb), "{name}");
            assert_eq!(decoded.to_working(), reference.to_working(), "{name}: fallback pixels");
        }
    }

    #[test]
    fn jpeg_icc_invalid_sequence_prevents_raw_colour_hint() {
        use crate::{NamedSpace, SpaceOrigin, icc};
        let profile = icc::write_named(NamedSpace::DisplayP3);
        let jpeg = jpeg_with_icc_chunks(&[app2_icc(1, 2, &profile)]);
        let decoded = crate::decode_jpeg_with_fallback(&jpeg, DecodeOptions::default(), NamedSpace::AdobeRgb).unwrap();
        assert_eq!(decoded.space.origin, SpaceOrigin::IccUnsupported);
        assert_eq!(decoded.space.named, Some(NamedSpace::Srgb));
    }

    #[test]
    fn jpeg_icc_rejects_truncated_app2_segment() {
        use crate::{NamedSpace, icc};
        let profile = icc::write_named(NamedSpace::DisplayP3);
        let mut segment = app2_icc(1, 1, &profile);
        let declared = u16::from_be_bytes([segment[2], segment[3]]).saturating_add(1);
        segment[2..4].copy_from_slice(&declared.to_be_bytes());
        let mut jpeg = vec![0xff, 0xd8];
        jpeg.extend_from_slice(&segment);
        let markers = parse_markers(&jpeg).unwrap();
        assert!(markers.icc.is_none());
        assert!(markers.icc_invalid);
    }

    #[test]
    fn unbounded_fit_box_decodes_at_full_size() {
        // a full-size load asks for the largest box (`usize::MAX` saturated to `u32::MAX`): no DCT scaling,
        // and no overflow working out whether to scale
        use crate::{ChromaSubsampling, EncodeImage, EncodeMeta, Samples, encode_jpeg};
        let pixels = vec![128u8; 32 * 24 * 3];
        let jpeg = encode_jpeg(&EncodeImage::new(32, 24, 3, Samples::U8(&pixels)), 90, ChromaSubsampling::S444, &EncodeMeta::default()).unwrap();
        let d = crate::decode(&jpeg, DecodeOptions::fit(u32::MAX, u32::MAX)).unwrap();
        assert_eq!((d.width, d.height), (32, 24));
    }

    #[test]
    fn markers_on_garbage() {
        assert!(parse_markers(b"").is_none());
        assert!(parse_markers(&[0xFF, 0xD8, 0xFF, 0xE1, 0, 1]).is_none());
        let m = parse_markers(&[0xFF, 0xD8, 0xFF, 0xE1, 0, 200, 1, 2]).unwrap();
        assert!(m.exif.is_none());
    }

    /// Rewrites an encoded file's frame-header dimensions: same tables and scan, a hostile
    /// annotated size.
    fn with_dims(bytes: &[u8], w: u16, h: u16) -> Vec<u8> {
        let mut out = bytes.to_vec();
        let mut p = 2usize;
        while p + 1 < out.len() {
            if out[p] != 0xFF {
                p += 1;
                continue;
            }
            match out[p + 1] {
                0xC0..=0xCF if out[p + 1] != 0xC4 && out[p + 1] != 0xC8 && out[p + 1] != 0xCC => {
                    out[p + 5..p + 7].copy_from_slice(&h.to_be_bytes());
                    out[p + 7..p + 9].copy_from_slice(&w.to_be_bytes());
                    return out;
                }
                0x01 | 0xD0..=0xD9 => p += 2,
                0xFF => p += 1,
                _ => {
                    let Some(l) = out.get(p + 2..p + 4) else { return out };
                    p += 2 + usize::from(u16::from_be_bytes([l[0], l[1]]));
                }
            }
        }
        out
    }

    /// A file claiming 65535×65535 must be refused for size before zune-jpeg allocates from
    /// its frame header (4 GB+ of samples from roughly five hundred bytes of tables).
    #[test]
    fn zune_refuses_its_own_frame_header_over_the_budget() {
        use crate::{ChromaSubsampling, EncodeImage, EncodeMeta, Samples, encode_jpeg};
        let pixels = vec![128u8; 32 * 24];
        let jpeg = encode_jpeg(&EncodeImage::new(32, 24, 1, Samples::U8(&pixels)), 90, ChromaSubsampling::S444, &EncodeMeta::default()).unwrap();
        let hostile = with_dims(&jpeg, 65535, 65535);
        assert_ne!(hostile, jpeg);
        match crate::decode(&hostile, DecodeOptions::default()) {
            Err(Error::TooLarge(w, h)) => assert_eq!((w, h), (65535, 65535)),
            other => panic!("unexpected: {other:?}"),
        }
    }

    /// The jpeg-decoder branch (a 4-component file) is checked the same way: its own
    /// `read_info` result is what a full-size decode allocates from.
    #[test]
    fn jpeg_decoder_refuses_its_own_frame_header_over_the_budget() {
        use crate::{ChromaSubsampling, EncodeImage, EncodeMeta, Samples, encode_jpeg};
        let pixels = vec![128u8; 32 * 24 * 4];
        let jpeg = encode_jpeg(&EncodeImage::new(32, 24, 4, Samples::U8(&pixels)), 90, ChromaSubsampling::S444, &EncodeMeta::default()).unwrap();
        let hostile = with_dims(&jpeg, 65535, 65535);
        match crate::decode(&hostile, DecodeOptions::default()) {
            Err(Error::TooLarge(w, h)) => assert_eq!((w, h), (65535, 65535)),
            other => panic!("unexpected: {other:?}"),
        }
    }

    /// Extended XMP is reassembled from every APP1 fragment: an unbounded count is quadratic
    /// memory in a marker walk, so the count is capped.
    #[test]
    fn an_app1_flood_is_refused_before_the_decoders_rewalk_it() {
        let mut b = vec![0xFF, 0xD8];
        // a 1-component SOF0: 8-bit, 2×2, component (1, 0x11, Q0)
        b.extend_from_slice(&[0xFF, 0xC0, 0x00, 0x0B, 8, 0, 2, 0, 2, 1, 0x01, 0x11, 0x00]);
        for _ in 0..200 {
            b.extend_from_slice(&[0xFF, 0xE1, 0x00, 0x02]);
        }
        b.extend_from_slice(&[0xFF, 0xD9]);
        match crate::decode(&b, DecodeOptions::default()) {
            Err(Error::Malformed(F, m)) => assert!(m.contains("APP1"), "{m}"),
            other => panic!("unexpected: {other:?}"),
        }
    }
}

/// [`crate::decode_jpeg_srgb8_fit`]: a plain 8-bit sRGB JPEG decoded (DCT-scaled when that still
/// covers the box) and area-averaged in linear light straight into 8-bit sRGB, without the
/// float image the general path builds (for a 45 MP camera preview fitted to 2560 px that float
/// image and its conversions were half the time). `Ok(None)` for anything else.
pub(crate) fn srgb8_fit(bytes: &[u8], max_w: u32, max_h: u32) -> Result<Option<(lightcraft_raster::Rgba8, u16)>> {
    // (a Samsung SEF trailer read as the file it stands for, as the general decode does: CodeRabbit on #3583)
    let bytes = &*without_sef_trailer(bytes);
    let Some(m) = parse_markers(bytes) else { return Ok(None) };
    let plain = matches!(m.components, 1 | 3)
        && m.precision == 8
        && matches!(m.sof, 0xC0..=0xC2)
        && m.icc.is_none()
        && !m.icc_invalid
        && m.adobe_transform.is_none();
    let summary = m.exif.as_deref().map(exif::summarize).unwrap_or_default();
    if !plain || summary.adobe_rgb_hint || max_w == 0 || max_h == 0 {
        return Ok(None);
    }
    let budget = DecodeOptions::default();
    check_size(F, m.width as u64, m.height as u64, &budget)?;
    // as in the general decode: the decoders below walk every APP1 again
    if m.app1 > MAX_APP1 {
        return Err(Error::Malformed(F, "too many APP1 markers".into()));
    }
    let raw = decode_via_craft_codecs(bytes, &budget)?;
    let Buf::U8(px) = &raw.buf else { return Ok(None) };
    let ch = match raw.model {
        Model::Rgb => 3,
        Model::Gray => 1,
        Model::Cmyk => return Ok(None),
    };
    let (w, h) = (raw.width, raw.height);
    if w == 0 || h == 0 || px.len() < w.saturating_mul(h).saturating_mul(ch) {
        return Err(Error::Malformed(F, "short output".into()));
    }
    // the size `lightcraft_raster::resample::fit` gives: within the box, aspect kept, never larger
    let s = (max_w as f64 / w as f64).min(max_h as f64 / h as f64).min(1.0);
    let tw = ((w as f64 * s).round() as usize).clamp(1, w);
    let th = ((h as f64 * s).round() as usize).clamp(1, h);
    let orientation = summary.orientation.filter(|o| (1..=8).contains(o)).unwrap_or(1);
    Ok(Some((area_average_srgb8(px, w, h, ch, tw, th), orientation)))
}

/// Source pixels and weights each output pixel along one axis covers: a box of the scale's width
/// (`n` source pixels onto `out`), partly covered pixels at its ends weighted by their share.
fn box_taps(n: usize, out: usize) -> Vec<(usize, Vec<f32>)> {
    let scale = n as f64 / out as f64;
    (0..out)
        .map(|o| {
            let (a, b) = (o as f64 * scale, ((o + 1) as f64 * scale).min(n as f64));
            let first = (a.floor() as usize).min(n - 1);
            let last = ((b.ceil() as usize).max(first + 1)).min(n);
            let ws: Vec<f32> =
                (first..last).map(|j| ((b.min(j as f64 + 1.0) - a.max(j as f64)).max(0.0) / (b - a).max(f64::MIN_POSITIVE)) as f32).collect();
            (first, ws)
        })
        .collect()
}

/// `px` (`w × h`, `ch` = 1 or 3 interleaved 8-bit sRGB samples) averaged over boxes in linear
/// light into `tw × th` opaque 8-bit sRGB pixels. A copy when the size doesn't change.
fn area_average_srgb8(px: &[u8], w: usize, h: usize, ch: usize, tw: usize, th: usize) -> lightcraft_raster::Rgba8 {
    use lightcraft_color::transfer::{decode_srgb8, encode_srgb8};
    let mut out = lightcraft_raster::Rgba8::new(tw, th);
    let sample = |x: usize, y: usize, c: usize| px.get((y * w + x) * ch + if ch == 3 { c } else { 0 }).copied().unwrap_or(0);
    if (tw, th) == (w, h) {
        lightcraft_raster::par_rows(&mut out.data, tw, |y, row| {
            for (x, o) in row.iter_mut().enumerate() {
                *o = [sample(x, y, 0), sample(x, y, 1), sample(x, y, 2), 255];
            }
        });
        return out;
    }
    let lut: [f32; 256] = std::array::from_fn(|i| decode_srgb8(i as u8));
    let xs = box_taps(w, tw);
    let ys = box_taps(h, th);
    // one source row filtered across into linear RGB at the output width
    let across = |sy: usize, buf: &mut Vec<[f32; 3]>| {
        buf.clear();
        let Some(src) = px.get(sy * w * ch..(sy + 1) * w * ch) else {
            buf.resize(tw, [0.0; 3]);
            return;
        };
        for (x0, wx) in &xs {
            let mut a = [0f32; 3];
            if let Some(span) = src.get(x0 * ch..(x0 + wx.len()) * ch) {
                for (p, &k) in span.chunks_exact(ch).zip(wx) {
                    match p {
                        [r, g, b] => {
                            a[0] += lut[*r as usize] * k;
                            a[1] += lut[*g as usize] * k;
                            a[2] += lut[*b as usize] * k;
                        }
                        [v] => {
                            let l = lut[*v as usize] * k;
                            a = [a[0] + l, a[1] + l, a[2] + l];
                        }
                        _ => {}
                    }
                }
            }
            buf.push(a);
        }
    };
    lightcraft_raster::par_rows(&mut out.data, tw, |y, row| {
        let Some((y0, wy)) = ys.get(y) else { return };
        let mut acc = vec![[0f32; 3]; tw];
        let mut line = Vec::with_capacity(tw);
        for (dy, &k) in wy.iter().enumerate() {
            across(y0 + dy, &mut line);
            for (a, l) in acc.iter_mut().zip(&line) {
                a[0] += l[0] * k;
                a[1] += l[1] * k;
                a[2] += l[2] * k;
            }
        }
        for (o, a) in row.iter_mut().zip(&acc) {
            *o = [encode_srgb8(a[0]), encode_srgb8(a[1]), encode_srgb8(a[2]), 255];
        }
    });
    out
}

#[cfg(test)]
mod srgb8_fit_tests {
    use super::*;

    fn jpeg(w: usize, h: usize, f: impl Fn(usize, usize) -> [u8; 3]) -> Vec<u8> {
        let data: Vec<[u8; 4]> = (0..w * h)
            .map(|i| {
                let [r, g, b] = f(i % w, i / w);
                [r, g, b, 255]
            })
            .collect();
        let img = lightcraft_raster::Rgba8 { width: w, height: h, data };
        crate::encode_jpeg(&crate::EncodeImage::rgba8(&img), 95, crate::ChromaSubsampling::S420, &crate::EncodeMeta::default()).unwrap()
    }

    /// The fast path shows what the general decode shows for a plain sRGB JPEG, at the same size.
    #[test]
    fn matches_the_general_decode_within_a_few_levels() {
        let b = jpeg(1203, 801, |x, y| [((x * 7) ^ (y * 3)) as u8, (x / 5 + y / 3) as u8, ((x * y) % 251) as u8]);
        for (mw, mh) in [(300u32, 300u32), (640, 640), (1203, 801), (5000, 5000), (500, 120)] {
            let (fast, o) = srgb8_fit(&b, mw, mh).unwrap().unwrap();
            let slow = crate::decode(&b, DecodeOptions::fit(mw, mh)).unwrap().to_srgb8();
            assert_eq!((fast.width, fast.height), (slow.width, slow.height), "box {mw}×{mh}");
            assert_eq!(o, 1);
            let diff: Vec<u32> =
                fast.data.iter().zip(&slow.data).flat_map(|(a, b)| (0..3).map(move |c| (a[c] as i32 - b[c] as i32).unsigned_abs())).collect();
            let mean = diff.iter().sum::<u32>() as f64 / diff.len() as f64;
            assert!(mean < 5.0, "box {mw}×{mh}: mean difference {mean}");
        }
    }

    /// Anything but plain 8-bit sRGB goes the general way; junk is an error or `None`, never a panic.
    #[test]
    fn declines_what_it_does_not_cover_and_never_panics() {
        let b = jpeg(64, 48, |x, _| [x as u8, 0, 0]);
        assert!(srgb8_fit(&b, 0, 10).unwrap().is_none());
        assert!(srgb8_fit(b"not a jpeg", 10, 10).unwrap().is_none());
        for cut in [0, 2, 20, b.len() / 2, b.len() - 3] {
            let _ = srgb8_fit(&b[..cut], 32, 32);
        }
        // a 1-pixel-wide image into a box much smaller in one axis
        let thin = jpeg(1, 300, |_, y| [y as u8, y as u8, y as u8]);
        let (img, _) = srgb8_fit(&thin, 10, 10).unwrap().unwrap();
        assert_eq!((img.width, img.height), (1, 10));
    }

    /// An incomplete ICC_PROFILE sequence (chunk 1 of 2) is "has a profile we can't read", not
    /// "plain sRGB": the general decode handles it, as the doc says (CodeRabbit on #3577).
    #[test]
    fn an_incomplete_icc_sequence_goes_the_general_way() {
        let b = jpeg(16, 16, |x, y| [x as u8, y as u8, 0]);
        let mut seg = vec![0xFF, 0xE2, 0, 0];
        seg.extend_from_slice(b"ICC_PROFILE");
        seg.extend_from_slice(&[1, 2]);
        seg.extend_from_slice(&[0u8; 64]);
        let len = u16::try_from(seg.len() - 2).unwrap();
        seg[2..4].copy_from_slice(&len.to_be_bytes());
        let mut with = b[..2].to_vec();
        with.extend_from_slice(&seg);
        with.extend_from_slice(&b[2..]);
        assert!(parse_markers(&with).unwrap().icc_invalid);
        assert!(srgb8_fit(&b, 8, 8).unwrap().is_some());
        assert!(srgb8_fit(&with, 8, 8).unwrap().is_none());
    }

    /// The 8-bit path refuses an APP1 flood before its decoders walk it again, as the general one does.
    #[test]
    fn an_app1_flood_is_refused_by_the_8_bit_path_too() {
        let b = jpeg(16, 16, |x, y| [x as u8, y as u8, 0]);
        let mut flood = b[..2].to_vec();
        for _ in 0..200 {
            flood.extend_from_slice(&[0xFF, 0xE1, 0x00, 0x02]);
        }
        flood.extend_from_slice(&b[2..]);
        match srgb8_fit(&flood, 8, 8) {
            Err(Error::Malformed(_, m)) => assert!(m.contains("APP1"), "{m}"),
            other => panic!("unexpected: {other:?}"),
        }
    }
}