use std::f32::consts::PI;
use thiserror::Error;
#[derive(Debug, Error)]
pub enum BackgroundError {
#[error("Invalid dimensions: {0}")]
InvalidDimensions(String),
#[error("Invalid color: {0}")]
InvalidColor(String),
#[error("Invalid config: {0}")]
InvalidConfig(String),
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct BackgroundColor {
pub r: f32,
pub g: f32,
pub b: f32,
}
impl BackgroundColor {
pub fn new(r: f32, g: f32, b: f32) -> Result<Self, BackgroundError> {
let channels = [("r", r), ("g", g), ("b", b)];
for (name, v) in channels {
if !v.is_finite() || !(0.0..=1.0).contains(&v) {
return Err(BackgroundError::InvalidColor(format!(
"channel '{name}' = {v} is not in [0, 1]"
)));
}
}
Ok(Self { r, g, b })
}
#[inline]
pub fn black() -> Self {
Self {
r: 0.0,
g: 0.0,
b: 0.0,
}
}
#[inline]
pub fn white() -> Self {
Self {
r: 1.0,
g: 1.0,
b: 1.0,
}
}
#[inline]
pub fn gray(v: f32) -> Self {
let v = v.clamp(0.0, 1.0);
Self { r: v, g: v, b: v }
}
pub fn from_hex(hex: &str) -> Result<Self, BackgroundError> {
let s = hex.trim();
let digits = s.strip_prefix('#').ok_or_else(|| {
BackgroundError::InvalidColor(format!("expected '#RRGGBB', got '{s}'"))
})?;
if digits.len() != 6 {
return Err(BackgroundError::InvalidColor(format!(
"hex color must have exactly 6 digits after '#', got {}: '{s}'",
digits.len()
)));
}
let parse_byte = |offset: usize| -> Result<u8, BackgroundError> {
u8::from_str_radix(&digits[offset..offset + 2], 16).map_err(|_| {
BackgroundError::InvalidColor(format!(
"invalid hex digits at position {offset} in '{s}'"
))
})
};
let r_byte = parse_byte(0)?;
let g_byte = parse_byte(2)?;
let b_byte = parse_byte(4)?;
Ok(Self {
r: r_byte as f32 / 255.0,
g: g_byte as f32 / 255.0,
b: b_byte as f32 / 255.0,
})
}
#[inline]
pub fn to_array(&self) -> [f32; 3] {
[self.r, self.g, self.b]
}
pub fn blend_with(&self, other: BackgroundColor, t: f32) -> BackgroundColor {
let t = t.clamp(0.0, 1.0);
BackgroundColor {
r: self.r + (other.r - self.r) * t,
g: self.g + (other.g - self.g) * t,
b: self.b + (other.b - self.b) * t,
}
}
}
impl Default for BackgroundColor {
fn default() -> Self {
Self::black()
}
}
#[derive(Debug, Clone)]
pub enum BackgroundType {
Solid(BackgroundColor),
VerticalGradient {
top: BackgroundColor,
bottom: BackgroundColor,
},
RadialGradient {
inner: BackgroundColor,
outer: BackgroundColor,
},
Checkerboard {
color_a: BackgroundColor,
color_b: BackgroundColor,
cell_size: u32,
},
Sky {
sky_color: BackgroundColor,
horizon_color: BackgroundColor,
ground_color: BackgroundColor,
},
Noise {
seed: u64,
},
RandomPixels {
seed: u64,
},
}
pub struct BackgroundImage {
pub width: u32,
pub height: u32,
pub pixels: Vec<f32>,
}
impl BackgroundImage {
pub fn new(width: u32, height: u32) -> Self {
let len = (width as usize) * (height as usize) * 3;
Self {
width,
height,
pixels: vec![0.0_f32; len],
}
}
pub fn pixel(&self, x: u32, y: u32) -> [f32; 3] {
if x >= self.width || y >= self.height {
return [0.0, 0.0, 0.0];
}
let idx = (y as usize * self.width as usize + x as usize) * 3;
[self.pixels[idx], self.pixels[idx + 1], self.pixels[idx + 2]]
}
pub fn set_pixel(&mut self, x: u32, y: u32, color: [f32; 3]) {
if x >= self.width || y >= self.height {
return;
}
let idx = (y as usize * self.width as usize + x as usize) * 3;
self.pixels[idx] = color[0];
self.pixels[idx + 1] = color[1];
self.pixels[idx + 2] = color[2];
}
pub fn to_u8(&self) -> Vec<u8> {
self.pixels
.iter()
.map(|&v| (v.clamp(0.0, 1.0) * 255.0) as u8)
.collect()
}
pub fn mean_color(&self) -> [f32; 3] {
let n = (self.width as usize) * (self.height as usize);
if n == 0 {
return [0.0, 0.0, 0.0];
}
let mut sum = [0.0_f64; 3];
for chunk in self.pixels.chunks_exact(3) {
sum[0] += chunk[0] as f64;
sum[1] += chunk[1] as f64;
sum[2] += chunk[2] as f64;
}
[
(sum[0] / n as f64) as f32,
(sum[1] / n as f64) as f32,
(sum[2] / n as f64) as f32,
]
}
pub fn composite_over(&self, foreground_rgba: &[f32]) -> Result<Vec<f32>, BackgroundError> {
let expected = (self.width as usize) * (self.height as usize) * 4;
if foreground_rgba.len() != expected {
return Err(BackgroundError::InvalidDimensions(format!(
"foreground_rgba has {} elements, expected {} ({}×{}×4)",
foreground_rgba.len(),
expected,
self.width,
self.height,
)));
}
let num_pixels = (self.width as usize) * (self.height as usize);
let mut out = Vec::with_capacity(num_pixels * 3);
for i in 0..num_pixels {
let fg_r = foreground_rgba[i * 4];
let fg_g = foreground_rgba[i * 4 + 1];
let fg_b = foreground_rgba[i * 4 + 2];
let fg_a = foreground_rgba[i * 4 + 3];
let bg_r = self.pixels[i * 3];
let bg_g = self.pixels[i * 3 + 1];
let bg_b = self.pixels[i * 3 + 2];
let inv_a = 1.0 - fg_a;
out.push(fg_r + bg_r * inv_a);
out.push(fg_g + bg_g * inv_a);
out.push(fg_b + bg_b * inv_a);
}
Ok(out)
}
}
#[inline]
fn xorshift64(state: &mut u64) -> u64 {
*state ^= *state << 13;
*state ^= *state >> 7;
*state ^= *state << 17;
*state
}
#[inline]
fn u64_to_f32_01(v: u64) -> f32 {
(v >> 40) as f32 / (1u64 << 24) as f32
}
#[inline]
fn pixel_seed(seed: u64, pixel_index: u64) -> u64 {
let mixed = pixel_index.wrapping_mul(0x9E3779B97F4A7C15);
let s = seed ^ mixed;
if s == 0 {
1
} else {
s
}
}
pub fn generate_background(
width: u32,
height: u32,
bg_type: &BackgroundType,
) -> Result<BackgroundImage, BackgroundError> {
if width == 0 || height == 0 {
return Err(BackgroundError::InvalidDimensions(format!(
"width and height must be > 0, got {}×{}",
width, height
)));
}
let mut img = BackgroundImage::new(width, height);
match bg_type {
BackgroundType::Solid(color) => {
let arr = color.to_array();
for chunk in img.pixels.chunks_exact_mut(3) {
chunk[0] = arr[0];
chunk[1] = arr[1];
chunk[2] = arr[2];
}
}
BackgroundType::VerticalGradient { top, bottom } => {
let h_denom = if height > 1 { (height - 1) as f32 } else { 1.0 };
for y in 0..height {
let t = y as f32 / h_denom;
let color = top.blend_with(*bottom, t).to_array();
for x in 0..width {
img.set_pixel(x, y, color);
}
}
}
BackgroundType::RadialGradient { inner, outer } => {
let cx = width as f32 / 2.0;
let cy = height as f32 / 2.0;
let half_diag = {
let d = (cx * cx + cy * cy).sqrt();
if d == 0.0 {
1.0
} else {
d
}
};
for y in 0..height {
for x in 0..width {
let dx = x as f32 - cx;
let dy = y as f32 - cy;
let dist = (dx * dx + dy * dy).sqrt();
let t = (dist / half_diag).min(1.0);
let color = inner.blend_with(*outer, t).to_array();
img.set_pixel(x, y, color);
}
}
}
BackgroundType::Checkerboard {
color_a,
color_b,
cell_size,
} => {
let cell = (*cell_size).max(1);
for y in 0..height {
for x in 0..width {
let cell_x = x / cell;
let cell_y = y / cell;
let parity = (cell_x + cell_y) % 2;
let color = if parity == 0 {
color_a.to_array()
} else {
color_b.to_array()
};
img.set_pixel(x, y, color);
}
}
}
BackgroundType::Sky {
sky_color,
horizon_color,
ground_color,
} => {
let h_denom = if height > 1 { (height - 1) as f32 } else { 1.0 };
for y in 0..height {
let ny = 1.0 - 2.0 * y as f32 / h_denom;
let color = if ny > 0.1 {
let t = 1.0 - (ny - 0.1) / 0.9;
sky_color.blend_with(*horizon_color, t).to_array()
} else if ny >= -0.1 {
horizon_color.to_array()
} else {
let t = (-ny - 0.1) / 0.9;
horizon_color.blend_with(*ground_color, t).to_array()
};
for x in 0..width {
img.set_pixel(x, y, color);
}
}
}
BackgroundType::Noise { seed } => {
for y in 0..height {
for x in 0..width {
let pixel_index = y as u64 * width as u64 + x as u64;
let mut state = pixel_seed(*seed, pixel_index);
let r = u64_to_f32_01(xorshift64(&mut state));
let g = u64_to_f32_01(xorshift64(&mut state));
let b = u64_to_f32_01(xorshift64(&mut state));
img.set_pixel(x, y, [r, g, b]);
}
}
}
BackgroundType::RandomPixels { seed } => {
for y in 0..height {
for x in 0..width {
let pixel_index = y as u64 * width as u64 + x as u64;
let base_seed = pixel_seed(*seed, pixel_index);
let mut state_r = pixel_seed(base_seed, 0x01);
let mut state_g = pixel_seed(base_seed, 0x02);
let mut state_b = pixel_seed(base_seed, 0x03);
let r = u64_to_f32_01(xorshift64(&mut state_r));
let g = u64_to_f32_01(xorshift64(&mut state_g));
let b = u64_to_f32_01(xorshift64(&mut state_b));
img.set_pixel(x, y, [r, g, b]);
}
}
}
}
Ok(img)
}
pub struct BackgroundAugmentor {
pub types: Vec<BackgroundType>,
pub width: u32,
pub height: u32,
call_count: u64,
pub base_seed: u64,
}
impl BackgroundAugmentor {
pub fn new(
types: Vec<BackgroundType>,
width: u32,
height: u32,
seed: u64,
) -> Result<Self, BackgroundError> {
if types.is_empty() {
return Err(BackgroundError::InvalidConfig(
"BackgroundAugmentor requires at least one background type".into(),
));
}
if width == 0 || height == 0 {
return Err(BackgroundError::InvalidDimensions(format!(
"augmentor dimensions must be > 0, got {}×{}",
width, height
)));
}
Ok(Self {
types,
width,
height,
call_count: 0,
base_seed: seed,
})
}
pub fn sample(&mut self) -> Result<BackgroundImage, BackgroundError> {
let call = self.call_count;
self.call_count += 1;
let type_seed = {
let s = self
.base_seed
.wrapping_add(call.wrapping_mul(0xA24BAED4963EE407));
let mut state = if s == 0 { 1 } else { s };
xorshift64(&mut state)
};
let type_idx = (type_seed % self.types.len() as u64) as usize;
let img_seed = self.base_seed ^ call.wrapping_mul(0x517CC1B727220A95);
let bg_type = patch_seed(&self.types[type_idx], img_seed);
generate_background(self.width, self.height, &bg_type)
}
pub fn num_calls(&self) -> u64 {
self.call_count
}
}
fn patch_seed(bg_type: &BackgroundType, new_seed: u64) -> BackgroundType {
match bg_type {
BackgroundType::Noise { .. } => BackgroundType::Noise { seed: new_seed },
BackgroundType::RandomPixels { .. } => BackgroundType::RandomPixels { seed: new_seed },
other => other.clone(),
}
}
pub fn standard_augmentor(width: u32, height: u32) -> BackgroundAugmentor {
let types = vec![
BackgroundType::Solid(BackgroundColor::black()),
BackgroundType::Solid(BackgroundColor::white()),
BackgroundType::RandomPixels { seed: 0 }, BackgroundType::VerticalGradient {
top: BackgroundColor {
r: 0.1,
g: 0.1,
b: 0.15,
},
bottom: BackgroundColor {
r: 0.9,
g: 0.9,
b: 0.95,
},
},
];
let w = width.max(1);
let h = height.max(1);
BackgroundAugmentor {
types,
width: w,
height: h,
call_count: 0,
base_seed: 0x3141592653589793,
}
}
pub struct BackgroundEnvMap {
pub width: u32,
pub height: u32,
pub pixels: Vec<f32>,
}
impl BackgroundEnvMap {
pub fn from_solid_color(width: u32, height: u32, color: BackgroundColor) -> Self {
let arr = color.to_array();
let len = (width as usize) * (height as usize) * 3;
let mut pixels = Vec::with_capacity(len);
for _ in 0..(width as usize * height as usize) {
pixels.push(arr[0]);
pixels.push(arr[1]);
pixels.push(arr[2]);
}
Self {
width,
height,
pixels,
}
}
pub fn sample(&self, direction: [f32; 3]) -> [f32; 3] {
if self.width == 0 || self.height == 0 {
return [0.0, 0.0, 0.0];
}
let dx = direction[0];
let dy = direction[1];
let dz = direction[2];
let len = (dx * dx + dy * dy + dz * dz).sqrt();
let (ndx, ndy, ndz) = if len > f32::EPSILON {
(dx / len, dy / len, dz / len)
} else {
(0.0, 0.0, -1.0) };
let azimuth = ndx.atan2(ndz); let elevation = ndy.clamp(-1.0, 1.0).asin();
let u = (azimuth + PI) / (2.0 * PI); let v = (elevation + PI / 2.0) / PI;
self.sample_uv_bilinear(u, v)
}
fn sample_uv_bilinear(&self, u: f32, v: f32) -> [f32; 3] {
if self.width == 0 || self.height == 0 {
return [0.0, 0.0, 0.0];
}
let w = self.width as f32;
let h = self.height as f32;
let fx = u * w - 0.5;
let fy = v * h - 0.5;
let x0 = fx.floor() as i64;
let y0 = fy.floor() as i64;
let tx = (fx - fx.floor()).clamp(0.0, 1.0);
let ty = (fy - fy.floor()).clamp(0.0, 1.0);
let width_i = self.width as i64;
let px0 = x0.rem_euclid(width_i) as usize;
let px1 = (x0 + 1).rem_euclid(width_i) as usize;
let py0 = y0.clamp(0, self.height as i64 - 1) as usize;
let py1 = (y0 + 1).clamp(0, self.height as i64 - 1) as usize;
let c00 = self.raw_pixel(px0, py0);
let c10 = self.raw_pixel(px1, py0);
let c01 = self.raw_pixel(px0, py1);
let c11 = self.raw_pixel(px1, py1);
let mut out = [0.0_f32; 3];
for c in 0..3 {
let top = c00[c] * (1.0 - tx) + c10[c] * tx;
let bot = c01[c] * (1.0 - tx) + c11[c] * tx;
out[c] = top * (1.0 - ty) + bot * ty;
}
out
}
#[inline]
fn raw_pixel(&self, px: usize, py: usize) -> [f32; 3] {
let idx = (py * self.width as usize + px) * 3;
[self.pixels[idx], self.pixels[idx + 1], self.pixels[idx + 2]]
}
pub fn render_background(
&self,
width: u32,
height: u32,
fov_x: f32,
camera_rotation: &[f32; 9],
) -> Result<BackgroundImage, BackgroundError> {
if width == 0 || height == 0 {
return Err(BackgroundError::InvalidDimensions(format!(
"render dimensions must be > 0, got {}×{}",
width, height
)));
}
let tan_half_fov_x = (fov_x / 2.0).tan();
let aspect = width as f32 / height as f32;
let tan_half_fov_y = tan_half_fov_x / aspect;
let mut img = BackgroundImage::new(width, height);
for y in 0..height {
let ndc_y = 1.0 - 2.0 * (y as f32 + 0.5) / height as f32;
for x in 0..width {
let ndc_x = 2.0 * (x as f32 + 0.5) / width as f32 - 1.0;
let d_cam_x = ndc_x * tan_half_fov_x;
let d_cam_y = ndc_y * tan_half_fov_y;
let d_cam_z = -1.0_f32;
let r = camera_rotation;
let d_world = [
r[0] * d_cam_x + r[1] * d_cam_y + r[2] * d_cam_z,
r[3] * d_cam_x + r[4] * d_cam_y + r[5] * d_cam_z,
r[6] * d_cam_x + r[7] * d_cam_y + r[8] * d_cam_z,
];
let color = self.sample(d_world);
img.set_pixel(x, y, color);
}
}
Ok(img)
}
}
#[cfg(test)]
mod tests {
use super::*;
const EPS: f32 = 1e-5;
fn approx_eq(a: f32, b: f32, tol: f32) -> bool {
(a - b).abs() <= tol
}
#[test]
fn test_background_color_new_valid() {
let c = BackgroundColor::new(0.2, 0.5, 0.8);
assert!(c.is_ok());
let c = c.expect("validated above");
assert!(approx_eq(c.r, 0.2, EPS));
assert!(approx_eq(c.g, 0.5, EPS));
assert!(approx_eq(c.b, 0.8, EPS));
}
#[test]
fn test_background_color_new_r_out_of_range() {
let result = BackgroundColor::new(1.5, 0.5, 0.5);
assert!(result.is_err(), "expected Err for r > 1, got Ok");
}
#[test]
fn test_background_color_from_hex_red() {
let c = BackgroundColor::from_hex("#FF0000").expect("valid hex");
assert!(approx_eq(c.r, 1.0, 1.0 / 255.0));
assert!(approx_eq(c.g, 0.0, EPS));
assert!(approx_eq(c.b, 0.0, EPS));
}
#[test]
fn test_background_color_from_hex_invalid() {
assert!(BackgroundColor::from_hex("FF0000").is_err()); assert!(BackgroundColor::from_hex("#ZZ0000").is_err()); assert!(BackgroundColor::from_hex("#F00").is_err()); }
#[test]
fn test_background_color_blend_with() {
let a = BackgroundColor::new(0.0, 0.0, 0.0).expect("black");
let b = BackgroundColor::new(1.0, 1.0, 1.0).expect("white");
let blend0 = a.blend_with(b, 0.0);
assert!(approx_eq(blend0.r, 0.0, EPS));
assert!(approx_eq(blend0.g, 0.0, EPS));
assert!(approx_eq(blend0.b, 0.0, EPS));
let blend1 = a.blend_with(b, 1.0);
assert!(approx_eq(blend1.r, 1.0, EPS));
assert!(approx_eq(blend1.g, 1.0, EPS));
assert!(approx_eq(blend1.b, 1.0, EPS));
}
#[test]
fn test_generate_solid() {
let color = BackgroundColor::new(0.3, 0.6, 0.9).expect("valid");
let img =
generate_background(8, 4, &BackgroundType::Solid(color)).expect("solid generation");
for i in 0..32 {
let [r, g, b] = img.pixel(i % 8, i / 8);
assert!(approx_eq(r, 0.3, EPS));
assert!(approx_eq(g, 0.6, EPS));
assert!(approx_eq(b, 0.9, EPS));
}
}
#[test]
fn test_generate_vertical_gradient() {
let top = BackgroundColor::new(0.0, 0.0, 0.0).expect("black");
let bottom = BackgroundColor::new(1.0, 1.0, 1.0).expect("white");
let img = generate_background(4, 16, &BackgroundType::VerticalGradient { top, bottom })
.expect("gradient");
let [r0, g0, b0] = img.pixel(0, 0);
assert!(approx_eq(r0, 0.0, EPS));
assert!(approx_eq(g0, 0.0, EPS));
assert!(approx_eq(b0, 0.0, EPS));
let [r15, g15, b15] = img.pixel(0, 15);
assert!(approx_eq(r15, 1.0, EPS));
assert!(approx_eq(g15, 1.0, EPS));
assert!(approx_eq(b15, 1.0, EPS));
}
#[test]
fn test_generate_radial_gradient_center() {
let inner = BackgroundColor::new(1.0, 0.0, 0.0).expect("red");
let outer = BackgroundColor::new(0.0, 0.0, 1.0).expect("blue");
let w = 21;
let h = 21;
let img = generate_background(w, h, &BackgroundType::RadialGradient { inner, outer })
.expect("radial");
let cx = w / 2;
let cy = h / 2;
let [r, g, b] = img.pixel(cx, cy);
assert!(r > 0.95, "center r={r} not near 1.0");
assert!(g < 0.05, "center g={g} not near 0.0");
assert!(b < 0.05, "center b={b} not near 0.0");
}
#[test]
fn test_generate_checkerboard() {
let ca = BackgroundColor::new(0.0, 0.0, 0.0).expect("black");
let cb = BackgroundColor::new(1.0, 1.0, 1.0).expect("white");
let img = generate_background(
4,
4,
&BackgroundType::Checkerboard {
color_a: ca,
color_b: cb,
cell_size: 1,
},
)
.expect("checkerboard");
let [r00, _, _] = img.pixel(0, 0);
assert!(approx_eq(r00, 0.0, EPS), "pixel(0,0) should be black");
let [r10, _, _] = img.pixel(1, 0);
assert!(approx_eq(r10, 1.0, EPS), "pixel(1,0) should be white");
let [r01, _, _] = img.pixel(0, 1);
assert!(approx_eq(r01, 1.0, EPS), "pixel(0,1) should be white");
}
#[test]
fn test_generate_sky() {
let sky = BackgroundColor::new(0.0, 0.5, 1.0).expect("sky blue");
let horizon = BackgroundColor::new(1.0, 1.0, 1.0).expect("white horizon");
let ground = BackgroundColor::new(0.3, 0.2, 0.1).expect("brown ground");
let h = 64;
let img = generate_background(
4,
h,
&BackgroundType::Sky {
sky_color: sky,
horizon_color: horizon,
ground_color: ground,
},
)
.expect("sky");
let [_r_top, g_top, _b_top] = img.pixel(0, 0);
assert!(
g_top < 0.9,
"top pixel g={g_top} should be near sky blue, not horizon white"
);
let [r_bot, _g_bot, b_bot] = img.pixel(0, h - 1);
assert!(
r_bot > b_bot,
"bottom r={r_bot} should exceed b={b_bot} (ground color)"
);
}
#[test]
fn test_generate_noise_in_range_and_varied() {
let img =
generate_background(16, 16, &BackgroundType::Noise { seed: 12345 }).expect("noise");
let mut all_same = true;
let first = img.pixel(0, 0);
for y in 0..16u32 {
for x in 0..16u32 {
let [r, g, b] = img.pixel(x, y);
assert!((0.0..=1.0).contains(&r), "r={r} out of range");
assert!((0.0..=1.0).contains(&g), "g={g} out of range");
assert!((0.0..=1.0).contains(&b), "b={b} out of range");
if [r, g, b] != first {
all_same = false;
}
}
}
assert!(
!all_same,
"all noise pixels are identical — PRNG not working"
);
}
#[test]
fn test_to_u8_black() {
let img = BackgroundImage::new(4, 4);
let bytes = img.to_u8();
assert!(
bytes.iter().all(|&b| b == 0),
"black image should all be 0u8"
);
}
#[test]
fn test_to_u8_white() {
let img = generate_background(4, 4, &BackgroundType::Solid(BackgroundColor::white()))
.expect("white solid");
let bytes = img.to_u8();
assert!(
bytes.iter().all(|&b| b == 255),
"white image should all be 255u8"
);
}
#[test]
fn test_mean_color_solid() {
let color = BackgroundColor::new(0.4, 0.6, 0.8).expect("valid");
let img = generate_background(32, 32, &BackgroundType::Solid(color)).expect("solid");
let [mr, mg, mb] = img.mean_color();
assert!(approx_eq(mr, 0.4, 1e-4));
assert!(approx_eq(mg, 0.6, 1e-4));
assert!(approx_eq(mb, 0.8, 1e-4));
}
#[test]
fn test_composite_over_fully_opaque() {
let bg = generate_background(2, 2, &BackgroundType::Solid(BackgroundColor::white()))
.expect("white bg");
let fg: Vec<f32> = vec![
1.0, 0.0, 0.0, 1.0, 1.0, 0.0, 0.0, 1.0, 1.0, 0.0, 0.0, 1.0, 1.0,
0.0, 0.0, 1.0,
];
let out = bg.composite_over(&fg).expect("composite");
for i in 0..4 {
assert!(approx_eq(out[i * 3], 1.0, EPS), "R should be 1.0");
assert!(approx_eq(out[i * 3 + 1], 0.0, EPS), "G should be 0.0");
assert!(approx_eq(out[i * 3 + 2], 0.0, EPS), "B should be 0.0");
}
}
#[test]
fn test_composite_over_fully_transparent() {
let bg_color = BackgroundColor::new(0.0, 1.0, 0.0).expect("green");
let bg = generate_background(2, 2, &BackgroundType::Solid(bg_color)).expect("green bg");
let fg: Vec<f32> = vec![
1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0,
0.0, 0.0, 0.0,
];
let _out = bg.composite_over(&fg).expect("composite");
let fg2: Vec<f32> = vec![
0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
0.0, 0.0, 0.0,
];
let out2 = bg.composite_over(&fg2).expect("composite2");
for i in 0..4 {
assert!(approx_eq(out2[i * 3], 0.0, EPS), "R={}", out2[i * 3]);
assert!(
approx_eq(out2[i * 3 + 1], 1.0, EPS),
"G={}",
out2[i * 3 + 1]
);
assert!(
approx_eq(out2[i * 3 + 2], 0.0, EPS),
"B={}",
out2[i * 3 + 2]
);
}
}
#[test]
fn test_composite_over_wrong_size() {
let bg = BackgroundImage::new(4, 4);
let fg = vec![0.0_f32; 4 * 4 * 4 + 1]; assert!(bg.composite_over(&fg).is_err());
}
#[test]
fn test_env_map_sample_forward_direction() {
let env = BackgroundEnvMap::from_solid_color(
16,
8,
BackgroundColor::new(1.0, 0.0, 0.0).expect("red"),
);
let [r, g, b] = env.sample([0.0, 0.0, -1.0]);
assert!(approx_eq(r, 1.0, 1e-3), "r={r}");
assert!(approx_eq(g, 0.0, 1e-3), "g={g}");
assert!(approx_eq(b, 0.0, 1e-3), "b={b}");
}
#[test]
fn test_env_map_sample_uv_wraps_azimuth_seam() {
let mut pixels = Vec::with_capacity(4 * 2 * 3);
for _row in 0..2 {
pixels.extend_from_slice(&[1.0, 0.0, 0.0]); pixels.extend_from_slice(&[0.0, 0.0, 0.0]); pixels.extend_from_slice(&[0.0, 0.0, 0.0]); pixels.extend_from_slice(&[0.0, 0.0, 1.0]); }
let env = BackgroundEnvMap {
width: 4,
height: 2,
pixels,
};
let [r, g, b] = env.sample_uv_bilinear(0.0, 0.5);
assert!(
approx_eq(r, 0.5, 0.05) && approx_eq(b, 0.5, 0.05) && approx_eq(g, 0.0, 1e-5),
"u=0 seam should blend column 3 (blue) with column 0 (red) via \
wraparound, got [{r}, {g}, {b}] (clamp-to-edge would give pure \
red [1, 0, 0])"
);
}
#[test]
fn test_env_map_sample_uv_v_axis_still_clamps() {
let rows: [[f32; 3]; 4] = [
[1.0, 0.0, 0.0],
[0.0, 0.0, 0.0],
[0.0, 0.0, 0.0],
[0.0, 0.0, 1.0],
];
let mut pixels = Vec::with_capacity(2 * 4 * 3);
for row_color in rows {
pixels.extend_from_slice(&row_color); pixels.extend_from_slice(&row_color); }
let env = BackgroundEnvMap {
width: 2,
height: 4,
pixels,
};
let [r, g, b] = env.sample_uv_bilinear(0.5, 0.0);
assert!(
approx_eq(r, 1.0, 0.05) && approx_eq(b, 0.0, 0.05) && approx_eq(g, 0.0, 1e-5),
"v=0 (top edge) should clamp to row 0 (red), not wrap to row 3 \
(blue), got [{r}, {g}, {b}]"
);
}
#[test]
fn test_env_map_render_background_dimensions() {
let env = BackgroundEnvMap::from_solid_color(32, 16, BackgroundColor::white());
let rotation: [f32; 9] = [1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0];
let img = env
.render_background(64, 48, std::f32::consts::PI / 3.0, &rotation)
.expect("render");
assert_eq!(img.width, 64);
assert_eq!(img.height, 48);
assert_eq!(img.pixels.len(), 64 * 48 * 3);
}
#[test]
fn test_augmentor_empty_types_error() {
let result = BackgroundAugmentor::new(vec![], 64, 64, 42);
assert!(result.is_err(), "empty types should be Err");
}
#[test]
fn test_augmentor_sample_valid() {
let mut aug = BackgroundAugmentor::new(
vec![
BackgroundType::Solid(BackgroundColor::white()),
BackgroundType::Solid(BackgroundColor::black()),
],
16,
16,
0,
)
.expect("valid augmentor");
let img = aug.sample().expect("sample");
assert_eq!(img.width, 16);
assert_eq!(img.height, 16);
assert_eq!(img.pixels.len(), 16 * 16 * 3);
assert_eq!(aug.num_calls(), 1);
}
#[test]
fn test_standard_augmentor_multi_sample() {
let mut aug = standard_augmentor(32, 32);
for i in 0..10 {
let img = aug.sample().unwrap_or_else(|_| panic!("sample {i}"));
assert_eq!(img.width, 32);
assert_eq!(img.height, 32);
}
assert_eq!(aug.num_calls(), 10);
}
#[test]
fn test_generate_zero_width_error() {
let result = generate_background(0, 10, &BackgroundType::Solid(BackgroundColor::black()));
assert!(result.is_err(), "width=0 should be Err");
}
#[test]
fn test_background_color_default_is_black() {
let c = BackgroundColor::default();
assert!(approx_eq(c.r, 0.0, EPS));
assert!(approx_eq(c.g, 0.0, EPS));
assert!(approx_eq(c.b, 0.0, EPS));
}
#[test]
fn test_vertical_gradient_height_one() {
let top = BackgroundColor::white();
let bottom = BackgroundColor::black();
let img = generate_background(4, 1, &BackgroundType::VerticalGradient { top, bottom })
.expect("1-row gradient");
let [r, g, b] = img.pixel(0, 0);
assert!(approx_eq(r, 1.0, EPS));
assert!(approx_eq(g, 1.0, EPS));
assert!(approx_eq(b, 1.0, EPS));
}
#[test]
fn test_radial_gradient_one_pixel() {
let inner = BackgroundColor::new(1.0, 0.0, 0.0).expect("red");
let outer = BackgroundColor::new(0.0, 0.0, 1.0).expect("blue");
let img = generate_background(1, 1, &BackgroundType::RadialGradient { inner, outer })
.expect("1×1 radial should not error");
let [r, g, b] = img.pixel(0, 0);
assert!((0.0..=1.0).contains(&r), "r={r} out of range");
assert!((0.0..=1.0).contains(&g), "g={g} out of range");
assert!((0.0..=1.0).contains(&b), "b={b} out of range");
assert!(approx_eq(r, 0.0, 1e-3), "r={r} should be near 0 (outer)");
assert!(
approx_eq(b, 1.0, 1e-3),
"b={b} should be near 1 (outer blue)"
);
}
}