Cotton code
52 lines · 1.7 KB · RustRaw
//! Sizes: exact where the phone says (iOS 27 `dataSize`, Android `SIZE`),//! "about" where it does not (older iPhones), from pixels, kind and length.//! A list whose total holds any estimate says "about".use super::{Item, Kind};/// Bytes and whether they are exact.#[derive(Debug, Clone, Copy, PartialEq, Eq)]pub struct Size {    pub bytes: u64,    pub exact: bool,}impl Size {    pub const ZERO: Size = Size { bytes: 0, exact: true };    pub fn add(self, other: Size) -> Size {        Size { bytes: self.bytes + other.bytes, exact: self.exact && other.exact }    }}/// Bytes a pixel for a photo (HEIC and JPEG as iPhones and Android phones/// write them; a measured average, not a promise), and a Live photo's video.const PHOTO_BYTES_PER_PIXEL: f64 = 0.22;const LIVE_EXTRA: u64 = 2_500_000;const PHOTO_FALLBACK: u64 = 2_500_000;/// Bytes a second for a video by its longest side (HEVC at 30 fps, as phones/// record by default).fn video_rate(longest: u32) -> u64 {    match longest {        0 => 1_000_000,        l if l >= 3000 => 2_900_000,        l if l >= 1900 => 1_000_000,        l if l >= 1200 => 550_000,        _ => 250_000,    }}pub fn of(item: &Item) -> Size {    if let Some(b) = item.bytes {        return Size { bytes: b, exact: true };    }    let bytes = match item.kind {        Kind::Photo => {            let px = item.width as u64 * item.height as u64;            let base = if px == 0 { PHOTO_FALLBACK } else { (px as f64 * PHOTO_BYTES_PER_PIXEL) as u64 };            base + if item.has("live") { LIVE_EXTRA } else { 0 }        }        Kind::Video => video_rate(item.width.max(item.height)) * (item.duration_ms / 1000).max(1),    };    Size { bytes, exact: false }}