235 lines · 9.4 KB · RustRaw
//! A synthetic library: no photos and no owner data, every "photo" a small//! grey picture drawn from a seed, with the truth known (which moment, which//! shot is blurred or dark), so the rules can be measured. The same generator//! as the Albums design prototype, plus screenshots, documents, videos,//! WhatsApp copies and photos with no picture on the phone.//!//! Moments: 55% one shot, 25% 2–4, 15% 5–12, 5% 13–25, seconds apart; 20%//! start 30–120 s after the moment before with a different scene; 5% revisit//! an earlier scene hours later. 15% of shots blurred, 6% dark.use super::look::{Grey, PlatformLook};use super::{Item, Kind};pub struct Rng(pub u64);impl Rng { pub fn next(&mut self) -> u64 { self.0 ^= self.0 >> 12; self.0 ^= self.0 << 25; self.0 ^= self.0 >> 27; self.0.wrapping_mul(0x2545_F491_4F6C_DD1D) } pub fn f(&mut self) -> f32 { (self.next() >> 40) as f32 / (1u64 << 24) as f32 } pub fn range(&mut self, a: f32, b: f32) -> f32 { a + (b - a) * self.f() } pub fn int(&mut self, a: u64, b: u64) -> u64 { a + self.next() % (b - a + 1) } pub fn gauss(&mut self) -> f32 { let u = self.f().max(1e-7); let v = self.f(); (-2.0 * u.ln()).sqrt() * (std::f32::consts::TAU * v).cos() }}#[derive(Clone)]struct Scene { grad: (f32, f32, f32), blobs: Vec<(f32, f32, f32, f32)>,}fn scene(r: &mut Rng) -> Scene { let n = r.int(3, 7) as usize; Scene { grad: (r.range(-0.4, 0.4), r.range(-0.4, 0.4), r.range(0.3, 0.7)), blobs: (0..n).map(|_| (r.range(0.0, 32.0), r.range(0.0, 32.0), r.range(2.0, 9.0), r.range(-0.6, 0.6))).collect(), }}/// What the generator knows about one item.pub struct Truth { pub moment: u32, pub blurred: bool, pub dark: bool, /// A WhatsApp copy of a camera photo: that photo's index. pub copy_of: Option<usize>,}pub struct Synth { pub item: Item, /// `None`: the phone had no picture without the network. pub grey: Option<Grey>, pub platform: PlatformLook, pub truth: Truth,}/// Draw one shot of a scene at `w×h` (scene units are 32 across).fn render(s: &Scene, r: &mut Rng, w: u32, h: u32, blurred: bool, dark: bool) -> Grey { let dx = r.range(-1.5, 1.5); let dy = r.range(-1.5, 1.5); let bright = r.range(-0.06, 0.06) - if dark { 0.35 } else { 0.0 }; let moved = if r.f() < 0.3 { Some((r.int(0, s.blobs.len() as u64 - 1) as usize, r.range(-3.0, 3.0), r.range(-3.0, 3.0))) } else { None }; let (wu, hu) = (w as usize, h as usize); let sx = 32.0 / w as f32; let sy = 32.0 / h as f32; let mut img = vec![0f32; wu * hu]; for y in 0..hu { for x in 0..wu { let (fx, fy) = (x as f32 * sx + dx, y as f32 * sy + dy); let mut v = s.grad.2 + s.grad.0 * (fx / 32.0 - 0.5) + s.grad.1 * (fy / 32.0 - 0.5); for (i, b) in s.blobs.iter().enumerate() { let (mut bx, mut by) = (b.0, b.1); if let Some((m, mx, my)) = moved { if m == i { bx += mx; by += my; } } let d2 = (fx - bx).powi(2) + (fy - by).powi(2); v += b.3 * (-d2 / (2.0 * b.2 * b.2)).exp(); } img[y * wu + x] = v + bright + 0.03 * r.gauss(); } } if blurred { // blur proportional to the picture's size: a shaken hand smears // about a 30th of the frame let passes = ((w as f32 / 32.0) * 2.0).round().max(2.0) as usize; for _ in 0..passes { img = box_blur(&img, wu, hu); } } Grey { width: w, height: h, pixels: img.iter().map(|v| (v.clamp(0.0, 1.0) * 255.0).round() as u8).collect() }}fn box_blur(a: &[f32], w: usize, h: usize) -> Vec<f32> { let mut o = vec![0f32; w * h]; for y in 0..h { for x in 0..w { let (mut s, mut n) = (0.0, 0.0); for yy in y.saturating_sub(1)..(y + 2).min(h) { for xx in x.saturating_sub(1)..(x + 2).min(w) { s += a[yy * w + xx]; n += 1.0; } } o[y * w + x] = s / n; } } o}fn shots_in_moment(r: &mut Rng) -> u64 { let p = r.f(); if p < 0.55 { 1 } else if p < 0.80 { r.int(2, 4) } else if p < 0.95 { r.int(5, 12) } else { r.int(13, 25) }}/// Settings for a library.pub struct Options { pub items: usize, pub width: u32, pub height: u32, pub seed: u64, /// Share of photos with no picture on the phone (Optimise Storage). pub cloud_only: f32, /// Add screenshots, documents, videos and WhatsApp copies (about 15% of items). pub others: bool, /// The phone reports exact sizes (iOS 27, Android) or not. pub exact_sizes: bool,}impl Default for Options { fn default() -> Self { Options { items: 64_000, width: 48, height: 36, seed: 0x00C0_77A1_B0A5_5EED, cloud_only: 0.0, others: false, exact_sizes: true } }}/// Make a library, oldest first. Item ids are `syn-<n>`.pub fn library(o: &Options) -> Vec<Synth> { let mut r = Rng(o.seed); let mut out: Vec<Synth> = Vec::with_capacity(o.items); let mut t: i64 = 1_420_070_400_000; // 1 Jan 2015 let mut moment = 0u32; let mut prev: Option<Scene> = None; let mut photos_so_far: Vec<usize> = Vec::new(); while out.len() < o.items { if o.others && r.f() < 0.12 { // one item that is not a camera photo t += (r.f().max(1e-6).ln().abs() * 1_800_000.0) as i64 + 60_000; let k = r.f(); let id = format!("syn-{}", out.len()); let mut item = Item { id, kind: Kind::Photo, taken_at_ms: t, width: 1170, height: 2532, duration_ms: 0, bytes: None, flags: Vec::new(), path: None, camera: None, cloud_id: None, offset_s: None, added_ms: Some(t), generation_added: None, modified: None }; let mut platform = PlatformLook::default(); let s = scene(&mut r); let grey = render(&s, &mut r, o.width, o.height, false, false); if k < 0.45 { item.flags.push("screenshot".into()); item.bytes = Some(r.int(150_000, 900_000)); } else if k < 0.55 { platform.document = Some(true); platform.utility = Some(true); platform.text_chars = Some(r.int(60, 900) as u32); (item.width, item.height) = (3024, 4032); item.bytes = Some(r.int(1_500_000, 3_500_000)); } else if k < 0.75 { item.kind = Kind::Video; (item.width, item.height) = if r.f() < 0.4 { (3840, 2160) } else { (1920, 1080) }; item.duration_ms = if r.f() < 0.3 { r.int(120_000, 900_000) } else { r.int(3_000, 60_000) }; let rate = if item.width >= 3840 { 2_900_000 } else { 1_000_000 }; item.bytes = Some(rate * (item.duration_ms / 1000).max(1)); } else { // a WhatsApp copy: of a recent camera photo (60%) or a forward (item.width, item.height) = (1600, 1200); item.path = Some(format!("IMG-20240101-WA{:04}.jpg", out.len() % 10_000)); item.bytes = Some(r.int(80_000, 400_000)); if let Some(&orig) = photos_so_far.last().filter(|_| r.f() < 0.6) { let g = out[orig].grey.clone(); let reencoded = g.map(|g| Grey { width: g.width, height: g.height, pixels: g.pixels.iter().map(|&p| (p as f32 + 6.0 * r.gauss()).clamp(0.0, 255.0) as u8).collect() }); out.push(Synth { item, grey: reencoded, platform, truth: Truth { moment: u32::MAX, blurred: false, dark: false, copy_of: Some(orig) } }); continue; } } if !o.exact_sizes { item.bytes = None; } out.push(Synth { item, grey: Some(grey), platform, truth: Truth { moment: u32::MAX, blurred: false, dark: false, copy_of: None } }); continue; } let near = r.f() < 0.20; t += if near { r.int(30, 120) as i64 * 1000 } else { (r.f().max(1e-6).ln().abs() * 1_800_000.0) as i64 + 300_000 }; let s = match (&prev, r.f() < 0.05) { (Some(p), true) if !near => p.clone(), _ => scene(&mut r), }; let k = shots_in_moment(&mut r); for _ in 0..k { if out.len() >= o.items { break; } let blurred = r.f() < 0.15; let dark = r.f() < 0.06; let grey = render(&s, &mut r, o.width, o.height, blurred, dark); let cloud = r.f() < o.cloud_only; let id = format!("syn-{}", out.len()); let bytes = if o.exact_sizes { Some(r.int(1_800_000, 4_200_000)) } else { None }; let item = Item { id, kind: Kind::Photo, taken_at_ms: t, width: 4032, height: 3024, duration_ms: 0, bytes, flags: Vec::new(), path: None, camera: Some("Synthetic".into()), cloud_id: None, offset_s: None, added_ms: Some(t), generation_added: None, modified: None }; photos_so_far.push(out.len()); out.push(Synth { item, grey: if cloud { None } else { Some(grey) }, platform: PlatformLook::default(), truth: Truth { moment, blurred, dark, copy_of: None } }); t += r.int(1, 20) as i64 * 1000; } prev = Some(s); moment += 1; } out}