use super::block_mode::WeightMode;
use super::context::Tables;
use super::decimation::{ENTRY, infill};
use super::{endpoint, ise, read_bits};
pub(crate) const ERROR: [u8; 4] = [255; 4];
#[cfg(test)]
pub(crate) const MAGENTA: [u8; 4] = [255, 0, 255, 255];
pub(super) const MAX_TEXELS: usize = 144;
pub(super) fn decode(
tables: &mut Tables,
block: &[u8; 16],
out: &mut [u8; 4 * MAX_TEXELS],
error: [u8; 4],
) {
let texels = tables.footprint().texels();
if symbolic(tables, block, out).is_none() {
for px in out.as_chunks_mut::<4>().0.iter_mut().take(texels) {
*px = error;
}
}
}
pub(super) fn is_error(tables: &mut Tables, block: &[u8; 16]) -> bool {
let mut out = [0; 4 * MAX_TEXELS];
symbolic(tables, block, &mut out).is_none()
}
fn symbolic(tables: &mut Tables, block: &[u8; 16], out: &mut [u8; 4 * MAX_TEXELS]) -> Option<()> {
let texels = tables.footprint().texels();
let mode = u16::from_le_bytes([block[0], block[1]]) & 0x7ff;
if mode & 0x1ff == 0x1fc {
let colour = void_extent(block, mode)?;
for px in out.as_chunks_mut::<4>().0.iter_mut().take(texels) {
*px = colour;
}
return Some(());
}
let weights = tables.mode(mode)?;
let dual = weights.dual_plane;
let per_plane = usize::from(weights.x) * usize::from(weights.y);
let count = per_plane * if dual { 2 } else { 1 };
let reversed = u128::from_le_bytes(*block).reverse_bits().to_le_bytes();
let (plane1, plane2) = grid_weights(weights, &reversed, count)?;
let layout = endpoint::decode_layout(block, weights)?;
let values = endpoint::decode_values(block, &layout)?;
let mut ends = [[[0; 4]; 2]; 4];
for ((&cem, v), e) in layout
.modes
.iter()
.zip(&values)
.zip(&mut ends)
.take(usize::from(layout.partitions))
{
*e = endpoint::unpack_ldr(cem, v)?.map(|end| end.map(u32::from));
}
let plane2_channel = if dual {
usize::try_from(read_bits(block, layout.below_weights.checked_sub(2)?, 2)?).ok()?
} else {
4
};
let partition = (layout.partitions > 1).then_some((layout.seed, layout.partitions));
let fp = tables.footprint();
let identity = (weights.x, weights.y) == (fp.width(), fp.height());
let (grid, parts) = tables.rows((weights.x, weights.y), partition)?;
let texels = (grid.as_chunks::<{ ENTRY }>().0.get(..texels)?, parts);
let weights = (&plane1, &plane2, plane2_channel);
match (identity, dual) {
(true, false) => colours::<true, false>(texels, weights, &ends, out),
(true, true) => colours::<true, true>(texels, weights, &ends, out),
(false, false) => colours::<false, false>(texels, weights, &ends, out),
(false, true) => colours::<false, true>(texels, weights, &ends, out),
}
Some(())
}
fn grid_weights(
weights: WeightMode,
reversed: &[u8; 16],
count: usize,
) -> Option<([u8; 64], [u8; 64])> {
let mut indices = [0; 64];
ise::decode(weights.quant, reversed, 0, indices.get_mut(..count)?)?;
let (mut plane1, mut plane2) = ([0; 64], [0; 64]);
if weights.dual_plane {
for ((&[first, second], a), b) in indices
.get(..count)?
.as_chunks::<2>()
.0
.iter()
.zip(&mut plane1)
.zip(&mut plane2)
{
*a = weights.quant.unquant_weight(first)?;
*b = weights.quant.unquant_weight(second)?;
}
} else {
for (&index, a) in indices.get(..count)?.iter().zip(&mut plane1) {
*a = weights.quant.unquant_weight(index)?;
}
}
Some((plane1, plane2))
}
#[inline(never)]
fn colours<const IDENTITY: bool, const DUAL: bool>(
(entries, parts): (&[[u8; ENTRY]], &[u8]),
(plane1, plane2, channel): (&[u8; 64], &[u8; 64], usize),
ends: &[[[u32; 4]; 2]; 4],
out: &mut [u8; 4 * MAX_TEXELS],
) {
let weight = |i: usize, entry: [u8; ENTRY], plane: &[u8; 64]| {
if IDENTITY {
plane.get(i & 63).copied().unwrap_or(0)
} else {
infill(entry, plane)
}
};
let pixels = out
.as_chunks_mut::<4>()
.0
.iter_mut()
.zip(entries)
.enumerate();
for (i, (px, &entry)) in pixels {
let mut w = [weight(i, entry, plane1); 4];
if DUAL {
if let Some(w) = w.get_mut(channel & 3) {
*w = weight(i, entry, plane2);
}
}
let part = parts.get(i).map_or(0, |&p| usize::from(p & 3));
let [e0, e1] = ends.get(part).copied().unwrap_or_default();
*px = lerp(e0, e1, w);
}
}
#[allow(clippy::inline_always, reason = "measured: the texel loop's hot call")]
#[inline(always)]
fn lerp(c0: [u32; 4], c1: [u32; 4], w: [u8; 4]) -> [u8; 4] {
#[allow(clippy::cast_possible_truncation, reason = "a 16-bit value's top byte")]
let lane = |c0: u32, c1: u32, w: u8| {
let (w1, w0) = (u32::from(w), 64 - u32::from(w.min(64)));
((c0 * w0 + c1 * w1 + 32) >> 14) as u8
};
[
lane(c0[0], c1[0], w[0]),
lane(c0[1], c1[1], w[1]),
lane(c0[2], c1[2], w[2]),
lane(c0[3], c1[3], w[3]),
]
}
fn void_extent(block: &[u8; 16], mode: u16) -> Option<[u8; 4]> {
if read_bits(block, 10, 2)? != 3 {
return None;
}
let low_s = read_bits(block, 12, 8)? | (read_bits(block, 20, 5)? << 8);
let high_s = read_bits(block, 25, 13)?;
let low_t = read_bits(block, 38, 8)? | (read_bits(block, 46, 5)? << 8);
let high_t = read_bits(block, 51, 13)?;
let all_ones = [low_s, high_s, low_t, high_t].iter().all(|&v| v == 0x1fff);
if (low_s >= high_s || low_t >= high_t) && !all_ones {
return None;
}
if mode & 0x200 != 0 {
return None;
}
Some([block[9], block[11], block[13], block[15]])
}
#[cfg(test)]
mod tests;