From df89f6b6b7534c717debfbcb19b287a14aba16fb Mon Sep 17 00:00:00 2001 From: vangel76 Date: Sun, 15 Nov 2026 18:13:19 +0100 Subject: [PATCH] craft-raw: test tables where code plus extra bits fill the lookup, canonical reference, clippy allow Co-Authored-By: Claude Sonnet 5.5 --- crates/raw/src/huffman.rs | 50 ++++++++++++++++++++++++++++------- crates/raw/src/ljpeg.rs | 1 + crates/raw/src/ljpeg/tests.rs | 41 +++++++++++++++++++++++----- 3 files changed, 76 insertions(+), 16 deletions(-) diff --git a/crates/raw/src/huffman.rs b/crates/raw/src/huffman.rs index 85b242be1..4f35f64bb 100644 --- a/crates/raw/src/huffman.rs +++ b/crates/raw/src/huffman.rs @@ -145,9 +145,10 @@ mod tests { /// Reference: match the code bit by bit, then read the category's additional bits (T.81 F.1.2.1). fn reference(src: &[u8], pos: &mut usize, codes: &[(u16, u8)]) -> Option { let window: u32 = (0..12).fold(0, |a, k| (a << 1) | bit(src, *pos + k)); - let (sym, len) = codes.iter().enumerate().find_map(|(s, &(c, l))| { - (l > 0 && (c as u32 >> (12 - l)) == window >> (12 - l)).then_some((s as u32, l as usize)) - })?; + let (sym, len) = codes + .iter() + .enumerate() + .find_map(|(s, &(c, l))| (l > 0 && (c as u32 >> (12 - l)) == window >> (12 - l)).then_some((s as u32, l as usize)))?; *pos += len; if sym == 0 { return Some(0); @@ -157,13 +158,25 @@ mod tests { Some(if v < 1 << (sym - 1) { v - (1 << sym) + 1 } else { v }) } - #[test] - fn diff_matches_bitwise_reference_for_every_category() { - // categories 0..=16, code lengths up to 12 so that short codes combine with their bits and long ones do not - let lens = [2u8, 2, 3, 3, 4, 4, 5, 6, 7, 8, 9, 10, 11, 12, 12, 12, 12]; - let codes = canonical(&lens); + /// `diff` against the reference on random streams (invalid codes are stepped over) and on every (code, extra + /// bits) pair of every symbol. + fn check_table(lens: &[u8]) { + let codes = canonical(lens); let h = Huffman::new(&codes).unwrap(); + for (sym, &(code, len)) in codes.iter().enumerate() { + for v in 0..1u64 << sym { + let (len, n) = (len as u32, sym as u32); + let word = (((code >> (12 - len)) as u64) << n | v) << (64 - len - n); + let src = word.to_be_bytes(); + let mut pos = 0; + let want = reference(&src, &mut pos, &codes); + let mut bits = Bits::new(&src); + assert_eq!(diff(&mut bits, &h), want, "symbol {sym} bits {v:#b}"); + assert_eq!(bits.consumed_bits(), pos); + } + } let mut s = 0x1234_5678_9abc_def0u64; + let (mut invalid, mut long) = (0, 0); for _ in 0..40 { let src: Vec = (0..400) .map(|_| { @@ -174,13 +187,32 @@ mod tests { let mut bits = Bits::new(&src); let mut pos = 0; while pos < src.len() * 8 - 64 { + let before = pos; let want = reference(&src, &mut pos, &codes); assert_eq!(diff(&mut bits, &h), want); if want.is_none() { - break; + // step over the invalid code + invalid += 1; + pos += 1; + bits.skip(1); + } else { + long += (pos - before > 12) as u32; } assert_eq!(bits.consumed_bits(), pos); } } + assert!(long > 0, "{invalid} invalid codes, no symbol longer than the lookup"); + } + + #[test] + fn diff_matches_bitwise_reference_for_every_category() { + // categories 0..=16, code lengths up to 12 so that short codes combine with their bits and long ones do not + check_table(&[2, 2, 3, 3, 4, 4, 5, 6, 7, 8, 9, 10, 11, 12, 12, 12, 12]); + } + + #[test] + fn diff_matches_reference_when_code_and_bits_fill_the_lookup_exactly() { + // category 8 with a 4-bit code and category 7 with a 5-bit code: len + category == 12 + check_table(&[2, 3, 3, 5, 5, 5, 6, 5, 4, 7, 8, 9, 10, 11, 12, 12, 12]); } } diff --git a/crates/raw/src/ljpeg.rs b/crates/raw/src/ljpeg.rs index ab27ed57f..498a8302f 100644 --- a/crates/raw/src/ljpeg.rs +++ b/crates/raw/src/ljpeg.rs @@ -778,6 +778,7 @@ pub fn decode(d: &[u8], max_samples: usize) -> Result { restart_left -= end - x; } // the first pixel of the run has the special predictions (start of the interval, first column) + #[allow(clippy::needless_range_loop)] // `c` indexes the tables and the sample position alike for c in 0..nc { let i = x * nc + c; let p = if reset_pending { diff --git a/crates/raw/src/ljpeg/tests.rs b/crates/raw/src/ljpeg/tests.rs index 17791ea8b..a77425d40 100644 --- a/crates/raw/src/ljpeg/tests.rs +++ b/crates/raw/src/ljpeg/tests.rs @@ -181,15 +181,42 @@ fn roundtrip_skewed_categories_with_long_codes() { } } -/// `decode_diff` is `decode` followed by `diff_value`, for tables with codes inside and beyond the lookup. +/// Canonical decoding from the code lengths alone, bit by bit (T.81 Annex F, Figure F.16): the reference for the +/// lookup tables. +fn canonical_symbol(br: &mut BitReader, lens: &[u8]) -> Option { + let window = br.peek(16); + let (mut first, mut index) = (0u32, 0usize); + for len in 1..=16u8 { + let code = window >> (16 - len); + let n = lens.iter().filter(|&&l| l == len).count() as u32; + if code < first + n { + br.consume(len as u32); + return Some((index + (code - first) as usize) as u8); + } + index += n as usize; + first = (first + n) << 1; + } + None +} + +/// `decode_diff` against canonical decoding and `diff_value`, for tables with codes inside and beyond the lookup +/// and with `len + category` equal to the lookup width. #[test] -fn decode_diff_matches_decode_and_diff_value() { - for lens in [[2u8, 2, 3, 3, 4, 4, 5, 6, 7, 8, 9, 10, 11, 12, 12, 12, 12], [2, 2, 3, 3, 4, 4, 5, 6, 7, 8, 9, 10, 13, 14, 15, 16, 16]] { +fn decode_diff_matches_canonical_decoding() { + let tables: [[u8; 17]; 3] = [ + [2, 2, 3, 3, 4, 4, 5, 6, 7, 8, 9, 10, 11, 12, 12, 12, 12], + [2, 2, 3, 3, 4, 4, 5, 6, 7, 8, 9, 10, 13, 14, 15, 16, 16], + [2, 3, 3, 5, 5, 5, 6, 5, 4, 7, 8, 9, 10, 11, 12, 12, 12], + ]; + for lens in tables { let mut counts = [0u8; 16]; for &l in &lens { counts[l as usize - 1] += 1; } - let values: Vec = (0..17).collect(); // lengths are already ascending + // symbols in code order: by length, ties by symbol + let mut values: Vec = (0..17).collect(); + values.sort_by_key(|&v| (lens[v as usize], v)); + let sorted: Vec = values.iter().map(|&v| lens[v as usize]).collect(); let t = Huffman::new(&counts, &values).unwrap(); let mut s = 77u64; let src: Vec = (0..6000) @@ -200,11 +227,11 @@ fn decode_diff_matches_decode_and_diff_value() { .collect(); let (mut a, mut b) = (BitReader::new(&src), BitReader::new(&src)); for n in 0..8000 { - let want = t.decode(&mut a).map(|ssss| diff_value(&mut a, ssss)); + let want = canonical_symbol(&mut a, &sorted).map(|i| diff_value(&mut a, values[i as usize])); let got = t.decode_diff(&mut b, false); - assert_eq!(got.ok(), want.ok(), "symbol {n}"); + assert_eq!(got.ok(), want, "symbol {n}"); assert_eq!(a.peek(32), b.peek(32), "symbol {n}"); - if want.is_err() { + if want.is_none() { // step past the invalid code so the walk goes on a.consume(1); b.consume(1);