mirror of
https://github.com/storytold/photocraft.git
synced 2026-11-19 08:26:19 +00:00
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 <noreply@anthropic.com>
This commit is contained in:
1 parent
319cb299d2
commit
dcf5c61a64
5 files changed
+69
-4
No files matched your search
@@ -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 |
|
||||
|
||||
@@ -234,7 +234,7 @@ pub(crate) fn decode(t: &Tiff, limits: &Limits) -> Result<Sensor> {
|
||||
// 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),
|
||||
|
||||
@@ -244,6 +244,11 @@ pub(crate) struct Opcodes {
|
||||
pub skipped: Vec<String>,
|
||||
}
|
||||
|
||||
/// 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
|
||||
|
||||
@@ -136,6 +136,8 @@ pub fn embedded_jxl_preview(bytes: &[u8], limits: &Limits) -> Option<DecodedPrev
|
||||
.into_iter()
|
||||
.filter(|i| t.tag_uint(i, tag::NEW_SUBFILE_TYPE) == Some(1))
|
||||
.filter(|i| t.tag_uint(i, tag::COMPRESSION) == Some(JPEG_XL) && t.tag_uint(i, tag::SAMPLES_PER_PIXEL) == Some(3))
|
||||
// A rendered RGB (or YCbCr) picture, not a reduced-resolution LinearRaw or CFA image.
|
||||
.filter(|i| matches!(t.tag_uint(i, tag::PHOTOMETRIC), Some(2 | 6)))
|
||||
.collect();
|
||||
previews.sort_by_key(|i| std::cmp::Reverse(area(i)));
|
||||
let (ifd, plane) = previews.into_iter().find_map(|i| read_plane(&t, &i, limits, JpegLayout::Flat).ok().map(|p| (i, p)))?;
|
||||
|
||||
@@ -850,6 +850,57 @@ fn a_jpeg_xl_preview_that_does_not_decode_gives_way_to_a_smaller_one() {
|
||||
assert_eq!(p.rgb, small);
|
||||
}
|
||||
|
||||
/// Only a rendered RGB picture is a preview: a reduced-resolution LinearRaw image (NewSubFileType 1,
|
||||
/// three samples, JPEG XL) holds undeveloped sensor data, so the RGB one is used even when smaller.
|
||||
#[test]
|
||||
fn a_reduced_resolution_linear_raw_is_not_a_jpeg_xl_preview() {
|
||||
use photocraft_raw::testgen::TiffBuilder;
|
||||
let mut t = TiffBuilder::default();
|
||||
let mut ifd = |w: usize, h: usize, photometric: u16, data: &[u16]| {
|
||||
let stream = jxl(data, w, h, 3, 8);
|
||||
let len = stream.len() as u32;
|
||||
let strip = t.blob(stream);
|
||||
t.ifd(vec![
|
||||
(254, Val::Long(vec![1])),
|
||||
(256, Val::Long(vec![w as u32])),
|
||||
(257, Val::Long(vec![h as u32])),
|
||||
(258, Val::Short(vec![8, 8, 8])),
|
||||
(259, Val::Short(vec![52546])),
|
||||
(262, Val::Short(vec![photometric])),
|
||||
(273, Val::Blobs(vec![strip])),
|
||||
(277, Val::Short(vec![3])),
|
||||
(278, Val::Long(vec![h as u32])),
|
||||
(279, Val::Long(vec![len])),
|
||||
])
|
||||
};
|
||||
let small = noise(20 * 15 * 3, 8);
|
||||
let linear_raw = ifd(40, 30, 34892, &noise(40 * 30 * 3, 8));
|
||||
let rgb = ifd(20, 15, 2, &small);
|
||||
let ifd0 = t.ifd(vec![(254, Val::Long(vec![1])), (330, Val::Ifds(vec![linear_raw, rgb])), (50706, Val::Byte(vec![1, 7, 0, 0]))]);
|
||||
t.chain = vec![ifd0];
|
||||
let p = embedded_jxl_preview(&t.build(), &Limits::default()).expect("the 20×15 RGB preview");
|
||||
assert_eq!((p.width, p.height), (20, 15));
|
||||
assert_eq!(p.rgb, small);
|
||||
}
|
||||
|
||||
/// A WhiteLevel too large for the sensor's levels (a DOUBLE past f32::MAX) is ignored, as a zero
|
||||
/// one is: the samples keep the default white instead of an infinite one that blacks them out.
|
||||
#[test]
|
||||
fn an_infinite_white_level_falls_back_to_the_default() {
|
||||
let (w, h) = (24, 16);
|
||||
let pixels: Vec<u16> = 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);
|
||||
|
||||
Reference in new issue
Block a user