diff --git a/crates/metadata/src/cr3.rs b/crates/metadata/src/cr3.rs index a7538e88b..409a69a3b 100644 --- a/crates/metadata/src/cr3.rs +++ b/crates/metadata/src/cr3.rs @@ -351,14 +351,25 @@ fn trak(bytes: &[u8], t: &BoxRef, budget: &mut usize) -> Option { } } let stbl = stbl?; - let (mut kind, mut size, mut offset) = (None, None, None); + let (mut kind, mut size, mut offset, mut mapping, mut sample_count) = (None, None, None, None, 0); for b in boxes(bytes, stbl.body, stbl.end, budget) { let body = bytes.get(b.body..b.end)?; match &b.kind { // full box: version/flags, entry count, entries b"stsd" if be32(body, 4)? > 0 => kind = boxes(bytes, b.body + 8, b.end, budget).first().map(|e| sample_entry(bytes, e, budget)), + b"stsc" => { + // Only the first sample is exposed. It must map chunk 1 to + // the first sample description; don't trust offsets without + // that mapping, or a damaged/raw track could become a JPEG. + mapping = None; + let count = be32(body, 4)? as usize; + if be32(body, 0)? == 0 && count > 0 && count <= body.len().saturating_sub(8) / 12 { + mapping = Some((be32(body, 8)?, be32(body, 12)?, be32(body, 16)?)); + } + } // version/flags, sample_size (0 = per-sample table), count, sizes b"stsz" => { + sample_count = be32(body, 8)?; size = match be32(body, 4)? { 0 if be32(body, 8)? > 0 => be32(body, 12), 0 => None, @@ -371,8 +382,12 @@ fn trak(bytes: &[u8], t: &BoxRef, budget: &mut usize) -> Option { _ => {} } } - let data = match (offset, size) { - (Some(o), Some(s)) if o.checked_add(s as usize).is_some_and(|e| e <= bytes.len()) => Some((o, s as usize)), + let data = match (offset, size, mapping) { + (Some(o), Some(s), Some((1, samples, 1))) + if samples > 0 && samples <= sample_count && o.checked_add(s as usize).is_some_and(|e| e <= bytes.len()) => + { + Some((o, s as usize)) + } _ => None, }; Some(Cr3Track { kind: kind?, data }) @@ -502,7 +517,8 @@ mod tests { stsz.extend(size.to_be_bytes()); let mut co64 = 1u32.to_be_bytes().to_vec(); co64.extend(offset.to_be_bytes()); - let stbl = [full(b"stsd", &stsd), full(b"stsz", &stsz), full(b"co64", &co64)].concat(); + let stsc = [1u32, 1, 1, 1].into_iter().flat_map(u32::to_be_bytes).collect::>(); + let stbl = [full(b"stsd", &stsd), full(b"stsc", &stsc), full(b"stsz", &stsz), full(b"co64", &co64)].concat(); bx(b"trak", &bx(b"mdia", &bx(b"minf", &bx(b"stbl", &stbl)))) } fn tiff_with(tag: u16, v: craft_tiff::Value) -> Vec { @@ -543,6 +559,34 @@ mod tests { assert_eq!(c.tracks[1].data, Some((4112, 32))); } + #[test] + fn first_sample_requires_a_complete_valid_sample_to_chunk_mapping() { + let file = |track: Vec| { + let mut file = bx(b"ftyp", b"crx \0\0\0\x01crx isom"); + file.extend(bx(b"moov", &track)); + file.resize(4112, 0); + file + }; + let valid = trak(craw(16, 16, &bx(b"JPEG", &[0; 4])), 4096, 16); + assert_eq!(parse_cr3(&file(valid.clone())).unwrap().tracks[0].data, Some((4096, 16))); + let at = valid.windows(4).position(|bytes| bytes == b"stsc").unwrap(); + // Missing table, impossible first chunk, zero samples, a different + // description and a count that extends beyond this box all deny data. + for (field, value) in [(0, u32::from_be_bytes(*b"xxxx")), (8, 0), (8, 2), (12, 0), (12, 2), (16, 0), (16, 2), (4, u32::MAX)] { + let mut damaged = valid.clone(); + let offset = if field == 0 { at } else { at + 4 + field }; + damaged[offset..offset + 4].copy_from_slice(&value.to_be_bytes()); + let bytes = file(damaged); + let parsed = parse_cr3(&bytes).unwrap(); + assert!(parsed.tracks.is_empty() || parsed.tracks[0].data.is_none()); + } + for entry in [craw(16, 16, &[]), craw(16, 16, &bx(b"CMP1", &[0xff, 0x10, 0, 0x30]))] { + let bytes = file(trak(entry, 4096, 16)); + let parsed = parse_cr3(&bytes).unwrap(); + assert!(!matches!(parsed.tracks[0].kind, Cr3TrackKind::Jpeg)); + } + } + #[test] fn hevc_preview_is_distinct_from_an_equal_size_raw_track() { let preview = trak(craw(6000, 4000, &bx(b"HEVC", &[0; 4])), 4096, 16); diff --git a/crates/raw/src/arw.rs b/crates/raw/src/arw.rs index 18273866c..3d531ec30 100644 --- a/crates/raw/src/arw.rs +++ b/crates/raw/src/arw.rs @@ -629,6 +629,19 @@ fn raw_ifd(tiff: &Tiff) -> Option<&Ifd> { .max_by_key(|i| i.u64(t::IMAGE_WIDTH).unwrap_or(0).saturating_mul(i.u64(t::IMAGE_LENGTH).unwrap_or(0))) } +fn valid_arw2_curve(curve: &[u64]) -> bool { + curve.len() == 4 && curve.iter().all(|&v| v <= 16383) && curve.windows(2).all(|v| (v[0] >> 2) < (v[1] >> 2)) +} + +fn declared_arw2_curve(raw: &Ifd, compression: u16, one_byte_per_sample: bool) -> Result>> { + let curve = raw.u64s(TONE_CURVE); + if compression == 32767 && one_byte_per_sample && curve.as_ref().is_some_and(|curve| !valid_arw2_curve(curve)) { + // Reordering or clamping malformed breakpoints fabricates a different transfer curve. + return Err(RawError::Unsupported("Sony ARW2 with an invalid tone curve".into())); + } + Ok(curve) +} + pub(crate) fn decode(bytes: &[u8], mode: Mode, limits: &Limits) -> Result { let tiff = Tiff::parse(bytes)?; let ifd0 = tiff.ifds.first().ok_or_else(|| RawError::Corrupt("TIFF without IFDs".into()))?; @@ -645,6 +658,7 @@ pub(crate) fn decode(bytes: &[u8], mode: Mode, limits: &Limits) -> Result (RawData::U16(read_ycbcr_tiles(bytes, &info, raw, mode, limits)?), 14), @@ -726,7 +740,10 @@ pub(crate) fn decode(bytes: &[u8], mode: Mode, limits: &Limits) -> Result Cfa::bayer_static("RGGB"), }; - let default_black = if scale_bits >= 14 { 512.0 } else { 128.0 }; + let curve_black = (info.compression == 32767 && one_byte_per_sample) + .then(|| declared_curve.as_ref().and_then(|curve| code_curve(curve).get(256).copied()).map(f32::from)) + .flatten(); + let default_black = curve_black.unwrap_or(if scale_bits >= 14 { 512.0 } else { 128.0 }); // the decrypted SR2SubIFD, read once and only when a plain black level or white balance is missing let sr2_cell = std::cell::OnceCell::new(); let sr2 = || sr2_cell.get_or_init(|| crate::sr2::SubIfd::read(bytes, ifd0, tiff.order)).as_ref(); @@ -741,7 +758,7 @@ pub(crate) fn decode(bytes: &[u8], mode: Mode, limits: &Limits) -> Result (BlackLevel::uniform(default_black + YCBCR_OFFSET), crate::LevelSource::Fallback), _ => match (scale_bits >= 14).then(|| sr2().and_then(sr2_black)).flatten() { Some(b) => (BlackLevel::uniform(b), crate::LevelSource::Metadata), - None => (BlackLevel::uniform(default_black), crate::LevelSource::Fallback), + None => (BlackLevel::uniform(default_black), curve_black.map_or(crate::LevelSource::Fallback, |_| crate::LevelSource::Metadata)), }, }; // ARW6 samples are in 2 x 14-bit units, so the file's 14-bit levels double; without the tag the white is the @@ -1111,6 +1128,25 @@ mod tests { let _ = code_curve(&[60000, 1, 0, 0]); } + #[test] + fn arw2_rejects_non_increasing_declared_curve_in_header_and_full_decode() { + let valid = arw_with_strip(32 * 4); + assert!(crate::decode(&valid).is_ok()); + let mut damaged = valid.clone(); + let tiff = Tiff::parse(&damaged).unwrap(); + let curve = raw_ifd(&tiff).unwrap().get(TONE_CURVE).unwrap(); + let at = curve.offset as usize; + for (dst, value) in damaged[at..at + 8].as_chunks_mut::<2>().0.iter_mut().zip([9000u16, 8000, 12000, 13000]) { + dst.copy_from_slice(&value.to_le_bytes()); + } + for mode in [Mode::Header, Mode::Full] { + assert!(matches!(decode(&damaged, mode, &Limits::default()), Err(RawError::Unsupported(why)) if why.contains("curve"))); + let image = decode(&valid, mode, &Limits::default()).unwrap(); + assert_eq!(image.black.mean(), 512.0); + assert_eq!(image.level_sources.black, crate::LevelSource::Metadata, "the declared curve maps the black code"); + } + } + #[test] fn decipher_inverts_the_cube_substitution() { let mut seen = [false; 256]; diff --git a/crates/raw/src/cr3.rs b/crates/raw/src/cr3.rs index 8f2bbd27e..f875cbff1 100644 --- a/crates/raw/src/cr3.rs +++ b/crates/raw/src/cr3.rs @@ -414,7 +414,8 @@ mod tests { let mut co64 = vec![0u8; 4]; co64.extend(1u32.to_be_bytes()); co64.extend(offset.to_be_bytes()); - let tables = [box_bytes(b"stsd", &stsd), box_bytes(b"stsz", &stsz), box_bytes(b"co64", &co64)].concat(); + let stsc = [0u32, 1, 1, 1, 1].into_iter().flat_map(u32::to_be_bytes).collect::>(); + let tables = [box_bytes(b"stsd", &stsd), box_bytes(b"stsc", &stsc), box_bytes(b"stsz", &stsz), box_bytes(b"co64", &co64)].concat(); box_bytes(b"trak", &box_bytes(b"mdia", &box_bytes(b"minf", &box_bytes(b"stbl", &tables)))) } diff --git a/crates/raw/src/dng.rs b/crates/raw/src/dng.rs index fea3d12a3..aaa08e49a 100644 --- a/crates/raw/src/dng.rs +++ b/crates/raw/src/dng.rs @@ -178,7 +178,15 @@ pub(crate) fn is_plain_cfa_tiff(tiff: &Tiff, bytes: &[u8]) -> bool { if !raw.contains(t::AS_SHOT_NEUTRAL) && !tiff.ifds.first().is_some_and(|i| i.contains(t::AS_SHOT_NEUTRAL)) { return false; } - let Ok(info) = raw.image() else { return false }; + let Ok(info) = raw.image() else { + // Recognition must not turn a damaged, otherwise supported plain CFA + // image into an unsupported container. Let decode_as report its layout + // error; these tags alone never decompress or allocate pixel samples. + return raw.u16(t::COMPRESSION).unwrap_or(1) == t::compression::NONE + && raw.u16(t::SAMPLES_PER_PIXEL).unwrap_or(1) == 1 + && (1..=16).contains(&raw.u16(t::BITS_PER_SAMPLE).unwrap_or(1)) + && raw.u16(t::SAMPLE_FORMAT).unwrap_or(1) != 3; + }; if info.samples_per_pixel != 1 || !(1..=16).contains(&info.bits()) || info.sample_format == 3 { return false; } @@ -656,3 +664,7 @@ mod tests { assert!(deinterleave(&mut hostile, 8, 6, 1, 1 << 20, 1).is_err()); } } + +#[cfg(all(test, feature = "hasselblad"))] +#[path = "dng/probe_review_tests.rs"] +mod probe_review_tests; diff --git a/crates/raw/src/dng/probe_review_tests.rs b/crates/raw/src/dng/probe_review_tests.rs new file mode 100644 index 000000000..8e54e8db6 --- /dev/null +++ b/crates/raw/src/dng/probe_review_tests.rs @@ -0,0 +1,64 @@ +use super::*; +use craft_tiff::{IfdBuilder, ImageData, TiffWriter}; + +#[allow(clippy::unwrap_used)] // Test fixture construction must fail the test if its independent writer fails. +fn plain() -> Vec { + let mut raw = IfdBuilder::new(); + for (tag, value) in [ + (t::IMAGE_WIDTH, Value::Long(vec![2])), + (t::IMAGE_LENGTH, Value::Long(vec![2])), + (t::BITS_PER_SAMPLE, Value::Short(vec![16])), + (t::SAMPLES_PER_PIXEL, Value::Short(vec![1])), + (t::PHOTOMETRIC, Value::Short(vec![photometric::CFA])), + (t::COMPRESSION, Value::Short(vec![1])), + (t::AS_SHOT_NEUTRAL, Value::Rational(vec![(1, 2), (1, 1), (2, 3)])), + ] { + raw.set(tag, value); + } + raw.set_image(ImageData::Strips { rows_per_strip: 2, strips: vec![[17u16, 31, 53, 79].into_iter().flat_map(u16::to_le_bytes).collect()] }); + TiffWriter::new(ByteOrder::Little, false).write(&[raw]).unwrap() +} + +#[allow(clippy::unwrap_used)] // Only known tags in this module's synthetic TIFF are inspected. +fn entry(bytes: &[u8], tag: u16) -> usize { + let at = u32::from_le_bytes(bytes[4..8].try_into().unwrap()) as usize; + let count = u16::from_le_bytes(bytes[at..at + 2].try_into().unwrap()) as usize; + (0..count).map(|i| at + 2 + i * 12).find(|&i| u16::from_le_bytes(bytes[i..i + 2].try_into().unwrap()) == tag).unwrap() +} + +#[test] +fn plain_cfa_probe_keeps_malformed_geometry_and_tables_in_the_supported_decoder() { + let valid = plain(); + assert_eq!(crate::probe(&valid), Some(RawFormat::CfaTiff)); + let decoded = crate::decode(&valid).unwrap(); + assert_eq!(decoded.data, RawData::U16(vec![17, 31, 53, 79])); + for tag in [t::IMAGE_WIDTH, t::IMAGE_LENGTH] { + let mut damaged = valid.clone(); + let at = entry(&damaged, tag); + damaged[at + 8..at + 12].copy_from_slice(&0u32.to_le_bytes()); + assert_eq!(crate::probe(&damaged), Some(RawFormat::CfaTiff)); + assert!(matches!(crate::probe_info(&damaged), Err(RawError::Tiff(_))), "zero geometry remains malformed before pixel decode"); + assert!(matches!(crate::decode(&damaged), Err(RawError::Tiff(_)))); + } + let mut missing = valid.clone(); + let at = entry(&missing, t::STRIP_OFFSETS); + missing[at..at + 2].copy_from_slice(&0xf123u16.to_le_bytes()); + assert_eq!(crate::probe(&missing), Some(RawFormat::CfaTiff)); + assert!(matches!(crate::probe_info(&missing), Err(RawError::Tiff(_)))); + assert!(matches!(crate::decode(&missing), Err(RawError::Tiff(_)))); +} + +#[test] +fn plain_cfa_probe_does_not_claim_unknown_tiff_codings_or_missing_color_contracts() { + for tag in [t::AS_SHOT_NEUTRAL, t::COMPRESSION] { + let mut unknown = plain(); + let at = entry(&unknown, tag); + if tag == t::COMPRESSION { + unknown[at + 8..at + 10].copy_from_slice(&65000u16.to_le_bytes()); + } else { + unknown[at..at + 2].copy_from_slice(&0xf123u16.to_le_bytes()); + } + assert_eq!(crate::probe(&unknown), Some(RawFormat::OtherTiff)); + assert!(matches!(crate::decode(&unknown), Err(RawError::Unsupported(_)))); + } +} diff --git a/crates/raw/src/nef.rs b/crates/raw/src/nef.rs index b903f308b..d08d39aae 100644 --- a/crates/raw/src/nef.rs +++ b/crates/raw/src/nef.rs @@ -13,7 +13,7 @@ //! refused. use crate::nefc; -use crate::shared::white_from_data; +use crate::shared::white_from_data_sourced; use crate::tiffraw::{Packing, check_image, read_image}; use crate::{BlackLevel, Camera, Cfa, ColorData, Limits, Mode, OpcodeLists, Orientation, RawData, RawError, RawFormat, RawImage, Rect, Result}; use craft_tiff::image::ImageInfo; @@ -266,14 +266,23 @@ pub(crate) fn decode(bytes: &[u8], mode: Mode, limits: &Limits) -> Result BlackLevel { - repeat_rows: 2, - repeat_cols: 2, - values: [a, b, c, d].iter().map(|v| (**v * black_scale) as f32).collect(), - ..Default::default() + let (black, black_source) = match mn.as_ref().and_then(|m| m.ifd.f64s(BLACK_LEVEL)).as_deref() { + Some([a, b, c, d]) if [a, b, c, d].iter().all(|v| **v < 16384.0) => ( + BlackLevel { + repeat_rows: 2, + repeat_cols: 2, + values: [a, b, c, d].iter().map(|v| (**v * black_scale) as f32).collect(), + ..Default::default() + }, + crate::LevelSource::Metadata, + ), + _ => match nrw_black(mn.as_ref(), bits) { + Some(value) => (BlackLevel::uniform(value), crate::LevelSource::Metadata), + None => { + let (value, source) = be16_black(&info, bytes.len() as u64, mn.as_ref(), samples); + (BlackLevel::uniform(value), source) + } }, - _ => BlackLevel::uniform(nrw_black(mn.as_ref(), bits).unwrap_or_else(|| be16_black(&info, bytes.len() as u64, mn.as_ref(), samples))), }; let wb = mn .as_ref() @@ -291,10 +300,10 @@ pub(crate) fn decode(bytes: &[u8], mode: Mode, limits: &Limits) -> Result white, - (None, Mode::Full) => white_from_data(samples, bits), - (None, Mode::Header) => ((1u32 << bits.clamp(1, 16)) - 1) as f32, + let (white, white_source) = match (body.and_then(|b| b.white), mode) { + (Some(white), _) => (white, crate::LevelSource::Inferred), + (None, Mode::Full) => white_from_data_sourced(samples, bits), + (None, Mode::Header) => (((1u32 << bits.clamp(1, 16)) - 1) as f32, crate::LevelSource::Fallback), }; let crop = match body { Some(&MeasuredBody { crop: (x, y, cw, ch), .. }) if x + cw <= active_w && y + ch <= h => Rect::new(x, y, cw, ch), @@ -329,7 +338,7 @@ pub(crate) fn decode(bytes: &[u8], mode: Mode, limits: &Limits) -> Result Option, samples: &[u16]) -> f32 { +fn be16_black(info: &ImageInfo, file_len: u64, mn: Option<&makernote::MakerNote>, samples: &[u16]) -> (f32, crate::LevelSource) { let has_table = mn.is_some_and(|m| m.ifd.bytes(LINEARIZATION_TABLE).is_some()); let single = matches!(info.chunks(file_len).as_slice(), [c] if be16_layout(info.width as usize, info.height as usize, info.bits() as u32, c.len).is_some()); if info.compression != t::compression::NIKON || has_table || info.samples_per_pixel != 1 || !single { - return 0.0; + return (0.0, crate::LevelSource::Fallback); } let darkest = samples.iter().copied().min().unwrap_or(0); - BE16_BLACK.min(f32::from(darkest)) + (BE16_BLACK.min(f32::from(darkest)), crate::LevelSource::Inferred) } /// Unpack a [`PackedLayout`] strip into `w * h` samples. Short input leaves the missing samples zero. @@ -731,6 +740,21 @@ mod tests { TiffWriter::new(order, false).write(&[ifd0]).unwrap() } + #[test] + fn level_provenance_distinguishes_missing_black_from_recorded_black() { + let strip: Vec = [1000u16; 16].into_iter().flat_map(u16::to_be_bytes).collect(); + let fallback = nef(1, 12, vec![strip.clone()], 4, 4, 4); + let mut note = IfdBuilder::new(); + note.set(BLACK_LEVEL, Value::Short(vec![400; 4])); + let recorded = nef_with_note(1, 12, vec![strip], 4, 4, 4, Some(note)); + for mode in [Mode::Header, Mode::Full] { + let missing = decode(&fallback, mode, &Limits::default()).unwrap(); + assert_eq!((missing.black.mean(), missing.level_sources.black), (0.0, crate::LevelSource::Fallback)); + let stated = decode(&recorded, mode, &Limits::default()).unwrap(); + assert_eq!((stated.black.mean(), stated.level_sources.black), (100.0, crate::LevelSource::Metadata)); + } + } + /// A Z 6II NEF carrying NikonNEFInfo gets Nikon's distortion correction as one warp, in the header probe and /// the full decode alike, and keeps it through DNG export; another body with the same data gets none. #[test] diff --git a/crates/raw/src/preview/tests.rs b/crates/raw/src/preview/tests.rs index 7f0833da9..e04c8574f 100644 --- a/crates/raw/src/preview/tests.rs +++ b/crates/raw/src/preview/tests.rs @@ -582,7 +582,19 @@ fn cr3_prefers_the_full_size_jpeg_track() { let stsd = full(b"stsd", &[&1u32.to_be_bytes()[..], &bx(b"CRAW", &craw)].concat()); let stsz = full(b"stsz", &[0u32.to_be_bytes(), 1u32.to_be_bytes(), (big.len() as u32).to_be_bytes()].concat()); let co64 = full(b"co64", &[&1u32.to_be_bytes()[..], &at.to_be_bytes()].concat()); - let trak = bx(b"trak", &bx(b"mdia", &bx(b"minf", &bx(b"stbl", &[stsd, stsz, co64].concat())))); + let trak = bx( + b"trak", + &bx( + b"mdia", + &bx( + b"minf", + &bx( + b"stbl", + &[stsd, full(b"stsc", &[1u32, 1, 1, 1].into_iter().flat_map(u32::to_be_bytes).collect::>()), stsz, co64].concat(), + ), + ), + ), + ); file.extend(bx(b"moov", &trak)); file.resize(at as usize, 0); file.extend_from_slice(&big); @@ -605,7 +617,19 @@ fn cr3_skips_bad_preview_tracks_but_never_uses_sensor_tracks_as_jpeg() { let stsd = full(b"stsd", &[&1u32.to_be_bytes()[..], &bx(b"CRAW", &entry)].concat()); let stsz = full(b"stsz", &[len.to_be_bytes(), 1u32.to_be_bytes()].concat()); let co64 = full(b"co64", &[&1u32.to_be_bytes()[..], &at.to_be_bytes()].concat()); - bx(b"trak", &bx(b"mdia", &bx(b"minf", &bx(b"stbl", &[stsd, stsz, co64].concat())))) + bx( + b"trak", + &bx( + b"mdia", + &bx( + b"minf", + &bx( + b"stbl", + &[stsd, full(b"stsc", &[1u32, 1, 1, 1].into_iter().flat_map(u32::to_be_bytes).collect::>()), stsz, co64].concat(), + ), + ), + ), + ) }; let good = fake_jpeg(100); for first in [track(true, u64::MAX, 32), track(true, 4096, 32)] { @@ -638,7 +662,19 @@ fn cr3_with(prvw_jpeg: &[u8], big: &[u8]) -> Vec { let stsd = full(b"stsd", &[&1u32.to_be_bytes()[..], &bx(b"CRAW", &craw)].concat()); let stsz = full(b"stsz", &[0u32.to_be_bytes(), 1u32.to_be_bytes(), (big.len() as u32).to_be_bytes()].concat()); let co64 = full(b"co64", &[&1u32.to_be_bytes()[..], &at.to_be_bytes()].concat()); - let trak = bx(b"trak", &bx(b"mdia", &bx(b"minf", &bx(b"stbl", &[stsd, stsz, co64].concat())))); + let trak = bx( + b"trak", + &bx( + b"mdia", + &bx( + b"minf", + &bx( + b"stbl", + &[stsd, full(b"stsc", &[1u32, 1, 1, 1].into_iter().flat_map(u32::to_be_bytes).collect::>()), stsz, co64].concat(), + ), + ), + ), + ); file.extend(bx(b"moov", &trak)); file.resize(at as usize, 0); file.extend_from_slice(big); @@ -686,7 +722,19 @@ fn the_smallest_preview_covering_the_size_is_chosen() { let stsd = full(b"stsd", &[&1u32.to_be_bytes()[..], &bx(b"CRAW", &craw)].concat()); let stsz = full(b"stsz", &[0u32.to_be_bytes(), 1u32.to_be_bytes(), (big.len() as u32).to_be_bytes()].concat()); let co64 = full(b"co64", &[&1u32.to_be_bytes()[..], &at.to_be_bytes()].concat()); - let trak = bx(b"trak", &bx(b"mdia", &bx(b"minf", &bx(b"stbl", &[stsd, stsz, co64].concat())))); + let trak = bx( + b"trak", + &bx( + b"mdia", + &bx( + b"minf", + &bx( + b"stbl", + &[stsd, full(b"stsc", &[1u32, 1, 1, 1].into_iter().flat_map(u32::to_be_bytes).collect::>()), stsz, co64].concat(), + ), + ), + ), + ); file.extend(bx(b"moov", &trak)); file.resize(at as usize, 0); file.extend_from_slice(&big); @@ -799,7 +847,19 @@ fn a_cr3_with_two_prvw_boxes_offers_both() { let stsd = full(b"stsd", &[&1u32.to_be_bytes()[..], &bx(b"CRAW", &craw)].concat()); let stsz = full(b"stsz", &[0u32.to_be_bytes(), 1u32.to_be_bytes(), (big.len() as u32).to_be_bytes()].concat()); let co64 = full(b"co64", &[&1u32.to_be_bytes()[..], &at.to_be_bytes()].concat()); - let trak = bx(b"trak", &bx(b"mdia", &bx(b"minf", &bx(b"stbl", &[stsd, stsz, co64].concat())))); + let trak = bx( + b"trak", + &bx( + b"mdia", + &bx( + b"minf", + &bx( + b"stbl", + &[stsd, full(b"stsc", &[1u32, 1, 1, 1].into_iter().flat_map(u32::to_be_bytes).collect::>()), stsz, co64].concat(), + ), + ), + ), + ); file.extend(bx(b"moov", &trak)); // (the track in the media data, as a camera writes it, after a `free` box up to it) let pad = at as usize - 16 - file.len(); diff --git a/crates/raw/src/raf.rs b/crates/raw/src/raf.rs index 464c086f1..6ff625835 100644 --- a/crates/raw/src/raf.rs +++ b/crates/raw/src/raf.rs @@ -203,8 +203,8 @@ pub(crate) fn decode(bytes: &[u8], mode: Mode, limits: &Limits) -> Result Result Rect::new(0, 0, active.width, active.height), }; - if !data.is_empty() { + // Packed formats 2/5/7 are lossless sample containers: zero is an ordinary + // code, not evidence of a mapped-out photosite. The documented zero repair + // applies to the predictive format 4 and older word containers only. + if !data.is_empty() && matches!(encoding, Encoding::Words | Encoding::Blocks { kind: Block::Format4, .. }) { fill_defects(&mut data, w, active); } @@ -1070,18 +1073,33 @@ mod tests { } } + #[test] + fn lossless_packed_formats_preserve_zero_codes_inside_the_active_area() { + let (w, h) = (90usize, 12usize); + for (format, bits, rotated) in [(5u16, 12u32, true), (5, 14, true), (7, 14, false), (2, 12, false)] { + let mut px = vec![1000u16; w * h]; + px[5 * w + 20] = 0; + let payload = pack(&px, bits); + let bytes = rw2(w as u16, h as u16, bits as u16, format, 100, &if rotated { rotate(payload) } else { payload }); + assert_eq!(samples(&crate::decode(&bytes).unwrap()), px, "format {format}/{bits}"); + } + } + #[test] fn levels_crop_defects_and_cfa() { let (w, h) = (90usize, 60usize); let mut px: Vec = (0..w * h).map(|i| 300 + (i % 7) as u16).collect(); - // a saturation plateau sets the white level; zeros inside the active area are defects + // A saturation plateau sets the white level. In the older word container, + // isolated zeros inside the active area are documented mapped-out defects. px[10 * w..20 * w].fill(3900); px[30 * w + 40] = 0; px[1] = 0; // in the border: left alone - let mut tags = vec![short(CFA_PATTERN, 2), short(BITS, 12), short(RAW_FORMAT, 5)]; + let mut tags = vec![short(CFA_PATTERN, 2), short(BITS, 12), short(COMPRESSION, 34828)]; tags.extend([0x002f, 0x0030, 0x0031, 0x0032].iter().zip([12u16, 10, 52, 80]).map(|(&tag, v)| short(tag, v))); tags.extend(LINEARITY_LIMIT.map(|tag| short(tag, 2000))); // recorded limits are not used - let bytes = file(w as u16, h as u16, &tags, &rotate(pack(&px, 12)), &|off| vec![(RAW_OFFSET, Value::Long(vec![off]))]); + // Compression 34828 stores left-justified samples in little-endian u16 words. + let words: Vec = px.iter().flat_map(|&v| (v << 4).to_le_bytes()).collect(); + let bytes = file(w as u16, h as u16, &tags, &words, &|off| vec![(t::STRIP_OFFSETS, Value::Long(vec![off]))]); let r = crate::decode(&bytes).unwrap(); let d = samples(&r); assert_eq!(d[30 * w + 40], ((px[30 * w + 38] as u32 + px[30 * w + 42] as u32 + px[28 * w + 40] as u32 + px[32 * w + 40] as u32) / 4) as u16);