| 1 | use std::path::Path; |
| 2 | |
| 3 | use image::codecs::gif::{GifEncoder, Repeat}; |
| 4 | use image::{Delay, Frame, Rgba, RgbaImage}; |
| 5 | use teams_core::Gif; |
| 6 | |
| 7 | pub const RECEIVED_GIF_KEY: &str = "demo://party-gif"; |
| 8 | pub const RECEIVED_GIF_NAME: &str = "party"; |
| 9 | pub const RECEIVED_GIF_SIZE: (u32, u32) = (200, 150); |
| 10 | const FRAME_COUNT: u32 = 10; |
| 11 | const FRAME_DELAY_MILLIS: u32 = 90; |
| 12 | |
| 13 | struct DemoGif { |
| 14 | name: &'static str, |
| 15 | title: &'static str, |
| 16 | size: (u32, u32), |
| 17 | hue: f32, |
| 18 | } |
| 19 | |
| 20 | const DEMO_GIFS: [DemoGif; 9] = [ |
| 21 | DemoGif { |
| 22 | name: "thumbs-up", |
| 23 | title: "Thumbs up", |
| 24 | size: (160, 160), |
| 25 | hue: 210., |
| 26 | }, |
| 27 | DemoGif { |
| 28 | name: "applause", |
| 29 | title: "Applause", |
| 30 | size: (200, 112), |
| 31 | hue: 30., |
| 32 | }, |
| 33 | DemoGif { |
| 34 | name: RECEIVED_GIF_NAME, |
| 35 | title: "Party", |
| 36 | size: RECEIVED_GIF_SIZE, |
| 37 | hue: 320., |
| 38 | }, |
| 39 | DemoGif { |
| 40 | name: "wave", |
| 41 | title: "Wave", |
| 42 | size: (150, 200), |
| 43 | hue: 150., |
| 44 | }, |
| 45 | DemoGif { |
| 46 | name: "laughing", |
| 47 | title: "Laughing", |
| 48 | size: (180, 135), |
| 49 | hue: 50., |
| 50 | }, |
| 51 | DemoGif { |
| 52 | name: "thank-you", |
| 53 | title: "Thank you", |
| 54 | size: (200, 100), |
| 55 | hue: 0., |
| 56 | }, |
| 57 | DemoGif { |
| 58 | name: "coffee", |
| 59 | title: "Coffee", |
| 60 | size: (140, 140), |
| 61 | hue: 20., |
| 62 | }, |
| 63 | DemoGif { |
| 64 | name: "dancing", |
| 65 | title: "Dancing", |
| 66 | size: (160, 200), |
| 67 | hue: 270., |
| 68 | }, |
| 69 | DemoGif { |
| 70 | name: "well-done", |
| 71 | title: "Well done", |
| 72 | size: (200, 120), |
| 73 | hue: 180., |
| 74 | }, |
| 75 | ]; |
| 76 | |
| 77 | pub fn file_name(name: &str) -> String { |
| 78 | format!("{name}.gif") |
| 79 | } |
| 80 | |
| 81 | /// Writes the demo GIFs into `directory` and describes them like search results, with file paths as URLs. |
| 82 | pub fn generate(directory: &Path) -> Vec<Gif> { |
| 83 | DEMO_GIFS |
| 84 | .iter() |
| 85 | .filter_map(|demo| { |
| 86 | let path = directory.join(file_name(demo.name)); |
| 87 | std::fs::write(&path, encode(demo)).ok()?; |
| 88 | let location = path.to_string_lossy().into_owned(); |
| 89 | Some(Gif { |
| 90 | url: location.clone(), |
| 91 | preview_url: location, |
| 92 | title: demo.title.to_owned(), |
| 93 | width: demo.size.0, |
| 94 | height: demo.size.1, |
| 95 | preview_width: demo.size.0, |
| 96 | preview_height: demo.size.1, |
| 97 | }) |
| 98 | }) |
| 99 | .collect() |
| 100 | } |
| 101 | |
| 102 | fn encode(demo: &DemoGif) -> Vec<u8> { |
| 103 | let mut bytes = Vec::new(); |
| 104 | { |
| 105 | let mut encoder = GifEncoder::new_with_speed(&mut bytes, 30); |
| 106 | let _ = encoder.set_repeat(Repeat::Infinite); |
| 107 | for index in 0..FRAME_COUNT { |
| 108 | let frame = Frame::from_parts( |
| 109 | frame_image(demo, index), |
| 110 | 0, |
| 111 | 0, |
| 112 | Delay::from_numer_denom_ms(FRAME_DELAY_MILLIS, 1), |
| 113 | ); |
| 114 | let _ = encoder.encode_frame(frame); |
| 115 | } |
| 116 | } |
| 117 | bytes |
| 118 | } |
| 119 | |
| 120 | fn frame_image(demo: &DemoGif, index: u32) -> RgbaImage { |
| 121 | let (width, height) = demo.size; |
| 122 | let progress = index as f32 / FRAME_COUNT as f32; |
| 123 | let center_x = width as f32 * (0.5 + 0.3 * (progress * std::f32::consts::TAU).sin()); |
| 124 | let center_y = height as f32 * (0.5 + 0.2 * (progress * std::f32::consts::TAU).cos()); |
| 125 | let radius = width.min(height) as f32 * 0.22; |
| 126 | RgbaImage::from_fn(width, height, |x, y| { |
| 127 | let distance = ((x as f32 - center_x).powi(2) + (y as f32 - center_y).powi(2)).sqrt(); |
| 128 | if distance < radius { |
| 129 | return Rgba([255, 255, 255, 255]); |
| 130 | } |
| 131 | let shade = 0.35 + 0.35 * (y as f32 / height as f32); |
| 132 | let [red, green, blue] = hsv(demo.hue + progress * 40., 0.6, shade + 0.15); |
| 133 | Rgba([red, green, blue, 255]) |
| 134 | }) |
| 135 | } |
| 136 | |
| 137 | fn hsv(hue: f32, saturation: f32, value: f32) -> [u8; 3] { |
| 138 | let hue = hue.rem_euclid(360.) / 60.; |
| 139 | let chroma = value * saturation; |
| 140 | let second = chroma * (1. - (hue % 2. - 1.).abs()); |
| 141 | let (red, green, blue) = match hue as u32 { |
| 142 | 0 => (chroma, second, 0.), |
| 143 | 1 => (second, chroma, 0.), |
| 144 | 2 => (0., chroma, second), |
| 145 | 3 => (0., second, chroma), |
| 146 | 4 => (second, 0., chroma), |
| 147 | _ => (chroma, 0., second), |
| 148 | }; |
| 149 | let offset = value - chroma; |
| 150 | [red, green, blue].map(|channel| ((channel + offset) * 255.).round() as u8) |
| 151 | } |
| 152 | |
| 153 | #[cfg(test)] |
| 154 | mod tests { |
| 155 | use super::*; |
| 156 | |
| 157 | #[test] |
| 158 | fn generated_gifs_are_animated_files_with_their_declared_size() { |
| 159 | let directory = tempfile::tempdir().unwrap(); |
| 160 | let gifs = generate(directory.path()); |
| 161 | assert_eq!(gifs.len(), DEMO_GIFS.len()); |
| 162 | let first = std::fs::read(&gifs[0].url).unwrap(); |
| 163 | assert!(first.starts_with(b"GIF89a")); |
| 164 | assert_eq!( |
| 165 | image::image_dimensions(&gifs[0].url).unwrap(), |
| 166 | (gifs[0].width, gifs[0].height) |
| 167 | ); |
| 168 | } |
| 169 | } |