Skip to content

LightCraft: An open-source non-destructive photo editor written by scratch in pure Rust

Merge PR #1176

Brandon Thomasf613aeba59b3
diff --cc Cargo.lock
index db6efd50e,65dffed24..9d967a202
--- a/Cargo.lock
+++ b/Cargo.lock
@@@ -3417,8 -3416,9 +3417,9 @@@ dependencies =
[[package]]
name = "lightcraft-raw"
-version = "0.7.1"
+version = "0.8.0"
dependencies = [
+ "dhat",
"jpeg-encoder",
"jxl-oxide",
"lightcraft-color",
diff --cc crates/raw/examples/rawinfo.rs
index c1e05bedc,d5cc21b15..78624a0de
--- a/crates/raw/examples/rawinfo.rs
+++ b/crates/raw/examples/rawinfo.rs
@@@ -150,87 -130,4 +150,88 @@@ fn inspect_files(args: &[String]) -> us
None => println!(" preview: none"),
}
}
+ failures
+}
+
+fn main() -> ExitCode {
+ let args: Vec<String> = std::env::args().skip(1).collect();
+ if inspect_files(&args) == 0 { ExitCode::SUCCESS } else { ExitCode::FAILURE }
+}
+
+#[cfg(test)]
+mod tests {
+ use super::inspect_files;
+ use lightcraft_raw::{BlackLevel, Cfa, ColorData, DngWriteOptions, OpcodeLists, Orientation, RawData, RawFormat, RawImage, Rect, write_dng};
+ use std::path::PathBuf;
+ use std::sync::atomic::{AtomicUsize, Ordering};
+
+ struct TempDir(PathBuf);
+
+ impl TempDir {
+ fn new() -> Self {
+ static NEXT: AtomicUsize = AtomicUsize::new(0);
+ let path = std::env::temp_dir().join(format!("lightcraft-rawinfo-{}-{}", std::process::id(), NEXT.fetch_add(1, Ordering::Relaxed)));
+ std::fs::create_dir(&path).expect("create test directory");
+ Self(path)
+ }
+
+ fn path(&self, name: &str) -> String {
+ self.0.join(name).to_string_lossy().into_owned()
+ }
+ }
+
+ impl Drop for TempDir {
+ fn drop(&mut self) {
+ let _ = std::fs::remove_dir_all(&self.0);
+ }
+ }
+
+ #[test]
+ fn unreadable_input_is_a_failure_without_panicking() {
+ let dir = TempDir::new();
+ assert_eq!(inspect_files(&[dir.path("absent.arw")]), 1);
+ }
+
+ #[test]
+ fn invalid_inputs_are_counted_without_stopping_the_batch() {
+ let dir = TempDir::new();
+ let path = dir.path("invalid.arw");
+ std::fs::write(&path, b"not a raw file").expect("write invalid fixture");
+ assert_eq!(inspect_files(&[path.clone(), path]), 2);
+ }
+
+ #[test]
+ fn input_paths_are_required() {
+ assert_eq!(inspect_files(&[]), 1);
+ assert_eq!(inspect_files(&["--tree".into()]), 1);
+ }
+
+ #[test]
+ fn valid_generated_dng_succeeds_after_an_unreadable_input() {
+ let dir = TempDir::new();
+ let raw = RawImage {
+ format: RawFormat::Dng,
+ width: 16,
+ height: 16,
+ cpp: 1,
+ data: RawData::U16(vec![2048; 16 * 16]),
+ cfa: Some(Cfa::bayer("RGGB").expect("valid CFA")),
+ bits: 14,
+ black: BlackLevel::uniform(0.0),
+ white: vec![16383.0],
+ active_area: Rect::new(0, 0, 16, 16),
+ crop: Rect::new(0, 0, 16, 16),
+ orientation: Orientation::Normal,
+ color: ColorData::default(),
+ wb_multipliers: Some([1.0; 3]),
+ linearized: false,
+ opcodes: OpcodeLists::default(),
+ metadata: Default::default(),
++ lens_metadata: Default::default(),
+ };
+ let path = dir.path("valid.dng");
+ std::fs::write(&path, write_dng(&raw, &DngWriteOptions::default()).expect("encode generated fixture")).expect("write fixture");
+ assert_eq!(inspect_files(std::slice::from_ref(&path)), 0);
+ assert_eq!(inspect_files(&[dir.path("absent.arw"), path]), 1);
+ }
}
diff --cc crates/raw/src/lens_metadata.rs
index 000000000,58fd76452..4529950c8
mode 000000,100644..100644
--- a/crates/raw/src/lens_metadata.rs
+++ b/crates/raw/src/lens_metadata.rs
@@@ -1,0 -1,187 +1,187 @@@
+ //! Lens-correction data a raw file records, kept with where it came from and separately from what LightCraft
+ //! applies.
+ //!
+ //! A readable table is not a correction. Each component carries a [`SonyLensStatus`]: only
+ //! [`SonyLensStatus::Applied`] components were converted into the image's `OpcodeList3`, and that is decided by
+ //! independent validation per model, coding and component (see `docs/sony-lens-corrections.md`), never by the mere
+ //! presence of a table. Everything else here is metadata for diagnostics and later validation work.
+
+ use crate::Rect;
+ use serde::{Deserialize, Serialize};
+
+ /// Vendor lens-correction metadata of a raw file (see the module docs). Empty for formats that record none, or
+ /// whose tables are read straight into opcodes.
+ #[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
+ pub struct LensMetadata {
+ /// Sony ARW: the raw image IFD's shading, lateral chromatic aberration and distortion records, with their
+ /// encrypted `SR2SubIFD` copies. `None` when neither directory holds any of them.
+ #[serde(default)]
+ pub sony: Option<SonyLensCorrections>,
+ }
+
+ /// Sony's in-file lens-correction record: three independent components, each a camera setting and a radial table.
+ ///
+ /// Tag identities and storage types from the ExifTool EXIF / Sony tag-name documentation: raw-IFD `0x7031` / `0x7032`
+ /// (shading, "VignettingCorrection" / "VignettingCorrParams"), `0x7034` / `0x7035` (lateral chromatic aberration),
+ /// `0x7036` / `0x7037` (distortion), and the `SR2SubIFD` tables `0x797d`, `0x7980`, `0x7982`. The `SR2SubIFD` setting
+ /// tags `0x797c`, `0x797f`, `0x7981` are unnamed there; they sit next to the tables and equal the raw-IFD settings
+ /// in every file inspected.
+ #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
+ pub struct SonyLensCorrections {
+ /// Shading (vignetting): 16 radial samples whose gain function, radius convention and processing stage are not
+ /// established. Opaque signed data.
+ pub shading: SonyComponent<[i16; 16]>,
+ /// Lateral chromatic aberration: two 16-sample radial curves. A public analysis reads them as red, then blue,
+ /// relative to green; that is not validated here.
+ pub chromatic_aberration: SonyComponent<[[i16; 16]; 2]>,
+ /// Distortion: 16 radial samples (see `docs/sony-lens-corrections.md` for the validated interpretation).
+ pub distortion: SonyComponent<[i16; 16]>,
+ /// Whether the encrypted `SR2SubIFD` was found and decrypted. `false`: no copies to fall back on or compare.
+ pub sr2_read: bool,
+ /// The framing and coding the tables have to be interpreted against.
+ pub geometry: SonyLensGeometry,
+ }
+
+ /// One correction component: the camera's setting, its table, and whether LightCraft applies it.
+ #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
+ pub struct SonyComponent<T> {
+ pub setting: SonyField<SonySetting>,
+ /// The table's samples after the leading sample count, which must equal their number.
+ pub table: SonyField<T>,
+ pub status: SonyLensStatus,
+ }
+
+ /// One field read from the raw IFD and from the `SR2SubIFD`.
+ #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
+ pub struct SonyField<T> {
+ /// The value in use: the raw IFD's when it holds a well-formed one, else the `SR2SubIFD`'s when the raw IFD
+ /// lacks the tag. `None` when no copy is usable, and when the raw IFD's copy is malformed: a corrupt field is
+ /// reported, not silently replaced by the other copy. The raw IFD's copy is the one the TIFF reader that decodes
+ /// the image parsed (its first occurrence, within the reader's limits).
+ pub value: Option<T>,
+ pub source: Option<SonySource>,
+ pub raw_ifd: SonyCopy,
+ pub sr2: SonyCopy,
+ /// Whether the two copies are equal, when both are well-formed. The raw IFD's wins when they differ.
+ pub copies_agree: Option<bool>,
+ /// Diagnostics: the tag occurs more than once in the raw IFD / the `SR2SubIFD`. Only the first occurrence is used,
+ /// as the TIFF reader does (the raw IFD's: the first one whose values it could read).
+ #[serde(default)]
+ pub raw_ifd_repeated: bool,
+ #[serde(default)]
+ pub sr2_repeated: bool,
+ }
+
+ /// Where a value was read.
+ #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
+ pub enum SonySource {
+ /// The plain raw image IFD.
+ RawIfd,
+ /// The encrypted `SR2SubIFD`.
+ Sr2SubIfd,
+ }
+
+ /// How one copy of a field was found.
+ #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
+ pub enum SonyCopy {
+ /// Not in that directory (or the `SR2SubIFD` could not be read).
+ Absent,
+ /// Present with exactly the supported type and layout.
+ Valid,
+ /// Present, but refused.
+ Rejected(SonyFieldError),
+ }
+
+ /// Why a present field was refused.
+ #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
+ pub enum SonyFieldError {
+ /// Another TIFF field type than SSHORT (8) for a table, or SHORT (3) / SSHORT for a setting.
+ FieldType(u16),
+ /// Another number of values than the supported layout's: 17 (shading, distortion), 33 (CA) or 1 (setting).
+ Count(u64),
+ /// The leading sample count is not the layout's 16 (32 for CA). Older bodies store 11 (22) samples padded to the
+ /// same length, a layout that is not interpreted; a count of 0 goes with an "unavailable" setting.
+ CountPrefix(i16),
+ /// The values lie outside the file (raw IFD) or the decrypted block (`SR2SubIFD`).
+ OutsideData,
+ /// In the raw IFD but past what the TIFF reader that decodes the image reads (8,192 entries per directory, a
+ /// total value budget), so the image doesn't use it either.
+ PastReaderLimits,
+ /// A setting stored as a negative SSHORT.
+ NegativeSetting(i16),
+ }
+
+ /// A camera correction setting: the stored number and its documented meaning.
+ #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
+ pub struct SonySetting {
+ pub value: u16,
+ pub meaning: SonySettingMeaning,
+ }
+
+ /// The documented meaning of a setting value (ExifTool EXIF tag-name documentation). Values are compared whole;
+ /// they are not read as bit masks.
+ #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
+ pub enum SonySettingMeaning {
+ /// Shading 256, CA 0, distortion 0. The table is still recorded: Off does not mean absent.
+ Off,
+ /// Shading 257 or 272, CA 1, distortion 1.
+ Auto,
+ /// Distortion 17: Auto, fixed by the lens.
+ ForcedAuto,
+ /// "No correction params available": shading 511, CA 255, distortion 255.
+ Unavailable,
+ /// Any other value (e.g. a shading "Low" setting, whose number is not documented). Never treated as Off.
+ Unknown(u16),
+ }
+
+ /// Whether a component is applied, and if not, why.
+ #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
+ pub enum SonyLensStatus {
- /// Converted into the image's `OpcodeList3` (today: ILCE-7RM4A Bayer distortion in native 3:2 framing).
++ /// Converted into the image's `OpcodeList3` (today: ILCE-7RM4A and ILCE-7RM6 Bayer distortion in native 3:2 framing).
+ Applied,
+ /// No usable table.
+ NoTable,
+ /// Decoded only: no independent validation covers this component's rendering semantics on any body (shading,
+ /// lateral CA).
+ NotValidatedComponent,
+ /// Decoded only: the component is validated on other camera models, not on this one.
+ NotValidatedModel,
+ /// Decoded only: the model is validated, but not this coding or framing (linear YCbCr, a non-3:2 crop) or a
+ /// table found only in the `SR2SubIFD`.
+ NotValidatedMode,
+ /// Validated scope, but the table is all zeros: a valid no-op, nothing to apply.
+ Neutral,
+ /// Validated scope, but the conversion refused this table (values outside the validated range, a degenerate
+ /// crop, or a fitted map that misses the table or folds).
+ Rejected,
+ }
+
+ /// The framing and coding evidence for interpreting the tables, all in raw-frame pixels.
+ #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
+ pub struct SonyLensGeometry {
+ /// The stored raw frame, including any padding (lossless L files are padded).
+ pub storage_width: usize,
+ pub storage_height: usize,
+ /// Sony's crop tags (`0x74c7` / `0x74c8`) as recorded.
+ pub sony_crop: Option<Rect>,
+ /// The raw IFD's `DefaultCropOrigin` / `DefaultCropSize` as recorded.
+ pub default_crop: Option<Rect>,
+ /// The image's crop (the decoder's precedence: Sony's tags, then the default crop, then the recorded size).
+ pub image_crop: Rect,
+ /// The rectangle the validated distortion conversion is centred on and normalised to: the default crop when
+ /// the file has one, else the image crop. Clipped to the frame.
+ pub reference: Rect,
+ /// TIFF compression of the raw image (1 uncompressed, 7 lossless, 32767 ARW2, 32766 ARW6).
+ pub compression: u16,
+ /// Downsized lossless M/S data (linear YCbCr, not sensor CFA).
+ pub linear_ycbcr: bool,
+ /// Both the image crop and the reference are 3:2 within 0.5 %, the only framing validated.
+ pub native_3_2: bool,
+ }
+
+ impl<T> SonyField<T> {
+ /// Whether either directory holds the tag (well-formed or not).
+ pub fn is_present(&self) -> bool {
+ self.raw_ifd != SonyCopy::Absent || self.sr2 != SonyCopy::Absent
+ }
+ }
diff --cc crates/raw/src/lib.rs
index 3a7f04a95,29ace7025..faa28c593
--- a/crates/raw/src/lib.rs
+++ b/crates/raw/src/lib.rs
@@@ -896,37 -890,6 +908,38 @@@ mod tests
assert_eq!(whole.develop_crop(8, 6), Rect::new(6, 4, 2, 2));
}
+ /// A maker-note crop (`PEF` AOC / `ORF` ImageProcessing) with an origin of `u64::MAX` used to
+ /// pass the bounds check (`x + width` wraps to 0) and then index out of bounds in
+ /// `normalized`. It must be a `Corrupt` error at validate time instead.
+ #[test]
+ fn an_active_area_that_wraps_past_the_image_is_corrupt() {
+ let (w, h) = (12usize, 10usize);
+ let mut raw = RawImage {
+ format: RawFormat::Dng,
+ width: w,
+ height: h,
+ cpp: 1,
+ data: RawData::U16(vec![100; w * h]),
+ cfa: Cfa::bayer("RGGB"),
+ bits: 12,
+ black: BlackLevel::uniform(64.0),
+ white: vec![1023.0],
+ active_area: Rect::new(usize::MAX, 0, 1, 1),
+ crop: Rect::default(),
+ orientation: Orientation::Normal,
+ color: ColorData::default(),
+ wb_multipliers: None,
+ linearized: false,
+ opcodes: OpcodeLists::default(),
+ metadata: Metadata::default(),
++ lens_metadata: Default::default(),
+ };
+ assert!(raw.validate().is_err(), "`x + width` wrapped: the area is outside the image");
+ assert!(raw.normalized().is_err());
+ raw.active_area = Rect::new(0, 0, w + 1, h);
+ assert!(raw.validate().is_err(), "one past the last column is outside too");
+ }
+
#[test]
fn black_levels() {
let b = BlackLevel { repeat_rows: 2, repeat_cols: 2, values: vec![1.0, 2.0, 3.0, 4.0], delta_h: vec![0.5, 0.0], delta_v: vec![0.0, 10.0] };
diff --cc crates/raw/src/vendor/arw.rs
index 49543c4fe,7a4e0cc45..fddf47bb9
--- a/crates/raw/src/vendor/arw.rs
+++ b/crates/raw/src/vendor/arw.rs
@@@ -749,6 -757,25 +758,39 @@@ pub(crate) fn decode(bytes: &[u8], mode
let mut metadata = lightcraft_meta::from_tiff(&tiff);
metadata.width = Some(crop.width as u32);
metadata.height = Some(crop.height as u32);
+ // The lens-correction record. The distortion table is centred on the DNG-style default crop when the file has
+ // one: on the ILCE-7RM4A it starts at x = 32 where Sony's crop tags (the image crop, unchanged) start at 0, and
+ // the warp was validated against Sony's exports with that centre. The opcode's centre is in active-area
+ // coordinates, so it stays on the optical centre whichever crop frames the image.
+ let reference = dng_default_crop(raw, w, h).unwrap_or(crop);
+ let geometry = SonyLensGeometry {
+ storage_width: w,
+ storage_height: h,
+ sony_crop: recorded_rect(raw, CROP_TOP_LEFT, CROP_SIZE),
+ default_crop: recorded_rect(raw, t::DEFAULT_CROP_ORIGIN, t::DEFAULT_CROP_SIZE),
+ image_crop: crop,
+ reference,
+ compression: info.compression,
+ linear_ycbcr: linear_rgb,
+ native_3_2: super::arw_lens::is_3_2(crop) && super::arw_lens::is_3_2(reference),
+ };
+ // decrypted for the copies' agreement check too, not only when a plain field is missing: a few dozen kB, found by
+ // reading only three `SR2Private` entries (see `sr2::SubIfd::read`)
- let (sony_lens, warp) = super::arw_lens::corrections(bytes, tiff.order, (raw, tiff.bigtiff), sr2(), &model, geometry);
++ let (mut sony_lens, warp) = super::arw_lens::corrections(bytes, tiff.order, (raw, tiff.bigtiff), sr2(), &model, geometry);
++ // The ILCE-7RM6's tables follow other conventions, measured against its camera JPEGs ([`super::arw_lens_rm6`]);
++ // its allowlist and `arw_lens`'s are disjoint, so at most one of them produces an opcode. Both the image framing
++ // and the table geometry must stay within the validated 3:2 scope.
++ let rm6: Vec<crate::Opcode> = if linear_rgb || !super::arw_lens::is_3_2(crop) {
++ Vec::new()
++ } else {
++ super::arw_lens_rm6::lens_opcodes(raw, &model, Rect::new(0, 0, w, h), reference)
++ };
++ if !rm6.is_empty()
++ && let Some(record) = sony_lens.as_mut()
++ {
++ // distortion only: its lateral CA planes are off (see `arw_lens_rm6::lens_opcodes`)
++ record.distortion.status = crate::lens_metadata::SonyLensStatus::Applied;
++ }
let img = RawImage {
format: RawFormat::Arw,
width: w,
@@@ -768,28 -795,10 +810,10 @@@
// makes a neutral surface magenta. WB edits remain relative to this as-shot RGB.
wb_multipliers: if linear_rgb { Some([1.0; 3]) } else { wb },
linearized: false,
- // The distortion table is centred on the DNG-style default crop when the file has one: on the ILCE-7RM4A
- // it starts at x = 32 where Sony's crop tags (the image crop, unchanged) start at 0, and the warp was
- // validated against Sony's exports with that centre. The opcode's centre is in active-area coordinates, so
- // it stays on the optical centre whichever crop frames the image. The ILCE-7RM6's tables follow other
- // conventions, measured against its camera JPEGs ([`super::arw_lens_rm6`]); the two allowlists are
- // disjoint, so at most one of them produces an opcode.
- opcodes: OpcodeLists {
- // DefaultCrop can retain native geometry while Sony's tags select an aspect crop.
- // Both the image framing and the table geometry must stay within the validated 3:2 scope.
- list3: if linear_rgb || crop.height == 0 || (crop.width as f64 / crop.height as f64 - 1.5).abs() > 0.005 {
- Vec::new()
- } else {
- let geometry = dng_default_crop(raw, w, h).unwrap_or(crop);
- let active = Rect::new(0, 0, w, h);
- super::arw_lens::distortion(&model, raw, active, geometry)
- .into_iter()
- .chain(super::arw_lens_rm6::lens_opcodes(raw, &model, active, geometry))
- .collect()
- },
- ..Default::default()
- },
- // only validated components become opcodes (see `arw_lens`)
- opcodes: OpcodeLists { list3: warp.into_iter().collect(), ..Default::default() },
++ // only validated components become opcodes (see `arw_lens`, and `arw_lens_rm6` for the ILCE-7RM6)
++ opcodes: OpcodeLists { list3: warp.into_iter().chain(rm6).collect(), ..Default::default() },
metadata,
+ lens_metadata: LensMetadata { sony: sony_lens },
};
img.validate_for(mode)?;
Ok(img)
diff --cc crates/raw/src/vendor/erf.rs
index fae120bcc,000000000..7f98ca121
mode 100644,000000..100644
--- a/crates/raw/src/vendor/erf.rs
+++ b/crates/raw/src/vendor/erf.rs
@@@ -1,197 -1,0 +1,198 @@@
+//! Epson ERF (R-D1, R-D1s, R-D1x): a big-endian TIFF whose `SubIFDs` child is a 12-bit CFA image in 16-byte blocks.
+//!
+//! Sources: TIFF 6.0 (container, `CFARepeatPatternDim` / `CFAPattern`) and our own black-box analysis of the three
+//! CC0 ERF samples on raw.pixls.us (one per body); every number below was measured on them.
+//!
+//! - IFD0 is a 160 x 120 RGB thumbnail; its `SubIFDs` child is the raw: 3040 x 2024, 12 bits, `Compression` 32769,
+//! photometric CFA, one strip of exactly `width * 8 / 5` bytes per row. Byte 15 of every 16 is zero in all rows:
+//! a row is 16-byte blocks, each 15 bytes of ten 12-bit samples, most significant bits first (`b0 << 4 | b1 >> 4`,
+//! `(b1 & 15) << 8 | b2`), then a pad byte. Read the other way (least significant first, as the Olympus blocks are)
+//! the frame is noise: the mean step between same-colour neighbours is 150..199 codes against 11..18.
+//! - `CFAPattern` `00 01 01 02`: RGGB.
+//! - The maker note (`EPSON\0\x01\0`, then a big-endian IFD with offsets from the file start) holds four black levels
+//! as `u32` in tag `0x0401` (61..65; in Bayer-cell order), above which the darkest 0.01% of samples sit; `0x020b` x
+//! `0x020c` is the picture size, 3008 x 2000. Registered against the 640 x 424 preview JPEG (correlation of the
+//! green channel over every even offset), that picture starts at column 20, row 12 in all three files.
+//! - White: the saturation plateau (4095 on the clipped R-D1x frame), else 4095.
+//! - No maker-note tag tracks the as-shot white balance across the three files, so it is measured from the
+//! preview's neutral pixels ([`crate::wb_estimate`]); on the R-D1s sample (54,000 neutral preview pixels) that
+//! gives red 2.68 and blue 1.31.
+//! - The preview is the maker note's tag `0x0280`, a JPEG whose first byte is stored as `ee` (found by the
+//! structural scan in `preview::scan_for_jpeg`).
+use crate::{BlackLevel, Cfa, ColorData, Mode, OpcodeLists, RawData, RawError, RawFormat, RawImage, Rect, Result};
+use lightcraft_geom::Orientation;
+use lightcraft_tiff::image::chunk_bytes;
+use lightcraft_tiff::{Ifd, Tiff, tags as t};
+
+/// Epson's private compression code for the 16-byte block layout.
+const BLOCKS16: u16 = 32769;
+/// Maker-note tags.
+const MN_BLACK: u16 = 0x0401;
+const MN_WIDTH: u16 = 0x020b;
+const MN_HEIGHT: u16 = 0x020c;
+/// Where the picture starts in the sensor frame (measured, see the module notes).
+const CROP_ORIGIN: (usize, usize) = (20, 12);
+
+/// The raw IFD of an Epson ERF: CFA, 12 bits, compression 32769, one strip of 16-byte blocks of ten samples.
+pub(crate) fn raw_ifd(tiff: &Tiff, len: usize) -> Option<&Ifd> {
+ let make = tiff.find(t::MAKE).and_then(|e| e.value.as_str())?;
+ if !make.to_ascii_uppercase().contains("EPSON") {
+ return None;
+ }
+ tiff.all_ifds().into_iter().find(|i| {
+ let Ok(info) = i.image() else { return false };
+ let (w, h) = (info.width as u64, info.height as u64);
+ i.u16(t::PHOTOMETRIC) == Some(t::photometric::CFA)
+ && i.u16(t::COMPRESSION) == Some(BLOCKS16)
+ && i.u16(t::BITS_PER_SAMPLE) == Some(12)
+ && w > 0
+ && w % 10 == 0
+ && matches!(info.chunks(len as u64).as_slice(), [c] if Some(c.len) == (w * 8 / 5).checked_mul(h))
+ })
+}
+
+/// The maker-note IFD (`EPSON\0\x01\0` then an IFD in the file's byte order, offsets from the file start).
+fn maker_note(bytes: &[u8], tiff: &Tiff) -> Option<Ifd> {
+ let e = tiff.exif()?.get(t::MAKER_NOTE)?;
+ let start = usize::try_from(e.offset).ok()?;
+ if !bytes.get(start..)?.starts_with(b"EPSON\0") {
+ return None;
+ }
+ let opts = lightcraft_tiff::ParseOptions { max_ifds: 1, max_depth: 0, follow_children: false, ..Default::default() };
+ lightcraft_tiff::parse_ifd_at(bytes, e.offset.checked_add(8)?, tiff.order, 0, false, &opts).ok().map(|(i, _)| i)
+}
+
+/// Unpack a strip of 16-byte blocks (15 bytes of ten MSB-first 12-bit samples, one pad byte).
+fn unpack(src: &[u8], out: &mut [u16]) {
+ let (blocks, _) = src.as_chunks::<16>();
+ let (pixels, _) = out.as_chunks_mut::<10>();
+ for (b, px) in blocks.iter().zip(pixels) {
+ crate::unpack::unpack_msb(&b[..15], 12, px);
+ }
+}
+
+pub(crate) fn decode(bytes: &[u8], mode: Mode) -> Result<RawImage> {
+ let tiff = Tiff::parse(bytes)?;
+ let ifd0 = tiff.ifds.first().ok_or_else(|| RawError::Corrupt("Erf TIFF without IFDs".into()))?;
+ let raw = raw_ifd(&tiff, bytes.len()).ok_or_else(|| RawError::Unsupported("Erf raw image not found".into()))?;
+ let info = raw.image()?;
+ let (w, h) = (info.width as usize, info.height as usize);
+ let n = w.checked_mul(h).filter(|n| *n > 0 && *n <= crate::MAX_SAMPLES).ok_or(RawError::Limit("image too large"))?;
+ let cfa = match (raw.u64s(t::CFA_REPEAT_PATTERN_DIM).as_deref(), raw.bytes(t::CFA_PATTERN_EP)) {
+ (Some([2, 2]), Some(p)) if p.len() == 4 && p.iter().all(|&c| c <= 2) => Cfa { width: 2, height: 2, pattern: p.to_vec() },
+ _ => return Err(RawError::Unsupported("Erf raw without a 2x2 CFAPattern".into())),
+ };
+ let mn = maker_note(bytes, &tiff);
+ let black = match mn.as_ref().and_then(|m| m.u64s(MN_BLACK)).as_deref() {
+ Some(v @ [_, _, _, _]) if v.iter().all(|&b| b < 4095) => {
+ BlackLevel { repeat_rows: 2, repeat_cols: 2, values: v.iter().map(|&b| b as f32).collect(), delta_h: vec![], delta_v: vec![] }
+ }
+ _ => BlackLevel::uniform(0.0),
+ };
+ let size = mn.as_ref().and_then(|m| Some((m.u32(MN_WIDTH)? as usize, m.u32(MN_HEIGHT)? as usize)));
+ let crop = match size {
+ Some((cw, ch)) if cw > 0 && ch > 0 && CROP_ORIGIN.0 + cw <= w && CROP_ORIGIN.1 + ch <= h => Rect::new(CROP_ORIGIN.0, CROP_ORIGIN.1, cw, ch),
+ _ => Rect::new(0, 0, w, h),
+ };
+ // the white balance is measured from the samples, so the header probe reads them too
+ let chunks = info.chunks(bytes.len() as u64);
+ let src = chunks.first().and_then(|c| chunk_bytes(bytes, c)).ok_or_else(|| RawError::Corrupt("Erf strip outside file".into()))?;
+ let mut d = vec![0u16; n];
+ unpack(src, &mut d);
+ let white = super::white_from_data(&d, 12);
+ let wb = crate::wb_estimate::as_shot(bytes, &crate::wb_estimate::Frame { data: &d, width: w, crop, cfa: &cfa, black: &black, white });
+ let data = if mode == Mode::Full { RawData::U16(d) } else { RawData::U16(Vec::new()) };
+ let mut metadata = lightcraft_meta::from_tiff(&tiff);
+ metadata.width = Some(crop.width as u32);
+ metadata.height = Some(crop.height as u32);
+ let img = RawImage {
+ format: RawFormat::Erf,
+ width: w,
+ height: h,
+ cpp: 1,
+ data,
+ cfa: Some(cfa),
+ bits: 12,
+ black,
+ white: vec![white],
+ active_area: Rect::new(0, 0, w, h),
+ crop,
+ orientation: Orientation::from_exif(ifd0.u16(t::ORIENTATION).unwrap_or(1)),
+ color: ColorData::default(),
+ wb_multipliers: wb,
+ linearized: false,
+ opcodes: OpcodeLists::default(),
+ metadata,
++ lens_metadata: Default::default(),
+ };
+ img.validate_for(mode)?;
+ Ok(img)
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+ use lightcraft_tiff::{IfdBuilder, ImageData, TiffWriter, Value};
+
+ /// Pack ten-sample groups into 16-byte blocks, MSB first, zero pad.
+ fn pack(px: &[u16]) -> Vec<u8> {
+ let mut out = Vec::new();
+ for g in px.chunks(10) {
+ for p in g.chunks(2) {
+ let (a, b) = (p[0], p[1]);
+ out.extend_from_slice(&[(a >> 4) as u8, ((a & 15) << 4) as u8 | (b >> 8) as u8, b as u8]);
+ }
+ out.push(0);
+ }
+ out
+ }
+
+ fn sample(w: u32, h: u32, px: &[u16]) -> Vec<u8> {
+ let mut raw = IfdBuilder::new();
+ raw.set(t::NEW_SUBFILE_TYPE, Value::Long(vec![0]));
+ raw.set(t::IMAGE_WIDTH, Value::Long(vec![w]));
+ raw.set(t::IMAGE_LENGTH, Value::Long(vec![h]));
+ raw.set(t::BITS_PER_SAMPLE, Value::Short(vec![12]));
+ raw.set(t::COMPRESSION, Value::Short(vec![BLOCKS16]));
+ raw.set(t::PHOTOMETRIC, Value::Short(vec![t::photometric::CFA]));
+ raw.set(t::CFA_REPEAT_PATTERN_DIM, Value::Short(vec![2, 2]));
+ raw.set(t::CFA_PATTERN_EP, Value::Byte(vec![0, 1, 1, 2]));
+ raw.set_image(ImageData::Strips { rows_per_strip: h, strips: vec![pack(px)] });
+ let mut ifd0 = IfdBuilder::new();
+ ifd0.set(t::MAKE, Value::Ascii("SEIKO EPSON CORP.".into()));
+ ifd0.set(t::MODEL, Value::Ascii("R-D1".into()));
+ ifd0.set(t::ORIENTATION, Value::Short(vec![1]));
+ ifd0.add_sub_ifd(raw);
+ TiffWriter::new(lightcraft_tiff::ByteOrder::Big, false).write(&[ifd0]).unwrap()
+ }
+
+ #[test]
+ fn decodes_the_block_layout() {
+ let (w, h) = (20u32, 6u32);
+ let px: Vec<u16> = (0..w * h).map(|i| (i * 37 % 4096) as u16).collect();
+ let f = sample(w, h, &px);
+ assert_eq!(crate::probe(&f), Some(RawFormat::Erf));
+ let r = crate::decode(&f).unwrap();
+ assert_eq!(r.data, RawData::U16(px));
+ assert_eq!(r.cfa.as_ref().map(Cfa::name).as_deref(), Some("RGGB"));
+ assert!(r.wb_multipliers.is_some());
+ assert_eq!(crate::probe_info(&f).unwrap(), r.info());
+ }
+
+ #[test]
+ fn malformed_files_never_panic() {
+ let px: Vec<u16> = (0..200).map(|i| (i * 13 % 4096) as u16).collect();
+ let f = sample(20, 10, &px);
+ for n in (0..f.len()).step_by(3) {
+ let _ = crate::decode(&f[..n]);
+ }
+ for i in 0..f.len() {
+ for v in [0u8, 0xff] {
+ let mut g = f.clone();
+ g[i] = v;
+ let _ = crate::decode(&g);
+ let _ = crate::probe_info(&g);
+ }
+ }
+ }
+}
diff --cc crates/raw/src/vendor/mef.rs
index 244245818,000000000..a879f59f7
mode 100644,000000..100644
--- a/crates/raw/src/vendor/mef.rs
+++ b/crates/raw/src/vendor/mef.rs
@@@ -1,185 -1,0 +1,186 @@@
+//! Mamiya MEF (ZD): a big-endian TIFF whose first `SubIFDs` child is an uncompressed 12-bit packed CFA image.
+//!
+//! Sources: TIFF 6.0 (container, MSB-first bit packing, `CFAPattern`) and our own black-box analysis of the one CC0
+//! MEF sample on raw.pixls.us (Mamiya ZD); every number below was measured on it.
+//!
+//! - IFD0 is a 144 x 192 RGB thumbnail with three `SubIFDs`: the raw (4016 x 5344, 12 bits, `Compression` 1,
+//! photometric CFA, one strip of exactly 1.5 bytes per pixel, two samples in three bytes most significant bits
+//! first, as TIFF packs them), a 992 x 1328 and a 240 x 320 RGB preview, all uncompressed and 8 bits. No JPEG.
+//! - `CFAPattern` `00 01 01 02`: RGGB; the two green sites agree (means 254 and 256 against 235 and 165).
+//! - No masked border and no stored black level: the darkest 0.01% of each site's samples sit at 3..6 and the
+//! minimum is 0, so black is 0. White: the saturation plateau, else 4095.
+//! - The 992 x 1328 preview shows a 3968 x 5312 window (4 sensor pixels per preview pixel) that starts at column 24,
+//! row 12: that offset gives the best correlation of the preview's luminance with the box-filtered mosaic (0.965,
+//! against 0.83 for the full frame). It is the default crop.
+//! - Orientation is the raw IFD's own `Orientation` (6 on the sample, a portrait shot).
+//! - No white-balance tag was found (the maker note holds an ICC-like profile), so the as-shot gains are measured
+//! from that preview's neutral pixels ([`crate::wb_estimate`]).
+use crate::{BlackLevel, Cfa, ColorData, Mode, OpcodeLists, RawData, RawError, RawFormat, RawImage, Rect, Result};
+use lightcraft_geom::Orientation;
+use lightcraft_tiff::image::chunk_bytes;
+use lightcraft_tiff::{Ifd, Tiff, tags as t};
+
+/// Where the preview's window starts in the sensor frame, and how many sensor pixels one preview pixel covers.
+const CROP_ORIGIN: (usize, usize) = (24, 12);
+const PREVIEW_SCALE: usize = 4;
+
+fn is_mamiya(tiff: &Tiff) -> bool {
+ tiff.find(t::MAKE).and_then(|e| e.value.as_str()).is_some_and(|m| m.to_ascii_uppercase().starts_with("MAMIYA"))
+}
+
+/// The raw IFD of a Mamiya MEF: CFA, 12 bits, uncompressed, one strip of 1.5 bytes per pixel.
+pub(crate) fn raw_ifd(tiff: &Tiff, len: usize) -> Option<&Ifd> {
+ if !is_mamiya(tiff) {
+ return None;
+ }
+ tiff.all_ifds().into_iter().find(|i| {
+ let Ok(info) = i.image() else { return false };
+ let (w, h) = (info.width as u64, info.height as u64);
+ i.u16(t::PHOTOMETRIC) == Some(t::photometric::CFA)
+ && i.u16(t::COMPRESSION).unwrap_or(1) == 1
+ && i.u16(t::BITS_PER_SAMPLE) == Some(12)
+ && w > 0
+ && w % 2 == 0
+ && matches!(info.chunks(len as u64).as_slice(), [c] if Some(c.len) == (w * 3 / 2).checked_mul(h))
+ })
+}
+
+/// The largest uncompressed 8-bit RGB sub-IFD (the preview): its pixels, width and height.
+fn rgb_preview<'a>(tiff: &Tiff, bytes: &'a [u8]) -> Option<(&'a [u8], usize, usize)> {
+ tiff.all_ifds()
+ .into_iter()
+ .filter_map(|i| {
+ let info = i.image().ok()?;
+ let ok = i.u16(t::PHOTOMETRIC) == Some(2)
+ && i.u16(t::COMPRESSION).unwrap_or(1) == 1
+ && i.u16(t::SAMPLES_PER_PIXEL) == Some(3)
+ && i.u64s(t::BITS_PER_SAMPLE).is_some_and(|b| b.iter().all(|&v| v == 8));
+ let (w, h) = (info.width as usize, info.height as usize);
+ let chunks = info.chunks(bytes.len() as u64);
+ let [c] = chunks.as_slice() else { return None };
+ let px = chunk_bytes(bytes, c)?;
+ (ok && px.len() >= w.checked_mul(h)?.checked_mul(3)?).then_some((px, w, h))
+ })
+ .max_by_key(|(_, w, h)| w * h)
+}
+
+pub(crate) fn decode(bytes: &[u8], mode: Mode) -> Result<RawImage> {
+ let tiff = Tiff::parse(bytes)?;
+ let raw = raw_ifd(&tiff, bytes.len()).ok_or_else(|| RawError::Unsupported("Mef raw image not found".into()))?;
+ let info = raw.image()?;
+ let (w, h) = (info.width as usize, info.height as usize);
+ let n = w.checked_mul(h).filter(|n| *n > 0 && *n <= crate::MAX_SAMPLES).ok_or(RawError::Limit("image too large"))?;
+ let cfa = match (raw.u64s(t::CFA_REPEAT_PATTERN_DIM).as_deref(), raw.bytes(t::CFA_PATTERN_EP)) {
+ (Some([2, 2]), Some(p)) if p.len() == 4 && p.iter().all(|&c| c <= 2) => Cfa { width: 2, height: 2, pattern: p.to_vec() },
+ _ => return Err(RawError::Unsupported("Mef raw without a 2x2 CFAPattern".into())),
+ };
+ let preview = rgb_preview(&tiff, bytes);
+ let crop = match preview {
+ Some((_, pw, ph)) => {
+ let (cw, ch) = (pw.saturating_mul(PREVIEW_SCALE), ph.saturating_mul(PREVIEW_SCALE));
+ if cw > 0 && ch > 0 && CROP_ORIGIN.0 + cw <= w && CROP_ORIGIN.1 + ch <= h {
+ Rect::new(CROP_ORIGIN.0, CROP_ORIGIN.1, cw, ch)
+ } else {
+ Rect::new(0, 0, w, h)
+ }
+ }
+ None => Rect::new(0, 0, w, h),
+ };
+ // the white balance is measured from the samples, so the header probe reads them too
+ let chunks = info.chunks(bytes.len() as u64);
+ let src = chunks.first().and_then(|c| chunk_bytes(bytes, c)).ok_or_else(|| RawError::Corrupt("Mef strip outside file".into()))?;
+ let mut d = vec![0u16; n];
+ crate::unpack::unpack_msb(src, 12, &mut d);
+ let white = super::white_from_data(&d, 12);
+ let black = BlackLevel::uniform(0.0);
+ let frame = crate::wb_estimate::Frame { data: &d, width: w, crop, cfa: &cfa, black: &black, white };
+ let wb = match preview {
+ Some((px, pw, ph)) => crate::wb_estimate::as_shot_rgb(px, pw, ph, &frame),
+ None => crate::wb_estimate::as_shot(bytes, &frame),
+ };
+ let data = if mode == Mode::Full { RawData::U16(d) } else { RawData::U16(Vec::new()) };
+ let mut metadata = lightcraft_meta::from_tiff(&tiff);
+ metadata.width = Some(crop.width as u32);
+ metadata.height = Some(crop.height as u32);
+ let orientation = raw.u16(t::ORIENTATION).or_else(|| tiff.ifds.first().and_then(|i| i.u16(t::ORIENTATION))).unwrap_or(1);
+ let img = RawImage {
+ format: RawFormat::Mef,
+ width: w,
+ height: h,
+ cpp: 1,
+ data,
+ cfa: Some(cfa),
+ bits: 12,
+ black,
+ white: vec![white],
+ active_area: Rect::new(0, 0, w, h),
+ crop,
+ orientation: Orientation::from_exif(orientation),
+ color: ColorData::default(),
+ wb_multipliers: wb,
+ linearized: false,
+ opcodes: OpcodeLists::default(),
+ metadata,
++ lens_metadata: Default::default(),
+ };
+ img.validate_for(mode)?;
+ Ok(img)
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+ use lightcraft_tiff::{ByteOrder, IfdBuilder, ImageData, TiffWriter, Value};
+
+ fn pack(px: &[u16]) -> Vec<u8> {
+ px.chunks(2).flat_map(|p| [(p[0] >> 4) as u8, ((p[0] & 15) << 4) as u8 | (p[1] >> 8) as u8, p[1] as u8]).collect()
+ }
+
+ fn sample(w: u32, h: u32, px: &[u16]) -> Vec<u8> {
+ let mut raw = IfdBuilder::new();
+ raw.set(t::NEW_SUBFILE_TYPE, Value::Long(vec![0]));
+ raw.set(t::IMAGE_WIDTH, Value::Long(vec![w]));
+ raw.set(t::IMAGE_LENGTH, Value::Long(vec![h]));
+ raw.set(t::BITS_PER_SAMPLE, Value::Short(vec![12]));
+ raw.set(t::COMPRESSION, Value::Short(vec![1]));
+ raw.set(t::PHOTOMETRIC, Value::Short(vec![t::photometric::CFA]));
+ raw.set(t::ORIENTATION, Value::Short(vec![6]));
+ raw.set(t::CFA_REPEAT_PATTERN_DIM, Value::Short(vec![2, 2]));
+ raw.set(t::CFA_PATTERN_EP, Value::Byte(vec![0, 1, 1, 2]));
+ raw.set_image(ImageData::Strips { rows_per_strip: h, strips: vec![pack(px)] });
+ let mut ifd0 = IfdBuilder::new();
+ ifd0.set(t::MAKE, Value::Ascii("Mamiya-OP Co.,Ltd.".into()));
+ ifd0.set(t::MODEL, Value::Ascii("MAMIYA ZD".into()));
+ ifd0.add_sub_ifd(raw);
+ TiffWriter::new(ByteOrder::Big, false).write(&[ifd0]).unwrap()
+ }
+
+ #[test]
+ fn decodes_packed_12_bit() {
+ let px: Vec<u16> = (0..16 * 6).map(|i| (i * 41 % 4096) as u16).collect();
+ let f = sample(16, 6, &px);
+ assert_eq!(crate::probe(&f), Some(RawFormat::Mef));
+ let r = crate::decode(&f).unwrap();
+ assert_eq!(r.data, RawData::U16(px));
+ assert_eq!(r.orientation, Orientation::from_exif(6));
+ assert_eq!(r.crop, Rect::new(0, 0, 16, 6));
+ assert_eq!(crate::probe_info(&f).unwrap(), r.info());
+ }
+
+ #[test]
+ fn malformed_files_never_panic() {
+ let px: Vec<u16> = (0..16 * 6).map(|i| (i * 7 % 4096) as u16).collect();
+ let f = sample(16, 6, &px);
+ for n in 0..f.len() {
+ let _ = crate::decode(&f[..n]);
+ }
+ for i in 0..f.len() {
+ for v in [0u8, 0xff] {
+ let mut g = f.clone();
+ g[i] = v;
+ let _ = crate::decode(&g);
+ let _ = crate::probe_info(&g);
+ }
+ }
+ }
+}
diff --cc crates/raw/tests/lossy_dng.rs
index dd34b5158,000000000..d2022b382
mode 100644,000000..100644
--- a/crates/raw/tests/lossy_dng.rs
+++ b/crates/raw/tests/lossy_dng.rs
@@@ -1,95 -1,0 +1,96 @@@
+//! Lossy DNG (compression 34892) round trip: write, decode, develop, compare with the source.
+
+use lightcraft_raw::{
+ BlackLevel, Cfa, ColorData, DngCompression, DngWriteOptions, Mat3, Method, OpcodeLists, Orientation, RawData, RawFormat, RawImage, Rect,
+};
+
+/// A smooth Bayer mosaic (12-bit, black 64) of `w × h` with a colour matrix and a white balance.
+fn mosaic(w: usize, h: usize) -> RawImage {
+ let data: Vec<u16> = (0..w * h)
+ .map(|i| {
+ let (x, y) = (i % w, i / w);
+ let c = (x % 2) + (y % 2);
+ (64.0 + 3000.0 * (x as f32 / w as f32) * (0.5 + c as f32 * 0.25) + 500.0 * (y as f32 / h as f32)) as u16
+ })
+ .collect();
+ let color = ColorData {
+ illuminant: [21, 0],
+ color_matrix: [Some(Mat3([[0.7, -0.1, -0.05], [-0.4, 1.2, 0.2], [-0.1, 0.2, 0.6]])), None],
+ as_shot_neutral: Some([0.5, 1.0, 0.7]),
+ ..Default::default()
+ };
+ RawImage {
+ format: RawFormat::Dng,
+ width: w,
+ height: h,
+ cpp: 1,
+ data: RawData::U16(data),
+ cfa: Some(Cfa::bayer("RGGB").unwrap()),
+ bits: 12,
+ black: BlackLevel::uniform(64.0),
+ white: vec![4095.0],
+ active_area: Rect::new(0, 0, w, h),
+ crop: Rect::new(2, 2, w - 4, h - 4),
+ orientation: Orientation::Rotate90,
+ color,
+ wb_multipliers: None,
+ linearized: false,
+ opcodes: OpcodeLists::default(),
+ metadata: Default::default(),
++ lens_metadata: Default::default(),
+ }
+}
+
+fn lossy(raw: &RawImage, long_edge: Option<u32>) -> Vec<u8> {
+ let opts = DngWriteOptions { compression: DngCompression::LossyJpeg { tile: 32, quality: 95, long_edge }, ..Default::default() };
+ lightcraft_raw::write_dng(raw, &opts).unwrap()
+}
+
+#[test]
+fn lossy_dng_round_trips_close_to_the_source() {
+ let raw = mosaic(80, 56);
+ let bytes = lossy(&raw, None);
+ let back = lightcraft_raw::decode(&bytes).unwrap();
+ assert_eq!((back.width, back.height, back.cpp, back.cfa.is_none()), (76, 52, 3, true), "demosaiced, default crop applied");
+ assert!(back.linearized);
+ assert_eq!(back.orientation, raw.orientation);
+ assert_eq!(back.color.color_matrix, raw.color.color_matrix);
+ assert_eq!(back.color.as_shot_neutral, raw.color.as_shot_neutral);
+ let a = raw.develop(Method::Ahd).unwrap();
+ let b = back.develop(Method::Ahd).unwrap();
+ assert_eq!((a.width, a.height), (b.width, b.height));
+ let (mut sum, mut worst) = (0f64, 0f32);
+ for (p, q) in a.data.iter().zip(&b.data) {
+ for k in 0..3 {
+ let d = (p[k].clamp(0.0, 1.0) - q[k]).abs();
+ sum += d as f64;
+ worst = worst.max(d);
+ }
+ }
+ let mean = sum / (a.data.len() * 3) as f64;
+ assert!(mean < 0.01 && worst < 0.08, "mean {mean} worst {worst}");
+}
+
+#[test]
+fn lossy_dng_scales_to_the_long_edge_and_is_smaller() {
+ let raw = mosaic(160, 96);
+ let small = lossy(&raw, Some(78));
+ let back = lightcraft_raw::decode(&small).unwrap();
+ assert_eq!(back.width.max(back.height), 78);
+ assert_eq!(back.width, 78);
+ assert!(back.height == 46 || back.height == 47, "{}", back.height);
+ let full = lightcraft_raw::write_dng(&raw, &DngWriteOptions { compression: DngCompression::Uncompressed, ..Default::default() }).unwrap();
+ assert!(small.len() < full.len() / 2, "{} vs {}", small.len(), full.len());
+}
+
+#[test]
+fn lossy_dng_of_linear_data_and_extreme_values() {
+ // a 3-sample linear raw with values above white, negative and non-finite samples: no panic, clipped
+ let (w, h) = (20usize, 12usize);
+ let data: Vec<f32> = (0..w * h * 3).map(|i| [2.0, -1.0, f32::NAN, 0.5, f32::INFINITY][i % 5]).collect();
+ let raw = RawImage { cpp: 3, cfa: None, data: RawData::F32(data), white: vec![1.0], black: BlackLevel::uniform(0.0), ..mosaic(w, h) };
+ let back = lightcraft_raw::decode(&lossy(&raw, Some(4))).unwrap();
+ assert_eq!(back.width.max(back.height), 4);
+ let rgb = back.develop(Method::Bilinear).unwrap();
+ assert!(rgb.data.iter().flatten().all(|v| v.is_finite() && (0.0..=1.0).contains(v)));
+}
diff --cc docs/parity-checklist.md
index ee05a4f55,e5e0feb9d..27051f50e
--- a/docs/parity-checklist.md
+++ b/docs/parity-checklist.md
@@@ -215,7 -211,7 +215,7 @@@ differences and missing bindings are i
| LR-EDIT-DETAIL-SUPERRES | Super resolution | P2 | ⬜ | | |
| LR-EDIT-DETAIL-AISHARPEN | AI sharpen | OOS | 🚫 | | |
| LR-EDIT-OPTICS-CA | Remove chromatic aberration | P1 | ✅ | `crates/ui-egui/src/panels/edit.rs` (checkbox), `ctl:optics.caRed`, `ctl:optics.caBlue` | |
- | LR-EDIT-OPTICS-PROFILE | Lens profile corrections | P1 | 🟡 | `ctl:optics.profileDistortion`, `ctl:optics.profileVignetting`, `crates/pipeline/src/optics.rs`, `crates/raw/src/vendor/rw2.rs`, `crates/raw/src/vendor/raf_lens.rs`, `crates/engine/src/crs.rs` | uses corrections embedded in the file: DNG `WarpRectilinear` / `WarpRectilinear2` (opcode 14, DNG 1.6: degree-14 radial polynomial, valid-radius clamp, reciprocal option, applied in list order with the spec's skip rule for a `WarpRectilinear` after an optional one, so Pixel DNGs' two-stage warp is corrected; up to 3 chained stages, sampled on the CPU because the polynomials need `f64`; issue #635) / `FixVignetteRadial`, and the camera's own distortion correction in Panasonic / Leica RW2 / RWL files (tag `0x0119`, read as the equivalent `WarpRectilinear`; on 79 photos from 13 bodies and 11 lenses the corrected render lines up with the camera JPEG within 0.75 px at 1440 px at the corners, from up to 13 % of the half diagonal uncorrected; issue #256); an imported preset's `LensProfileEnable=0` leaves them on, as Lightroom applies built-in corrections regardless (inferred); Sony ILCE-7RM4A Bayer ARW raw-IFD distortion tables (`0x7037`, 16-sample layout, native 3:2 framing) use the same warp path, enabled only for the independently validated ILCE-7RM4A with FE 24–105 and FE 200–600 lenses; ILCE-7RM6 verified against the camera JPEG on 54 photos with three lenses (`docs/sony-ilce7rm6-lens-corrections.md`); other Sony models remain uncorrected (see `docs/sony-lens-corrections.md`); Nikon NEF distortion from NikonNEFInfo (`0xc7d5`: polynomial, frame-filling zoom and the optical-centre offset left by in-body stabilisation; `crates/raw/src/vendor/nef_lens.rs`, `docs/nikon-lens-corrections.md`) for the Z 6II, Z 8 and Z f, full-frame image area only: F-mount lenses on the FTZ match the camera JPEG within 0.2 px median, Z lenses within 0.5–1.5 px except the 24-70 f/4's wide end, where the camera applies a further correction that is not in the readable data (7 px median at 24 mm, from 16 px / 106 px p95 uncorrected); Fujifilm RAF raw-IFD tables (`0xf00b` distortion, `0xf010` vignetting; `crates/raw/src/vendor/raf_lens.rs`, `docs/fuji-lens-corrections.md`) become a `WarpRectilinear` with the camera's frame-filling zoom and a `FixVignetteRadial` gain for 59 body models with sample files checked against the camera JPEG (the table indexed by the source radius with last knot at the corner; on 67 body/lens combinations the corrected render's radial shape error against the camera JPEG falls from a median of 0.72 % to 0.05 % of the half diagonal, largest 0.42 %, and the vignetting gain of the JPEG tracks the table at a median correlation of 0.987); files whose table scale is not the recorded image's half diagonal (X-S20, X-T30 III, X-M5, X-T5 crops) and six unmatched bodies stay uncorrected; Fujifilm's lateral CA table (`0xf00f`) is real but is not applied, because our demosaic already registers the planes and the stored shift over-corrects; vignetting and lateral CA data of Olympus and Sony raws not read yet; Reload from Disk reads the file's lens data for photos catalogued without it and turns the correction on for those still at import settings (issue #314); lens-profile database: a lensfun-format XML database in `LIGHTCRAFT_LENSFUN_DIR` (none is bundled) supplies distortion (poly3 / poly5 / PTLens), TCA and vignetting for lenses it lists (`crates/lensdb`, `crates/engine/src/lens_profiles.rs`, `docs/lens-profiles.md`); no settings switch or bundled database yet |
-| LR-EDIT-OPTICS-PROFILE | Lens profile corrections | P1 | 🟡 | `ctl:optics.profileDistortion`, `ctl:optics.profileVignetting`, `crates/pipeline/src/optics.rs`, `crates/raw/src/vendor/rw2.rs`, `crates/raw/src/vendor/raf_lens.rs`, `crates/engine/src/crs.rs` | uses corrections embedded in the file: DNG `WarpRectilinear` / `WarpRectilinear2` (opcode 14, DNG 1.6: degree-14 radial polynomial, valid-radius clamp, reciprocal option, applied in list order with the spec's skip rule for a `WarpRectilinear` after an optional one, so Pixel DNGs' two-stage warp is corrected; up to 3 chained stages, sampled on the CPU because the polynomials need `f64`; issue #635) / `FixVignetteRadial`, and the camera's own distortion correction in Panasonic / Leica RW2 / RWL files (tag `0x0119`, read as the equivalent `WarpRectilinear`; on 79 photos from 13 bodies and 11 lenses the corrected render lines up with the camera JPEG within 0.75 px at 1440 px at the corners, from up to 13 % of the half diagonal uncorrected; issue #256); an imported preset's `LensProfileEnable=0` leaves them on, as Lightroom applies built-in corrections regardless (inferred); Sony ILCE-7RM4A Bayer ARW raw-IFD distortion tables (`0x7037`, 16-sample layout, native 3:2 framing) use the same warp path, enabled only for the independently validated ILCE-7RM4A with FE 24–105 and FE 200–600 lenses; other Sony models remain uncorrected (see `docs/sony-lens-corrections.md`); Nikon NEF distortion from NikonNEFInfo (`0xc7d5`: polynomial, frame-filling zoom and the optical-centre offset left by in-body stabilisation; `crates/raw/src/vendor/nef_lens.rs`, `docs/nikon-lens-corrections.md`) for the Z 6II, Z 8 and Z f, full-frame image area only: F-mount lenses on the FTZ match the camera JPEG within 0.2 px median, Z lenses within 0.5–1.5 px except the 24-70 f/4's wide end, where the camera applies a further correction that is not in the readable data (7 px median at 24 mm, from 16 px / 106 px p95 uncorrected); Fujifilm RAF raw-IFD tables (`0xf00b` distortion, `0xf010` vignetting; `crates/raw/src/vendor/raf_lens.rs`, `docs/fuji-lens-corrections.md`) become a `WarpRectilinear` with the camera's frame-filling zoom and a `FixVignetteRadial` gain for 59 body models with sample files checked against the camera JPEG (the table indexed by the source radius with last knot at the corner; on 67 body/lens combinations the corrected render's radial shape error against the camera JPEG falls from a median of 0.72 % to 0.05 % of the half diagonal, largest 0.42 %, and the vignetting gain of the JPEG tracks the table at a median correlation of 0.987); files whose table scale is not the recorded image's half diagonal (X-S20, X-T30 III, X-M5, X-T5 crops) and six unmatched bodies stay uncorrected; Fujifilm's lateral CA table (`0xf00f`) is real but is not applied, because our demosaic already registers the planes and the stored shift over-corrects; vignetting and lateral CA data of Olympus raws not read yet; Sony ARW shading, lateral CA and distortion tables (raw IFD `0x7031`–`0x7037`, with the encrypted `SR2SubIFD` copies as a checked fallback) are decoded with their camera settings, provenance and framing evidence for every body that records them (`crates/raw/src/lens_metadata.rs`), but only the ILCE-7RM4A distortion above is applied: on the ILCE-7CR the unchanged distortion interpretation misses the camera JPEG at 24 mm (median 1.6 px, p95 6.2 px at 2376 px), and Sony shading and CA semantics are not validated on any body (`docs/sony-lens-corrections.md`); Reload from Disk reads the file's lens data for photos catalogued without it and turns the correction on for those still at import settings (issue #314); lens-profile database: a lensfun-format XML database in `LIGHTCRAFT_LENSFUN_DIR` (none is bundled) supplies distortion (poly3 / poly5 / PTLens), TCA and vignetting for lenses it lists (`crates/lensdb`, `crates/engine/src/lens_profiles.rs`, `docs/lens-profiles.md`); no settings switch or bundled database yet |
++| LR-EDIT-OPTICS-PROFILE | Lens profile corrections | P1 | 🟡 | `ctl:optics.profileDistortion`, `ctl:optics.profileVignetting`, `crates/pipeline/src/optics.rs`, `crates/raw/src/vendor/rw2.rs`, `crates/raw/src/vendor/raf_lens.rs`, `crates/engine/src/crs.rs` | uses corrections embedded in the file: DNG `WarpRectilinear` / `WarpRectilinear2` (opcode 14, DNG 1.6: degree-14 radial polynomial, valid-radius clamp, reciprocal option, applied in list order with the spec's skip rule for a `WarpRectilinear` after an optional one, so Pixel DNGs' two-stage warp is corrected; up to 3 chained stages, sampled on the CPU because the polynomials need `f64`; issue #635) / `FixVignetteRadial`, and the camera's own distortion correction in Panasonic / Leica RW2 / RWL files (tag `0x0119`, read as the equivalent `WarpRectilinear`; on 79 photos from 13 bodies and 11 lenses the corrected render lines up with the camera JPEG within 0.75 px at 1440 px at the corners, from up to 13 % of the half diagonal uncorrected; issue #256); an imported preset's `LensProfileEnable=0` leaves them on, as Lightroom applies built-in corrections regardless (inferred); Sony ILCE-7RM4A Bayer ARW raw-IFD distortion tables (`0x7037`, 16-sample layout, native 3:2 framing) use the same warp path, enabled only for the independently validated ILCE-7RM4A with FE 24–105 and FE 200–600 lenses; ILCE-7RM6 verified against the camera JPEG on 54 photos with three lenses (`docs/sony-ilce7rm6-lens-corrections.md`); other Sony models remain uncorrected (see `docs/sony-lens-corrections.md`); Nikon NEF distortion from NikonNEFInfo (`0xc7d5`: polynomial, frame-filling zoom and the optical-centre offset left by in-body stabilisation; `crates/raw/src/vendor/nef_lens.rs`, `docs/nikon-lens-corrections.md`) for the Z 6II, Z 8 and Z f, full-frame image area only: F-mount lenses on the FTZ match the camera JPEG within 0.2 px median, Z lenses within 0.5–1.5 px except the 24-70 f/4's wide end, where the camera applies a further correction that is not in the readable data (7 px median at 24 mm, from 16 px / 106 px p95 uncorrected); Fujifilm RAF raw-IFD tables (`0xf00b` distortion, `0xf010` vignetting; `crates/raw/src/vendor/raf_lens.rs`, `docs/fuji-lens-corrections.md`) become a `WarpRectilinear` with the camera's frame-filling zoom and a `FixVignetteRadial` gain for 59 body models with sample files checked against the camera JPEG (the table indexed by the source radius with last knot at the corner; on 67 body/lens combinations the corrected render's radial shape error against the camera JPEG falls from a median of 0.72 % to 0.05 % of the half diagonal, largest 0.42 %, and the vignetting gain of the JPEG tracks the table at a median correlation of 0.987); files whose table scale is not the recorded image's half diagonal (X-S20, X-T30 III, X-M5, X-T5 crops) and six unmatched bodies stay uncorrected; Fujifilm's lateral CA table (`0xf00f`) is real but is not applied, because our demosaic already registers the planes and the stored shift over-corrects; vignetting and lateral CA data of Olympus raws not read yet; Sony ARW shading, lateral CA and distortion tables (raw IFD `0x7031`–`0x7037`, with the encrypted `SR2SubIFD` copies as a checked fallback) are decoded with their camera settings, provenance and framing evidence for every body that records them (`crates/raw/src/lens_metadata.rs`), but only the ILCE-7RM4A and ILCE-7RM6 distortion above is applied: on the ILCE-7CR the unchanged distortion interpretation misses the camera JPEG at 24 mm (median 1.6 px, p95 6.2 px at 2376 px), and Sony shading and CA semantics are not validated beyond the ILCE-7RM6's (lateral CA off) (`docs/sony-lens-corrections.md`); Reload from Disk reads the file's lens data for photos catalogued without it and turns the correction on for those still at import settings (issue #314); lens-profile database: a lensfun-format XML database in `LIGHTCRAFT_LENSFUN_DIR` (none is bundled) supplies distortion (poly3 / poly5 / PTLens), TCA and vignetting for lenses it lists (`crates/lensdb`, `crates/engine/src/lens_profiles.rs`, `docs/lens-profiles.md`); no settings switch or bundled database yet |
)
| LR-EDIT-OPTICS-DEFRINGE | Defringe | P1 | ✅ | `ctl:optics.defringe*` | no fringe eyedropper |
| LR-EDIT-OPTICS-MANUAL | Manual distortion / vignetting | P1 | ✅ | `ctl:optics.distortion`, `ctl:optics.vignetting`, `ctl:optics.vignettingMidpoint` | |
@@@ -797,13 -790,7 +797,14 @@@ Latin stays upright. Advanced Japanese
| Date | Change | Summary |
|---|---|---|
-| 2026-10-12 | minor | LR-EDIT-OPTICS-PROFILE: Sony ARW shading, CA and distortion tables decoded with settings and provenance (raw IFD, `SR2SubIFD` fallback); applied corrections unchanged (ILCE-7RM4A distortion only); status unchanged (🟡) |
++| 2026-10-12 | minor | LR-EDIT-OPTICS-PROFILE: Sony ARW shading, CA and distortion tables decoded with settings and provenance (raw IFD, `SR2SubIFD` fallback); applied corrections unchanged (ILCE-7RM4A and ILCE-7RM6 distortion); status unchanged (🟡) |
+| 2026-10-14 | minor | LR-IMP-CAMERA-COVERAGE: 17 uncompressed Olympus and Samsung Adobe models verified on one CC0 file each; status unchanged (🟡) |
+| 2026-10-13 | minor | LR-EDIT-SECTION-SOLO: Solo Mode for Edit sections (Window ▸ Edit Sections, section-header menu, ⌥-click); KEYC-PANELS notes it |
+| 2026-10-14 | minor | LR-MERGE-HDR ✅ → 🟡: tested on real 67 MP Sony brackets; moving clipped highlights (black-hole stars) and dark-sky ghosts fixed, merge memory cut ~25 %; AI Denoise on merged DNGs, JPEG response curves and per-frame memory remain (gap 29). LR-MERGE-HEADLESS, MENU-PHOTO-MERGE: HDR for Each Bracket Set (`merge.hdrBatch`) |
+| 2026-10-15 | patch | LR-VIEW-BEFOREAFTER: side by side and stacked panes share click-zoom animation for whole-frame images, window tiles and input; render requests keep the final target (#1821); status unchanged |
+| 2026-10-14 | patch | LR-META-XMP: automatic sidecar writes that fail (folder offline or read-only) wait and are written again once the files are reachable, with a notice (`cmd:library.retrySidecars`); status unchanged |
+| 2026-10-13 | minor | LR-BEHAV-WINDOW (new, 🟡): the desktop app's window opens at the size it was left at, zoomed / maximized if it was; full screen and the place on the desktop are not remembered |
+| 2026-10-14 | minor | LR-IMP-CAMERA-COVERAGE: Nikon D600, D610 and D5000 NEFs drop the filler and noise columns on the right (a band), and the D600 and D610 measure their white level inside the image (#1627); status unchanged (🟡) |
| 2026-10-13 | patch | LR-VIEW-COMPARE, LRC-LIB-COMPARE: ⌘/Ctrl- and Shift-clicks on the filmstrip in Compare change the selection as in the grid; the two photos shown stay selected and the active photo is unchanged (#187); statuses unchanged |
| 2026-10-13 | minor | KEYC-SELECTONLYACTIVE: Edit → Select Only Active Photo (⌘⇧D) lands as a new row; status 🟡 (headless-tested, native macOS menu path unverified) |
| 2026-10-13 | patch | LRC-LIB-CATALOG-IMPORT: a catalog whose tables are all still in its `-wal` file (main file schema format 0) opens instead of failing as an unsupported schema format; status unchanged |
diff --cc docs/sony-lens-corrections.md
index dbc8090c7,53c5e9755..ed32fc85d
--- a/docs/sony-lens-corrections.md
+++ b/docs/sony-lens-corrections.md
@@@ -1,23 -1,124 +1,130 @@@
- # Sony embedded distortion corrections
+ # Sony embedded lens corrections
+The **ILCE-7RM6** (A7R VI) has its own conversion, with other conventions measured against its camera JPEGs:
+[Sony ARW lens corrections](sony-ilce7rm6-lens-corrections.md). On ILCE-7RM6 photos the formula below misses the 24–70 GM II by
+up to 1.43 px at 1440 px while fitting the 100 mm and 200–600 better, and no single unit fits every lens under either
+convention; that page's *Two conventions* section has the comparison and what would settle it.
+
- LightCraft reads the signed 16-sample distortion table in the raw image IFD (`0x7037`) of
- Sony **ILCE-7RM4A (A7R IVA)** Bayer ARWs. Other camera models are deliberately left uncorrected,
- even when they contain a table with the same layout.
- It converts that table into `OpcodeList3` / `WarpRectilinear`, so the existing profile correction control,
- CPU/GPU optics path, EXIF orientation handling, and DNG export use the same correction.
+ Sony ARWs record three lens corrections per shot in the raw image IFD: shading (vignetting), lateral chromatic
+ aberration (CA) and distortion, each as a camera setting and a radial table, with copies in the encrypted
+ `SR2SubIFD`. LightCraft **decodes all three** wherever a file carries them and **applies one**: the distortion table
+ of the **ILCE-7RM4A (A7R IVA)** in Bayer ARWs with native 3:2 framing, independently validated with the
-FE 24–105mm F4 G OSS and FE 200–600mm F5.6–6.3 G OSS. Every other model, and shading and CA on every model, are
-decoded only: no warp, vignetting or CA correction is added for them. The decoded record stays with the image
++FE 24–105mm F4 G OSS and FE 200–600mm F5.6–6.3 G OSS (the ILCE-7RM6's distortion is applied too, by its own
++conversion above). Every other model,
++and shading and CA on every model, are decoded only: no warp, vignetting or CA correction is added for them. The decoded record stays with the image
+ (`RawInfo::lens_metadata` / `RawImage::lens_metadata`, types in `crates/raw/src/lens_metadata.rs`, decoder in
+ `crates/raw/src/vendor/arw_lens.rs`) for diagnostics and for the validation work described below.
- This adds **distortion only**. Sony vignetting and lateral chromatic-aberration tables are not decoded.
- The enabled and independently validated model is ILCE-7RM4A with the FE 24–105mm F4 G OSS and
- FE 200–600mm F5.6–6.3 G OSS. Linear YCbCr ARWs and non-3:2 camera crops are excluded pending validation. An aspect crop may
- retain the full-frame radial normalization; the decoder does not guess that relationship. Older files that carry
- only encrypted correction metadata, different table lengths, and rejected tables remain uncorrected.
- The camera's distortion Off setting does not erase its table. Newly imported photos use LightCraft's existing
- embedded-profile default (enabled); the profile correction control can disable or adjust it.
+ The applied distortion is converted into `OpcodeList3` / `WarpRectilinear`, so the existing profile correction
+ control, CPU/GPU optics path, EXIF orientation handling, and DNG export use the same correction. Linear YCbCr ARWs
+ and non-3:2 camera crops are excluded pending validation. An aspect crop may retain the full-frame radial
+ normalization; the decoder does not guess that relationship. Tables of other lengths and rejected tables remain
+ uncorrected, and a table found only in the encrypted `SR2SubIFD` is never applied. The camera's distortion Off
+ setting does not erase its table. Newly imported photos use LightCraft's existing embedded-profile default
+ (enabled); the profile correction control can disable or adjust it.
+
+ ## What is decoded
+
+ | Component | Raw-IFD setting / table | `SR2SubIFD` setting / table | Accepted layout |
+ |---|---|---|---|
+ | Shading | `0x7031` / `0x7032` | `0x797c` / `0x797d` | SHORT\[1\]; SSHORT\[17\]: 16, then 16 samples |
+ | Lateral CA | `0x7034` / `0x7035` | `0x797f` / `0x7980` | SHORT\[1\]; SSHORT\[33\]: 32, then two 16-sample curves |
+ | Distortion | `0x7036` / `0x7037` | `0x7981` / `0x7982` | SHORT\[1\]; SSHORT\[17\]: 16, then 16 samples |
+
+ - **Bounded, exact reading.** A raw-IFD field is the one the TIFF reader parsed, the copy the image itself uses:
+ its first occurrence, within the reader's limits (8,192 entries per directory, a total value budget). The directory
+ is also scanned, in either byte order and in BigTIFF, for diagnostics only: a tag that occurs more than once is
+ flagged (`raw_ifd_repeated`, `sr2_repeated`), and an entry the reader left out is reported with its reason (values
+ outside the file, an unknown type, or past the reader's limits) rather than treated as absent. `SR2SubIFD` fields
+ are read straight from their first entry. A setting may also be a nonnegative SSHORT (the documented editor type
+ differs from the type the cameras store). Anything else is refused with its reason (`SonyFieldError`: field type,
+ count, leading sample count, values outside the file or block, past the reader's limits, a negative setting).
+ Nothing is allocated from a count in the file.
+ - **Raw IFD first, `SR2SubIFD` as a checked fallback.** The raw IFD's copy wins. The `SR2SubIFD`'s copy is used only
+ when the raw IFD lacks the tag; a malformed raw-IFD copy is reported, never silently replaced. When both copies are
+ well-formed their agreement is recorded (`copies_agree`), and on disagreement the raw IFD's copy is used. The block
+ is decrypted (by the existing `vendor/sr2.rs`) for every ARW that has one, so the check always runs; a block that
+ can't be found or decrypted gives no copies. Locating it selects the `SR2Private` directory's offset, length and
+ key exactly as the TIFF reader would (the reader's own directory layout, 8,192-entry limit, readable-entry rules
+ and value budget, then the first readable occurrence of each), but decodes only each field's first value, so the
+ directory's other entries cost no memory however large they claim to be; the block itself is limited to 1 MiB. The
+ plain tag numbers are not looked up inside the `SR2SubIFD`.
+ - **Settings keep their number and a documented meaning:** shading 256 Off, 257 and 272 Auto, 511 unavailable; CA
+ 0 Off, 1 Auto, 255 unavailable; distortion 0 Off, 1 Auto, 17 Auto fixed by the lens, 255 unavailable. Any other value
+ is `Unknown(value)`, never Off (the A7CR's Low shading setting has no documented number). Values are not read as
+ bit masks. Off does not mean absent: the table is still recorded.
+ - **Only the 16-sample (CA: 2 × 16) layout is interpreted.** Many bodies store 11 (CA: 22) samples padded into the
+ same 17 (33) values: older compacts and bodies such as the DSC-RX100 III and ILCE-3500, and the ILCE-7M4's APS-C
+ captures (its full-frame captures carry 16). Those tables are refused as `CountPrefix(11)` and stay
+ uninterpreted. An all-zero table is kept: it is a valid no-op, not a sign of an unsupported lens.
+ - **Framing evidence travels with the tables:** the stored frame (padding included), Sony's crop tags and the DNG
+ default crop as recorded, the image crop, the reference rectangle the distortion conversion is centred on and
+ normalized to (the default crop when present, else the image crop), compression, whether the data is linear
+ YCbCr (lossless M/S), and whether both crops are native 3:2.
+ - **Per-component status.** `Applied` (converted into an opcode), `NoTable`, `NotValidatedComponent` (shading and CA
+ on every body), `NotValidatedModel`, `NotValidatedMode` (linear YCbCr, non-3:2 framing, or a distortion table found
+ only in the `SR2SubIFD`), `Neutral` (all-zero table in the validated scope) and `Rejected` (the validated
+ conversion refused the table). A readable table never implies a usable correction; only `Applied` components
+ become opcodes.
+
+ The applied path is unchanged: the validated conversion still receives the raw IFD's `0x7037` table exactly as the
+ TIFF reader parsed it, with the same model, framing and fit checks, and produces the same warp. A repeated tag keeps
+ the reader's choice and is only flagged, and a table the reader never reads (past its entry limit) is reported but
+ never applied, so already-catalogued photos, their previews and their stored lens data stay consistent with a fresh
+ decode.
+
+ ## What each component still needs
+
+ The following summarizes an investigation on the ILCE-7CR (A7CR): an owner's collection of 131 raws with four
+ lenses (Voigtländer APO-LANTHAR 28mm F2 and 50mm F2, both manual focus; Sony FE 24–50mm F2.8 G; Tamron 35–150mm
+ F2–2.8), plus the three public raw.pixls.us A7CR samples. No private photographs, names or derived images are
+ part of this repository.
+
+ - **Inventory.** All 131 raws carry all three tables in both the raw IFD and the `SR2SubIFD`, and the copies agree
+ in every file; all four lenses provide tables, the manual-focus ones included. Shading is 272 (Auto) and CA 1
+ (Auto) everywhere; distortion is 17 (Auto, fixed by the lens) on the nine FE 24–50 files and 0 (Off) on the rest.
+ Every image crop is 9504 × 6336 at (32, 20), in uncompressed, compressed and lossless-L (padded to 9728 × 6656)
+ storage. The decoder reproduces this inventory exactly (see *Local inventory check*).
+ - **Distortion.** The table layout and crop tags match the A7R IVA's, but its interpretation applied unchanged does
+ not reproduce the A7CR camera JPEG at 24 mm: at 2376 px wide, median / 95th-percentile feature residuals were
+ 1.61 / 6.17 px (12.98 / 51.19 px uncorrected, 6,539 matches), with a signed radial pattern (about +1.7 px at
+ radii 0.25–0.5, −7.4 px at 0.8–1) that no translation removes. At 50 mm it agrees (0.23 / 1.05 px, from
+ 2.21 / 12.25 px). Scaling the raw table by about 10/9, or using the MakerNote's distortion array instead, brings
+ 24 mm to about 0.37 / 1.10 and 0.33 / 0.97 px. Those are exploratory comparisons on two files against the camera
+ JPEG, not a model: the MakerNote values are roughly 1.11 times the raw table without one exact ratio, and the
+ A7R IVA was validated against a different reference renderer (Imaging Edge exports). Needed: A7CR Imaging Edge
+ On/Off exports at 24 and 50 mm and with another lens, compared with the raw table at its fixed scale; a decision on
+ the reference (camera JPEG or Imaging Edge); then ILCE-7CR added to the validated models for the full-frame Bayer
+ 3:2 mode only. Neither a 10/9 multiplier nor the MakerNote table is used.
+ - **Shading.** The layout decodes, but no public specification gives the sample-to-gain function, its radius
+ convention on the A7CR, or whether the raw samples already contain part of the correction. Brightness ratios on six
+ natural scenes show no consistent missing peripheral brightening. Needed: evenly lit flat fields with shading
+ Off / Auto / Low at several apertures and focal lengths, raw CFA ratios between them, and Imaging Edge On/Off
+ exports, to fix the gain function and processing stage and rule out double correction.
+ - **Lateral CA.** A public analysis reads the table as a red, then a blue curve relative to green, at a 2⁻²¹
+ relative radial scale. On three distortion-Off files that candidate reduced red and blue feature residuals
+ modestly (for example 0.35 → 0.29 px median for red with the 28 mm), which supports investigating it but certifies
+ neither scale, interpolation nor processing stage. Two product changes come before it: embedded CA must be
+ independent of the distortion amount (today `pipeline::optics` scales the whole embedded per-plane warp with the
+ profile distortion control), and the red and blue planes must be resampled in camera RGB, before the camera colour
+ model mixes them (`engine::files::load_bytes_now` applies the colour model before the optics stage). Then
+ validation on full-resolution achromatic edges.
+ - **Other modes.** APS-C, aspect crops and lossless M/S (linear YCbCr) captures were not measured and stay
+ excluded.
+
+ ## Local inventory check
+
+ `crates/raw/tests/sony_lens_inventory.rs` is opt-in and does nothing in CI. With `LIGHTCRAFT_SONY_LENS_DIR` naming a
+ directory of ARWs it decodes each from its headers and prints its record (settings, sources, copies' agreement,
+ sample ranges, statuses); `LIGHTCRAFT_SONY_LENS_FULL=1` also compares header and full decode. With
+ `LIGHTCRAFT_SONY_LENS_INVENTORY` naming a CSV inventory it checks every file's table counts, sizes, sample counts,
+ minima and maxima, raw-IFD versus `SR2SubIFD` agreement, settings (numbers and labels), stored size, compression and
+ crop tags. A file with a row must decode, and every file must have a row, whether it decodes or not. Any decode
+ error fails the check except an unsupported variant, which is counted and must equal `LIGHTCRAFT_SONY_LENS_UNSUPPORTED`
+ (default 0). On the 131 A7CR raws above, all 131 inventory rows reproduce and every file decodes. On 133 public Sony
+ ARWs (127 raw.pixls.us samples from 100 bodies, four corpus copies and two files attached to a public issue), 110 carry
+ a record, 14 decode without one, and 9 are unsupported variants (eight packed ARWs of older bodies and one ARW
+ without a CFA image); header and full decode agree on the 124 that decode, and no record has disagreeing copies.
## Clean-room evidence