From ed3f746bcd76acad5a1d6d59f76a6bed00e46e22 Mon Sep 17 00:00:00 2001 From: vangel76 Date: Sun, 15 Nov 2026 18:08:14 +0100 Subject: [PATCH] craft-raw: test odd chunk placement, fix strip chunk sizes in layout tests Co-Authored-By: Claude Sonnet 5.5 --- crates/raw/src/tiffraw.rs | 80 ++++++++++++++++++++++++++++++++++++++ crates/raw/src/unpack.rs | 16 ++++++++ crates/raw/tests/it/dng.rs | 6 ++- 3 files changed, 100 insertions(+), 2 deletions(-) diff --git a/crates/raw/src/tiffraw.rs b/crates/raw/src/tiffraw.rs index f2f573ae8..e4c9a26b4 100644 --- a/crates/raw/src/tiffraw.rs +++ b/crates/raw/src/tiffraw.rs @@ -608,6 +608,86 @@ fn unpack_chunk( Ok(ChunkPx::U16(out)) } +#[cfg(test)] +mod place_tests { + use super::*; + + /// The serial per-sample copy `place_chunk` replaced: the oracle. + #[allow(clippy::too_many_arguments)] + fn reference(out: &mut [u16], c: &Chunk, v: &[u16], w: usize, h: usize, cpp: usize, planar: bool) { + let ccpp = if planar { 1 } else { cpp }; + let (cw, ch) = (c.width as usize, c.height as usize); + let (x0, y0) = (c.x as usize, c.y as usize); + if x0 >= w || y0 >= h { + return; + } + for y in 0..ch.min(h - y0) { + for x in 0..cw.min(w - x0) { + for s in 0..ccpp { + let src = (y * cw + x) * ccpp + s; + let dst_s = if planar { c.plane as usize } else { s }; + if dst_s >= cpp { + continue; + } + let dst = ((y0 + y) * w + x0 + x) * cpp + dst_s; + if let (Some(o), Some(&s)) = (out.get_mut(dst), v.get(src)) { + *o = s; + } + } + } + } + } + + fn chunk(x: u32, y: u32, width: u32, height: u32, plane: u16) -> Chunk { + Chunk { index: 0, x, y, width, height, plane, offset: 0, len: 0 } + } + + /// Chunks that overlap (the later one wins), run past the image edge, hold fewer samples than their size, or name a + /// plane the image does not have, placed through bands of several heights, equal the serial copy. + #[test] + fn place_chunk_matches_the_serial_copy_for_odd_chunks() { + let (w, h) = (11, 9); + for cpp in [1usize, 3] { + for planar in [false, true] { + let ccpp = if planar { 1 } else { cpp }; + let specs = [ + (0, 0, 8, 6, 0, 1u16), + (4, 3, 8, 8, 0, 2), // overlaps the first and runs past both edges + (4, 3, 8, 8, 0, 3), // the same place again, later wins + (0, 5, 5, 4, 1, 4), + (2, 2, 6, 6, 2, 5), + (0, 0, 4, 4, 3, 6), // plane 3: not an image plane for cpp <= 3 + (9, 0, 6, 12, 0, 7), // only two columns inside + (20, 0, 4, 4, 0, 8), // right of the image + (0, 0, 11, 9, 0, 9), // whole image, short source below + ]; + let chunks: Vec<(Chunk, Vec)> = specs + .iter() + .map(|&(x, y, cw, ch, plane, val)| { + let n = (cw * ch) as usize * ccpp; + // the last chunk holds only 40 samples + let n = if val == 9 { 40 } else { n }; + (chunk(x, y, cw, ch, plane), (0..n).map(|i| val * 100 + i as u16).collect()) + }) + .collect(); + let mut want = vec![0u16; w * h * cpp]; + for (c, v) in &chunks { + reference(&mut want, c, v, w, h, cpp, planar); + } + for band_rows in [1usize, 2, 4, 9, 20] { + let mut got = vec![0u16; w * h * cpp]; + for (bi, band) in got.chunks_mut(band_rows * w * cpp).enumerate() { + for (c, v) in &chunks { + place_chunk(band, bi * band_rows, c, v, w, h, cpp, planar); + } + } + assert_eq!(got, want, "cpp {cpp} planar {planar} band rows {band_rows}"); + } + } + } + } +} + #[cfg(test)] #[cfg(feature = "dng")] mod lossy_tests { diff --git a/crates/raw/src/unpack.rs b/crates/raw/src/unpack.rs index 9aaa1e0f2..27d77b55e 100644 --- a/crates/raw/src/unpack.rs +++ b/crates/raw/src/unpack.rs @@ -175,6 +175,22 @@ mod tests { assert_eq!(o, [0x000, 0x012]); // the word 0x00001234, top bits first } + #[test] + fn words32_scratch_is_reusable() { + let long: Vec = (0u8..24).map(|i| i.wrapping_mul(37).wrapping_add(5)).collect(); + let short = &long[3..10]; + let mut scratch = Vec::new(); + for order in [ByteOrder::Little, ByteOrder::Big] { + for (src, n) in [(&long[..], 16usize), (short, 4)] { + let mut want = vec![0u16; n]; + unpack_words32_msb(src, order, 12, &mut want); + let mut got = vec![0u16; n]; + unpack_words32_msb_with(src, order, 12, &mut got, &mut scratch); + assert_eq!(got, want); + } + } + } + #[test] fn packing() { // 12-bit MSB: 0xABC, 0xDEF -> AB CD EF diff --git a/crates/raw/tests/it/dng.rs b/crates/raw/tests/it/dng.rs index e8c665852..43b0d8a94 100644 --- a/crates/raw/tests/it/dng.rs +++ b/crates/raw/tests/it/dng.rs @@ -404,7 +404,7 @@ fn floating_point_dngs() { } /// The chunks (tiles of `tw` x `th`, or strips when `tw == w`) of an image of `cpp` interleaved samples per pixel, each -/// nominal-sized with zero padding past the image edge: one chunk per tile, and for `planar` one set of chunks per +/// nominal-sized (strips: clipped to the image height) with zero padding past the image edge: one chunk per tile, and for `planar` one set of chunks per /// plane (one sample per pixel), plane after plane. fn chunk_samples(px: &[T], w: usize, h: usize, cpp: usize, tw: usize, th: usize, planar: bool) -> Vec> { let planes = if planar { cpp } else { 1 }; @@ -413,7 +413,9 @@ fn chunk_samples(px: &[T], w: usize, h: usize, cpp: usize, tw for plane in 0..planes { for ty in 0..h.div_ceil(th) { for tx in 0..w.div_ceil(tw) { - let mut c = vec![T::default(); tw * th * ccpp]; + // a strip holds only the rows inside the image, a tile its nominal size + let rows = if tw == w { th.min(h - ty * th) } else { th }; + let mut c = vec![T::default(); tw * rows * ccpp]; for y in 0..th.min(h - ty * th) { for x in 0..tw.min(w - tx * tw) { for s in 0..ccpp {