use super::config::TaaError;
#[derive(Debug, Clone)]
pub struct TaaHistory {
pub width: usize,
pub height: usize,
pub color: Vec<f32>,
pub frame_count: usize,
pub last_mean_blend_factor: Option<f32>,
}
impl TaaHistory {
pub fn new(width: usize, height: usize) -> Self {
let size = width.saturating_mul(height).saturating_mul(3);
Self {
width,
height,
color: vec![0.0_f32; size],
frame_count: 0,
last_mean_blend_factor: None,
}
}
pub fn is_empty(&self) -> bool {
self.frame_count == 0
}
pub fn reset(&mut self) {
self.color.iter_mut().for_each(|v| *v = 0.0);
self.frame_count = 0;
self.last_mean_blend_factor = None;
}
pub fn get_pixel(&self, x: usize, y: usize) -> [f32; 3] {
if x >= self.width || y >= self.height {
return [0.0; 3];
}
let base = (y * self.width + x) * 3;
let r = self.color.get(base).copied().unwrap_or(0.0);
let g = self.color.get(base + 1).copied().unwrap_or(0.0);
let b = self.color.get(base + 2).copied().unwrap_or(0.0);
[r, g, b]
}
pub fn set_pixel(&mut self, x: usize, y: usize, color: [f32; 3]) -> Result<(), TaaError> {
if x >= self.width || y >= self.height {
return Err(TaaError::DimensionMismatch {
expected: self.width * self.height,
got: y * self.width + x,
});
}
let base = (y * self.width + x) * 3;
if let Some(slot) = self.color.get_mut(base) {
*slot = color[0];
}
if let Some(slot) = self.color.get_mut(base + 1) {
*slot = color[1];
}
if let Some(slot) = self.color.get_mut(base + 2) {
*slot = color[2];
}
Ok(())
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_history_new_is_empty() {
let h = TaaHistory::new(8, 8);
assert!(h.is_empty(), "new history must be empty");
}
#[test]
fn test_history_new_frame_count_zero() {
let h = TaaHistory::new(4, 4);
assert_eq!(h.frame_count, 0);
}
#[test]
fn test_history_new_all_black() {
let h = TaaHistory::new(3, 3);
for i in 0..9_usize {
let px = i % 3;
let py = i / 3;
assert_eq!(
h.get_pixel(px, py),
[0.0; 3],
"pixel ({px},{py}) must be black"
);
}
}
#[test]
fn test_history_set_get_roundtrip() {
let mut h = TaaHistory::new(4, 4);
let color = [0.1, 0.5, 0.9];
h.set_pixel(2, 3, color).expect("set_pixel must succeed");
let got = h.get_pixel(2, 3);
for c in 0..3 {
assert!((got[c] - color[c]).abs() < 1e-6, "channel {c} mismatch");
}
}
#[test]
fn test_history_set_oob_returns_err() {
let mut h = TaaHistory::new(2, 2);
let result = h.set_pixel(5, 5, [1.0, 0.0, 0.0]);
assert!(result.is_err(), "OOB set must return Err");
}
#[test]
fn test_history_get_oob_returns_black() {
let h = TaaHistory::new(2, 2);
assert_eq!(h.get_pixel(10, 10), [0.0; 3]);
}
#[test]
fn test_history_reset() {
let mut h = TaaHistory::new(4, 4);
h.set_pixel(0, 0, [1.0, 1.0, 1.0])
.expect("set_pixel must succeed");
h.frame_count = 5;
h.reset();
assert!(h.is_empty());
assert_eq!(h.frame_count, 0);
assert_eq!(h.get_pixel(0, 0), [0.0; 3]);
}
}