From dcf5c61a64716bec507ee79735ef18ce0837fbce Mon Sep 17 00:00:00 2001 From: David Mann Date: Sun, 25 Oct 2026 14:55:45 +0100 Subject: [PATCH] Integration: cap MapPolynomial opcodes, finite WhiteLevel, RGB-only JPEG XL preview (#8402) - An opcode list may hold up to 4096 opcodes, and every MapPolynomial is checked for every sample, so a crafted DNG with thousands of whole-image polynomials (a few hundred KB) hung the open for minutes. Keep at most 64 per list; the rest are named as not applied. - A DOUBLE WhiteLevel past f32::MAX became an infinite white, which blacked out that channel. A non-finite level now falls back to the default, as a zero one does. - A reduced-resolution LinearRaw IFD (NewSubFileType 1, three samples, JPEG XL) was a JPEG XL preview candidate, and its undeveloped samples would show as an sRGB picture. Previews now need PhotometricInterpretation RGB or YCbCr. - README: floating-point JPEG XL raw data and OpcodeList2 MapPolynomial are supported. Co-authored-by: SBaksa <18648812+SBaksa@users.noreply.github.com> Co-Authored-By: Claude Opus 5.5 --- crates/raw/README.md | 4 +-- crates/raw/src/dng.rs | 2 +- crates/raw/src/opcodes.rs | 14 ++++++++++- crates/raw/src/preview.rs | 2 ++ crates/raw/tests/it/dng.rs | 51 ++++++++++++++++++++++++++++++++++++++ 5 files changed, 69 insertions(+), 4 deletions(-) diff --git a/crates/raw/README.md b/crates/raw/README.md index e266c64cde..268e200b7f 100644 --- a/crates/raw/README.md +++ b/crates/raw/README.md @@ -67,8 +67,8 @@ camera colour tables were copied. | Format | Status | |---|---| -| DNG | Uncompressed (8–16 bit, packed or not), Deflate (compression 8, predictors 1 and 2), lossless JPEG, lossy JPEG (DNG 1.4 compression 34892: 8-bit baseline JPEG tiles or strips, gray for CFA, YCbCr-coded RGB for LinearRaw; decoded by `zune-jpeg`, the stored 8-bit values then go through the same LinearizationTable / BlackLevel / WhiteLevel as every other depth) and JPEG XL (DNG 1.7, e.g. Samsung Expert RAW; decoded by `jxl-oxide`; not in wasm32 builds, where it is `Unsupported`); strips and tiles; CFA (Bayer) and LinearRaw (three-sample colour, or one-sample monochrome such as Leica Monochrom and film-scanner DNGs, developed as neutral grey with no white balance or colour matrix); LinearizationTable, BlackLevel (+ repeat, DeltaH/V), WhiteLevel, ActiveArea, DefaultCrop, ColorMatrix1/2, CameraCalibration, ForwardMatrix, AnalogBalance, AsShotNeutral / AsShotWhiteXY, BaselineExposure, Orientation, OpcodeList2 GainMap (lens shading) | -| DNG (lossy JPEG, floating point; opcodes other than GainMap; ProfileGainTableMap) | Unsupported / not applied (reported) | +| DNG | Uncompressed (8–16 bit, packed or not), Deflate (compression 8, predictors 1 and 2), lossless JPEG, lossy JPEG (DNG 1.4 compression 34892: 8-bit baseline JPEG tiles or strips, gray for CFA, YCbCr-coded RGB for LinearRaw; decoded by `zune-jpeg`, the stored 8-bit values then go through the same LinearizationTable / BlackLevel / WhiteLevel as every other depth) and JPEG XL (DNG 1.7, e.g. Samsung Expert RAW; decoded by `jxl-oxide`; not in wasm32 builds, where it is `Unsupported`); strips and tiles; CFA (Bayer) and LinearRaw (three-sample colour, or one-sample monochrome such as Leica Monochrom and film-scanner DNGs, developed as neutral grey with no white balance or colour matrix); LinearizationTable, BlackLevel (+ repeat, DeltaH/V), WhiteLevel, ActiveArea, DefaultCrop, ColorMatrix1/2, CameraCalibration, ForwardMatrix, AnalogBalance, AsShotNeutral / AsShotWhiteXY, BaselineExposure, Orientation, floating-point JPEG XL raw data (DNG 1.7 lossy LinearRaw, as Adobe DNG Converter writes it; scaled by the highest WhiteLevel), OpcodeList2 GainMap (lens shading) and MapPolynomial | +| DNG (floating point other than JPEG XL; opcodes other than OpcodeList2 GainMap and MapPolynomial; ProfileGainTableMap) | Unsupported / not applied (reported) | | CR2 | Lossless JPEG with slices, borders and as-shot white balance from the maker note, black measured on the masked border. CR2 has no CFA tag and the row phase varies by model, so it is measured from the data (the green diagonal), with a Canon model-ID table as the fallback (see `src/cr2.rs`) | | CR2 sRAW / mRAW | Unsupported | | NEF / NRW, ARW, PEF and other TIFF/EP raws | Uncompressed and lossless-JPEG (incl. Sony lossless ARW) CFA data | diff --git a/crates/raw/src/dng.rs b/crates/raw/src/dng.rs index cc7f77fb69..8329a80d39 100644 --- a/crates/raw/src/dng.rs +++ b/crates/raw/src/dng.rs @@ -234,7 +234,7 @@ pub(crate) fn decode(t: &Tiff, limits: &Limits) -> Result { // Floating-point levels are in the floats' units: scale them as the samples were. let unit = plane.float_scale.unwrap_or(1.0); let wl = t.tag_floats(&raw, tag::WHITE_LEVEL); - let white = [0, 1, 2].map(|s| wl.get(s).or_else(|| wl.first()).map(|v| *v as f32 * unit).filter(|v| *v > 0.0).unwrap_or(white_default)); + let white = [0, 1, 2].map(|s| wl.get(s).or_else(|| wl.first()).map(|v| *v as f32 * unit).filter(|v| v.is_finite() && *v > 0.0).unwrap_or(white_default)); let (rows, cols) = match t.tag_uints(&raw, tag::BLACK_LEVEL_REPEAT_DIM).as_slice() { [r, c] if (1..=16).contains(r) && (1..=16).contains(c) => (*r as usize, *c as usize), diff --git a/crates/raw/src/opcodes.rs b/crates/raw/src/opcodes.rs index dff2f53b9b..9b333fd4cf 100644 --- a/crates/raw/src/opcodes.rs +++ b/crates/raw/src/opcodes.rs @@ -244,6 +244,11 @@ pub(crate) struct Opcodes { pub skipped: Vec, } +/// The most MapPolynomial opcodes kept from one list. Real files carry one per plane; every +/// polynomial is checked for every sample, so a crafted list of thousands would hang the open. +/// Those past the cap are named as not applied. +const MAX_POLYNOMIALS: usize = 64; + /// Parses an opcode list. pub(crate) fn parse(list: &[u8]) -> Opcodes { let mut out = Opcodes::default(); @@ -255,7 +260,7 @@ pub(crate) fn parse(list: &[u8]) -> Opcodes { let Some(params) = list.get(start..start.saturating_add(size as usize)) else { break }; let applied = match id { 9 => gain_map(params).map(|m| out.gain_maps.push(m)).is_some(), - 8 => map_polynomial(params).map(|p| out.polynomials.push(p)).is_some(), + 8 if out.polynomials.len() < MAX_POLYNOMIALS => map_polynomial(params).map(|p| out.polynomials.push(p)).is_some(), _ => false, }; if !applied { @@ -357,6 +362,13 @@ mod tests { } } + #[test] + fn map_polynomials_past_the_cap_are_named_not_applied() { + let ops = parse(&list(&vec![(8, polynomial_params(1, 1, &[0.0, 1.0])); MAX_POLYNOMIALS + 1])); + assert_eq!(ops.polynomials.len(), MAX_POLYNOMIALS); + assert_eq!(ops.skipped, vec!["MapPolynomial".to_string()]); + } + #[test] fn at_rv_matches_row_then_at() { // at_rv (direct 4-corner bilinear) must equal the old row() + at() path for a range of diff --git a/crates/raw/src/preview.rs b/crates/raw/src/preview.rs index 933c2df47d..020539a386 100644 --- a/crates/raw/src/preview.rs +++ b/crates/raw/src/preview.rs @@ -136,6 +136,8 @@ pub fn embedded_jxl_preview(bytes: &[u8], limits: &Limits) -> Option = scene(w, h).iter().flat_map(|p| p.map(|v| (v * 20000.0) as u16)).collect(); + let mut ints = DngSpec::cfa(w, h, pixels.clone()); + ints.samples = 3; + ints.storage = DngStorage::Strips { rows: h }; + ints.white = 0; + ints.raw_extra.push((50717, Val::Double(vec![1e300; 3]))); + assert_eq!(sensor(&ints.build()).white, [65535.0; 3], "integer data"); + let float = float_jxl_dng(&pixels, w, h, Some(1e300)).build(); + assert_eq!(sensor(&float).white, [65535.0; 3], "floating-point data"); + let default = develop(&float_jxl_dng(&pixels, w, h, None).build(), &DevelopOptions::default()).unwrap(); + assert_eq!(develop(&float, &DevelopOptions::default()).unwrap().rgb, default.rgb); +} + #[test] fn jpeg_xl_mismatches_are_errors() { let (w, h) = (16, 8);