From bc76eb8d3941ecc3c9b562583c2833eb253111ec Mon Sep 17 00:00:00 2001 From: local Date: Sat, 28 Nov 2026 20:13:59 +0100 Subject: [PATCH] BCPack: BC7 dictionary indices read as Microsoft's decoder reads them The decoder takes the word after an entry's index widths (the widths again in all entries but the first) as pixel 0's index, so the stored indices land one pixel later than we placed them. Read all 32 slots in order; the test checks the layout BCPack.dll uses. Co-Authored-By: Claude Opus 5.5 (cherry picked from commit f9de61c52e59ca1ddc6b9b088c7648021b52b9a2) --- crates/victory-formats/src/bcpack.rs | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/crates/victory-formats/src/bcpack.rs b/crates/victory-formats/src/bcpack.rs index 11dada0f..a86cd4a7 100644 --- a/crates/victory-formats/src/bcpack.rs +++ b/crates/victory-formats/src/bcpack.rs @@ -222,10 +222,11 @@ impl Tables { bits: [r.u8(), r.u8()], ..Default::default() }; - // Unused: the widths again in all entries but the first. - r.u16(); - // Only 15 slots for set 1, which no entry uses (0 bits). - for v in &mut ix.ix.as_flattened_mut()[..31] { + // Microsoft's decoder takes the word after the widths (the + // widths again in all entries but the first) as pixel 0's + // index, so the stored indices land one pixel late and the + // last spills into set 1, which no entry uses (0 bits). + for v in ix.ix.as_flattened_mut() { *v = r.u16() as i16; } (ep, ix) @@ -1699,14 +1700,15 @@ mod tests { assert!(n > 0); } - /// A dictionary entry stands in for a block the decoder made: a set with - /// 0 index bits holds no indices, and pixel 0 is the anchor (top bit clear). + /// Dictionary entries as Microsoft's decoder reads them (checked against + /// BCPack.dll): pixel 0 holds the widths word in all entries but the + /// first, and set 1 never has index bits. #[test] fn bc7_dictionary_indices_line_up() { let t = Tables::builtin().unwrap(); for (k, (_, ix)) in t.bc7.iter().enumerate() { - let anchor = 0..1 << (ix.bits[0] - 1); - let ok = (ix.bits[1] != 0 || ix.ix[1] == [0; 16]) && anchor.contains(&ix.ix[0][0]); + let widths = i16::from(ix.bits[0]) | i16::from(ix.bits[1]) << 8; + let ok = ix.bits[1] == 0 && (k == 0 || ix.ix[0][0] == widths); assert!(ok, "entry {k}: {:?}", ix.ix); } }