use crate::color::source::Source;
use crate::upsample;
const SLOTS: usize = 3;
pub(crate) struct Ring<'s> {
keys: [Option<usize>; SLOTS],
rows: &'s mut [u16],
width: usize,
}
impl<'s> Ring<'s> {
pub const fn len(width: usize) -> usize {
SLOTS * 2 * width
}
pub fn new(rows: &'s mut [u16], width: usize) -> Ring<'s> {
Ring {
keys: [None; SLOTS],
rows,
width,
}
}
pub fn slot(
&mut self,
src: &Source<'_>,
r: usize,
x_shift: u32,
tmp: &mut [u16],
) -> Option<usize> {
if let Some(s) = self.keys.iter().position(|k| *k == Some(r)) {
return Some(s);
}
let s = self
.keys
.iter()
.enumerate()
.max_by_key(|(_, k)| k.map_or(usize::MAX, |k| k.abs_diff(r)))
.map(|(s, _)| s)?;
let w = self.width;
let slot = self.rows.get_mut(s * 2 * w..(s + 1) * 2 * w)?;
let (cb, cr) = slot.split_at_mut(w);
upsample::horizontal(cb, src.chroma_row(false, r, tmp)?, x_shift);
upsample::horizontal(cr, src.chroma_row(true, r, tmp)?, x_shift);
self.keys[s] = Some(r);
Some(s)
}
pub fn get(&self, s: usize) -> Option<(&[u16], &[u16])> {
let w = self.width;
let slot = self.rows.get(s * 2 * w..(s + 1) * 2 * w)?;
Some(slot.split_at(w))
}
}