mirror of
https://github.com/storytold/photocraft.git
synced 2026-12-02 06:20:48 +01:00
CR2: read ColorData's as-shot white balance as words (#1640)
* CR2: read ColorData's as-shot white balance as words CANON_COLOR_DATA is an UNDEFINED entry: uints() hands its bytes back one by one, but the WB_RGGBLevelsAsShot offsets count 16-bit words. Reading word i at entry offset i*2 (file byte order, bounds-checked) finds the quadruple at 0x47 in the corpus PowerShot SX50 HS: [1613, 951, 951, 1605] -> [1.70, 1.0, 1.69], matching the white balance its own DNG conversion neutralises (AsShotNeutral -> [1.58, 0.955, 1.666] through the color matrix). With this the file's balance is used instead of the grey-world estimate; the regression test pins the words and the missing estimate warning. (The remaining brightness gap between the CR2 and the DNG of the same camera is the CR2 path's exposure normalisation, to be looked at next; the estimate was never what brightened the file.) * cr2: drop the unused glob import in the corpus test * CR2 WB: synthetic word-offset oracle, entry bounds, corpus test behind the feature testgen writes ColorData as Canon does, UNDEFINED bytes that are 16-bit words with the PowerShot's WB_RGGBLevelsAsShot at word offset 0x47, and the new color_data_wb_as_words test checks the decoded balance in every CI run (the corpus oracle stays behind the corpus feature and now panics when its file is missing instead of silently skipping). word() reads stay inside the entry's own byte count, and the shared inspect example moves back to the corpus PR.
This commit is contained in:
1 parent
c58d20bd39
commit
6cd2d61398
4 files changed
+94
-8
No files matched your search
@@ -11,6 +11,8 @@ default = []
|
||||
# Synthetic raw-file generators (DNG / CR2 / TIFF-EP writers and a lossless-JPEG
|
||||
# encoder) for tests and fuzz seeds.
|
||||
testgen = []
|
||||
# Real-file corpus tests (opt-in; `cargo xtask test-corpus`), defined with the corpus PR.
|
||||
corpus = []
|
||||
|
||||
[dependencies]
|
||||
|
||||
|
||||
+82
-3
@@ -116,11 +116,18 @@ pub(crate) fn maker_note(t: &Tiff, ifds: &[Ifd]) -> Option<Ifd> {
|
||||
/// As-shot white-balance multipliers (R, G, B) from ColorData.
|
||||
fn as_shot_wb(t: &Tiff, mn: &Ifd) -> Option<[f64; 3]> {
|
||||
let e = mn.get(CANON_COLOR_DATA)?;
|
||||
let v = t.uints(e);
|
||||
// The entry is UNDEFINED: its bytes are a table of 16-bit words (file byte order), and the
|
||||
// offsets below count words, not bytes. Reads stay inside the entry's own byte count.
|
||||
let bytes = e.unit().checked_mul(e.count as usize)?;
|
||||
let word = |off: usize| {
|
||||
let at = off.checked_mul(2)?.checked_add(e.at)?;
|
||||
(at.checked_add(2)? <= e.at.checked_add(bytes)?).then(|| t.u16_at(at)).flatten().map(f64::from)
|
||||
};
|
||||
// Word offsets of WB_RGGBLevelsAsShot used by the ColorData versions.
|
||||
for off in [0x3F, 0x47, 0x19, 0x22] {
|
||||
let Some(l) = v.get(off..off + 4) else { continue };
|
||||
let [r, g1, g2, b] = [l[0], l[1], l[2], l[3]].map(f64::from);
|
||||
let (Some(r), Some(g1), Some(g2), Some(b)) = (word(off), word(off + 1), word(off + 2), word(off + 3)) else {
|
||||
continue;
|
||||
};
|
||||
let g = (g1 + g2) / 2.0;
|
||||
let plausible = (256.0..16384.0).contains(&g) && (g1 - g2).abs() <= g * 0.05 && (0.25..8.0).contains(&(r / g)) && (0.25..8.0).contains(&(b / g));
|
||||
if plausible {
|
||||
@@ -345,3 +352,75 @@ mod tests {
|
||||
}
|
||||
}
|
||||
}
|
||||
#[cfg(all(test, feature = "corpus"))]
|
||||
mod wb_debug {
|
||||
|
||||
/// The corpus PowerShot CR2: ColorData's WB_RGGBLevelsAsShot sits at word offset 0x47 (the
|
||||
/// PowerShot SX50 HS's ColorData version), the same words its DNG conversion neutralises
|
||||
/// (AsShotNeutral -> WB ≈ [1.66, 1.0, 1.74]; the ColorData words give [1.70, 1.0, 1.69] —
|
||||
/// the converter re-derives them through the color matrix). The synthetic twin of this
|
||||
/// oracle is `color_data_wb_as_words` above; it runs in every CI.
|
||||
#[test]
|
||||
fn power_shot_color_data_holds_the_as_shot_wb() {
|
||||
let Ok(bytes) = std::fs::read(concat!(env!("CARGO_MANIFEST_DIR"), "/../../corpus/pixls/IMG_4059.CR2")) else {
|
||||
panic!("corpus/pixls is fetched for corpus runs: cargo xtask corpus --pixls");
|
||||
};
|
||||
let s = crate::decode(&bytes, &crate::Limits::default()).expect("decode");
|
||||
let wb = s.camera_wb.expect("the PowerShot's as-shot WB is read now");
|
||||
assert!((wb[0] - 1.696).abs() < 0.01 && wb[1] == 1.0 && (wb[2] - 1.688).abs() < 0.01, "{wb:?}");
|
||||
// The file's white balance is used, not an estimate: no grey-world note in the develop.
|
||||
let d = crate::develop_sensor(&s, &crate::DevelopOptions::default()).expect("develop");
|
||||
assert!(!d.warnings.iter().any(|w| w.contains("estimated automatically")), "{:?}", d.warnings);
|
||||
}
|
||||
}
|
||||
#[cfg(test)]
|
||||
mod wb_words {
|
||||
use crate::testgen::Cr2Spec;
|
||||
|
||||
/// ColorData arrives as UNDEFINED bytes that are 16-bit words; the offsets count words.
|
||||
/// Regression for the byte-scaled lookup that never found the balance (synthetic twin of
|
||||
/// the corpus oracle, runs in every CI).
|
||||
#[test]
|
||||
fn color_data_wb_as_words() {
|
||||
let spec = Cr2Spec {
|
||||
width: 8,
|
||||
height: 8,
|
||||
data: vec![2048u16; 64],
|
||||
precision: 12,
|
||||
components: 1,
|
||||
slices: Vec::new(),
|
||||
borders: None,
|
||||
wb_rggb: Some([1613, 951, 951, 1605]),
|
||||
orientation: 1,
|
||||
model_id: None,
|
||||
};
|
||||
let bytes = spec.build();
|
||||
let s = crate::decode(&bytes, &crate::Limits::default()).expect("decode");
|
||||
let wb = s.camera_wb.expect("the synthetic as-shot WB is read");
|
||||
assert!((wb[0] - 1613.0 / 951.0).abs() < 1e-9 && wb[1] == 1.0 && (wb[2] - 1605.0 / 951.0).abs() < 1e-9, "{wb:?}");
|
||||
}
|
||||
|
||||
/// A ColorData entry too short for the offsets reads no balance instead of running past it.
|
||||
#[test]
|
||||
fn short_color_data_reads_no_wb() {
|
||||
let spec = Cr2Spec { wb_rggb: None, ..minimal_spec() };
|
||||
let bytes = spec.build();
|
||||
let s = crate::decode(&bytes, &crate::Limits::default()).expect("decode");
|
||||
assert!(s.camera_wb.is_none());
|
||||
}
|
||||
|
||||
fn minimal_spec() -> Cr2Spec {
|
||||
Cr2Spec {
|
||||
width: 8,
|
||||
height: 8,
|
||||
data: vec![2048u16; 64],
|
||||
precision: 12,
|
||||
components: 1,
|
||||
slices: Vec::new(),
|
||||
borders: None,
|
||||
wb_rggb: None,
|
||||
orientation: 1,
|
||||
model_id: None,
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -663,7 +663,7 @@ pub struct Cr2Spec {
|
||||
pub slices: Vec<usize>,
|
||||
/// Image borders: left, top, right, bottom (inclusive), as in SensorInfo.
|
||||
pub borders: Option<[u16; 4]>,
|
||||
/// As-shot RGGB levels written to ColorData at word offset 0x3F.
|
||||
/// As-shot RGGB levels written to ColorData at word offset 0x47 (the PowerShot version).
|
||||
pub wb_rggb: Option<[u16; 4]>,
|
||||
pub orientation: u16,
|
||||
/// Canon model ID written to MakerNote tag 0x0010.
|
||||
@@ -703,9 +703,14 @@ impl Cr2Spec {
|
||||
mn.push((0x00E0, Val::Short(vec![34, w as u16, h as u16, 0, 0, l, tp, r, b, 0, 0, 0, 0, 0, 0, 0, 0])));
|
||||
}
|
||||
if let Some(wb) = self.wb_rggb {
|
||||
let mut cd = vec![0u16; 1273];
|
||||
cd[0x3F..0x43].copy_from_slice(&wb);
|
||||
mn.push((0x4001, Val::Short(cd)));
|
||||
// Canon writes ColorData (0x4001) as UNDEFINED bytes that are 16-bit words; the
|
||||
// WB_RGGBLevelsAsShot of the PowerShot version sits at word offset 0x47 (the EOS
|
||||
// versions use 0x3F — see as_shot_wb).
|
||||
let mut cd = vec![0u8; 2 * 1273];
|
||||
for (i, v) in wb.iter().enumerate() {
|
||||
cd[2 * (0x47 + i)..2 * (0x47 + i) + 2].copy_from_slice(&v.to_le_bytes());
|
||||
}
|
||||
mn.push((0x4001, Val::Undefined(cd)));
|
||||
}
|
||||
let mut exif: Vec<(u16, Val)> = vec![(33434, Val::Rational(vec![(1, 100)]))];
|
||||
if !mn.is_empty() {
|
||||
|
||||
@@ -89,7 +89,7 @@ pub(crate) struct Entry {
|
||||
|
||||
impl Entry {
|
||||
/// Size in bytes of one value of this entry's type (0 = unknown type).
|
||||
fn unit(&self) -> usize {
|
||||
pub(crate) fn unit(&self) -> usize {
|
||||
type_size(self.typ)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user