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