use super::{blend, IndexedColor, Ink, TILE_SAMPLES};
use std::collections::HashMap;
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
struct Packed(u32);
const COMPLEX: u32 = 1 << 24;
impl Packed {
fn of(ink: &Ink) -> Option<Self> {
match ink.background {
IndexedColor::Solid(background) if ink.alpha <= 255 => Some(Self(
u32::from(ink.foreground) | ink.alpha << 8 | u32::from(background) << 16,
)),
_ => None,
}
}
fn complex(self) -> bool {
self.0 & COMPLEX != 0
}
fn ink(self) -> Ink {
Ink {
foreground: self.0 as u8,
alpha: (self.0 >> 8) & 0xFF,
background: IndexedColor::Solid((self.0 >> 16) as u8),
}
}
}
pub(super) type ComplexInk = HashMap<(u32, u8), Ink>;
#[derive(Clone, Copy, Debug, Default)]
pub(super) struct CellInk {
present: u16,
complex: u16,
packed: [Packed; TILE_SAMPLES],
}
impl CellInk {
pub(super) const EMPTY: Self = Self {
present: 0,
complex: 0,
packed: [Packed(0); TILE_SAMPLES],
};
pub(super) fn mask(&self) -> u16 {
self.present
}
pub(super) fn is_empty(&self) -> bool {
self.present == 0
}
pub(super) fn has_complex(&self) -> bool {
self.complex != 0
}
pub(super) fn mapped(&self, table: Option<&[u8; 256]>) -> Self {
debug_assert!(!self.has_complex());
let Some(table) = table else {
return *self;
};
let mut out = *self;
for sample in Bits(self.present) {
let packed = self.packed[sample].0;
out.packed[sample] = Packed(
(packed & 0xFF00)
| u32::from(table[(packed & 0xFF) as usize])
| u32::from(table[((packed >> 16) & 0xFF) as usize]) << 16,
);
}
out
}
pub(super) fn painted(mask: u16, foreground: u8, alphas: &[u8], backgrounds: &[u8]) -> Self {
let mut block = Self::EMPTY;
block.present = mask;
for sample in Bits(mask) {
block.packed[sample] = Packed(
u32::from(foreground)
| u32::from(alphas[sample]) << 8
| u32::from(backgrounds[sample]) << 16,
);
}
block
}
pub(super) fn within(&self, len: usize) -> Self {
let mut out = *self;
if len < TILE_SAMPLES {
out.present &= (1u16 << len) - 1;
out.complex &= out.present;
}
out
}
pub(super) fn same_as(&self, other: &Self) -> bool {
!self.has_complex()
&& !other.has_complex()
&& self.present == other.present
&& Bits(self.present).all(|sample| self.packed[sample] == other.packed[sample])
}
pub(super) fn rgb(&self, sample: usize, palette: &[[u8; 3]; 256]) -> [u8; 3] {
let packed = self.packed[sample].0;
blend(
palette[(packed & 0xFF) as usize],
palette[((packed >> 16) & 0xFF) as usize],
(packed >> 8) & 0xFF,
)
}
pub(super) fn samples(&self) -> impl Iterator<Item = usize> {
Bits(self.present)
}
}
#[derive(Clone, Copy)]
pub(super) struct InkView<'a> {
cell: &'a CellInk,
complex: &'a ComplexInk,
slot: u32,
}
impl<'a> InkView<'a> {
pub(super) fn new(cell: &'a CellInk, complex: &'a ComplexInk, slot: u32) -> Self {
Self {
cell,
complex,
slot,
}
}
#[cfg(test)]
pub(super) fn mask(&self) -> u16 {
self.cell.present
}
pub(super) fn block(&self) -> &'a CellInk {
self.cell
}
pub(super) fn len(&self) -> usize {
self.cell.present.count_ones() as usize
}
pub(super) fn get(&self, sample: usize) -> Option<Ink> {
(self.cell.present & (1 << sample) != 0).then(|| self.at(sample))
}
fn at(&self, sample: usize) -> Ink {
let packed = self.cell.packed[sample];
if packed.complex() {
self.complex[&(self.slot, sample as u8)].clone()
} else {
packed.ink()
}
}
pub(super) fn iter(&self) -> impl Iterator<Item = (usize, Ink)> + 'a {
let view = *self;
Bits(self.cell.present).map(move |sample| (sample, view.at(sample)))
}
pub(super) fn eq_list(&self, list: &[(u8, Ink)]) -> bool {
self.len() == list.len()
&& list.iter().all(|(sample, ink)| {
let sample = usize::from(*sample);
self.cell.present & (1 << sample) != 0
&& match Packed::of(ink) {
Some(packed) => self.cell.packed[sample] == packed,
None => self.cell.packed[sample].complex() && self.at(sample) == *ink,
}
})
}
}
pub(super) struct InkViewMut<'a> {
cell: &'a mut CellInk,
complex: &'a mut ComplexInk,
slot: u32,
}
impl<'a> InkViewMut<'a> {
pub(super) fn new(cell: &'a mut CellInk, complex: &'a mut ComplexInk, slot: u32) -> Self {
Self {
cell,
complex,
slot,
}
}
pub(super) fn view(&self) -> InkView<'_> {
InkView {
cell: self.cell,
complex: self.complex,
slot: self.slot,
}
}
pub(super) fn mask(&self) -> u16 {
self.cell.present
}
pub(super) fn assign(&mut self, block: &CellInk) {
debug_assert!(!block.has_complex());
self.clear();
*self.cell = *block;
}
pub(super) fn clear(&mut self) {
if self.cell.complex != 0 {
for sample in Bits(self.cell.complex) {
self.complex.remove(&(self.slot, sample as u8));
}
}
self.cell.present = 0;
self.cell.complex = 0;
}
pub(super) fn remove(&mut self, sample: usize) {
let bit = 1 << sample;
if self.cell.complex & bit != 0 {
self.complex.remove(&(self.slot, sample as u8));
self.cell.complex &= !bit;
}
self.cell.present &= !bit;
}
pub(super) fn set(&mut self, sample: usize, ink: Ink) {
let bit = 1 << sample;
self.cell.present |= bit;
match Packed::of(&ink) {
Some(packed) => {
if self.cell.complex & bit != 0 {
self.complex.remove(&(self.slot, sample as u8));
self.cell.complex &= !bit;
}
self.cell.packed[sample] = packed;
}
None => {
self.cell.complex |= bit;
self.cell.packed[sample] = Packed(COMPLEX);
self.complex.insert((self.slot, sample as u8), ink);
}
}
}
pub(super) fn update(
&mut self,
sample: usize,
ink: impl FnOnce() -> Ink,
change: impl FnOnce(&mut Ink),
) {
let mut held = self.view().get(sample).unwrap_or_else(ink);
change(&mut held);
self.set(sample, held);
}
}
struct Bits(u16);
impl Iterator for Bits {
type Item = usize;
fn next(&mut self) -> Option<usize> {
if self.0 == 0 {
return None;
}
let bit = self.0.trailing_zeros() as usize;
self.0 &= self.0 - 1;
Some(bit)
}
}
#[cfg(test)]
mod tests {
use super::*;
fn solid(foreground: u8, alpha: u32, background: u8) -> Ink {
Ink {
foreground,
alpha,
background: IndexedColor::Solid(background),
}
}
#[test]
fn packed_and_complex_ink_round_trip() {
let mut cell = CellInk::default();
let mut complex = ComplexInk::new();
let blended = Ink {
foreground: 4,
alpha: 90,
background: IndexedColor::Solid(2).over(7, 128),
};
{
let mut ink = InkViewMut::new(&mut cell, &mut complex, 3);
ink.set(5, solid(1, 200, 9));
ink.set(0, blended.clone());
ink.update(5, || unreachable!(), |ink| ink.alpha = 255);
ink.update(7, || solid(6, 0, 3), |ink| ink.alpha = 40);
}
let view = InkView::new(&cell, &complex, 3);
assert_eq!(view.mask(), 0b1010_0001);
let listed: Vec<(usize, Ink)> = view.iter().collect();
assert_eq!(
listed,
vec![
(0, blended.clone()),
(5, solid(1, 255, 9)),
(7, solid(6, 40, 3))
]
);
assert!(view.eq_list(&[
(0, blended.clone()),
(5, solid(1, 255, 9)),
(7, solid(6, 40, 3))
]));
assert!(!view.eq_list(&[(0, blended), (5, solid(1, 255, 9))]));
{
let mut ink = InkViewMut::new(&mut cell, &mut complex, 3);
ink.set(0, solid(2, 3, 4));
assert!(complex.is_empty(), "replacing complex ink drops its entry");
}
let mut ink = InkViewMut::new(&mut cell, &mut complex, 3);
ink.set(
1,
Ink {
foreground: 1,
alpha: 1,
background: IndexedColor::Solid(1).over(2, 3),
},
);
ink.clear();
assert!(cell.is_empty() && complex.is_empty());
}
}