LightCraft: An open-source non-destructive photo editor written by scratch in pure Rust
LR-IMP-FORMATS: ARW6 chroma planes are base-free; white fallback not doubled
dev-null-undefined1a146c12b007
diff --git a/crates/raw/src/vendor/arw.rs b/crates/raw/src/vendor/arw.rsindex f7dc1831b..72eee061e 100644--- a/crates/raw/src/vendor/arw.rs+++ b/crates/raw/src/vendor/arw.rs@@ -654,15 +654,14 @@ pub(crate) fn decode(bytes: &[u8], mode: Mode) -> Result<RawImage> {_ if linear_rgb => BlackLevel::uniform(default_black + YCBCR_OFFSET),_ => BlackLevel::uniform((scale_bits >= 14).then(|| sr2().and_then(sr2_black)).flatten().unwrap_or(default_black)),};+ // ARW6 samples are in 2 x 14-bit units, so the file's 14-bit levels double (a data-derived white is already in them)+ let unit = if info.compression == 32766 { 2.0 } else { 1.0 };let white = if linear_rgb {YCBCR_WHITE + YCBCR_OFFSET} else {- raw.f64(t::WHITE_LEVEL).map(|v| v as f32).filter(|v| *v > 0.0).unwrap_or_else(|| super::white_from_data(samples, scale_bits))+ raw.f64(t::WHITE_LEVEL).map(|v| v as f32 * unit).filter(|v| *v > 0.0).unwrap_or_else(|| super::white_from_data(samples, scale_bits))};- // ARW6 samples are in 2 x 14-bit units, so the file's 14-bit levels double- let unit = if info.compression == 32766 { 2.0 } else { 1.0 };let black = BlackLevel { values: black.values.iter().map(|v| v * unit).collect(), ..black };- let white = white * unit;let model = ifd0.string(t::MODEL).unwrap_or_default();let mn = tiff.exif()diff --git a/crates/raw/src/vendor/arw6.rs b/crates/raw/src/vendor/arw6.rsindex 50ea20348..d3eab0bda 100644--- a/crates/raw/src/vendor/arw6.rs+++ b/crates/raw/src/vendor/arw6.rs@@ -396,12 +396,12 @@ fn component(c: usize, streams: &[Vec<HalfLines>], layouts: &[HalfRows], vs: usilet grp = |g: usize, n: usize| streams.get(3 * g + c).and_then(|s| s.get(n)).ok_or_else(|| corrupt("missing half"));for (n, r) in layouts.iter().enumerate() {let (h0, l0) = grp(0, n)?;- // LL3: the nibble must be 0 (Phase 0: never seen otherwise); lines are DPCM from 2048 (spec *Container*).+ // LL3: the nibble must be 0 (Phase 0: never seen otherwise); lines are DPCM from 2048 for the green mean (c = 0) and from 0 for the base-free chroma planes (spec *Container*).if h0.qi[0] != 0 {return Err(unsupported("LL3 quantiser index"));}for (&row, line) in r.l3.iter().zip(l0) {- let mut acc = 2048i32;+ let mut acc = if c == 0 { 2048i32 } else { 0 };put(&mut b.ll3, row, line, |d| {acc = acc.saturating_add(d);acc@@ -899,6 +899,40 @@ mod tests {assert!(matches!(decode(&strip, 64, 48, crate::Mode::Full), Err(RawError::Corrupt(_))));}+ #[test]+ fn chroma_planes_are_base_free() {+ let (w2, h2) = (32, 24);+ let flat = |v: i32| Plane { width: w2, height: h2, data: vec![v; w2 * h2] };+ let (m, _, _, res) = planes(w2, h2, 7);+ for (c1, c2) in [(0, 0), (5, -3)] {+ let t = TileSpec { s: 0, qi: Qis::ZERO, m: m.clone(), c1: flat(c1), c2: flat(c2), res: res.clone() };+ let tile = encode_tile(&t);+ let p = decode_tile_planes(&tile, 64, 48).unwrap();+ assert!(p.c1.data.iter().all(|&v| v == c1) && p.c2.data.iter().all(|&v| v == c2));+ if c1 == 0 {+ // zero chroma: R = B = (G1 + G2) >> 1 at every cell (no 2048 base, no saturation)+ let out = decode_tile(&tile, 64, 48).unwrap();+ for j in 0..h2 {+ for x in 0..w2 {+ let at = |dy: usize, dx: usize| out[(2 * j + dy) * 64 + 2 * x + dx];+ assert_eq!(at(0, 0), at(1, 1), "R == B at ({x},{j})");+ assert!(at(0, 0) < 39002);+ }+ }+ }+ }+ }++ #[test]+ fn data_derived_white_is_not_doubled() {+ let (tile, _) = one_tile(64, 48, 0, Qis::ZERO);+ let file = arw6_file_no_white(&[(0, 0, 64, 48, tile)], 64, 48);+ let img = crate::decode(&file).unwrap();+ let crate::RawData::U16(ref d) = img.data else { panic!() };+ assert_eq!(img.white, vec![super::super::white_from_data(d, 16)]);+ assert!(img.white[0] <= 65535.0);+ }+#[test]fn corrupt_streams_import_then_fail() {// Garbage VLD data is self-delimiting and decodes to *something*; what is detectable is a half whose indexdiff --git a/crates/raw/src/vendor/arw6_testenc.rs b/crates/raw/src/vendor/arw6_testenc.rsindex ba7e30af6..dd364caf7 100644--- a/crates/raw/src/vendor/arw6_testenc.rs+++ b/crates/raw/src/vendor/arw6_testenc.rs@@ -312,7 +312,7 @@ pub(crate) fn encode_tile(t: &TileSpec) -> Vec<u8> {let dpcm = {let mut ll = quantised_rows(&b.ll3, &low3, 0);for l in &mut ll {- let mut prev = 2048;+ let mut prev = if c == 0 { 2048 } else { 0 };for v in l.iter_mut() {(*v, prev) = (*v - prev, *v);}@@ -378,6 +378,15 @@ pub(crate) fn encode_tile(t: &TileSpec) -> Vec<u8> {/// A little-endian ARW6 TIFF: IFD0 (Sony, ILCE-7RM6) + a SubIFD with the strip = tile table, zero padding to 512,/// then the tiles `(x, y, w, h, bytes)` back to back.pub(crate) fn arw6_file(tiles: &[(usize, usize, usize, usize, Vec<u8>)], width: usize, height: usize) -> Vec<u8> {+ arw6_file_with(tiles, width, height, true)+}++/// [`arw6_file`] without the WhiteLevel tag.+pub(crate) fn arw6_file_no_white(tiles: &[(usize, usize, usize, usize, Vec<u8>)], width: usize, height: usize) -> Vec<u8> {+ arw6_file_with(tiles, width, height, false)+}++fn arw6_file_with(tiles: &[(usize, usize, usize, usize, Vec<u8>)], width: usize, height: usize, white: bool) -> Vec<u8> {let mut strip = (tiles.len() as u32).to_le_bytes().to_vec();strip.extend([0; 4]);let mut offset = 512u64;@@ -402,7 +411,9 @@ pub(crate) fn arw6_file(tiles: &[(usize, usize, usize, usize, Vec<u8>)], width:raw.set(t::CFA_REPEAT_PATTERN_DIM, Value::Short(vec![2, 2]));raw.set(t::CFA_PATTERN_EP, Value::Byte(vec![0, 1, 1, 2]));raw.set(0x7310, Value::Short(vec![512; 4]));- raw.set(t::WHITE_LEVEL, Value::Long(vec![15360]));+ if white {+ raw.set(t::WHITE_LEVEL, Value::Long(vec![15360]));+ }raw.set_image(ImageData::Strips { rows_per_strip: height as u32, strips: vec![strip] });let mut ifd0 = IfdBuilder::new();ifd0.set(t::MAKE, Value::Ascii("SONY".into()));@@ -413,7 +424,7 @@ pub(crate) fn arw6_file(tiles: &[(usize, usize, usize, usize, Vec<u8>)], width:}/// A tile whose bands are random quantised integers `q` (level 1 wider), `v = dequant(q, qi)`, planes by-/// `reconstruct3` (so `quantize` in the encoder is exact on them); LL3 is random around 2048, the residual+/// `reconstruct3` (so `quantize` in the encoder is exact on them); LL3 is random around 2048 (green mean) or 0 (chroma), the residual/// `dequant(q, qi.res)`.pub(crate) fn one_tile_quantised(w: usize, h: usize, s: u8, qi: Qis) -> (Vec<u8>, TileSpec) {use crate::llvc::{dequant, reconstruct3};@@ -425,7 +436,13 @@ pub(crate) fn one_tile_quantised(w: usize, h: usize, s: u8, qi: Qis) -> (Vec<u8>x ^= x >> 7;x ^= x << 17;let v = (x % (2 * span as u64 + 1)) as i32 - span;- if centre == 0 { dequant(v, q) } else { centre + v }+ if centre == i32::MIN {+ v+ } else if centre == 0 {+ dequant(v, q)+ } else {+ centre + v+ }};let mut plane = |wd: usize, ht: usize, span: i32, centre: i32, q: u32| Plane {width: wd,@@ -440,7 +457,7 @@ pub(crate) fn one_tile_quantised(w: usize, h: usize, s: u8, qi: Qis) -> (Vec<u8>let (w1, w2b, w3) = (w2 / 2, w2 / 4, w2 / 8);let (a, b, d) = (qi.l1[c], qi.l2[c], qi.l3[c]);let bands = Bands3 {- ll3: plane(w3, n3l, 60, 2048, 0),+ ll3: plane(w3, n3l, 60, if c == 0 { 2048 } else { i32::MIN }, 0),hl3: plane(w3, n3l, 7, 0, d[0]),lh3: plane(w3, n3h, 7, 0, d[1]),hh3: plane(w3, n3h, 7, 0, d[2]),