pub(crate) fn decode_bc5_snorm(data: &[u8], width: usize, height: usize) -> Vec<u8> {
let blocks_x = width.div_ceil(4);
let blocks_y = height.div_ceil(4);
debug_assert_eq!(data.len(), blocks_x * blocks_y * 16);
let mut rg = vec![0u8; width * height * 2];
for (block_i, block) in data.chunks_exact(16).enumerate() {
let bx = (block_i % blocks_x) * 4;
let by = (block_i / blocks_x) * 4;
let red = decode_bc4_snorm_block(block[..8].try_into().unwrap());
let green = decode_bc4_snorm_block(block[8..].try_into().unwrap());
for ty in 0..4 {
for tx in 0..4 {
let (x, y) = (bx + tx, by + ty);
if x >= width || y >= height {
continue;
}
let i = (y * width + x) * 2;
rg[i] = red[ty * 4 + tx] as u8;
rg[i + 1] = green[ty * 4 + tx] as u8;
}
}
}
rg
}
fn decode_bc4_snorm_block(block: [u8; 8]) -> [i8; 16] {
let e0 = block[0] as i8;
let e1 = block[1] as i8;
let r0 = e0.max(-127) as f32;
let r1 = e1.max(-127) as f32;
let palette: [f32; 8] = if e0 > e1 {
std::array::from_fn(|i| match i {
0 => r0,
1 => r1,
i => (r0 * (8 - i) as f32 + r1 * (i - 1) as f32) / 7.0,
})
} else {
std::array::from_fn(|i| match i {
0 => r0,
1 => r1,
6 => -127.0,
7 => 127.0,
i => (r0 * (6 - i) as f32 + r1 * (i - 1) as f32) / 5.0,
})
};
let indices = u64::from_le_bytes(block) >> 16;
std::array::from_fn(|texel| palette[(indices >> (texel * 3)) as usize & 0b111].round() as i8)
}
#[cfg(test)]
mod tests {
use super::*;
fn bc4_block(e0: u8, e1: u8, indices: [u8; 16]) -> [u8; 8] {
let mut packed = 0u64;
for (i, &index) in indices.iter().enumerate() {
assert!(index < 8);
packed |= (index as u64) << (i * 3);
}
let bits = packed.to_le_bytes();
[e0, e1, bits[0], bits[1], bits[2], bits[3], bits[4], bits[5]]
}
#[test]
fn endpoints_map_to_full_range() {
let block = bc4_block(127, 0x81, [0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1]);
let texels = decode_bc4_snorm_block(block);
for pair in texels.chunks_exact(2) {
assert_eq!(pair, [127, -127]);
}
}
#[test]
fn neg_128_is_clamped_to_neg_127() {
let block = bc4_block(0x80, 0x80, [0; 16]);
assert_eq!(decode_bc4_snorm_block(block), [-127; 16]);
}
#[test]
fn mode_selection_uses_raw_endpoints_not_clamped() {
let block = bc4_block(0x81, 0x80, [7; 16]);
assert_eq!(decode_bc4_snorm_block(block), [-127; 16]);
}
#[test]
fn zero_stays_zero() {
let block = bc4_block(0, 0x81, [0; 16]);
assert_eq!(decode_bc4_snorm_block(block), [0; 16]);
}
#[test]
fn six_entry_mode_has_explicit_min_max() {
let block = bc4_block(0x81, 127, [6, 7, 6, 7, 6, 7, 6, 7, 6, 7, 6, 7, 6, 7, 6, 7]);
let texels = decode_bc4_snorm_block(block);
for pair in texels.chunks_exact(2) {
assert_eq!(pair, [-127, 127]);
}
}
#[test]
fn eight_entry_mode_interpolates() {
let block = bc4_block(127, 0x81, [2; 16]);
assert_eq!(decode_bc4_snorm_block(block), [91; 16]);
}
#[test]
fn decodes_rg_channels_and_partial_blocks() {
let mut block = [0u8; 16];
block[..8].copy_from_slice(&bc4_block(127, 0x81, [0; 16])); block[8..].copy_from_slice(&bc4_block(0x81, 127, [0; 16]));
let rg = decode_bc5_snorm(&block, 2, 2);
assert_eq!(rg.len(), 2 * 2 * 2);
for pixel in rg.chunks_exact(2) {
assert_eq!(pixel, [127i8 as u8, -127i8 as u8]);
}
}
}