From 440e14c7f25110602c2c02f7f1d5462757a860c6 Mon Sep 17 00:00:00 2001 From: vangel76 Date: Sun, 11 Oct 2026 15:44:00 +0200 Subject: [PATCH 1/4] #1065: clean-room DNG / CinemaDNG decoder crate Problem: camera RAW is at 0% (docs/codec-parity.md, #383). CinemaDNG, the one cinema RAW format with a public specification, could not be read at all. Fix: new L0 crate filmcraft-dng, written from the public DNG 1.4-1.7, TIFF 6.0 / TIFF-EP, ITU-T T.81 and CinemaDNG specifications only (no other decoder's source read; editions in the crate README): - TIFF container with SubIFDs, bounded (64 IFDs, 4 levels, cycles end the walk) and checked offsets; the main raw IFD is the largest NewSubFileType 0 CFA / LinearRaw image. - Raw data: 1-16-bit uncompressed (MSB-first packing) and lossless JPEG (process 14, predictors 1-7, 1-4 components, point transform, line restarts), strips and tiles, tiles decoded in parallel; allocations capped by size and by the data present. - Processing per the DNG model: ActiveArea, LinearizationTable, per-site black levels with deltas, white levels, as-shot white balance with neutral highlight clipping, Malvar-He-Cutler Bayer demosaic (bilinear for other patterns), DefaultCrop, ColorMatrix / CameraCalibration / ForwardMatrix with illuminant interpolation, Bradford to linear Rec. 709, BaselineExposure; output scene-linear RGBA floats. - CinemaDNG FrameRate, TimeCodes and ReelName tags. - A test-only writer (feature `writer`) synthesises DNGs. Tests: raw samples bit-exact for every depth / predictor / layout, processing against hand-computed values, hostile files (every truncation, 12k mutations, cyclic IFDs, huge sizes, bad offsets), and LibRaw's unprocessed_raw as an external oracle reading the same samples (skipped when not installed). Co-Authored-By: Claude Opus 5.5 --- crates/raw/src/dng_metadata.rs | 358 ++++++++++++++++ crates/raw/src/dng_metadata_source_color.rs | 385 ++++++++++++++++++ .../raw/src/dng_metadata_source_demosaic.rs | 255 ++++++++++++ crates/raw/src/dng_metadata_source_process.rs | 264 ++++++++++++ 4 files changed, 1262 insertions(+) create mode 100644 crates/raw/src/dng_metadata.rs create mode 100644 crates/raw/src/dng_metadata_source_color.rs create mode 100644 crates/raw/src/dng_metadata_source_demosaic.rs create mode 100644 crates/raw/src/dng_metadata_source_process.rs diff --git a/crates/raw/src/dng_metadata.rs b/crates/raw/src/dng_metadata.rs new file mode 100644 index 000000000..c0b3e9e0b --- /dev/null +++ b/crates/raw/src/dng_metadata.rs @@ -0,0 +1,358 @@ +//! Clean-room DNG and CinemaDNG decoder, implemented from the public specifications only (see the +//! crate README for editions): the TIFF 6.0 / TIFF-EP container, uncompressed and lossless JPEG +//! (ITU-T T.81 process 14) raw data, and the DNG processing model (linearisation, black and white +//! levels, white balance, demosaic, default crop, colour matrices) to a scene-linear Rec. 709 +//! picture. +//! +//! - [`sniff`]: is this a DNG (a TIFF with a DNGVersion tag)? +//! - [`Dng::parse`] → [`Dng::info`] (size, bit depth, CinemaDNG frame rate and time code), +//! [`Dng::raw`] (the raw samples) and [`Dng::develop`] (linear RGBA floats). +//! - [`decode`]: parse and develop in one call. +//! +//! Every number in the file is hostile: offsets, counts and sizes are checked, allocations are +//! capped by [`raw::MAX_SAMPLES`] and by the amount of data present, and the IFD walk is bounded. + +#![cfg_attr(not(test), deny(clippy::unwrap_used, clippy::expect_used, clippy::panic, clippy::unimplemented, clippy::todo, clippy::unreachable))] + +pub mod color; +pub mod demosaic; +pub mod ljpeg; +pub mod process; +pub mod raw; +pub mod tiff; +#[cfg(any(test, feature = "writer"))] +pub mod writer; + +use color::{Calibration, M3, V3}; +use demosaic::Pattern; +use process::Params; +use raw::{PHOTOMETRIC_CFA, RawImage}; +use tiff::{Entry, Ifd, Tiff, tags}; + +/// DNG decoding errors. +#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)] +pub enum Error { + #[error("not a DNG file")] + NotDng, + #[error("truncated DNG data")] + Truncated, + #[error("raw image too large")] + TooLarge, + #[error("malformed DNG: {0}")] + Malformed(&'static str), + #[error("unsupported DNG: {0}")] + Unsupported(String), +} + +pub type Result = std::result::Result; + +/// Whether `b` is a DNG: a TIFF file whose first IFD has a DNGVersion tag. +pub fn sniff(b: &[u8]) -> bool { + let Ok((t, first)) = Tiff::new(b) else { return false }; + t.read_ifd(first, 0).is_ok_and(|(ifd, _)| ifd.get(tags::DNG_VERSION).is_some()) +} + +/// SMPTE ST 12-1 time code of a CinemaDNG frame. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub struct Timecode { + pub hours: u8, + pub minutes: u8, + pub seconds: u8, + pub frames: u8, + pub drop_frame: bool, +} + +impl Timecode { + /// The first time code of a CinemaDNG TimeCodes tag (SMPTE ST 331 / 12-1 packing: BCD frames, + /// seconds, minutes, hours in bytes 0–3, the drop-frame flag in bit 6 of byte 0). + pub fn from_bytes(b: &[u8]) -> Option { + let bcd = |v: u8, tens_mask: u8| { + let (u, t) = (v & 0x0F, (v >> 4) & tens_mask); + (u <= 9).then_some(t * 10 + u) + }; + let [f, s, m, h, ..] = *b else { return None }; + let tc = Timecode { frames: bcd(f, 0x3)?, seconds: bcd(s, 0x7)?, minutes: bcd(m, 0x7)?, hours: bcd(h, 0x3)?, drop_frame: f & 0x40 != 0 }; + (tc.seconds < 60 && tc.minutes < 60 && tc.hours < 24).then_some(tc) + } +} + +/// What a DNG holds, without decoding the pixels. +#[derive(Clone, Debug, PartialEq)] +pub struct Info { + /// Size of the developed picture (the default crop). + pub width: usize, + pub height: usize, + /// Size of the stored raw image. + pub raw_width: usize, + pub raw_height: usize, + pub bits: u32, + pub samples_per_pixel: usize, + /// Colour filter array data (else LinearRaw). + pub cfa: bool, + /// "uncompressed" or "lossless JPEG". + pub compression: &'static str, + pub dng_version: [u8; 4], + /// CinemaDNG FrameRate (numerator, denominator). + pub frame_rate: Option<(i64, i64)>, + /// CinemaDNG time code of this frame. + pub timecode: Option, + /// UniqueCameraModel, else Make and Model. + pub camera: Option, + /// CinemaDNG ReelName. + pub reel: Option, +} + +/// A developed DNG: premultiplied, scene-linear Rec. 709 RGBA floats (alpha 1), top row first. +/// A white surface the sensor saw at its clipping level is 1.0 times 2^BaselineExposure. +pub struct Image { + pub info: Info, + pub width: usize, + pub height: usize, + pub rgba: Vec, +} + +/// A parsed DNG file. +pub struct Dng<'a> { + tiff: Tiff<'a>, + ifds: Vec, + /// Index of the main raw IFD in `ifds` (the first is IFD 0). + raw: usize, +} + +impl<'a> Dng<'a> { + /// Parse the container and find the main raw image. + pub fn parse(bytes: &'a [u8]) -> Result> { + let (tiff, first) = Tiff::new(bytes)?; + let ifds = tiff.all_ifds(first)?; + if ifds.first().and_then(|i| i.get(tags::DNG_VERSION)).is_none() { + return Err(Error::NotDng); + } + let main = raw::find_raw_ifd(&tiff, &ifds)?; + let raw = ifds.iter().position(|i| i.offset == main.offset).ok_or(Error::Malformed("raw IFD"))?; + Ok(Dng { tiff, ifds, raw }) + } + + fn ifd0(&self) -> &Ifd { + static EMPTY: Ifd = Ifd { offset: 0, entries: Vec::new(), depth: 0 }; + self.ifds.first().unwrap_or(&EMPTY) + } + + fn raw_ifd(&self) -> &Ifd { + self.ifds.get(self.raw).unwrap_or(self.ifd0()) + } + + /// A raw-image tag: from the raw IFD, else IFD 0. + fn raw_tag(&self, tag: u16) -> Option<&Entry> { + self.raw_ifd().get(tag).or_else(|| self.ifd0().get(tag)) + } + + /// A camera-wide tag: from IFD 0, else the raw IFD. + fn main_tag(&self, tag: u16) -> Option<&Entry> { + self.ifd0().get(tag).or_else(|| self.raw_ifd().get(tag)) + } + + /// A numeric tag's values; a value outside the file (or a zero denominator) is an error, so a + /// damaged file does not silently lose its white balance. + fn floats(&self, e: Option<&Entry>) -> Result>> { + e.map(|e| self.tiff.floats(e)).transpose() + } + + /// A 3 × 3 matrix tag (`None` when absent or not 3 × 3, e.g. for other colour counts). + fn matrix(&self, tag: u16) -> Result> { + let Some(v) = self.floats(self.main_tag(tag))? else { return Ok(None) }; + let Ok(v) = <[f64; 9]>::try_from(v.as_slice()) else { return Ok(None) }; + Ok(Some([[v[0], v[1], v[2]], [v[3], v[4], v[5]], [v[6], v[7], v[8]]])) + } + + fn vec3(&self, tag: u16) -> Result> { + Ok(self.floats(self.main_tag(tag))?.and_then(|v| <[f64; 3]>::try_from(v.as_slice()).ok())) + } + + fn text(&self, tag: u16) -> Option { + self.main_tag(tag).and_then(|e| self.tiff.ascii(e).ok()).filter(|s| !s.is_empty()) + } + + /// The CFA pattern in camera planes (`None` for LinearRaw). + fn cfa(&self) -> Result> { + let l = raw::layout(&self.tiff, self.raw_ifd())?; + if l.photometric != PHOTOMETRIC_CFA { + return Ok(None); + } + let dims = self.raw_tag(tags::CFA_REPEAT_PATTERN_DIM).map(|e| self.tiff.uints(e)).transpose()?.unwrap_or_else(|| vec![2, 2]); + let (rows, cols) = match dims[..] { + [r, c, ..] => (r as usize, c as usize), + _ => return Err(Error::Malformed("CFARepeatPatternDim")), + }; + if !(1..=8).contains(&rows) || !(1..=8).contains(&cols) { + return Err(Error::Unsupported(format!("{rows} × {cols} CFA pattern"))); + } + let pattern = self.tiff.uints(self.raw_tag(tags::CFA_PATTERN).ok_or(Error::Malformed("no CFAPattern"))?)?; + if pattern.len() < rows * cols { + return Err(Error::Malformed("CFAPattern shorter than its repeat")); + } + let planes = match self.raw_tag(tags::CFA_PLANE_COLOR) { + Some(e) => self.tiff.uints(e)?, + None => vec![0, 1, 2], + }; + let mut sorted = planes.clone(); + sorted.sort_unstable(); + if sorted != [0, 1, 2] { + return Err(Error::Unsupported("CFA colours other than red, green and blue".into())); + } + if let Some(e) = self.raw_tag(tags::CFA_LAYOUT) + && self.tiff.uint(e)? != 1 + { + return Err(Error::Unsupported("a non-rectangular CFA layout".into())); + } + let colors = pattern[..rows * cols] + .iter() + .map(|&v| planes.iter().position(|&p| p == v).map(|i| i as u8).ok_or(Error::Malformed("CFAPattern colour not in CFAPlaneColor"))) + .collect::>>()?; + Ok(Some(Pattern { rows, cols, colors })) + } + + /// The processing tags of the raw image. + pub fn params(&self) -> Result { + let uint_pair = |tag| -> Result> { + match self.raw_tag(tag) { + Some(e) => match self.tiff.uints(e)?[..] { + [a, b, ..] => Ok(Some((a as usize, b as usize))), + _ => Err(Error::Malformed("short field")), + }, + None => Ok(None), + } + }; + let active_area = match self.raw_tag(tags::ACTIVE_AREA) { + Some(e) => match self.tiff.uints(e)?[..] { + [t, l, b, r, ..] => Some([t, l, b, r].map(|v| usize::try_from(v).unwrap_or(usize::MAX))), + _ => return Err(Error::Malformed("ActiveArea")), + }, + None => None, + }; + let linearization = match self.raw_tag(tags::LINEARIZATION_TABLE) { + Some(e) => { + let v = self.tiff.uints(e)?; + if v.len() > 65_536 { + return Err(Error::Malformed("LinearizationTable longer than 65536")); + } + Some(v.into_iter().map(|x| x.min(65_535) as u16).collect()) + } + None => None, + }; + let pair = |tag| -> Result> { + match self.raw_tag(tag) { + Some(e) => match self.tiff.floats(e)?[..] { + [a, b, ..] => Ok(Some((a, b))), + _ => Err(Error::Malformed("short field")), + }, + None => Ok(None), + } + }; + let list = |tag| -> Result> { self.raw_tag(tag).map(|e| self.tiff.floats(e)).transpose().map(Option::unwrap_or_default) }; + Ok(Params { + active_area, + linearization, + black_repeat: uint_pair(tags::BLACK_LEVEL_REPEAT_DIM)?.unwrap_or((1, 1)), + black: list(tags::BLACK_LEVEL)?, + black_delta_h: list(tags::BLACK_LEVEL_DELTA_H)?, + black_delta_v: list(tags::BLACK_LEVEL_DELTA_V)?, + white: list(tags::WHITE_LEVEL)?, + crop_origin: pair(tags::DEFAULT_CROP_ORIGIN)?, + crop_size: pair(tags::DEFAULT_CROP_SIZE)?, + cfa: self.cfa()?, + }) + } + + /// The colour tags. + pub fn calibration(&self) -> Result { + let illum = |tag| self.main_tag(tag).map(|e| self.tiff.uint(e)).transpose(); + let white_xy = self.floats(self.main_tag(tags::AS_SHOT_WHITE_XY))?.and_then(|v| match v[..] { + [x, y] => Some((x, y)), + _ => None, + }); + Ok(Calibration { + color_matrix: [self.matrix(tags::COLOR_MATRIX_1)?, self.matrix(tags::COLOR_MATRIX_2)?], + camera_calibration: [self.matrix(tags::CAMERA_CALIBRATION_1)?, self.matrix(tags::CAMERA_CALIBRATION_2)?], + forward_matrix: [self.matrix(tags::FORWARD_MATRIX_1)?, self.matrix(tags::FORWARD_MATRIX_2)?], + illuminant: [illum(tags::CALIBRATION_ILLUMINANT_1)?, illum(tags::CALIBRATION_ILLUMINANT_2)?], + analog_balance: self.vec3(tags::ANALOG_BALANCE)?, + as_shot_neutral: self.vec3(tags::AS_SHOT_NEUTRAL)?, + as_shot_white_xy: white_xy, + baseline_exposure: self.floats(self.main_tag(tags::BASELINE_EXPOSURE))?.and_then(|v| v.first().copied()).unwrap_or(0.0), + }) + } + + /// Size, depth and CinemaDNG metadata. + pub fn info(&self) -> Result { + let l = raw::layout(&self.tiff, self.raw_ifd())?; + let p = self.params()?; + let (_, _, aw, ah) = process::active_rect(&p, l.width, l.height)?; + let (_, _, width, height) = process::crop_rect(&p, aw, ah); + let dng_version = self + .main_tag(tags::DNG_VERSION) + .and_then(|e| self.tiff.uints(e).ok()) + .and_then(|v| v.get(..4).map(|v| [0, 1, 2, 3].map(|i| v[i] as u8))) + .unwrap_or_default(); + let frame_rate = self.main_tag(tags::FRAME_RATE).and_then(|e| { + let b = self.tiff.value_bytes(e).ok()?; + if !matches!(e.ty, tiff::Type::SRational | tiff::Type::Rational) { + return None; + } + let n = i64::from(self.tiff.u32_of(b.get(0..4)?.try_into().ok()?) as i32); + let d = i64::from(self.tiff.u32_of(b.get(4..8)?.try_into().ok()?) as i32); + (n > 0 && d > 0 && n <= 1000 * d && n * 1000 >= d).then_some((n, d)) + }); + let timecode = self.main_tag(tags::TIME_CODES).and_then(|e| self.tiff.value_bytes(e).ok()).and_then(Timecode::from_bytes); + let camera = self.text(tags::UNIQUE_CAMERA_MODEL).or_else(|| { + let mm = [self.text(tags::MAKE), self.text(tags::MODEL)].into_iter().flatten().collect::>().join(" "); + (!mm.is_empty()).then_some(mm) + }); + Ok(Info { + width, + height, + raw_width: l.width, + raw_height: l.height, + bits: l.bits, + samples_per_pixel: l.samples_per_pixel, + cfa: p.cfa.is_some(), + compression: if l.compression == 7 { "lossless JPEG" } else { "uncompressed" }, + dng_version, + frame_rate, + timecode, + camera, + reel: self.text(tags::REEL_NAME), + }) + } + + /// The raw samples of the main image, as stored (before any processing). + pub fn raw(&self) -> Result { + raw::read(&self.tiff, self.raw_ifd()) + } + + /// Develop the main image to scene-linear Rec. 709. + pub fn develop(&self) -> Result { + let info = self.info()?; + let p = self.params()?; + let raw = self.raw()?; + let calibration = self.calibration()?; + let solved = if p.cfa.is_none() && raw.samples_per_pixel == 1 { + // monochrome: no colour, only the exposure + let e = 2f64.powf(calibration.baseline_exposure.clamp(-16.0, 16.0)); + color::Solved { neutral: [1.0; 3], matrix: color::scale(&color::IDENTITY, e), white_xy: color::D65 } + } else { + calibration.solve()? + }; + let d = process::develop(&raw, &p, &solved.neutral, &solved.matrix)?; + Ok(Image { info, width: d.width, height: d.height, rgba: d.rgba }) + } +} + +/// Parse and develop a DNG file. +pub fn decode(bytes: &[u8]) -> Result { + Dng::parse(bytes)?.develop() +} + +/// Parse a DNG file's metadata. +pub fn probe(bytes: &[u8]) -> Result { + Dng::parse(bytes)?.info() +} diff --git a/crates/raw/src/dng_metadata_source_color.rs b/crates/raw/src/dng_metadata_source_color.rs new file mode 100644 index 000000000..fb02f2c8d --- /dev/null +++ b/crates/raw/src/dng_metadata_source_color.rs @@ -0,0 +1,385 @@ +//! Camera colour to linear Rec. 709, following the DNG specification's "Mapping Camera Color Space +//! to CIE XYZ Space" (DNG 1.4–1.7 chapter 6): +//! +//! - The two calibrations (ColorMatrix, CameraCalibration, ForwardMatrix 1 and 2) are blended +//! linearly in inverse correlated colour temperature between their CalibrationIlluminants. +//! - The white balance is AsShotNeutral (found as a white xy by the specification's iteration) or +//! AsShotWhiteXY; without either, D65. +//! - With ForwardMatrix: CameraToXYZ_D50 = FM · D · (AB · CC)⁻¹, D = diag((AB · CC)⁻¹ · neutral)⁻¹. +//! Without: CameraToXYZ_D50 = CA · (AB · CC · CM)⁻¹, CA a linear Bradford adaptation from the +//! white to D50. +//! - XYZ (D50) goes to linear Rec. 709 / sRGB primaries (D65 white) through Bradford again. +//! +//! The correlated colour temperature of an xy white comes from McCamy's cubic approximation +//! (C. S. McCamy, "Correlated color temperature as an explicit function of chromaticity +//! coordinates", Color Research & Application 17(2), 1992); the illuminant temperatures are the +//! CIE / EXIF 2.3 LightSource values. + +use crate::{Error, Result}; + +pub type M3 = [[f64; 3]; 3]; +pub type V3 = [f64; 3]; + +pub const IDENTITY: M3 = [[1.0, 0.0, 0.0], [0.0, 1.0, 0.0], [0.0, 0.0, 1.0]]; + +/// CIE D50 and D65 white chromaticities (the DNG specification's D50). +pub const D50: (f64, f64) = (0.3457, 0.3585); +pub const D65: (f64, f64) = (0.3127, 0.3290); + +/// Linear Bradford cone response matrix. +const BRADFORD: M3 = [[0.8951, 0.2664, -0.1614], [-0.7502, 1.7135, 0.0367], [0.0389, -0.0685, 1.0296]]; + +pub fn mul(a: &M3, b: &M3) -> M3 { + let mut m = [[0.0; 3]; 3]; + for (i, row) in m.iter_mut().enumerate() { + for (j, v) in row.iter_mut().enumerate() { + *v = (0..3).map(|k| a[i][k] * b[k][j]).sum(); + } + } + m +} + +pub fn mulv(a: &M3, v: &V3) -> V3 { + [0, 1, 2].map(|i| a[i][0] * v[0] + a[i][1] * v[1] + a[i][2] * v[2]) +} + +pub fn diag(v: &V3) -> M3 { + [[v[0], 0.0, 0.0], [0.0, v[1], 0.0], [0.0, 0.0, v[2]]] +} + +pub fn scale(a: &M3, s: f64) -> M3 { + a.map(|r| r.map(|v| v * s)) +} + +fn blend(a: &M3, b: &M3, w: f64) -> M3 { + let mut m = [[0.0; 3]; 3]; + for i in 0..3 { + for j in 0..3 { + m[i][j] = w * a[i][j] + (1.0 - w) * b[i][j]; + } + } + m +} + +/// The inverse of `a`, or `None` when it is (nearly) singular. +pub fn inverse(a: &M3) -> Option { + let c = |r: usize, s: usize| a[(r + 1) % 3][(s + 1) % 3] * a[(r + 2) % 3][(s + 2) % 3] - a[(r + 1) % 3][(s + 2) % 3] * a[(r + 2) % 3][(s + 1) % 3]; + let det = a[0][0] * c(0, 0) + a[0][1] * c(0, 1) + a[0][2] * c(0, 2); + let norm: f64 = a.iter().flatten().map(|v| v.abs()).fold(0.0, f64::max); + if !det.is_finite() || det.abs() <= 1e-12 * norm.powi(3).max(1e-300) { + return None; + } + let mut m = [[0.0; 3]; 3]; + for (i, row) in m.iter_mut().enumerate() { + for (j, v) in row.iter_mut().enumerate() { + *v = c(j, i) / det; + } + } + Some(m) +} + +/// XYZ (Y = 1) of a chromaticity. +pub fn xy_to_xyz((x, y): (f64, f64)) -> V3 { + let y = y.max(1e-6); + [x / y, 1.0, (1.0 - x - y) / y] +} + +pub fn xyz_to_xy(v: &V3) -> Option<(f64, f64)> { + let s = v[0] + v[1] + v[2]; + (s.is_finite() && s > 1e-12).then(|| (v[0] / s, v[1] / s)) +} + +/// Correlated colour temperature (kelvin) of a chromaticity, McCamy (1992), limited to +/// 1 000–50 000 K. +pub fn cct((x, y): (f64, f64)) -> f64 { + let d = 0.1858 - y; + if d.abs() < 1e-9 { + return 50_000.0; + } + let n = (x - 0.3320) / d; + let t = 449.0 * n.powi(3) + 3525.0 * n.powi(2) + 6823.3 * n + 5520.33; + if t.is_finite() { t.clamp(1_000.0, 50_000.0) } else { 50_000.0 } +} + +/// Colour temperature of an EXIF LightSource code used as a DNG CalibrationIlluminant. +pub fn illuminant_kelvin(code: u64) -> Option { + Some(match code { + 1 | 4 | 9 => 5500.0, // daylight, flash, fine weather + 2 | 14 => 4150.0, // fluorescent, cool white fluorescent (W 3800–4500 K) + 3 => 2850.0, // tungsten + 10 => 6500.0, // cloudy + 11 => 7500.0, // shade + 12 => 6430.0, // daylight fluorescent (D 5700–7100 K) + 13 => 5050.0, // day white fluorescent (N 4600–5500 K) + 15 => 3525.0, // white fluorescent (WW 3250–3800 K) + 16 => 2925.0, // warm white fluorescent (L 2600–3250 K) + 17 => 2856.0, // standard light A + 18 => 4874.0, // standard light B + 19 => 6774.0, // standard light C + 20 => 5503.0, // D55 + 21 => 6504.0, // D65 + 22 => 7504.0, // D75 + 23 => 5003.0, // D50 + 24 => 3200.0, // ISO studio tungsten + _ => return None, + }) +} + +/// Linear Bradford adaptation from one white (XYZ) to another. +pub fn bradford(src: &V3, dst: &V3) -> Option { + let s = mulv(&BRADFORD, src); + let d = mulv(&BRADFORD, dst); + if s.iter().any(|v| v.abs() < 1e-12) { + return None; + } + let g = [d[0] / s[0], d[1] / s[1], d[2] / s[2]]; + Some(mul(&inverse(&BRADFORD)?, &mul(&diag(&g), &BRADFORD))) +} + +/// Linear RGB (Rec. 709 / sRGB primaries, D65 white) → XYZ. +pub fn rec709_to_xyz() -> Option { + let p = [(0.64, 0.33), (0.30, 0.60), (0.15, 0.06)].map(xy_to_xyz); + let pm: M3 = [[p[0][0], p[1][0], p[2][0]], [p[0][1], p[1][1], p[2][1]], [p[0][2], p[1][2], p[2][2]]]; + let s = mulv(&inverse(&pm)?, &xy_to_xyz(D65)); + Some(mul(&pm, &diag(&s))) +} + +/// XYZ relative to D50 → linear Rec. 709. +pub fn xyz_d50_to_rec709() -> Option { + let to_d65 = bradford(&xy_to_xyz(D50), &xy_to_xyz(D65))?; + Some(mul(&inverse(&rec709_to_xyz()?)?, &to_d65)) +} + +/// The colour tags of a three-colour DNG. +#[derive(Clone, Debug, Default)] +pub struct Calibration { + pub color_matrix: [Option; 2], + pub camera_calibration: [Option; 2], + pub forward_matrix: [Option; 2], + /// EXIF LightSource codes of the two calibrations. + pub illuminant: [Option; 2], + pub analog_balance: Option, + pub as_shot_neutral: Option, + pub as_shot_white_xy: Option<(f64, f64)>, + pub baseline_exposure: f64, +} + +/// The result of [`Calibration::solve`]. +#[derive(Clone, Debug, PartialEq)] +pub struct Solved { + /// Camera neutral: raw (normalised) camera values of a white surface. Dividing by it white + /// balances the camera values. + pub neutral: V3, + /// White-balanced camera values → linear Rec. 709, including the baseline exposure. The white + /// balanced white (1, 1, 1) goes to Y = 2^BaselineExposure. + pub matrix: M3, + /// The white point found, for information. + pub white_xy: (f64, f64), +} + +impl Calibration { + /// Weight of calibration 1 for a white of temperature `t` (1 when only one is usable). + fn weight(&self, t: f64) -> f64 { + let (Some(_), Some(_)) = (self.color_matrix[0], self.color_matrix[1]) else { return 1.0 }; + let (Some(t1), Some(t2)) = (self.illuminant[0].and_then(illuminant_kelvin), self.illuminant[1].and_then(illuminant_kelvin)) else { + return 1.0; + }; + if (t1 - t2).abs() < 1.0 { + return 1.0; + } + // the formula needs t1 < t2; swap the roles otherwise + let (lo, hi, lo_is_1) = if t1 < t2 { (t1, t2, true) } else { (t2, t1, false) }; + let w_lo = if t <= lo { + 1.0 + } else if t >= hi { + 0.0 + } else { + (1.0 / t - 1.0 / hi) / (1.0 / lo - 1.0 / hi) + }; + if lo_is_1 { w_lo } else { 1.0 - w_lo } + } + + fn pick(&self, m: &[Option; 2], w: f64, default: M3) -> M3 { + match (m[0], m[1]) { + (Some(a), Some(b)) => blend(&a, &b, w), + (Some(a), None) => a, + (None, Some(b)) if self.color_matrix[0].is_none() => b, + _ => default, + } + } + + /// XYZ → camera at weight `w`: AB · CC · CM. + fn xyz_to_camera(&self, w: f64) -> Option { + let cm = match self.color_matrix { + [None, None] => return None, + _ => self.pick(&self.color_matrix, w, IDENTITY), + }; + let cc = self.pick(&self.camera_calibration, w, IDENTITY); + let ab = diag(&self.analog_balance.unwrap_or([1.0; 3])); + Some(mul(&ab, &mul(&cc, &cm))) + } + + /// Work out the white balance and the camera → linear Rec. 709 matrix. + pub fn solve(&self) -> Result { + let out_matrix = xyz_d50_to_rec709().ok_or(Error::Malformed("colour maths"))?; + let exposure = 2f64.powf(self.baseline_exposure.clamp(-16.0, 16.0)); + if self.color_matrix.iter().all(Option::is_none) { + // no colour calibration: the camera values are taken as Rec. 709, white balanced + let neutral = self.as_shot_neutral.unwrap_or([1.0; 3]); + if neutral.iter().any(|v| !v.is_finite() || *v <= 1e-9) { + return Err(Error::Malformed("camera neutral must be positive")); + } + return Ok(Solved { neutral, matrix: scale(&IDENTITY, exposure), white_xy: D65 }); + } + let bad = || Error::Malformed("singular colour matrix"); + // the white point and the camera neutral + let (white_xy, neutral) = if let Some(n) = self.as_shot_neutral { + let mut xy = D50; + for _ in 0..50 { + let w = self.weight(cct(xy)); + let to_xyz = inverse(&self.xyz_to_camera(w).ok_or_else(bad)?).ok_or_else(bad)?; + let next = xyz_to_xy(&mulv(&to_xyz, &n)).ok_or(Error::Malformed("AsShotNeutral is not a colour"))?; + let done = (next.0 - xy.0).abs() + (next.1 - xy.1).abs() < 1e-9; + xy = next; + if done { + break; + } + } + (xy, n) + } else { + let xy = self.as_shot_white_xy.unwrap_or(D65); + let w = self.weight(cct(xy)); + let n = mulv(&self.xyz_to_camera(w).ok_or_else(bad)?, &xy_to_xyz(xy)); + (xy, n) + }; + if neutral.iter().any(|v| !v.is_finite() || *v <= 1e-9) { + return Err(Error::Malformed("camera neutral must be positive")); + } + let w = self.weight(cct(white_xy)); + let ab_cc = mul(&diag(&self.analog_balance.unwrap_or([1.0; 3])), &self.pick(&self.camera_calibration, w, IDENTITY)); + let camera_to_xyz_d50 = if self.forward_matrix.iter().any(Option::is_some) { + let fm = self.pick(&self.forward_matrix, w, IDENTITY); + let inv_ab_cc = inverse(&ab_cc).ok_or_else(bad)?; + let reference = mulv(&inv_ab_cc, &neutral); + if reference.iter().any(|v| v.abs() < 1e-12) { + return Err(bad()); + } + mul(&fm, &mul(&diag(&reference.map(|v| 1.0 / v)), &inv_ab_cc)) + } else { + let to_xyz = inverse(&self.xyz_to_camera(w).ok_or_else(bad)?).ok_or_else(bad)?; + let ca = bradford(&xy_to_xyz(white_xy), &xy_to_xyz(D50)).ok_or_else(bad)?; + mul(&ca, &to_xyz) + }; + // white-balanced camera values: camera = neutral · wb + let wb_to_xyz = mul(&camera_to_xyz_d50, &diag(&neutral)); + let y = mulv(&wb_to_xyz, &[1.0; 3])[1]; + if !y.is_finite() || y <= 1e-9 { + return Err(bad()); + } + let matrix = scale(&mul(&out_matrix, &wb_to_xyz), exposure / y); + if matrix.iter().flatten().any(|v| !v.is_finite()) { + return Err(bad()); + } + Ok(Solved { neutral, matrix, white_xy }) + } +} + +#[cfg(test)] +mod tests { + use super::*; + + fn close(a: &M3, b: &M3, tol: f64) -> bool { + a.iter().flatten().zip(b.iter().flatten()).all(|(x, y)| (x - y).abs() <= tol) + } + + #[test] + fn rec709_matrix_matches_the_published_one() { + // IEC 61966-2-1 (sRGB) linear RGB → XYZ, four decimals + let m = rec709_to_xyz().unwrap(); + let want = [[0.4124, 0.3576, 0.1805], [0.2126, 0.7152, 0.0722], [0.0193, 0.1192, 0.9505]]; + assert!(close(&m, &want, 1e-3), "{m:?}"); + } + + #[test] + fn inverse_round_trips_and_refuses_singular() { + let a = [[2.0, 1.0, 0.5], [0.1, 3.0, 0.2], [0.3, 0.4, 1.5]]; + assert!(close(&mul(&a, &inverse(&a).unwrap()), &IDENTITY, 1e-12)); + assert!(inverse(&[[1.0, 2.0, 3.0], [2.0, 4.0, 6.0], [0.0, 0.0, 1.0]]).is_none()); + assert!(inverse(&[[f64::NAN; 3]; 3]).is_none()); + } + + #[test] + fn mccamy_temperatures() { + assert!((cct(D65) - 6504.0).abs() < 15.0, "{}", cct(D65)); + assert!((cct(D50) - 5003.0).abs() < 15.0, "{}", cct(D50)); + assert!((cct((0.44757, 0.40745)) - 2856.0).abs() < 15.0, "illuminant A"); + assert_eq!(cct((0.3320, 0.1858)), 50_000.0, "no division by zero"); + } + + #[test] + fn interpolation_weight_follows_inverse_temperature() { + let c = Calibration { color_matrix: [Some(IDENTITY), Some(IDENTITY)], illuminant: [Some(17), Some(21)], ..Default::default() }; + assert_eq!(c.weight(2000.0), 1.0); + assert_eq!(c.weight(9000.0), 0.0); + let mid = c.weight(4000.0); + let want = (1.0 / 4000.0 - 1.0 / 6504.0) / (1.0 / 2856.0 - 1.0 / 6504.0); + assert!((mid - want).abs() < 1e-12); + // swapped order: weight of calibration 1 (now D65) + let s = Calibration { illuminant: [Some(21), Some(17)], ..c.clone() }; + assert!((s.weight(4000.0) - (1.0 - want)).abs() < 1e-12); + // unknown illuminant: calibration 1 only + let u = Calibration { illuminant: [Some(0), Some(21)], ..c }; + assert_eq!(u.weight(4000.0), 1.0); + } + + #[test] + fn an_srgb_camera_maps_to_itself() { + // ColorMatrix = XYZ → linear sRGB, white D65: the camera is Rec. 709 already + let cm = inverse(&rec709_to_xyz().unwrap()).unwrap(); + let c = Calibration { color_matrix: [Some(cm), None], illuminant: [Some(21), None], as_shot_neutral: Some([1.0; 3]), ..Default::default() }; + let s = c.solve().unwrap(); + assert!(close(&s.matrix, &IDENTITY, 1e-6), "{:?}", s.matrix); + assert!((s.white_xy.0 - D65.0).abs() < 1e-4 && (s.white_xy.1 - D65.1).abs() < 1e-4, "{:?}", s.white_xy); + // a baseline exposure of +1 doubles everything + let e = Calibration { baseline_exposure: 1.0, ..c }.solve().unwrap(); + assert!(close(&e.matrix, &scale(&IDENTITY, 2.0), 1e-6)); + } + + #[test] + fn forward_matrix_white_balances_by_the_neutral() { + // FM = linear Rec. 709 → XYZ D50: the white-balanced camera values come out unchanged + let fm = mul(&bradford(&xy_to_xyz(D65), &xy_to_xyz(D50)).unwrap(), &rec709_to_xyz().unwrap()); + let cm = inverse(&rec709_to_xyz().unwrap()).unwrap(); + let c = Calibration { + color_matrix: [Some(cm), None], + forward_matrix: [Some(fm), None], + illuminant: [Some(21), None], + as_shot_neutral: Some([0.5, 1.0, 0.8]), + ..Default::default() + }; + let s = c.solve().unwrap(); + assert_eq!(s.neutral, [0.5, 1.0, 0.8]); + assert!(close(&s.matrix, &IDENTITY, 1e-6), "{:?}", s.matrix); + } + + #[test] + fn white_xy_gives_the_neutral() { + let cm = inverse(&rec709_to_xyz().unwrap()).unwrap(); + let c = Calibration { color_matrix: [Some(cm), None], illuminant: [Some(21), None], as_shot_white_xy: Some(D65), ..Default::default() }; + let s = c.solve().unwrap(); + for v in s.neutral { + assert!((v - s.neutral[1]).abs() < 1e-3, "D65 is neutral for an sRGB camera: {:?}", s.neutral); + } + } + + #[test] + fn hostile_calibrations_are_errors() { + let zero = Calibration { color_matrix: [Some([[0.0; 3]; 3]), None], as_shot_neutral: Some([1.0; 3]), ..Default::default() }; + assert!(zero.solve().is_err()); + let cm = inverse(&rec709_to_xyz().unwrap()).unwrap(); + let neg = Calibration { color_matrix: [Some(cm), None], as_shot_neutral: Some([-1.0, 1.0, 1.0]), ..Default::default() }; + assert!(neg.solve().is_err()); + let none = Calibration { as_shot_neutral: Some([0.5, 1.0, 0.5]), ..Default::default() }.solve().unwrap(); + assert_eq!(none.matrix, IDENTITY); + } +} diff --git a/crates/raw/src/dng_metadata_source_demosaic.rs b/crates/raw/src/dng_metadata_source_demosaic.rs new file mode 100644 index 000000000..911236929 --- /dev/null +++ b/crates/raw/src/dng_metadata_source_demosaic.rs @@ -0,0 +1,255 @@ +//! Demosaicing: one colour sample per pixel (a colour filter array) to three. +//! +//! - A 2 × 2 Bayer pattern (one red, two green, one blue) uses the gradient-corrected linear +//! interpolation of H. S. Malvar, L.-W. He and R. Cutler, "High-quality linear interpolation for +//! demosaicing of Bayer-patterned color images", IEEE ICASSP 2004: bilinear interpolation plus a +//! Laplacian correction from the pixel's own colour, as 5 × 5 filters. +//! - Any other pattern (up to 8 × 8) uses bilinear interpolation: the mean of the nearest samples +//! of each missing colour in the 3 × 3 neighbourhood, else the 5 × 5 one. +//! +//! The picture is extended by two pixels on every side by mirroring (without repeating the edge), +//! which keeps a 2 × 2 pattern's phase. + +#[cfg(feature = "threads")] +use rayon::prelude::*; + +/// A CFA pattern: `rows` × `cols` colour indices (0 red, 1 green, 2 blue), origin at the top left +/// of the image. +#[derive(Clone, Debug, PartialEq, Eq)] +pub struct Pattern { + pub rows: usize, + pub cols: usize, + pub colors: Vec, +} + +impl Pattern { + pub fn color(&self, x: usize, y: usize) -> u8 { + self.colors.get((y % self.rows) * self.cols + x % self.cols).copied().unwrap_or(1) + } + + /// Whether this is a 2 × 2 Bayer pattern. + pub fn is_bayer(&self) -> bool { + if (self.rows, self.cols) != (2, 2) { + return false; + } + let [a, b, c, d] = self.colors[..] else { return false }; + let count = |k: u8| [a, b, c, d].iter().filter(|&&v| v == k).count(); + // the two greens sit on a diagonal + count(0) == 1 && count(1) == 2 && count(2) == 1 && ((a == 1 && d == 1) || (b == 1 && c == 1)) + } +} + +const PAD: usize = 2; + +/// Mirror index `i` (which may be up to PAD outside) into 0..n. +fn mirror(i: isize, n: usize) -> usize { + let n = n as isize; + let mut i = i; + if i < 0 { + i = -i; + } + if i >= n { + i = 2 * (n - 1) - i; + } + i.clamp(0, n - 1) as usize +} + +/// The plane extended by PAD mirrored pixels on each side. +fn padded(plane: &[f32], w: usize, h: usize) -> (Vec, usize) { + let pw = w + 2 * PAD; + let ph = h + 2 * PAD; + let mut out = vec![0f32; pw * ph]; + for y in 0..ph { + let sy = mirror(y as isize - PAD as isize, h); + for x in 0..pw { + let sx = mirror(x as isize - PAD as isize, w); + out[y * pw + x] = plane.get(sy * w + sx).copied().unwrap_or(0.0); + } + } + (out, pw) +} + +/// Demosaic `plane` (`w` × `h`, one sample per pixel) into interleaved RGB. +pub fn demosaic(plane: &[f32], w: usize, h: usize, pattern: &Pattern) -> Vec { + let mut out = vec![0f32; w * h * 3]; + if w == 0 || h == 0 || plane.len() < w * h { + return out; + } + let (p, pw) = padded(plane, w, h); + let bayer = pattern.is_bayer() && w >= 3 && h >= 3; + let row = |y: usize, o: &mut [f32]| { + if bayer { + malvar_row(&p, pw, y, w, pattern, o); + } else { + bilinear_row(&p, pw, y, w, h, pattern, o); + } + }; + #[cfg(feature = "threads")] + out.par_chunks_mut(w * 3).enumerate().for_each(|(y, o)| row(y, o)); + #[cfg(not(feature = "threads"))] + out.chunks_mut(w * 3).enumerate().for_each(|(y, o)| row(y, o)); + out +} + +fn malvar_row(p: &[f32], pw: usize, y: usize, w: usize, pat: &Pattern, out: &mut [f32]) { + let py = y + PAD; + for x in 0..w { + let px = x + PAD; + let at = |dx: isize, dy: isize| p[((py as isize + dy) as usize) * pw + (px as isize + dx) as usize]; + let c = at(0, 0); + let cross1 = at(-1, 0) + at(1, 0) + at(0, -1) + at(0, 1); + let cross2 = at(-2, 0) + at(2, 0) + at(0, -2) + at(0, 2); + let diag = at(-1, -1) + at(1, -1) + at(-1, 1) + at(1, 1); + let me = pat.color(x, y); + let rgb = match me { + 1 => { + let h1 = at(-1, 0) + at(1, 0); + let h2 = at(-2, 0) + at(2, 0); + let v1 = at(0, -1) + at(0, 1); + let v2 = at(0, -2) + at(0, 2); + // colour along the row and along the column + let along_row = (5.0 * c + 4.0 * h1 - h2 - diag + 0.5 * v2) / 8.0; + let along_col = (5.0 * c + 4.0 * v1 - v2 - diag + 0.5 * h2) / 8.0; + if pat.color(x + 1, y) == 0 { [along_row, c, along_col] } else { [along_col, c, along_row] } + } + _ => { + let g = (4.0 * c + 2.0 * cross1 - cross2) / 8.0; + let other = (6.0 * c + 2.0 * diag - 1.5 * cross2) / 8.0; + if me == 0 { [c, g, other] } else { [other, g, c] } + } + }; + if let Some(o) = out.get_mut(x * 3..x * 3 + 3) { + o.copy_from_slice(&rgb); + } + } +} + +fn bilinear_row(p: &[f32], pw: usize, y: usize, w: usize, h: usize, pat: &Pattern, out: &mut [f32]) { + for x in 0..w { + let me = pat.color(x, y); + let mut rgb = [0f32; 3]; + for (k, v) in rgb.iter_mut().enumerate() { + if usize::from(me) == k { + *v = p[(y + PAD) * pw + x + PAD]; + continue; + } + for r in 1..=PAD as isize { + let (mut sum, mut n) = (0f32, 0u32); + for dy in -r..=r { + for dx in -r..=r { + // the colour of a mirrored neighbour is the colour where it was mirrored from + let sx = mirror(x as isize + dx, w); + let sy = mirror(y as isize + dy, h); + if usize::from(pat.color(sx, sy)) == k { + sum += p[(sy + PAD) * pw + sx + PAD]; + n += 1; + } + } + } + if n > 0 { + *v = sum / n as f32; + break; + } + } + } + if let Some(o) = out.get_mut(x * 3..x * 3 + 3) { + o.copy_from_slice(&rgb); + } + } +} + +#[cfg(test)] +mod tests { + use super::*; + + fn rggb() -> Pattern { + Pattern { rows: 2, cols: 2, colors: vec![0, 1, 1, 2] } + } + + /// A mosaic of an RGB picture given per pixel. + fn mosaic(w: usize, h: usize, pat: &Pattern, rgb: impl Fn(usize, usize) -> [f32; 3]) -> Vec { + (0..h).flat_map(|y| (0..w).map(move |x| (x, y))).map(|(x, y)| rgb(x, y)[usize::from(pat.color(x, y))]).collect() + } + + #[test] + fn bayer_detection() { + assert!(rggb().is_bayer()); + for c in [[1, 0, 2, 1], [1, 2, 0, 1], [2, 1, 1, 0]] { + assert!(Pattern { rows: 2, cols: 2, colors: c.to_vec() }.is_bayer(), "{c:?}"); + } + assert!(!Pattern { rows: 2, cols: 2, colors: vec![0, 1, 2, 1] }.is_bayer(), "greens side by side"); + assert!(!Pattern { rows: 2, cols: 2, colors: vec![0, 0, 1, 2] }.is_bayer()); + assert!(!Pattern { rows: 1, cols: 3, colors: vec![0, 1, 2] }.is_bayer()); + } + + #[test] + fn flat_fields_stay_flat_for_every_bayer_phase_and_other_patterns() { + let pats = [[0, 1, 1, 2], [1, 0, 2, 1], [1, 2, 0, 1], [2, 1, 1, 0]].map(|c| Pattern { rows: 2, cols: 2, colors: c.to_vec() }); + let stripes = Pattern { rows: 1, cols: 3, colors: vec![0, 1, 2] }; + for pat in pats.iter().chain([&stripes]) { + let (w, h) = (9, 7); + let m = mosaic(w, h, pat, |_, _| [0.25, 0.5, 0.75]); + let out = demosaic(&m, w, h, pat); + for (i, px) in out.chunks(3).enumerate() { + for (k, want) in [0.25, 0.5, 0.75].iter().enumerate() { + assert!((px[k] - want).abs() < 1e-6, "{pat:?} pixel {i} channel {k}: {}", px[k]); + } + } + } + } + + #[test] + fn known_samples_are_kept_and_linear_ramps_are_exact() { + // a horizontal ramp is reconstructed exactly away from the border (all filters are + // symmetric and sum to one, the correction terms sum to zero) + let pat = rggb(); + let (w, h) = (16, 12); + let f = |x: usize, _y: usize| { + let v = x as f32 / 16.0; + [v, 0.5 * v + 0.1, 0.2 + 0.25 * v] + }; + let m = mosaic(w, h, &pat, f); + let out = demosaic(&m, w, h, &pat); + for y in 0..h { + for x in 0..w { + let px = &out[(y * w + x) * 3..][..3]; + let own = usize::from(pat.color(x, y)); + assert_eq!(px[own], m[y * w + x], "own sample kept"); + if (2..w - 2).contains(&x) { + let want = f(x, y); + for k in 0..3 { + assert!((px[k] - want[k]).abs() < 1e-5, "({x},{y}) ch {k}: {} vs {}", px[k], want[k]); + } + } + } + } + } + + #[test] + fn a_vertical_edge_does_not_smear_far() { + // white left half, black right half: pixels two columns from the edge are untouched + let pat = rggb(); + let (w, h) = (12, 8); + let m = mosaic(w, h, &pat, |x, _| if x < 6 { [1.0; 3] } else { [0.0; 3] }); + let out = demosaic(&m, w, h, &pat); + for y in 0..h { + for x in (0..3).chain(9..12) { + let want = if x < 6 { 1.0 } else { 0.0 }; + for k in 0..3 { + assert!((out[(y * w + x) * 3 + k] - want).abs() < 1e-6, "({x},{y}) ch {k}"); + } + } + } + } + + #[test] + fn tiny_and_empty_pictures_do_not_panic() { + let pat = rggb(); + for (w, h) in [(0, 0), (1, 1), (2, 1), (1, 5), (2, 2), (3, 2)] { + let m = vec![0.5; w * h]; + let out = demosaic(&m, w, h, &pat); + assert_eq!(out.len(), w * h * 3); + } + assert_eq!(demosaic(&[0.5; 3], 4, 4, &pat).len(), 48, "short plane"); + } +} diff --git a/crates/raw/src/dng_metadata_source_process.rs b/crates/raw/src/dng_metadata_source_process.rs new file mode 100644 index 000000000..41d9b25ae --- /dev/null +++ b/crates/raw/src/dng_metadata_source_process.rs @@ -0,0 +1,264 @@ +//! Raw samples to a linear Rec. 709 picture, in the order of the DNG specification's chapter 5 +//! ("Mapping Raw Values to Linear Reference Values") and chapter 6 (colour): +//! +//! 1. ActiveArea: only the active (unmasked) sensor area is used; black levels, the CFA pattern +//! and the default crop are relative to its top left corner. +//! 2. LinearizationTable maps each sample. +//! 3. The black level (BlackLevel over its BlackLevelRepeatDim pattern, plus BlackLevelDeltaH for +//! the column and BlackLevelDeltaV for the row) is subtracted and the range up to WhiteLevel +//! scaled to 0–1 (clipped there). +//! 4. White balance: each sample is divided by the camera neutral of its colour and clipped at +//! 1, so highlights the sensor clipped stay neutral. +//! 5. A CFA is demosaiced ([`crate::demosaic`]). +//! 6. DefaultCropOrigin / DefaultCropSize cut the picture. +//! 7. The colour matrix of [`crate::color`] takes the white-balanced camera values to linear +//! Rec. 709 (including BaselineExposure); negative results are clipped to 0. + +#[cfg(feature = "threads")] +use rayon::prelude::*; + +use crate::color::{M3, V3}; +use crate::demosaic::{Pattern, demosaic}; +use crate::raw::RawImage; +use crate::{Error, Result}; + +/// The processing tags of a raw image. +#[derive(Clone, Debug, Default)] +pub struct Params { + /// top, left, bottom, right + pub active_area: Option<[usize; 4]>, + pub linearization: Option>, + /// rows, columns of the BlackLevel pattern + pub black_repeat: (usize, usize), + /// rows × columns × samples per pixel values + pub black: Vec, + pub black_delta_h: Vec, + pub black_delta_v: Vec, + /// per sample + pub white: Vec, + pub crop_origin: Option<(f64, f64)>, + pub crop_size: Option<(f64, f64)>, + /// CFA pattern in camera planes (`None`: LinearRaw). + pub cfa: Option, +} + +/// A rectangle: x, y, width, height. +type Rect = (usize, usize, usize, usize); + +/// The active area of a `w` × `h` raw image. +pub fn active_rect(p: &Params, w: usize, h: usize) -> Result { + match p.active_area { + None => Ok((0, 0, w, h)), + Some([top, left, bottom, right]) => { + if top >= bottom || left >= right || bottom > h || right > w { + return Err(Error::Malformed("ActiveArea outside the image")); + } + Ok((left, top, right - left, bottom - top)) + } + } +} + +/// The default crop inside an active area of `aw` × `ah`. +pub fn crop_rect(p: &Params, aw: usize, ah: usize) -> Rect { + let (ox, oy) = p.crop_origin.unwrap_or((0.0, 0.0)); + let (sw, sh) = p.crop_size.unwrap_or((aw as f64, ah as f64)); + let to = |v: f64, max: usize| if v.is_finite() && v > 0.0 { (v.min(max as f64)) as usize } else { 0 }; + let x = to(ox.floor(), aw.saturating_sub(1)); + let y = to(oy.floor(), ah.saturating_sub(1)); + let w = to(sw.round(), aw - x).max(1); + let h = to(sh.round(), ah - y).max(1); + (x, y, w, h) +} + +/// Normalised, white-balanced samples of the active area (steps 1–4), one plane per sample. +fn normalise(raw: &RawImage, p: &Params, (ax, ay, aw, ah): Rect, neutral: &V3, planes: usize) -> Result> { + let spp = raw.samples_per_pixel; + let (br, bc) = (p.black_repeat.0.max(1), p.black_repeat.1.max(1)); + if br > 64 || bc > 64 { + return Err(Error::Unsupported("a BlackLevelRepeatDim larger than 64".into())); + } + let need = br * bc * spp; + let black: Vec = match p.black.len() { + 0 => vec![0.0; need], + 1 => vec![p.black[0]; need], + n if n >= need => p.black[..need].to_vec(), + _ => return Err(Error::Malformed("BlackLevel has too few values")), + }; + let max_code = ((1u32 << raw.bits) - 1) as f64; + let white: Vec = (0..spp).map(|s| p.white.get(s).or(p.white.first()).copied().unwrap_or(max_code)).collect(); + let lin = p.linearization.as_deref().filter(|t| !t.is_empty()); + let inv_neutral: Vec = (0..planes).map(|c| (1.0 / neutral.get(c).copied().unwrap_or(1.0)) as f32).collect(); + let cfa = p.cfa.as_ref(); + let mut out = vec![0f32; aw * ah * spp]; + let row = |y: usize, o: &mut [f32]| { + let dv = p.black_delta_v.get(y).copied().unwrap_or(0.0); + let src_row = (ay + y) * raw.width * spp; + for x in 0..aw { + let dh = p.black_delta_h.get(x).copied().unwrap_or(0.0); + for s in 0..spp { + let code = raw.data.get(src_row + (ax + x) * spp + s).copied().unwrap_or(0); + let v = match lin { + Some(t) => f64::from(t.get(usize::from(code)).or(t.last()).copied().unwrap_or(0)), + None => f64::from(code), + }; + let b = black[((y % br) * bc + x % bc) * spp + s] + dh + dv; + let range = white[s] - b; + let n = if range > 0.0 { ((v - b) / range).clamp(0.0, 1.0) } else { 0.0 }; + let plane = match cfa { + Some(c) => usize::from(c.color(x, y)), + None => s, + }; + let wb = inv_neutral.get(plane).copied().unwrap_or(1.0); + if let Some(o) = o.get_mut(x * spp + s) { + *o = (n as f32 * wb).min(1.0); + } + } + } + }; + #[cfg(feature = "threads")] + out.par_chunks_mut((aw * spp).max(1)).enumerate().for_each(|(y, o)| row(y, o)); + #[cfg(not(feature = "threads"))] + out.chunks_mut((aw * spp).max(1)).enumerate().for_each(|(y, o)| row(y, o)); + Ok(out) +} + +/// A developed picture: premultiplied linear Rec. 709 RGBA floats (alpha 1). +pub struct Developed { + pub width: usize, + pub height: usize, + pub rgba: Vec, +} + +/// Develop `raw` (steps 1–7). `neutral` and `matrix` come from [`crate::color::Calibration::solve`]. +pub fn develop(raw: &RawImage, p: &Params, neutral: &V3, matrix: &M3) -> Result { + let spp = raw.samples_per_pixel; + if p.cfa.is_some() && spp != 1 { + return Err(Error::Malformed("a CFA image with more than one sample per pixel")); + } + if p.cfa.is_none() && !matches!(spp, 1 | 3) { + return Err(Error::Unsupported(format!("LinearRaw with {spp} samples per pixel"))); + } + let active = active_rect(p, raw.width, raw.height)?; + let (_, _, aw, ah) = active; + let planes = if p.cfa.is_some() { 3 } else { spp }; + let norm = normalise(raw, p, active, neutral, planes)?; + // three samples per pixel + let rgb = match (&p.cfa, spp) { + (Some(pat), _) => demosaic(&norm, aw, ah, pat), + (None, 3) => norm, + _ => norm.iter().flat_map(|&v| [v, v, v]).collect(), + }; + let (cx, cy, cw, ch) = crop_rect(p, aw, ah); + let m: [[f32; 3]; 3] = matrix.map(|r| r.map(|v| v as f32)); + let mono = p.cfa.is_none() && spp == 1; + let mut rgba = vec![0f32; cw * ch * 4]; + let row = |y: usize, o: &mut [f32]| { + let src = (cy + y) * aw + cx; + for x in 0..cw { + let Some(c) = rgb.get((src + x) * 3..(src + x) * 3 + 3) else { continue }; + let px = if mono { + // grey: the matrix's luminance gain only + let g = c[0] * (m[1][0] + m[1][1] + m[1][2]); + [g, g, g] + } else { + [0, 1, 2].map(|i| m[i][0] * c[0] + m[i][1] * c[1] + m[i][2] * c[2]) + }; + if let Some(d) = o.get_mut(x * 4..x * 4 + 4) { + d.copy_from_slice(&[px[0].max(0.0), px[1].max(0.0), px[2].max(0.0), 1.0]); + } + } + }; + #[cfg(feature = "threads")] + rgba.par_chunks_mut(cw * 4).enumerate().for_each(|(y, o)| row(y, o)); + #[cfg(not(feature = "threads"))] + rgba.chunks_mut(cw * 4).enumerate().for_each(|(y, o)| row(y, o)); + Ok(Developed { width: cw, height: ch, rgba }) +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::color::IDENTITY; + + fn raw(w: usize, h: usize, spp: usize, data: Vec) -> RawImage { + RawImage { width: w, height: h, samples_per_pixel: spp, bits: 12, data } + } + + #[test] + fn black_white_linearisation_and_white_balance() { + // LinearRaw 2 × 1: black 100, white 1100; neutral (0.5, 1, 0.25) + let r = raw(2, 1, 3, vec![600, 600, 600, 100, 1100, 2000]); + let p = Params { black: vec![100.0], white: vec![1100.0], ..Default::default() }; + let d = develop(&r, &p, &[0.5, 1.0, 0.25], &IDENTITY).unwrap(); + assert_eq!((d.width, d.height), (2, 1)); + // 0.5 / 0.5 = 1, 0.5 / 1, 0.5 / 0.25 = 2 → clipped to 1 + assert_eq!(&d.rgba[..4], &[1.0, 0.5, 1.0, 1.0]); + // below black → 0, at white → 1, above white → 1 + assert_eq!(&d.rgba[4..], &[0.0, 1.0, 1.0, 1.0]); + // a linearisation table doubling the codes + let t: Vec = (0..4096u32).map(|v| (v * 2).min(65_535) as u16).collect(); + let p = Params { linearization: Some(t), black: vec![200.0], white: vec![2200.0], ..p }; + let d = develop(&r, &p, &[1.0; 3], &IDENTITY).unwrap(); + assert_eq!(&d.rgba[..3], &[0.5, 0.5, 0.5]); + } + + #[test] + fn per_cfa_black_levels_and_deltas() { + // 2 × 2 CFA, black repeat 2 × 2 with a different black per site, plus row/column deltas + let pat = Pattern { rows: 2, cols: 2, colors: vec![0, 1, 1, 2] }; + let p = Params { + black_repeat: (2, 2), + black: vec![10.0, 20.0, 30.0, 40.0], + black_delta_h: vec![0.0, 5.0], + black_delta_v: vec![0.0, 1.0], + white: vec![1010.0], + cfa: Some(pat), + ..Default::default() + }; + let r = raw(2, 2, 1, vec![510, 525, 531, 546]); + let n = normalise(&r, &p, (0, 0, 2, 2), &[1.0; 3], 3).unwrap(); + // (v - black) / (white - black) for black 10, 25, 31, 46 + let want = [500.0 / 1000.0, 500.0 / 985.0, 500.0 / 979.0, 500.0 / 964.0]; + for (a, b) in n.iter().zip(want) { + assert!((a - b).abs() < 1e-6, "{a} vs {b}"); + } + } + + #[test] + fn active_area_and_default_crop() { + // 6 × 4 LinearRaw grey ramp; active area cuts a 1-pixel border, crop takes 2 × 1 at (1, 1) + let data: Vec = (0..24u16).flat_map(|i| [i * 10; 3]).collect(); + let r = raw(6, 4, 3, data); + let p = + Params { active_area: Some([1, 1, 3, 5]), white: vec![4095.0], crop_origin: Some((1.0, 1.0)), crop_size: Some((2.0, 1.0)), ..Default::default() }; + let d = develop(&r, &p, &[1.0; 3], &IDENTITY).unwrap(); + assert_eq!((d.width, d.height), (2, 1)); + // active (1,1)–(5,3); crop origin (1,1) inside it → raw (2, 2) and (3, 2): samples 14, 15 + assert!((d.rgba[0] - 140.0 / 4095.0).abs() < 1e-6); + assert!((d.rgba[4] - 150.0 / 4095.0).abs() < 1e-6); + // hostile active areas and crops + let bad = Params { active_area: Some([3, 0, 1, 6]), ..Default::default() }; + assert!(develop(&r, &bad, &[1.0; 3], &IDENTITY).is_err()); + let bad = Params { active_area: Some([0, 0, 4, 7]), ..Default::default() }; + assert!(develop(&r, &bad, &[1.0; 3], &IDENTITY).is_err()); + let huge = Params { crop_origin: Some((1e12, -5.0)), crop_size: Some((f64::NAN, 1e9)), white: vec![4095.0], ..Default::default() }; + let d = develop(&r, &huge, &[1.0; 3], &IDENTITY).unwrap(); + assert!(d.width >= 1 && d.width <= 6 && d.height >= 1 && d.height <= 4); + } + + #[test] + fn matrix_and_clipping_of_negatives() { + let r = raw(1, 1, 3, vec![4095, 0, 0]); + let p = Params { white: vec![4095.0], ..Default::default() }; + let m = [[1.0, 0.0, 0.0], [-0.5, 1.0, 0.0], [0.0, 0.0, 2.0]]; + let d = develop(&r, &p, &[1.0; 3], &m).unwrap(); + assert_eq!(&d.rgba, &[1.0, 0.0, 0.0, 1.0]); + } + + #[test] + fn short_black_level_is_an_error() { + let r = raw(2, 2, 1, vec![0; 4]); + let p = Params { black_repeat: (2, 2), black: vec![1.0, 2.0], cfa: Some(Pattern { rows: 2, cols: 2, colors: vec![0, 1, 1, 2] }), ..Default::default() }; + assert!(develop(&r, &p, &[1.0; 3], &IDENTITY).is_err()); + } +} From 61a9330f4e71cd20c38d505abc33b9fd42c1da44 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 15 Nov 2026 21:49:46 -0500 Subject: [PATCH 2/4] Adapt attributed FilmCraft metadata to bounded shared reader Preserve vangel76 original FilmCraft source commit 28247afae7806590dcdb149d744408739fb738df before the shared metadata extraction. Final metadata implementation is unchanged from the existing PR. --- crates/raw/src/dng_metadata.rs | 287 +++---------- crates/raw/src/dng_metadata_source_color.rs | 385 ------------------ .../raw/src/dng_metadata_source_demosaic.rs | 255 ------------ crates/raw/src/dng_metadata_source_process.rs | 264 ------------ crates/raw/src/lib.rs | 3 + 5 files changed, 69 insertions(+), 1125 deletions(-) delete mode 100644 crates/raw/src/dng_metadata_source_color.rs delete mode 100644 crates/raw/src/dng_metadata_source_demosaic.rs delete mode 100644 crates/raw/src/dng_metadata_source_process.rs diff --git a/crates/raw/src/dng_metadata.rs b/crates/raw/src/dng_metadata.rs index c0b3e9e0b..b8ba5077f 100644 --- a/crates/raw/src/dng_metadata.rs +++ b/crates/raw/src/dng_metadata.rs @@ -1,160 +1,62 @@ -//! Clean-room DNG and CinemaDNG decoder, implemented from the public specifications only (see the -//! crate README for editions): the TIFF 6.0 / TIFF-EP container, uncompressed and lossless JPEG -//! (ITU-T T.81 process 14) raw data, and the DNG processing model (linearisation, black and white -//! levels, white balance, demosaic, default crop, colour matrices) to a scene-linear Rec. 709 -//! picture. -//! -//! - [`sniff`]: is this a DNG (a TIFF with a DNGVersion tag)? -//! - [`Dng::parse`] → [`Dng::info`] (size, bit depth, CinemaDNG frame rate and time code), -//! [`Dng::raw`] (the raw samples) and [`Dng::develop`] (linear RGBA floats). -//! - [`decode`]: parse and develop in one call. -//! -//! Every number in the file is hostile: offsets, counts and sizes are checked, allocations are -//! capped by [`raw::MAX_SAMPLES`] and by the amount of data present, and the IFD walk is bounded. - -#![cfg_attr(not(test), deny(clippy::unwrap_used, clippy::expect_used, clippy::panic, clippy::unimplemented, clippy::todo, clippy::unreachable))] - -pub mod color; -pub mod demosaic; -pub mod ljpeg; -pub mod process; -pub mod raw; -pub mod tiff; -#[cfg(any(test, feature = "writer"))] -pub mod writer; - -use color::{Calibration, M3, V3}; -use demosaic::Pattern; -use process::Params; -use raw::{PHOTOMETRIC_CFA, RawImage}; -use tiff::{Entry, Ifd, Tiff, tags}; - -/// DNG decoding errors. -#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)] -pub enum Error { - #[error("not a DNG file")] - NotDng, - #[error("truncated DNG data")] - Truncated, - #[error("raw image too large")] - TooLarge, - #[error("malformed DNG: {0}")] - Malformed(&'static str), - #[error("unsupported DNG: {0}")] - Unsupported(String), +//! Bounded DNG processing and camera metadata, before application rendering. +use crate::dng_lines::{self, Error, PHOTOMETRIC_CFA, Result}; +use craft_tiff::classic::{Entry, Ifd, Tiff, tags}; +type M3 = [[f64; 3]; 3]; +type V3 = [f64; 3]; +/// Camera-plane colour filter pattern. +#[derive(Clone, Debug, PartialEq, Eq)] +pub struct Pattern { + pub rows: usize, + pub cols: usize, + pub colors: Vec, } - -pub type Result = std::result::Result; - -/// Whether `b` is a DNG: a TIFF file whose first IFD has a DNGVersion tag. -pub fn sniff(b: &[u8]) -> bool { - let Ok((t, first)) = Tiff::new(b) else { return false }; - t.read_ifd(first, 0).is_ok_and(|(ifd, _)| ifd.get(tags::DNG_VERSION).is_some()) +/// Parsed image processing tags. +#[derive(Clone, Debug, Default)] +pub struct Params { + pub active_area: Option<[usize; 4]>, + pub linearization: Option>, + pub black_repeat: (usize, usize), + pub black: Vec, + pub black_delta_h: Vec, + pub black_delta_v: Vec, + pub white: Vec, + pub crop_origin: Option<(f64, f64)>, + pub crop_size: Option<(f64, f64)>, + pub cfa: Option, } - -/// SMPTE ST 12-1 time code of a CinemaDNG frame. -#[derive(Clone, Copy, Debug, PartialEq, Eq)] -pub struct Timecode { - pub hours: u8, - pub minutes: u8, - pub seconds: u8, - pub frames: u8, - pub drop_frame: bool, +/// Parsed camera colour calibration. +#[derive(Clone, Debug, Default)] +pub struct Calibration { + pub color_matrix: [Option; 2], + pub camera_calibration: [Option; 2], + pub forward_matrix: [Option; 2], + pub illuminant: [Option; 2], + pub analog_balance: Option, + pub as_shot_neutral: Option, + pub as_shot_white_xy: Option<(f64, f64)>, + pub baseline_exposure: f64, } - -impl Timecode { - /// The first time code of a CinemaDNG TimeCodes tag (SMPTE ST 331 / 12-1 packing: BCD frames, - /// seconds, minutes, hours in bytes 0–3, the drop-frame flag in bit 6 of byte 0). - pub fn from_bytes(b: &[u8]) -> Option { - let bcd = |v: u8, tens_mask: u8| { - let (u, t) = (v & 0x0F, (v >> 4) & tens_mask); - (u <= 9).then_some(t * 10 + u) - }; - let [f, s, m, h, ..] = *b else { return None }; - let tc = Timecode { frames: bcd(f, 0x3)?, seconds: bcd(s, 0x7)?, minutes: bcd(m, 0x7)?, hours: bcd(h, 0x3)?, drop_frame: f & 0x40 != 0 }; - (tc.seconds < 60 && tc.minutes < 60 && tc.hours < 24).then_some(tc) - } +/// Metadata reader with the file's main and raw IFDs. +pub struct Metadata<'a, 'i> { + pub tiff: Tiff<'a>, + pub main: &'i Ifd, + pub raw: &'i Ifd, } - -/// What a DNG holds, without decoding the pixels. -#[derive(Clone, Debug, PartialEq)] -pub struct Info { - /// Size of the developed picture (the default crop). - pub width: usize, - pub height: usize, - /// Size of the stored raw image. - pub raw_width: usize, - pub raw_height: usize, - pub bits: u32, - pub samples_per_pixel: usize, - /// Colour filter array data (else LinearRaw). - pub cfa: bool, - /// "uncompressed" or "lossless JPEG". - pub compression: &'static str, - pub dng_version: [u8; 4], - /// CinemaDNG FrameRate (numerator, denominator). - pub frame_rate: Option<(i64, i64)>, - /// CinemaDNG time code of this frame. - pub timecode: Option, - /// UniqueCameraModel, else Make and Model. - pub camera: Option, - /// CinemaDNG ReelName. - pub reel: Option, -} - -/// A developed DNG: premultiplied, scene-linear Rec. 709 RGBA floats (alpha 1), top row first. -/// A white surface the sensor saw at its clipping level is 1.0 times 2^BaselineExposure. -pub struct Image { - pub info: Info, - pub width: usize, - pub height: usize, - pub rgba: Vec, -} - -/// A parsed DNG file. -pub struct Dng<'a> { - tiff: Tiff<'a>, - ifds: Vec, - /// Index of the main raw IFD in `ifds` (the first is IFD 0). - raw: usize, -} - -impl<'a> Dng<'a> { - /// Parse the container and find the main raw image. - pub fn parse(bytes: &'a [u8]) -> Result> { - let (tiff, first) = Tiff::new(bytes)?; - let ifds = tiff.all_ifds(first)?; - if ifds.first().and_then(|i| i.get(tags::DNG_VERSION)).is_none() { - return Err(Error::NotDng); - } - let main = raw::find_raw_ifd(&tiff, &ifds)?; - let raw = ifds.iter().position(|i| i.offset == main.offset).ok_or(Error::Malformed("raw IFD"))?; - Ok(Dng { tiff, ifds, raw }) - } - - fn ifd0(&self) -> &Ifd { - static EMPTY: Ifd = Ifd { offset: 0, entries: Vec::new(), depth: 0 }; - self.ifds.first().unwrap_or(&EMPTY) - } - - fn raw_ifd(&self) -> &Ifd { - self.ifds.get(self.raw).unwrap_or(self.ifd0()) - } - +impl Metadata<'_, '_> { /// A raw-image tag: from the raw IFD, else IFD 0. fn raw_tag(&self, tag: u16) -> Option<&Entry> { - self.raw_ifd().get(tag).or_else(|| self.ifd0().get(tag)) + self.raw.get(tag).or_else(|| self.main.get(tag)) } /// A camera-wide tag: from IFD 0, else the raw IFD. fn main_tag(&self, tag: u16) -> Option<&Entry> { - self.ifd0().get(tag).or_else(|| self.raw_ifd().get(tag)) + self.main.get(tag).or_else(|| self.raw.get(tag)) } /// A numeric tag's values; a value outside the file (or a zero denominator) is an error, so a /// damaged file does not silently lose its white balance. fn floats(&self, e: Option<&Entry>) -> Result>> { - e.map(|e| self.tiff.floats(e)).transpose() + Ok(e.map(|e| self.tiff.floats(e)).transpose()?) } /// A 3 × 3 matrix tag (`None` when absent or not 3 × 3, e.g. for other colour counts). @@ -168,13 +70,9 @@ impl<'a> Dng<'a> { Ok(self.floats(self.main_tag(tag))?.and_then(|v| <[f64; 3]>::try_from(v.as_slice()).ok())) } - fn text(&self, tag: u16) -> Option { - self.main_tag(tag).and_then(|e| self.tiff.ascii(e).ok()).filter(|s| !s.is_empty()) - } - /// The CFA pattern in camera planes (`None` for LinearRaw). - fn cfa(&self) -> Result> { - let l = raw::layout(&self.tiff, self.raw_ifd())?; + pub fn cfa(&self) -> Result> { + let l = dng_lines::layout(&self.tiff, self.raw)?; if l.photometric != PHOTOMETRIC_CFA { return Ok(None); } @@ -211,7 +109,6 @@ impl<'a> Dng<'a> { Ok(Some(Pattern { rows, cols, colors })) } - /// The processing tags of the raw image. pub fn params(&self) -> Result { let uint_pair = |tag| -> Result> { match self.raw_tag(tag) { @@ -248,7 +145,7 @@ impl<'a> Dng<'a> { None => Ok(None), } }; - let list = |tag| -> Result> { self.raw_tag(tag).map(|e| self.tiff.floats(e)).transpose().map(Option::unwrap_or_default) }; + let list = |tag| -> Result> { Ok(self.raw_tag(tag).map(|e| self.tiff.floats(e)).transpose()?.unwrap_or_default()) }; Ok(Params { active_area, linearization, @@ -281,78 +178,26 @@ impl<'a> Dng<'a> { baseline_exposure: self.floats(self.main_tag(tags::BASELINE_EXPOSURE))?.and_then(|v| v.first().copied()).unwrap_or(0.0), }) } - - /// Size, depth and CinemaDNG metadata. - pub fn info(&self) -> Result { - let l = raw::layout(&self.tiff, self.raw_ifd())?; - let p = self.params()?; - let (_, _, aw, ah) = process::active_rect(&p, l.width, l.height)?; - let (_, _, width, height) = process::crop_rect(&p, aw, ah); - let dng_version = self - .main_tag(tags::DNG_VERSION) - .and_then(|e| self.tiff.uints(e).ok()) - .and_then(|v| v.get(..4).map(|v| [0, 1, 2, 3].map(|i| v[i] as u8))) - .unwrap_or_default(); - let frame_rate = self.main_tag(tags::FRAME_RATE).and_then(|e| { - let b = self.tiff.value_bytes(e).ok()?; - if !matches!(e.ty, tiff::Type::SRational | tiff::Type::Rational) { - return None; - } - let n = i64::from(self.tiff.u32_of(b.get(0..4)?.try_into().ok()?) as i32); - let d = i64::from(self.tiff.u32_of(b.get(4..8)?.try_into().ok()?) as i32); - (n > 0 && d > 0 && n <= 1000 * d && n * 1000 >= d).then_some((n, d)) - }); - let timecode = self.main_tag(tags::TIME_CODES).and_then(|e| self.tiff.value_bytes(e).ok()).and_then(Timecode::from_bytes); - let camera = self.text(tags::UNIQUE_CAMERA_MODEL).or_else(|| { - let mm = [self.text(tags::MAKE), self.text(tags::MODEL)].into_iter().flatten().collect::>().join(" "); - (!mm.is_empty()).then_some(mm) - }); - Ok(Info { - width, - height, - raw_width: l.width, - raw_height: l.height, - bits: l.bits, - samples_per_pixel: l.samples_per_pixel, - cfa: p.cfa.is_some(), - compression: if l.compression == 7 { "lossless JPEG" } else { "uncompressed" }, - dng_version, - frame_rate, - timecode, - camera, - reel: self.text(tags::REEL_NAME), - }) - } - - /// The raw samples of the main image, as stored (before any processing). - pub fn raw(&self) -> Result { - raw::read(&self.tiff, self.raw_ifd()) - } - - /// Develop the main image to scene-linear Rec. 709. - pub fn develop(&self) -> Result { - let info = self.info()?; - let p = self.params()?; - let raw = self.raw()?; - let calibration = self.calibration()?; - let solved = if p.cfa.is_none() && raw.samples_per_pixel == 1 { - // monochrome: no colour, only the exposure - let e = 2f64.powf(calibration.baseline_exposure.clamp(-16.0, 16.0)); - color::Solved { neutral: [1.0; 3], matrix: color::scale(&color::IDENTITY, e), white_xy: color::D65 } - } else { - calibration.solve()? - }; - let d = process::develop(&raw, &p, &solved.neutral, &solved.matrix)?; - Ok(Image { info, width: d.width, height: d.height, rgba: d.rgba }) - } } -/// Parse and develop a DNG file. -pub fn decode(bytes: &[u8]) -> Result { - Dng::parse(bytes)?.develop() -} +#[cfg(test)] +mod tests { + use super::*; -/// Parse a DNG file's metadata. -pub fn probe(bytes: &[u8]) -> Result { - Dng::parse(bytes)?.info() + #[test] + fn calibration_defaults_and_rejects_bad_rational() -> Result<()> { + let empty = Ifd::default(); + let tiff = Tiff { data: &[], big_endian: false }; + let metadata = Metadata { tiff, main: &empty, raw: &empty }; + let calibration = metadata.calibration()?; + assert_eq!(calibration.baseline_exposure, 0.0); + assert_eq!(calibration.color_matrix, [None, None]); + + // IFD with one BaselineExposure SRational pointing past the end of the file. + let data = [b'I', b'I', 42, 0, 8, 0, 0, 0, 1, 0, 42, 198, 10, 0, 1, 0, 0, 0, 200, 0, 0, 0, 0, 0, 0, 0]; + let (tiff, first) = Tiff::new(&data)?; + let (ifd, _) = tiff.read_ifd(first, 0)?; + assert!(Metadata { tiff, main: &ifd, raw: &ifd }.calibration().is_err()); + Ok(()) + } } diff --git a/crates/raw/src/dng_metadata_source_color.rs b/crates/raw/src/dng_metadata_source_color.rs deleted file mode 100644 index fb02f2c8d..000000000 --- a/crates/raw/src/dng_metadata_source_color.rs +++ /dev/null @@ -1,385 +0,0 @@ -//! Camera colour to linear Rec. 709, following the DNG specification's "Mapping Camera Color Space -//! to CIE XYZ Space" (DNG 1.4–1.7 chapter 6): -//! -//! - The two calibrations (ColorMatrix, CameraCalibration, ForwardMatrix 1 and 2) are blended -//! linearly in inverse correlated colour temperature between their CalibrationIlluminants. -//! - The white balance is AsShotNeutral (found as a white xy by the specification's iteration) or -//! AsShotWhiteXY; without either, D65. -//! - With ForwardMatrix: CameraToXYZ_D50 = FM · D · (AB · CC)⁻¹, D = diag((AB · CC)⁻¹ · neutral)⁻¹. -//! Without: CameraToXYZ_D50 = CA · (AB · CC · CM)⁻¹, CA a linear Bradford adaptation from the -//! white to D50. -//! - XYZ (D50) goes to linear Rec. 709 / sRGB primaries (D65 white) through Bradford again. -//! -//! The correlated colour temperature of an xy white comes from McCamy's cubic approximation -//! (C. S. McCamy, "Correlated color temperature as an explicit function of chromaticity -//! coordinates", Color Research & Application 17(2), 1992); the illuminant temperatures are the -//! CIE / EXIF 2.3 LightSource values. - -use crate::{Error, Result}; - -pub type M3 = [[f64; 3]; 3]; -pub type V3 = [f64; 3]; - -pub const IDENTITY: M3 = [[1.0, 0.0, 0.0], [0.0, 1.0, 0.0], [0.0, 0.0, 1.0]]; - -/// CIE D50 and D65 white chromaticities (the DNG specification's D50). -pub const D50: (f64, f64) = (0.3457, 0.3585); -pub const D65: (f64, f64) = (0.3127, 0.3290); - -/// Linear Bradford cone response matrix. -const BRADFORD: M3 = [[0.8951, 0.2664, -0.1614], [-0.7502, 1.7135, 0.0367], [0.0389, -0.0685, 1.0296]]; - -pub fn mul(a: &M3, b: &M3) -> M3 { - let mut m = [[0.0; 3]; 3]; - for (i, row) in m.iter_mut().enumerate() { - for (j, v) in row.iter_mut().enumerate() { - *v = (0..3).map(|k| a[i][k] * b[k][j]).sum(); - } - } - m -} - -pub fn mulv(a: &M3, v: &V3) -> V3 { - [0, 1, 2].map(|i| a[i][0] * v[0] + a[i][1] * v[1] + a[i][2] * v[2]) -} - -pub fn diag(v: &V3) -> M3 { - [[v[0], 0.0, 0.0], [0.0, v[1], 0.0], [0.0, 0.0, v[2]]] -} - -pub fn scale(a: &M3, s: f64) -> M3 { - a.map(|r| r.map(|v| v * s)) -} - -fn blend(a: &M3, b: &M3, w: f64) -> M3 { - let mut m = [[0.0; 3]; 3]; - for i in 0..3 { - for j in 0..3 { - m[i][j] = w * a[i][j] + (1.0 - w) * b[i][j]; - } - } - m -} - -/// The inverse of `a`, or `None` when it is (nearly) singular. -pub fn inverse(a: &M3) -> Option { - let c = |r: usize, s: usize| a[(r + 1) % 3][(s + 1) % 3] * a[(r + 2) % 3][(s + 2) % 3] - a[(r + 1) % 3][(s + 2) % 3] * a[(r + 2) % 3][(s + 1) % 3]; - let det = a[0][0] * c(0, 0) + a[0][1] * c(0, 1) + a[0][2] * c(0, 2); - let norm: f64 = a.iter().flatten().map(|v| v.abs()).fold(0.0, f64::max); - if !det.is_finite() || det.abs() <= 1e-12 * norm.powi(3).max(1e-300) { - return None; - } - let mut m = [[0.0; 3]; 3]; - for (i, row) in m.iter_mut().enumerate() { - for (j, v) in row.iter_mut().enumerate() { - *v = c(j, i) / det; - } - } - Some(m) -} - -/// XYZ (Y = 1) of a chromaticity. -pub fn xy_to_xyz((x, y): (f64, f64)) -> V3 { - let y = y.max(1e-6); - [x / y, 1.0, (1.0 - x - y) / y] -} - -pub fn xyz_to_xy(v: &V3) -> Option<(f64, f64)> { - let s = v[0] + v[1] + v[2]; - (s.is_finite() && s > 1e-12).then(|| (v[0] / s, v[1] / s)) -} - -/// Correlated colour temperature (kelvin) of a chromaticity, McCamy (1992), limited to -/// 1 000–50 000 K. -pub fn cct((x, y): (f64, f64)) -> f64 { - let d = 0.1858 - y; - if d.abs() < 1e-9 { - return 50_000.0; - } - let n = (x - 0.3320) / d; - let t = 449.0 * n.powi(3) + 3525.0 * n.powi(2) + 6823.3 * n + 5520.33; - if t.is_finite() { t.clamp(1_000.0, 50_000.0) } else { 50_000.0 } -} - -/// Colour temperature of an EXIF LightSource code used as a DNG CalibrationIlluminant. -pub fn illuminant_kelvin(code: u64) -> Option { - Some(match code { - 1 | 4 | 9 => 5500.0, // daylight, flash, fine weather - 2 | 14 => 4150.0, // fluorescent, cool white fluorescent (W 3800–4500 K) - 3 => 2850.0, // tungsten - 10 => 6500.0, // cloudy - 11 => 7500.0, // shade - 12 => 6430.0, // daylight fluorescent (D 5700–7100 K) - 13 => 5050.0, // day white fluorescent (N 4600–5500 K) - 15 => 3525.0, // white fluorescent (WW 3250–3800 K) - 16 => 2925.0, // warm white fluorescent (L 2600–3250 K) - 17 => 2856.0, // standard light A - 18 => 4874.0, // standard light B - 19 => 6774.0, // standard light C - 20 => 5503.0, // D55 - 21 => 6504.0, // D65 - 22 => 7504.0, // D75 - 23 => 5003.0, // D50 - 24 => 3200.0, // ISO studio tungsten - _ => return None, - }) -} - -/// Linear Bradford adaptation from one white (XYZ) to another. -pub fn bradford(src: &V3, dst: &V3) -> Option { - let s = mulv(&BRADFORD, src); - let d = mulv(&BRADFORD, dst); - if s.iter().any(|v| v.abs() < 1e-12) { - return None; - } - let g = [d[0] / s[0], d[1] / s[1], d[2] / s[2]]; - Some(mul(&inverse(&BRADFORD)?, &mul(&diag(&g), &BRADFORD))) -} - -/// Linear RGB (Rec. 709 / sRGB primaries, D65 white) → XYZ. -pub fn rec709_to_xyz() -> Option { - let p = [(0.64, 0.33), (0.30, 0.60), (0.15, 0.06)].map(xy_to_xyz); - let pm: M3 = [[p[0][0], p[1][0], p[2][0]], [p[0][1], p[1][1], p[2][1]], [p[0][2], p[1][2], p[2][2]]]; - let s = mulv(&inverse(&pm)?, &xy_to_xyz(D65)); - Some(mul(&pm, &diag(&s))) -} - -/// XYZ relative to D50 → linear Rec. 709. -pub fn xyz_d50_to_rec709() -> Option { - let to_d65 = bradford(&xy_to_xyz(D50), &xy_to_xyz(D65))?; - Some(mul(&inverse(&rec709_to_xyz()?)?, &to_d65)) -} - -/// The colour tags of a three-colour DNG. -#[derive(Clone, Debug, Default)] -pub struct Calibration { - pub color_matrix: [Option; 2], - pub camera_calibration: [Option; 2], - pub forward_matrix: [Option; 2], - /// EXIF LightSource codes of the two calibrations. - pub illuminant: [Option; 2], - pub analog_balance: Option, - pub as_shot_neutral: Option, - pub as_shot_white_xy: Option<(f64, f64)>, - pub baseline_exposure: f64, -} - -/// The result of [`Calibration::solve`]. -#[derive(Clone, Debug, PartialEq)] -pub struct Solved { - /// Camera neutral: raw (normalised) camera values of a white surface. Dividing by it white - /// balances the camera values. - pub neutral: V3, - /// White-balanced camera values → linear Rec. 709, including the baseline exposure. The white - /// balanced white (1, 1, 1) goes to Y = 2^BaselineExposure. - pub matrix: M3, - /// The white point found, for information. - pub white_xy: (f64, f64), -} - -impl Calibration { - /// Weight of calibration 1 for a white of temperature `t` (1 when only one is usable). - fn weight(&self, t: f64) -> f64 { - let (Some(_), Some(_)) = (self.color_matrix[0], self.color_matrix[1]) else { return 1.0 }; - let (Some(t1), Some(t2)) = (self.illuminant[0].and_then(illuminant_kelvin), self.illuminant[1].and_then(illuminant_kelvin)) else { - return 1.0; - }; - if (t1 - t2).abs() < 1.0 { - return 1.0; - } - // the formula needs t1 < t2; swap the roles otherwise - let (lo, hi, lo_is_1) = if t1 < t2 { (t1, t2, true) } else { (t2, t1, false) }; - let w_lo = if t <= lo { - 1.0 - } else if t >= hi { - 0.0 - } else { - (1.0 / t - 1.0 / hi) / (1.0 / lo - 1.0 / hi) - }; - if lo_is_1 { w_lo } else { 1.0 - w_lo } - } - - fn pick(&self, m: &[Option; 2], w: f64, default: M3) -> M3 { - match (m[0], m[1]) { - (Some(a), Some(b)) => blend(&a, &b, w), - (Some(a), None) => a, - (None, Some(b)) if self.color_matrix[0].is_none() => b, - _ => default, - } - } - - /// XYZ → camera at weight `w`: AB · CC · CM. - fn xyz_to_camera(&self, w: f64) -> Option { - let cm = match self.color_matrix { - [None, None] => return None, - _ => self.pick(&self.color_matrix, w, IDENTITY), - }; - let cc = self.pick(&self.camera_calibration, w, IDENTITY); - let ab = diag(&self.analog_balance.unwrap_or([1.0; 3])); - Some(mul(&ab, &mul(&cc, &cm))) - } - - /// Work out the white balance and the camera → linear Rec. 709 matrix. - pub fn solve(&self) -> Result { - let out_matrix = xyz_d50_to_rec709().ok_or(Error::Malformed("colour maths"))?; - let exposure = 2f64.powf(self.baseline_exposure.clamp(-16.0, 16.0)); - if self.color_matrix.iter().all(Option::is_none) { - // no colour calibration: the camera values are taken as Rec. 709, white balanced - let neutral = self.as_shot_neutral.unwrap_or([1.0; 3]); - if neutral.iter().any(|v| !v.is_finite() || *v <= 1e-9) { - return Err(Error::Malformed("camera neutral must be positive")); - } - return Ok(Solved { neutral, matrix: scale(&IDENTITY, exposure), white_xy: D65 }); - } - let bad = || Error::Malformed("singular colour matrix"); - // the white point and the camera neutral - let (white_xy, neutral) = if let Some(n) = self.as_shot_neutral { - let mut xy = D50; - for _ in 0..50 { - let w = self.weight(cct(xy)); - let to_xyz = inverse(&self.xyz_to_camera(w).ok_or_else(bad)?).ok_or_else(bad)?; - let next = xyz_to_xy(&mulv(&to_xyz, &n)).ok_or(Error::Malformed("AsShotNeutral is not a colour"))?; - let done = (next.0 - xy.0).abs() + (next.1 - xy.1).abs() < 1e-9; - xy = next; - if done { - break; - } - } - (xy, n) - } else { - let xy = self.as_shot_white_xy.unwrap_or(D65); - let w = self.weight(cct(xy)); - let n = mulv(&self.xyz_to_camera(w).ok_or_else(bad)?, &xy_to_xyz(xy)); - (xy, n) - }; - if neutral.iter().any(|v| !v.is_finite() || *v <= 1e-9) { - return Err(Error::Malformed("camera neutral must be positive")); - } - let w = self.weight(cct(white_xy)); - let ab_cc = mul(&diag(&self.analog_balance.unwrap_or([1.0; 3])), &self.pick(&self.camera_calibration, w, IDENTITY)); - let camera_to_xyz_d50 = if self.forward_matrix.iter().any(Option::is_some) { - let fm = self.pick(&self.forward_matrix, w, IDENTITY); - let inv_ab_cc = inverse(&ab_cc).ok_or_else(bad)?; - let reference = mulv(&inv_ab_cc, &neutral); - if reference.iter().any(|v| v.abs() < 1e-12) { - return Err(bad()); - } - mul(&fm, &mul(&diag(&reference.map(|v| 1.0 / v)), &inv_ab_cc)) - } else { - let to_xyz = inverse(&self.xyz_to_camera(w).ok_or_else(bad)?).ok_or_else(bad)?; - let ca = bradford(&xy_to_xyz(white_xy), &xy_to_xyz(D50)).ok_or_else(bad)?; - mul(&ca, &to_xyz) - }; - // white-balanced camera values: camera = neutral · wb - let wb_to_xyz = mul(&camera_to_xyz_d50, &diag(&neutral)); - let y = mulv(&wb_to_xyz, &[1.0; 3])[1]; - if !y.is_finite() || y <= 1e-9 { - return Err(bad()); - } - let matrix = scale(&mul(&out_matrix, &wb_to_xyz), exposure / y); - if matrix.iter().flatten().any(|v| !v.is_finite()) { - return Err(bad()); - } - Ok(Solved { neutral, matrix, white_xy }) - } -} - -#[cfg(test)] -mod tests { - use super::*; - - fn close(a: &M3, b: &M3, tol: f64) -> bool { - a.iter().flatten().zip(b.iter().flatten()).all(|(x, y)| (x - y).abs() <= tol) - } - - #[test] - fn rec709_matrix_matches_the_published_one() { - // IEC 61966-2-1 (sRGB) linear RGB → XYZ, four decimals - let m = rec709_to_xyz().unwrap(); - let want = [[0.4124, 0.3576, 0.1805], [0.2126, 0.7152, 0.0722], [0.0193, 0.1192, 0.9505]]; - assert!(close(&m, &want, 1e-3), "{m:?}"); - } - - #[test] - fn inverse_round_trips_and_refuses_singular() { - let a = [[2.0, 1.0, 0.5], [0.1, 3.0, 0.2], [0.3, 0.4, 1.5]]; - assert!(close(&mul(&a, &inverse(&a).unwrap()), &IDENTITY, 1e-12)); - assert!(inverse(&[[1.0, 2.0, 3.0], [2.0, 4.0, 6.0], [0.0, 0.0, 1.0]]).is_none()); - assert!(inverse(&[[f64::NAN; 3]; 3]).is_none()); - } - - #[test] - fn mccamy_temperatures() { - assert!((cct(D65) - 6504.0).abs() < 15.0, "{}", cct(D65)); - assert!((cct(D50) - 5003.0).abs() < 15.0, "{}", cct(D50)); - assert!((cct((0.44757, 0.40745)) - 2856.0).abs() < 15.0, "illuminant A"); - assert_eq!(cct((0.3320, 0.1858)), 50_000.0, "no division by zero"); - } - - #[test] - fn interpolation_weight_follows_inverse_temperature() { - let c = Calibration { color_matrix: [Some(IDENTITY), Some(IDENTITY)], illuminant: [Some(17), Some(21)], ..Default::default() }; - assert_eq!(c.weight(2000.0), 1.0); - assert_eq!(c.weight(9000.0), 0.0); - let mid = c.weight(4000.0); - let want = (1.0 / 4000.0 - 1.0 / 6504.0) / (1.0 / 2856.0 - 1.0 / 6504.0); - assert!((mid - want).abs() < 1e-12); - // swapped order: weight of calibration 1 (now D65) - let s = Calibration { illuminant: [Some(21), Some(17)], ..c.clone() }; - assert!((s.weight(4000.0) - (1.0 - want)).abs() < 1e-12); - // unknown illuminant: calibration 1 only - let u = Calibration { illuminant: [Some(0), Some(21)], ..c }; - assert_eq!(u.weight(4000.0), 1.0); - } - - #[test] - fn an_srgb_camera_maps_to_itself() { - // ColorMatrix = XYZ → linear sRGB, white D65: the camera is Rec. 709 already - let cm = inverse(&rec709_to_xyz().unwrap()).unwrap(); - let c = Calibration { color_matrix: [Some(cm), None], illuminant: [Some(21), None], as_shot_neutral: Some([1.0; 3]), ..Default::default() }; - let s = c.solve().unwrap(); - assert!(close(&s.matrix, &IDENTITY, 1e-6), "{:?}", s.matrix); - assert!((s.white_xy.0 - D65.0).abs() < 1e-4 && (s.white_xy.1 - D65.1).abs() < 1e-4, "{:?}", s.white_xy); - // a baseline exposure of +1 doubles everything - let e = Calibration { baseline_exposure: 1.0, ..c }.solve().unwrap(); - assert!(close(&e.matrix, &scale(&IDENTITY, 2.0), 1e-6)); - } - - #[test] - fn forward_matrix_white_balances_by_the_neutral() { - // FM = linear Rec. 709 → XYZ D50: the white-balanced camera values come out unchanged - let fm = mul(&bradford(&xy_to_xyz(D65), &xy_to_xyz(D50)).unwrap(), &rec709_to_xyz().unwrap()); - let cm = inverse(&rec709_to_xyz().unwrap()).unwrap(); - let c = Calibration { - color_matrix: [Some(cm), None], - forward_matrix: [Some(fm), None], - illuminant: [Some(21), None], - as_shot_neutral: Some([0.5, 1.0, 0.8]), - ..Default::default() - }; - let s = c.solve().unwrap(); - assert_eq!(s.neutral, [0.5, 1.0, 0.8]); - assert!(close(&s.matrix, &IDENTITY, 1e-6), "{:?}", s.matrix); - } - - #[test] - fn white_xy_gives_the_neutral() { - let cm = inverse(&rec709_to_xyz().unwrap()).unwrap(); - let c = Calibration { color_matrix: [Some(cm), None], illuminant: [Some(21), None], as_shot_white_xy: Some(D65), ..Default::default() }; - let s = c.solve().unwrap(); - for v in s.neutral { - assert!((v - s.neutral[1]).abs() < 1e-3, "D65 is neutral for an sRGB camera: {:?}", s.neutral); - } - } - - #[test] - fn hostile_calibrations_are_errors() { - let zero = Calibration { color_matrix: [Some([[0.0; 3]; 3]), None], as_shot_neutral: Some([1.0; 3]), ..Default::default() }; - assert!(zero.solve().is_err()); - let cm = inverse(&rec709_to_xyz().unwrap()).unwrap(); - let neg = Calibration { color_matrix: [Some(cm), None], as_shot_neutral: Some([-1.0, 1.0, 1.0]), ..Default::default() }; - assert!(neg.solve().is_err()); - let none = Calibration { as_shot_neutral: Some([0.5, 1.0, 0.5]), ..Default::default() }.solve().unwrap(); - assert_eq!(none.matrix, IDENTITY); - } -} diff --git a/crates/raw/src/dng_metadata_source_demosaic.rs b/crates/raw/src/dng_metadata_source_demosaic.rs deleted file mode 100644 index 911236929..000000000 --- a/crates/raw/src/dng_metadata_source_demosaic.rs +++ /dev/null @@ -1,255 +0,0 @@ -//! Demosaicing: one colour sample per pixel (a colour filter array) to three. -//! -//! - A 2 × 2 Bayer pattern (one red, two green, one blue) uses the gradient-corrected linear -//! interpolation of H. S. Malvar, L.-W. He and R. Cutler, "High-quality linear interpolation for -//! demosaicing of Bayer-patterned color images", IEEE ICASSP 2004: bilinear interpolation plus a -//! Laplacian correction from the pixel's own colour, as 5 × 5 filters. -//! - Any other pattern (up to 8 × 8) uses bilinear interpolation: the mean of the nearest samples -//! of each missing colour in the 3 × 3 neighbourhood, else the 5 × 5 one. -//! -//! The picture is extended by two pixels on every side by mirroring (without repeating the edge), -//! which keeps a 2 × 2 pattern's phase. - -#[cfg(feature = "threads")] -use rayon::prelude::*; - -/// A CFA pattern: `rows` × `cols` colour indices (0 red, 1 green, 2 blue), origin at the top left -/// of the image. -#[derive(Clone, Debug, PartialEq, Eq)] -pub struct Pattern { - pub rows: usize, - pub cols: usize, - pub colors: Vec, -} - -impl Pattern { - pub fn color(&self, x: usize, y: usize) -> u8 { - self.colors.get((y % self.rows) * self.cols + x % self.cols).copied().unwrap_or(1) - } - - /// Whether this is a 2 × 2 Bayer pattern. - pub fn is_bayer(&self) -> bool { - if (self.rows, self.cols) != (2, 2) { - return false; - } - let [a, b, c, d] = self.colors[..] else { return false }; - let count = |k: u8| [a, b, c, d].iter().filter(|&&v| v == k).count(); - // the two greens sit on a diagonal - count(0) == 1 && count(1) == 2 && count(2) == 1 && ((a == 1 && d == 1) || (b == 1 && c == 1)) - } -} - -const PAD: usize = 2; - -/// Mirror index `i` (which may be up to PAD outside) into 0..n. -fn mirror(i: isize, n: usize) -> usize { - let n = n as isize; - let mut i = i; - if i < 0 { - i = -i; - } - if i >= n { - i = 2 * (n - 1) - i; - } - i.clamp(0, n - 1) as usize -} - -/// The plane extended by PAD mirrored pixels on each side. -fn padded(plane: &[f32], w: usize, h: usize) -> (Vec, usize) { - let pw = w + 2 * PAD; - let ph = h + 2 * PAD; - let mut out = vec![0f32; pw * ph]; - for y in 0..ph { - let sy = mirror(y as isize - PAD as isize, h); - for x in 0..pw { - let sx = mirror(x as isize - PAD as isize, w); - out[y * pw + x] = plane.get(sy * w + sx).copied().unwrap_or(0.0); - } - } - (out, pw) -} - -/// Demosaic `plane` (`w` × `h`, one sample per pixel) into interleaved RGB. -pub fn demosaic(plane: &[f32], w: usize, h: usize, pattern: &Pattern) -> Vec { - let mut out = vec![0f32; w * h * 3]; - if w == 0 || h == 0 || plane.len() < w * h { - return out; - } - let (p, pw) = padded(plane, w, h); - let bayer = pattern.is_bayer() && w >= 3 && h >= 3; - let row = |y: usize, o: &mut [f32]| { - if bayer { - malvar_row(&p, pw, y, w, pattern, o); - } else { - bilinear_row(&p, pw, y, w, h, pattern, o); - } - }; - #[cfg(feature = "threads")] - out.par_chunks_mut(w * 3).enumerate().for_each(|(y, o)| row(y, o)); - #[cfg(not(feature = "threads"))] - out.chunks_mut(w * 3).enumerate().for_each(|(y, o)| row(y, o)); - out -} - -fn malvar_row(p: &[f32], pw: usize, y: usize, w: usize, pat: &Pattern, out: &mut [f32]) { - let py = y + PAD; - for x in 0..w { - let px = x + PAD; - let at = |dx: isize, dy: isize| p[((py as isize + dy) as usize) * pw + (px as isize + dx) as usize]; - let c = at(0, 0); - let cross1 = at(-1, 0) + at(1, 0) + at(0, -1) + at(0, 1); - let cross2 = at(-2, 0) + at(2, 0) + at(0, -2) + at(0, 2); - let diag = at(-1, -1) + at(1, -1) + at(-1, 1) + at(1, 1); - let me = pat.color(x, y); - let rgb = match me { - 1 => { - let h1 = at(-1, 0) + at(1, 0); - let h2 = at(-2, 0) + at(2, 0); - let v1 = at(0, -1) + at(0, 1); - let v2 = at(0, -2) + at(0, 2); - // colour along the row and along the column - let along_row = (5.0 * c + 4.0 * h1 - h2 - diag + 0.5 * v2) / 8.0; - let along_col = (5.0 * c + 4.0 * v1 - v2 - diag + 0.5 * h2) / 8.0; - if pat.color(x + 1, y) == 0 { [along_row, c, along_col] } else { [along_col, c, along_row] } - } - _ => { - let g = (4.0 * c + 2.0 * cross1 - cross2) / 8.0; - let other = (6.0 * c + 2.0 * diag - 1.5 * cross2) / 8.0; - if me == 0 { [c, g, other] } else { [other, g, c] } - } - }; - if let Some(o) = out.get_mut(x * 3..x * 3 + 3) { - o.copy_from_slice(&rgb); - } - } -} - -fn bilinear_row(p: &[f32], pw: usize, y: usize, w: usize, h: usize, pat: &Pattern, out: &mut [f32]) { - for x in 0..w { - let me = pat.color(x, y); - let mut rgb = [0f32; 3]; - for (k, v) in rgb.iter_mut().enumerate() { - if usize::from(me) == k { - *v = p[(y + PAD) * pw + x + PAD]; - continue; - } - for r in 1..=PAD as isize { - let (mut sum, mut n) = (0f32, 0u32); - for dy in -r..=r { - for dx in -r..=r { - // the colour of a mirrored neighbour is the colour where it was mirrored from - let sx = mirror(x as isize + dx, w); - let sy = mirror(y as isize + dy, h); - if usize::from(pat.color(sx, sy)) == k { - sum += p[(sy + PAD) * pw + sx + PAD]; - n += 1; - } - } - } - if n > 0 { - *v = sum / n as f32; - break; - } - } - } - if let Some(o) = out.get_mut(x * 3..x * 3 + 3) { - o.copy_from_slice(&rgb); - } - } -} - -#[cfg(test)] -mod tests { - use super::*; - - fn rggb() -> Pattern { - Pattern { rows: 2, cols: 2, colors: vec![0, 1, 1, 2] } - } - - /// A mosaic of an RGB picture given per pixel. - fn mosaic(w: usize, h: usize, pat: &Pattern, rgb: impl Fn(usize, usize) -> [f32; 3]) -> Vec { - (0..h).flat_map(|y| (0..w).map(move |x| (x, y))).map(|(x, y)| rgb(x, y)[usize::from(pat.color(x, y))]).collect() - } - - #[test] - fn bayer_detection() { - assert!(rggb().is_bayer()); - for c in [[1, 0, 2, 1], [1, 2, 0, 1], [2, 1, 1, 0]] { - assert!(Pattern { rows: 2, cols: 2, colors: c.to_vec() }.is_bayer(), "{c:?}"); - } - assert!(!Pattern { rows: 2, cols: 2, colors: vec![0, 1, 2, 1] }.is_bayer(), "greens side by side"); - assert!(!Pattern { rows: 2, cols: 2, colors: vec![0, 0, 1, 2] }.is_bayer()); - assert!(!Pattern { rows: 1, cols: 3, colors: vec![0, 1, 2] }.is_bayer()); - } - - #[test] - fn flat_fields_stay_flat_for_every_bayer_phase_and_other_patterns() { - let pats = [[0, 1, 1, 2], [1, 0, 2, 1], [1, 2, 0, 1], [2, 1, 1, 0]].map(|c| Pattern { rows: 2, cols: 2, colors: c.to_vec() }); - let stripes = Pattern { rows: 1, cols: 3, colors: vec![0, 1, 2] }; - for pat in pats.iter().chain([&stripes]) { - let (w, h) = (9, 7); - let m = mosaic(w, h, pat, |_, _| [0.25, 0.5, 0.75]); - let out = demosaic(&m, w, h, pat); - for (i, px) in out.chunks(3).enumerate() { - for (k, want) in [0.25, 0.5, 0.75].iter().enumerate() { - assert!((px[k] - want).abs() < 1e-6, "{pat:?} pixel {i} channel {k}: {}", px[k]); - } - } - } - } - - #[test] - fn known_samples_are_kept_and_linear_ramps_are_exact() { - // a horizontal ramp is reconstructed exactly away from the border (all filters are - // symmetric and sum to one, the correction terms sum to zero) - let pat = rggb(); - let (w, h) = (16, 12); - let f = |x: usize, _y: usize| { - let v = x as f32 / 16.0; - [v, 0.5 * v + 0.1, 0.2 + 0.25 * v] - }; - let m = mosaic(w, h, &pat, f); - let out = demosaic(&m, w, h, &pat); - for y in 0..h { - for x in 0..w { - let px = &out[(y * w + x) * 3..][..3]; - let own = usize::from(pat.color(x, y)); - assert_eq!(px[own], m[y * w + x], "own sample kept"); - if (2..w - 2).contains(&x) { - let want = f(x, y); - for k in 0..3 { - assert!((px[k] - want[k]).abs() < 1e-5, "({x},{y}) ch {k}: {} vs {}", px[k], want[k]); - } - } - } - } - } - - #[test] - fn a_vertical_edge_does_not_smear_far() { - // white left half, black right half: pixels two columns from the edge are untouched - let pat = rggb(); - let (w, h) = (12, 8); - let m = mosaic(w, h, &pat, |x, _| if x < 6 { [1.0; 3] } else { [0.0; 3] }); - let out = demosaic(&m, w, h, &pat); - for y in 0..h { - for x in (0..3).chain(9..12) { - let want = if x < 6 { 1.0 } else { 0.0 }; - for k in 0..3 { - assert!((out[(y * w + x) * 3 + k] - want).abs() < 1e-6, "({x},{y}) ch {k}"); - } - } - } - } - - #[test] - fn tiny_and_empty_pictures_do_not_panic() { - let pat = rggb(); - for (w, h) in [(0, 0), (1, 1), (2, 1), (1, 5), (2, 2), (3, 2)] { - let m = vec![0.5; w * h]; - let out = demosaic(&m, w, h, &pat); - assert_eq!(out.len(), w * h * 3); - } - assert_eq!(demosaic(&[0.5; 3], 4, 4, &pat).len(), 48, "short plane"); - } -} diff --git a/crates/raw/src/dng_metadata_source_process.rs b/crates/raw/src/dng_metadata_source_process.rs deleted file mode 100644 index 41d9b25ae..000000000 --- a/crates/raw/src/dng_metadata_source_process.rs +++ /dev/null @@ -1,264 +0,0 @@ -//! Raw samples to a linear Rec. 709 picture, in the order of the DNG specification's chapter 5 -//! ("Mapping Raw Values to Linear Reference Values") and chapter 6 (colour): -//! -//! 1. ActiveArea: only the active (unmasked) sensor area is used; black levels, the CFA pattern -//! and the default crop are relative to its top left corner. -//! 2. LinearizationTable maps each sample. -//! 3. The black level (BlackLevel over its BlackLevelRepeatDim pattern, plus BlackLevelDeltaH for -//! the column and BlackLevelDeltaV for the row) is subtracted and the range up to WhiteLevel -//! scaled to 0–1 (clipped there). -//! 4. White balance: each sample is divided by the camera neutral of its colour and clipped at -//! 1, so highlights the sensor clipped stay neutral. -//! 5. A CFA is demosaiced ([`crate::demosaic`]). -//! 6. DefaultCropOrigin / DefaultCropSize cut the picture. -//! 7. The colour matrix of [`crate::color`] takes the white-balanced camera values to linear -//! Rec. 709 (including BaselineExposure); negative results are clipped to 0. - -#[cfg(feature = "threads")] -use rayon::prelude::*; - -use crate::color::{M3, V3}; -use crate::demosaic::{Pattern, demosaic}; -use crate::raw::RawImage; -use crate::{Error, Result}; - -/// The processing tags of a raw image. -#[derive(Clone, Debug, Default)] -pub struct Params { - /// top, left, bottom, right - pub active_area: Option<[usize; 4]>, - pub linearization: Option>, - /// rows, columns of the BlackLevel pattern - pub black_repeat: (usize, usize), - /// rows × columns × samples per pixel values - pub black: Vec, - pub black_delta_h: Vec, - pub black_delta_v: Vec, - /// per sample - pub white: Vec, - pub crop_origin: Option<(f64, f64)>, - pub crop_size: Option<(f64, f64)>, - /// CFA pattern in camera planes (`None`: LinearRaw). - pub cfa: Option, -} - -/// A rectangle: x, y, width, height. -type Rect = (usize, usize, usize, usize); - -/// The active area of a `w` × `h` raw image. -pub fn active_rect(p: &Params, w: usize, h: usize) -> Result { - match p.active_area { - None => Ok((0, 0, w, h)), - Some([top, left, bottom, right]) => { - if top >= bottom || left >= right || bottom > h || right > w { - return Err(Error::Malformed("ActiveArea outside the image")); - } - Ok((left, top, right - left, bottom - top)) - } - } -} - -/// The default crop inside an active area of `aw` × `ah`. -pub fn crop_rect(p: &Params, aw: usize, ah: usize) -> Rect { - let (ox, oy) = p.crop_origin.unwrap_or((0.0, 0.0)); - let (sw, sh) = p.crop_size.unwrap_or((aw as f64, ah as f64)); - let to = |v: f64, max: usize| if v.is_finite() && v > 0.0 { (v.min(max as f64)) as usize } else { 0 }; - let x = to(ox.floor(), aw.saturating_sub(1)); - let y = to(oy.floor(), ah.saturating_sub(1)); - let w = to(sw.round(), aw - x).max(1); - let h = to(sh.round(), ah - y).max(1); - (x, y, w, h) -} - -/// Normalised, white-balanced samples of the active area (steps 1–4), one plane per sample. -fn normalise(raw: &RawImage, p: &Params, (ax, ay, aw, ah): Rect, neutral: &V3, planes: usize) -> Result> { - let spp = raw.samples_per_pixel; - let (br, bc) = (p.black_repeat.0.max(1), p.black_repeat.1.max(1)); - if br > 64 || bc > 64 { - return Err(Error::Unsupported("a BlackLevelRepeatDim larger than 64".into())); - } - let need = br * bc * spp; - let black: Vec = match p.black.len() { - 0 => vec![0.0; need], - 1 => vec![p.black[0]; need], - n if n >= need => p.black[..need].to_vec(), - _ => return Err(Error::Malformed("BlackLevel has too few values")), - }; - let max_code = ((1u32 << raw.bits) - 1) as f64; - let white: Vec = (0..spp).map(|s| p.white.get(s).or(p.white.first()).copied().unwrap_or(max_code)).collect(); - let lin = p.linearization.as_deref().filter(|t| !t.is_empty()); - let inv_neutral: Vec = (0..planes).map(|c| (1.0 / neutral.get(c).copied().unwrap_or(1.0)) as f32).collect(); - let cfa = p.cfa.as_ref(); - let mut out = vec![0f32; aw * ah * spp]; - let row = |y: usize, o: &mut [f32]| { - let dv = p.black_delta_v.get(y).copied().unwrap_or(0.0); - let src_row = (ay + y) * raw.width * spp; - for x in 0..aw { - let dh = p.black_delta_h.get(x).copied().unwrap_or(0.0); - for s in 0..spp { - let code = raw.data.get(src_row + (ax + x) * spp + s).copied().unwrap_or(0); - let v = match lin { - Some(t) => f64::from(t.get(usize::from(code)).or(t.last()).copied().unwrap_or(0)), - None => f64::from(code), - }; - let b = black[((y % br) * bc + x % bc) * spp + s] + dh + dv; - let range = white[s] - b; - let n = if range > 0.0 { ((v - b) / range).clamp(0.0, 1.0) } else { 0.0 }; - let plane = match cfa { - Some(c) => usize::from(c.color(x, y)), - None => s, - }; - let wb = inv_neutral.get(plane).copied().unwrap_or(1.0); - if let Some(o) = o.get_mut(x * spp + s) { - *o = (n as f32 * wb).min(1.0); - } - } - } - }; - #[cfg(feature = "threads")] - out.par_chunks_mut((aw * spp).max(1)).enumerate().for_each(|(y, o)| row(y, o)); - #[cfg(not(feature = "threads"))] - out.chunks_mut((aw * spp).max(1)).enumerate().for_each(|(y, o)| row(y, o)); - Ok(out) -} - -/// A developed picture: premultiplied linear Rec. 709 RGBA floats (alpha 1). -pub struct Developed { - pub width: usize, - pub height: usize, - pub rgba: Vec, -} - -/// Develop `raw` (steps 1–7). `neutral` and `matrix` come from [`crate::color::Calibration::solve`]. -pub fn develop(raw: &RawImage, p: &Params, neutral: &V3, matrix: &M3) -> Result { - let spp = raw.samples_per_pixel; - if p.cfa.is_some() && spp != 1 { - return Err(Error::Malformed("a CFA image with more than one sample per pixel")); - } - if p.cfa.is_none() && !matches!(spp, 1 | 3) { - return Err(Error::Unsupported(format!("LinearRaw with {spp} samples per pixel"))); - } - let active = active_rect(p, raw.width, raw.height)?; - let (_, _, aw, ah) = active; - let planes = if p.cfa.is_some() { 3 } else { spp }; - let norm = normalise(raw, p, active, neutral, planes)?; - // three samples per pixel - let rgb = match (&p.cfa, spp) { - (Some(pat), _) => demosaic(&norm, aw, ah, pat), - (None, 3) => norm, - _ => norm.iter().flat_map(|&v| [v, v, v]).collect(), - }; - let (cx, cy, cw, ch) = crop_rect(p, aw, ah); - let m: [[f32; 3]; 3] = matrix.map(|r| r.map(|v| v as f32)); - let mono = p.cfa.is_none() && spp == 1; - let mut rgba = vec![0f32; cw * ch * 4]; - let row = |y: usize, o: &mut [f32]| { - let src = (cy + y) * aw + cx; - for x in 0..cw { - let Some(c) = rgb.get((src + x) * 3..(src + x) * 3 + 3) else { continue }; - let px = if mono { - // grey: the matrix's luminance gain only - let g = c[0] * (m[1][0] + m[1][1] + m[1][2]); - [g, g, g] - } else { - [0, 1, 2].map(|i| m[i][0] * c[0] + m[i][1] * c[1] + m[i][2] * c[2]) - }; - if let Some(d) = o.get_mut(x * 4..x * 4 + 4) { - d.copy_from_slice(&[px[0].max(0.0), px[1].max(0.0), px[2].max(0.0), 1.0]); - } - } - }; - #[cfg(feature = "threads")] - rgba.par_chunks_mut(cw * 4).enumerate().for_each(|(y, o)| row(y, o)); - #[cfg(not(feature = "threads"))] - rgba.chunks_mut(cw * 4).enumerate().for_each(|(y, o)| row(y, o)); - Ok(Developed { width: cw, height: ch, rgba }) -} - -#[cfg(test)] -mod tests { - use super::*; - use crate::color::IDENTITY; - - fn raw(w: usize, h: usize, spp: usize, data: Vec) -> RawImage { - RawImage { width: w, height: h, samples_per_pixel: spp, bits: 12, data } - } - - #[test] - fn black_white_linearisation_and_white_balance() { - // LinearRaw 2 × 1: black 100, white 1100; neutral (0.5, 1, 0.25) - let r = raw(2, 1, 3, vec![600, 600, 600, 100, 1100, 2000]); - let p = Params { black: vec![100.0], white: vec![1100.0], ..Default::default() }; - let d = develop(&r, &p, &[0.5, 1.0, 0.25], &IDENTITY).unwrap(); - assert_eq!((d.width, d.height), (2, 1)); - // 0.5 / 0.5 = 1, 0.5 / 1, 0.5 / 0.25 = 2 → clipped to 1 - assert_eq!(&d.rgba[..4], &[1.0, 0.5, 1.0, 1.0]); - // below black → 0, at white → 1, above white → 1 - assert_eq!(&d.rgba[4..], &[0.0, 1.0, 1.0, 1.0]); - // a linearisation table doubling the codes - let t: Vec = (0..4096u32).map(|v| (v * 2).min(65_535) as u16).collect(); - let p = Params { linearization: Some(t), black: vec![200.0], white: vec![2200.0], ..p }; - let d = develop(&r, &p, &[1.0; 3], &IDENTITY).unwrap(); - assert_eq!(&d.rgba[..3], &[0.5, 0.5, 0.5]); - } - - #[test] - fn per_cfa_black_levels_and_deltas() { - // 2 × 2 CFA, black repeat 2 × 2 with a different black per site, plus row/column deltas - let pat = Pattern { rows: 2, cols: 2, colors: vec![0, 1, 1, 2] }; - let p = Params { - black_repeat: (2, 2), - black: vec![10.0, 20.0, 30.0, 40.0], - black_delta_h: vec![0.0, 5.0], - black_delta_v: vec![0.0, 1.0], - white: vec![1010.0], - cfa: Some(pat), - ..Default::default() - }; - let r = raw(2, 2, 1, vec![510, 525, 531, 546]); - let n = normalise(&r, &p, (0, 0, 2, 2), &[1.0; 3], 3).unwrap(); - // (v - black) / (white - black) for black 10, 25, 31, 46 - let want = [500.0 / 1000.0, 500.0 / 985.0, 500.0 / 979.0, 500.0 / 964.0]; - for (a, b) in n.iter().zip(want) { - assert!((a - b).abs() < 1e-6, "{a} vs {b}"); - } - } - - #[test] - fn active_area_and_default_crop() { - // 6 × 4 LinearRaw grey ramp; active area cuts a 1-pixel border, crop takes 2 × 1 at (1, 1) - let data: Vec = (0..24u16).flat_map(|i| [i * 10; 3]).collect(); - let r = raw(6, 4, 3, data); - let p = - Params { active_area: Some([1, 1, 3, 5]), white: vec![4095.0], crop_origin: Some((1.0, 1.0)), crop_size: Some((2.0, 1.0)), ..Default::default() }; - let d = develop(&r, &p, &[1.0; 3], &IDENTITY).unwrap(); - assert_eq!((d.width, d.height), (2, 1)); - // active (1,1)–(5,3); crop origin (1,1) inside it → raw (2, 2) and (3, 2): samples 14, 15 - assert!((d.rgba[0] - 140.0 / 4095.0).abs() < 1e-6); - assert!((d.rgba[4] - 150.0 / 4095.0).abs() < 1e-6); - // hostile active areas and crops - let bad = Params { active_area: Some([3, 0, 1, 6]), ..Default::default() }; - assert!(develop(&r, &bad, &[1.0; 3], &IDENTITY).is_err()); - let bad = Params { active_area: Some([0, 0, 4, 7]), ..Default::default() }; - assert!(develop(&r, &bad, &[1.0; 3], &IDENTITY).is_err()); - let huge = Params { crop_origin: Some((1e12, -5.0)), crop_size: Some((f64::NAN, 1e9)), white: vec![4095.0], ..Default::default() }; - let d = develop(&r, &huge, &[1.0; 3], &IDENTITY).unwrap(); - assert!(d.width >= 1 && d.width <= 6 && d.height >= 1 && d.height <= 4); - } - - #[test] - fn matrix_and_clipping_of_negatives() { - let r = raw(1, 1, 3, vec![4095, 0, 0]); - let p = Params { white: vec![4095.0], ..Default::default() }; - let m = [[1.0, 0.0, 0.0], [-0.5, 1.0, 0.0], [0.0, 0.0, 2.0]]; - let d = develop(&r, &p, &[1.0; 3], &m).unwrap(); - assert_eq!(&d.rgba, &[1.0, 0.0, 0.0, 1.0]); - } - - #[test] - fn short_black_level_is_an_error() { - let r = raw(2, 2, 1, vec![0; 4]); - let p = Params { black_repeat: (2, 2), black: vec![1.0, 2.0], cfa: Some(Pattern { rows: 2, cols: 2, colors: vec![0, 1, 1, 2] }), ..Default::default() }; - assert!(develop(&r, &p, &[1.0; 3], &IDENTITY).is_err()); - } -} diff --git a/crates/raw/src/lib.rs b/crates/raw/src/lib.rs index bfc098734..8f31f2f6e 100644 --- a/crates/raw/src/lib.rs +++ b/crates/raw/src/lib.rs @@ -93,6 +93,9 @@ mod arw_lens_rm6; /// Bounded DNG strip and tile sample reader. #[cfg(feature = "line-dng")] pub mod dng_lines; +/// Bounded DNG camera and processing metadata. +#[cfg(feature = "line-dng")] +pub mod dng_metadata; #[allow(dead_code)] mod huffman; #[cfg(feature = "jxl")] From 6225363b1fdd580241bc214268a3c71a6309233b Mon Sep 17 00:00:00 2001 From: FredApps <153173854+FredApps@users.noreply.github.com> Date: Mon, 16 Nov 2026 08:33:43 +0100 Subject: [PATCH 3/4] raw: dng_metadata tests against hand-built DNGs and the full reader; document why it keeps its own tag reads --- crates/raw/src/dng_metadata.rs | 7 ++ crates/raw/tests/it/dng.rs | 2 +- crates/raw/tests/it/dng_metadata.rs | 114 ++++++++++++++++++++++++++++ crates/raw/tests/it/main.rs | 2 + 4 files changed, 124 insertions(+), 1 deletion(-) create mode 100644 crates/raw/tests/it/dng_metadata.rs diff --git a/crates/raw/src/dng_metadata.rs b/crates/raw/src/dng_metadata.rs index b8ba5077f..4b1dda130 100644 --- a/crates/raw/src/dng_metadata.rs +++ b/crates/raw/src/dng_metadata.rs @@ -1,4 +1,11 @@ //! Bounded DNG processing and camera metadata, before application rendering. +//! +//! This is the [`crate::dng_lines`] companion on the same `craft_tiff::classic` IFDs. The full +//! reader's `dng::color_data` works on `craft_tiff::Tiff` and returns the values it will render +//! with (raw IFD first, implausible or singular values dropped, `BaselineExposureOffset` added). +//! This reader returns the tags as written, IFD 0 first, and fails on a value outside the file, +//! so a damaged file does not silently lose its white balance. For a well-formed file both read the +//! same matrices and neutral (see `tests/it/dng_metadata.rs`). use crate::dng_lines::{self, Error, PHOTOMETRIC_CFA, Result}; use craft_tiff::classic::{Entry, Ifd, Tiff, tags}; type M3 = [[f64; 3]; 3]; diff --git a/crates/raw/tests/it/dng.rs b/crates/raw/tests/it/dng.rs index 450b8ea96..21074a828 100644 --- a/crates/raw/tests/it/dng.rs +++ b/crates/raw/tests/it/dng.rs @@ -214,7 +214,7 @@ pub(super) fn dng(raw: IfdBuilder, order: ByteOrder) -> Vec { } /// [`dng`] with extra IFD 0 tags (where DNG writers put camera-profile tags). -fn dng_with(raw: IfdBuilder, order: ByteOrder, extra: impl FnOnce(&mut IfdBuilder)) -> Vec { +pub(super) fn dng_with(raw: IfdBuilder, order: ByteOrder, extra: impl FnOnce(&mut IfdBuilder)) -> Vec { let mut ifd0 = IfdBuilder::new(); ifd0.set(t::NEW_SUBFILE_TYPE, Value::Long(vec![1])); ifd0.set(t::DNG_VERSION, Value::Byte(vec![1, 6, 0, 0])); diff --git a/crates/raw/tests/it/dng_metadata.rs b/crates/raw/tests/it/dng_metadata.rs new file mode 100644 index 000000000..5cf75ae2a --- /dev/null +++ b/crates/raw/tests/it/dng_metadata.rs @@ -0,0 +1,114 @@ +//! The bounded DNG metadata reader (`dng_metadata`) on hand-built DNGs, checked against the tags +//! written and, for the colour tags, against the full reader. + +use super::dng::{base_ifd, dng, dng_with}; +use craft_raw::dng_lines::{self, Error}; +use craft_raw::dng_metadata::{Metadata, Pattern}; +use craft_tiff::classic::{Tiff, tags as ct}; +use craft_tiff::{ByteOrder, IfdBuilder, ImageData, Value}; + +fn cfa_raw(edit: impl FnOnce(&mut IfdBuilder)) -> IfdBuilder { + let mut raw = base_ifd(8, 4, 16, 1, true); + raw.set(ct::COMPRESSION, Value::Short(vec![1])); + raw.set_image(ImageData::Strips { rows_per_strip: 4, strips: vec![vec![0; 64]] }); + edit(&mut raw); + raw +} + +fn with_metadata(bytes: &[u8], f: impl FnOnce(&Metadata) -> T) -> T { + let (tiff, first) = Tiff::new(bytes).unwrap(); + let ifds = tiff.all_ifds(first).unwrap(); + let raw = dng_lines::find_raw_ifd(&tiff, &ifds).unwrap(); + f(&Metadata { tiff, main: &ifds[0], raw }) +} + +#[test] +fn processing_tags_are_read_from_the_raw_ifd() { + let raw = cfa_raw(|r| { + r.set(ct::ACTIVE_AREA, Value::Long(vec![1, 2, 3, 7])); + r.set(ct::LINEARIZATION_TABLE, Value::Short(vec![0, 10, 20])); + r.set(ct::BLACK_LEVEL_REPEAT_DIM, Value::Short(vec![2, 2])); + r.set(ct::BLACK_LEVEL, Value::Rational(vec![(64, 1), (65, 1), (66, 1), (67, 1)])); + r.set(ct::BLACK_LEVEL_DELTA_H, Value::SRational(vec![(1, 2); 8])); + r.set(ct::BLACK_LEVEL_DELTA_V, Value::SRational(vec![(-1, 2); 4])); + r.set(ct::WHITE_LEVEL, Value::Long(vec![4000])); + r.set(ct::DEFAULT_CROP_ORIGIN, Value::Rational(vec![(1, 2), (1, 1)])); + r.set(ct::DEFAULT_CROP_SIZE, Value::Long(vec![6, 2])); + }); + for order in [ByteOrder::Little, ByteOrder::Big] { + let p = with_metadata(&dng(raw.clone(), order), |m| m.params().unwrap()); + assert_eq!(p.active_area, Some([1, 2, 3, 7])); + assert_eq!(p.linearization, Some(vec![0, 10, 20])); + assert_eq!(p.black_repeat, (2, 2)); + assert_eq!(p.black, [64.0, 65.0, 66.0, 67.0]); + assert_eq!(p.black_delta_h, [0.5; 8]); + assert_eq!(p.black_delta_v, [-0.5; 4]); + assert_eq!(p.white, [4000.0]); + assert_eq!((p.crop_origin, p.crop_size), (Some((0.5, 1.0)), Some((6.0, 2.0)))); + // base_ifd writes CFAPattern 1 0 2 1 (GRBG) + assert_eq!(p.cfa, Some(Pattern { rows: 2, cols: 2, colors: vec![1, 0, 2, 1] })); + } + // absent tags take the DNG defaults + let p = with_metadata(&dng(cfa_raw(|_| {}), ByteOrder::Little), |m| m.params().unwrap()); + assert_eq!((p.active_area, p.linearization, p.black_repeat), (None, None, (1, 1))); + assert!(p.black.is_empty() && p.white.is_empty() && p.crop_origin.is_none()); +} + +#[test] +fn cfa_patterns_map_plane_colours_and_refuse_odd_layouts() { + let pattern = |edit: fn(&mut IfdBuilder)| with_metadata(&dng(cfa_raw(edit), ByteOrder::Little), |m| m.cfa()); + // CFAPlaneColor reorders the planes: file colour 2 is plane 0 + let swapped = pattern(|r| { + r.set(ct::CFA_PLANE_COLOR, Value::Byte(vec![2, 1, 0])); + r.set(ct::CFA_LAYOUT, Value::Short(vec![1])); + }); + assert_eq!(swapped.unwrap(), Some(Pattern { rows: 2, cols: 2, colors: vec![1, 2, 0, 1] })); + let unsupported = |r: Result, Error>| matches!(r, Err(Error::Unsupported(_))); + let malformed = |r: Result, Error>| matches!(r, Err(Error::Malformed(_))); + assert!(unsupported(pattern(|r| { + r.set(ct::CFA_LAYOUT, Value::Short(vec![2])); + }))); + assert!(unsupported(pattern(|r| { + r.set(ct::CFA_PLANE_COLOR, Value::Byte(vec![0, 1, 3])); + }))); + assert!(unsupported(pattern(|r| { + r.set(ct::CFA_REPEAT_PATTERN_DIM, Value::Short(vec![9, 2])); + }))); + assert!(malformed(pattern(|r| { + r.set(ct::CFA_REPEAT_PATTERN_DIM, Value::Short(vec![2])); + }))); + assert!(malformed(pattern(|r| { + r.set(ct::CFA_REPEAT_PATTERN_DIM, Value::Short(vec![4, 4])); + }))); + assert!(malformed(pattern(|r| { + r.set(ct::CFA_PATTERN, Value::Byte(vec![0, 1, 1, 3])); + }))); + // LinearRaw has no pattern + let mut linear = base_ifd(4, 2, 16, 3, false); + linear.set(ct::COMPRESSION, Value::Short(vec![1])); + linear.set_image(ImageData::Strips { rows_per_strip: 2, strips: vec![vec![0; 48]] }); + assert_eq!(with_metadata(&dng(linear, ByteOrder::Big), |m| m.cfa()).unwrap(), None); +} + +#[test] +fn calibration_matches_the_full_reader() { + let bytes = dng_with(cfa_raw(|_| {}), ByteOrder::Little, |ifd0| { + ifd0.set(ct::COLOR_MATRIX_2, Value::SRational(vec![(2, 1), (0, 1), (0, 1), (0, 1), (2, 1), (0, 1), (0, 1), (0, 1), (2, 1)])); + ifd0.set(ct::CALIBRATION_ILLUMINANT_2, Value::Short(vec![17])); + ifd0.set(ct::ANALOG_BALANCE, Value::Rational(vec![(1, 1); 3])); + ifd0.set(ct::AS_SHOT_NEUTRAL, Value::Rational(vec![(1, 2), (1, 1), (3, 4)])); + ifd0.set(ct::BASELINE_EXPOSURE, Value::SRational(vec![(1, 4)])); + // not 3 x 3 (a four-colour camera): not a matrix here + ifd0.set(ct::FORWARD_MATRIX_1, Value::SRational(vec![(1, 1); 12])); + }); + let c = with_metadata(&bytes, |m| m.calibration().unwrap()); + let full = craft_raw::decode(&bytes).unwrap().color; + assert_eq!(c.color_matrix, full.color_matrix); + assert_eq!(c.illuminant, [Some(21), Some(17)]); + assert_eq!(c.as_shot_neutral, full.as_shot_neutral); + assert_eq!(c.analog_balance, Some([1.0; 3])); + assert_eq!(c.as_shot_white_xy, Some((0.3127, 0.329))); + assert_eq!(c.baseline_exposure, 0.25); + assert_eq!(c.forward_matrix, [None, None]); + assert_eq!(c.camera_calibration, [None, None]); +} diff --git a/crates/raw/tests/it/main.rs b/crates/raw/tests/it/main.rs index 2f1b1061f..5a9386eb0 100644 --- a/crates/raw/tests/it/main.rs +++ b/crates/raw/tests/it/main.rs @@ -4,6 +4,8 @@ mod dng; #[cfg(feature = "line-dng")] mod dng_lines; +#[cfg(feature = "line-dng")] +mod dng_metadata; #[cfg(feature = "dng")] mod robust; mod vendor_robust; From 9b87ff0a0e81199a745e1ae6684bafc879555588 Mon Sep 17 00:00:00 2001 From: FredApps <153173854+FredApps@users.noreply.github.com> Date: Mon, 16 Nov 2026 10:34:00 +0100 Subject: [PATCH 4/4] raw: dng_metadata checks the LinearizationTable count before reading its entries --- crates/raw/src/dng_metadata.rs | 5 +++-- crates/raw/tests/it/dng_metadata.rs | 5 +++++ 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/crates/raw/src/dng_metadata.rs b/crates/raw/src/dng_metadata.rs index 4b1dda130..6d8e6d958 100644 --- a/crates/raw/src/dng_metadata.rs +++ b/crates/raw/src/dng_metadata.rs @@ -135,10 +135,11 @@ impl Metadata<'_, '_> { }; let linearization = match self.raw_tag(tags::LINEARIZATION_TABLE) { Some(e) => { - let v = self.tiff.uints(e)?; - if v.len() > 65_536 { + // checked before `uints` allocates a u64 per entry + if e.count > 65_536 { return Err(Error::Malformed("LinearizationTable longer than 65536")); } + let v = self.tiff.uints(e)?; Some(v.into_iter().map(|x| x.min(65_535) as u16).collect()) } None => None, diff --git a/crates/raw/tests/it/dng_metadata.rs b/crates/raw/tests/it/dng_metadata.rs index 5cf75ae2a..2aa0e6d80 100644 --- a/crates/raw/tests/it/dng_metadata.rs +++ b/crates/raw/tests/it/dng_metadata.rs @@ -48,6 +48,11 @@ fn processing_tags_are_read_from_the_raw_ifd() { // base_ifd writes CFAPattern 1 0 2 1 (GRBG) assert_eq!(p.cfa, Some(Pattern { rows: 2, cols: 2, colors: vec![1, 0, 2, 1] })); } + // a LinearizationTable longer than 65536 entries is refused + let long = cfa_raw(|r| { + r.set(ct::LINEARIZATION_TABLE, Value::Short(vec![1; 65_537])); + }); + assert!(matches!(with_metadata(&dng(long, ByteOrder::Little), |m| m.params()), Err(Error::Malformed(_)))); // absent tags take the DNG defaults let p = with_metadata(&dng(cfa_raw(|_| {}), ByteOrder::Little), |m| m.params().unwrap()); assert_eq!((p.active_area, p.linearization, p.black_repeat), (None, None, (1, 1)));