diff --git a/crates/raw/README.md b/crates/raw/README.md index 2a4e0ec4ee..81ef81130d 100644 --- a/crates/raw/README.md +++ b/crates/raw/README.md @@ -55,7 +55,7 @@ One module and one Cargo feature per maker or container family. All are on by de | `crw` | Canon CRW (CIFF) | Decoded by [`craft-crw`](../crw/README.md), the one CRW decoder, and returned through this crate's types | | `cr2` | Canon CR2 | The lossless-JPEG frame of IFD 3 de-sliced (`0xc640`); the layout from `CR2CFAPattern` (`0xc5e0`); image area from `SensorInfo`; black measured on the masked columns; white from the saturation plateau; as-shot white balance from `ColorData` or `WBInfo`; the white-balance presets at their temperatures (`ColorData::kelvin_presets`). sRAW / mRAW (subsampled YCbCr, 4:2:2 and 4:2:0) come out as linear white-balanced RGB with three samples per pixel, the chroma gains measured against the file's own 16-bit RGB image in IFD 2 | | `nef` | Nikon NEF / NRW | Uncompressed (16-bit words, 12-bit packed, Coolpix word packing), the packed layouts labelled compression 34713, Nikon's Huffman coding (lossless and lossy types, 12 and 14 bits, "lossy after split") with the `0x0096` linearisation table, small raw (YCbCr 4:2:2) as linear white-balanced RGB; black from `0x003d` or the NRW block, as-shot white balance from `0x000c`, `CropArea` or the measured image area of bodies that record none, masked trailing columns left out of the active area, the D1X pixel aspect, and the in-file distortion correction as a `WarpRectilinear` opcode ([derivation](docs/nikon-lens-corrections.md)). High Efficiency (HE / HE★) is refused by name; the previews still read | -| `rw2` | Panasonic RW2, Leica RWL, older Panasonic / Leica RAW | Packed blocks (formats 2, 5, 7), formats 4 and 6, format 8 prefix-coded strips and tiles, 16-bit words; black per colour from `0x001c`–`0x001e` (plus the 15 counts formats 4 and 6 store above it, 15 when no black is recorded); white from the saturation plateau; sensor borders as the active area and the in-camera aspect crop; mapped-out defect pixels filled; as-shot white balance from `0x0024`–`0x0026`; the in-camera distortion correction (`0x0119`) as a `WarpRectilinear` opcode. Raw formats 1 and 3 and compression 34826 are refused (no samples of them) | +| `rw2` | Panasonic RW2, Leica RWL, older Panasonic / Leica RAW | Packed blocks (formats 2, 5, 7), formats 4 and 6, format 8 prefix-coded strips and tiles (a sample outside the bit depth makes the strip corrupt), 16-bit words; black per colour from `0x001c`–`0x001e` (plus the 15 counts formats 4 and 6 store above it, 15 when no black is recorded); white from the saturation plateau; sensor borders as the active area and the in-camera aspect crop; mapped-out defect pixels filled; as-shot white balance from `0x0024`–`0x0026`; the in-camera distortion correction (`0x0119`) as a `WarpRectilinear` opcode. Raw formats 1 and 3 and compression 34826 are refused (no samples of them) | | `mrw` | Minolta / Konica Minolta MRW | The `\0MRM` block container (`PRD`, `WBG`, `TTW`), 12-bit packed and 16-bit word mosaics, as-shot white balance from `WBG`, white from the saturation plateau | | `erf` | Epson ERF (R-D1, R-D1s, R-D1x) | 12-bit samples in 16-byte blocks, black levels from the maker note, the measured picture origin. No white balance (the file states none) | | `mef` | Mamiya MEF (ZD) | Uncompressed 12-bit packed; default crop from the RGB preview's measured window. No white balance (the file states none) | diff --git a/crates/raw/src/rw2.rs b/crates/raw/src/rw2.rs index cd2260db06..9cee448116 100644 --- a/crates/raw/src/rw2.rs +++ b/crates/raw/src/rw2.rs @@ -458,7 +458,7 @@ fn code_table(pairs: &[(u32, u32)]) -> Result { } /// Decode one strip into its column range of each output row. -fn decode_strip(s: &Strip, codes: &Huffman, mut rows: Vec<&mut [u16]>) -> Result<()> { +fn decode_strip(s: &Strip, codes: &Huffman, max: i32, mut rows: Vec<&mut [u16]>) -> Result<()> { // the stream's bits are taken from each byte starting at bit 0: reversed, it is an ordinary MSB-first stream let reversed: Vec = s.data.iter().map(|b| b.reverse_bits()).collect(); let mut bits = Bits::new(&reversed); @@ -469,8 +469,12 @@ fn decode_strip(s: &Strip, codes: &Huffman, mut rows: Vec<&mut [u16]>) -> Result for (k, v) in cell.iter_mut().enumerate() { let d = diff(&mut bits, codes).ok_or_else(|| RawError::Corrupt(format!("RW2 format 8: invalid code in row {}", 2 * pair)))?; *v = v.saturating_add(d); + // a value outside the sample depth is a corrupt stream, not a sample to clamp + let sample = u16::try_from(*v).ok().filter(|_| *v <= max); + let sample = + sample.ok_or_else(|| RawError::Corrupt(format!("RW2 format 8: sample {} outside the {max} range in row {}", *v, 2 * pair)))?; if let Some(px) = rows.get_mut(2 * pair + k % 2).and_then(|r| r.get_mut(2 * cx + k / 2)) { - *px = (*v).clamp(0, u16::MAX as i32) as u16; + *px = sample; } } if cx == 0 { @@ -484,7 +488,9 @@ fn decode_strip(s: &Strip, codes: &Huffman, mut rows: Vec<&mut [u16]>) -> Result Ok(()) } -fn decode_strips(strips: &[Strip], codes: &Huffman, w: usize, out: &mut [u16]) -> Result<()> { +/// `bits`: the sample depth; a decoded value above it (or below 0) makes the strip corrupt. +fn decode_strips(strips: &[Strip], codes: &Huffman, w: usize, bits: u32, out: &mut [u16]) -> Result<()> { + let max = (1i32 << bits.clamp(1, 16)) - 1; let mut parts: Vec> = strips.iter().map(|s| Vec::with_capacity(s.height)).collect(); for (y, row) in out.chunks_mut(w).enumerate() { // the strips crossing this row, left to right, each take their columns off its front @@ -503,7 +509,7 @@ fn decode_strips(strips: &[Strip], codes: &Huffman, w: usize, out: &mut [u16]) - x = s.left + s.width; } } - strips.par_iter().zip(parts).try_for_each(|(s, rows)| decode_strip(s, codes, rows)) + strips.par_iter().zip(parts).try_for_each(|(s, rows)| decode_strip(s, codes, max, rows)) } /// Replace samples of 0 inside `area` (mapped-out defects) by the mean of the non-zero samples of the same colour two @@ -591,7 +597,7 @@ pub(crate) fn decode(bytes: &[u8], mode: Mode, limits: &Limits) -> Result