BlueGit

ict rusty-teams

Files

rusty-teams / crates / app / src / call / demo_scene.rs

demo_scene.rs

demo_scene.rs 3.0 KiB Download Raw
1use calling::VideoPicture;
2use calling::blur::{MASK_HEIGHT, MASK_WIDTH, compose};
3
4pub const SCENE_WIDTH: usize = 640;
5pub const SCENE_HEIGHT: usize = 360;
6const STRIPE: usize = 18;
7const HEAD: (f32, f32, f32, f32) = (0.5, 0.36, 0.11, 0.2);
8const SHOULDERS: (f32, f32, f32, f32) = (0.5, 0.92, 0.3, 0.38);
9const SKIN: [u8; 3] = [226, 172, 140];
10const SHIRT: [u8; 3] = [52, 92, 168];
11
12fn inside(x: f32, y: f32, (center_x, center_y, radius_x, radius_y): (f32, f32, f32, f32)) -> bool {
13 let (dx, dy) = ((x - center_x) / radius_x, (y - center_y) / radius_y);
14 dx * dx + dy * dy <= 1.
15}
16
17fn person_at(x: f32, y: f32) -> Option<[u8; 3]> {
18 if inside(x, y, HEAD) {
19 Some(SKIN)
20 } else if inside(x, y, SHOULDERS) {
21 Some(SHIRT)
22 } else {
23 None
24 }
25}
26
27fn background_at(column: usize, row: usize, tick: usize) -> [u8; 3] {
28 let stripe = ((column + tick) / STRIPE + row / STRIPE).is_multiple_of(2);
29 let shade = (row * 60 / SCENE_HEIGHT) as u8;
30 if stripe { [214 - shade, 120 + shade, 60] } else { [48, 150 - shade, 168 + shade / 2] }
31}
32
33fn scene(tick: usize) -> Vec<u8> {
34 let mut rgba = vec![255u8; SCENE_WIDTH * SCENE_HEIGHT * 4];
35 for row in 0..SCENE_HEIGHT {
36 for column in 0..SCENE_WIDTH {
37 let (x, y) = ((column as f32 + 0.5) / SCENE_WIDTH as f32, (row as f32 + 0.5) / SCENE_HEIGHT as f32);
38 let color = person_at(x, y).unwrap_or_else(|| background_at(column, row, tick));
39 rgba[(row * SCENE_WIDTH + column) * 4..][..3].copy_from_slice(&color);
40 }
41 }
42 rgba
43}
44
45fn person_mask() -> Vec<f32> {
46 (0..MASK_WIDTH * MASK_HEIGHT)
47 .map(|index| {
48 let (x, y) = (((index % MASK_WIDTH) as f32 + 0.5) / MASK_WIDTH as f32, ((index / MASK_WIDTH) as f32 + 0.5) / MASK_HEIGHT as f32);
49 if person_at(x, y).is_some() { 1. } else { 0. }
50 })
51 .collect()
52}
53
54/// A drawn head-and-shoulders scene; the mask comes from the drawing, not from the model.
55pub fn self_view(tick: usize, blur: bool) -> VideoPicture {
56 let mut rgba = scene(tick);
57 if blur {
58 compose(&mut rgba, SCENE_WIDTH, SCENE_HEIGHT, &person_mask());
59 }
60 let bgra = rgba.as_chunks::<4>().0.iter().flat_map(|pixel| [pixel[2], pixel[1], pixel[0], pixel[3]]).collect();
61 VideoPicture { width: SCENE_WIDTH as u32, height: SCENE_HEIGHT as u32, bgra }
62}
63
64#[cfg(test)]
65mod tests {
66 use super::*;
67
68 fn roughness(picture: &VideoPicture, x: usize, y: usize) -> i32 {
69 let at = |column: usize| i32::from(picture.bgra[(y * SCENE_WIDTH + column) * 4]);
70 (0..12).map(|step| (at(x + step + 1) - at(x + step)).abs()).sum()
71 }
72
73 #[test]
74 fn blurring_the_scene_smooths_the_stripes_and_keeps_the_person() {
75 let sharp = self_view(0, false);
76 let blurred = self_view(0, true);
77 assert!(roughness(&blurred, 30, 20) * 3 < roughness(&sharp, 30, 20));
78 let center = (SCENE_HEIGHT / 2 * SCENE_WIDTH + SCENE_WIDTH / 2) * 4;
79 assert_eq!(blurred.bgra[center..center + 3], sharp.bgra[center..center + 3]);
80 }
81}