use super::render::{RenderJob, RenderResult}; use super::source::{PreviewLoader, SourceLevel}; use super::{Arc, Deserialize, Hash128, Hasher128, Histogram, Photo, PhotoId, PreviewCache, Rendered, Rgba8, Serialize, Source, SourceInfo}; /// Where a [`QuickJob`]'s stand-in image came from. #[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)] #[serde(rename_all = "camelCase")] pub enum QuickSource { /// The photo's own develop render for its current settings (cached view or thumbnail render). Cached, /// The camera's embedded JPEG (unedited raws). Embedded, /// A thumbnail-level develop render made for the occasion. Small, } /// Something to show *now* for a photo while its real render is prepared. Tried in order: cached /// renders, the embedded preview of an unedited raw, a thumbnail-level render. #[derive(Clone)] pub struct QuickJob { pub request_id: u64, pub photo: PhotoId, pub key: u64, /// Cached renders to try first, in order: (cache, key). pub cached: Vec<(Arc, Hash128)>, /// Embedded preview of an unedited raw: (path, loader, max edge). pub embedded: Option<(String, PreviewLoader, usize)>, /// Import tiles need this many pixels when a render can provide them. `None` accepts any /// camera JPEG immediately (ordinary transient stand-ins and preview-only imports). /// An undersized JPEG remains the fallback if the render fails or is unavailable. pub minimum_embedded_edge: Option, /// Last resort: run this thumbnail job. pub small: Option>, } impl QuickJob { /// The stand-in, from the first source that has one. A panic inside is this job's failure /// (see [`RenderJob::run`]). pub fn run(self) -> RenderResult { let (request_id, photo, key) = (self.request_id, self.photo, self.key); crate::guard::catch("the render", || self.run_unguarded()).unwrap_or_else(|e| RenderResult { request_id, source_key: None, photo, level: SourceLevel::Thumb, key, rendered: Err(e), gpu_fallback: None, loaded: None, quick: None, display: None, frame: None, }) } fn run_unguarded(self) -> RenderResult { let (photo, key) = (self.photo, self.key); let request_id = self.request_id; let done = |rendered: Result, quick| RenderResult { request_id, source_key: None, photo, level: SourceLevel::Thumb, key, rendered, gpu_fallback: None, loaded: None, quick, display: None, frame: None, }; let rendered = |image: Rgba8| { let histogram = Histogram::of_srgb8(&image); Ok(Rendered { image, histogram, deep: None }) }; for (cache, k) in &self.cached { if let Some(img) = cache.get(*k) { return done(rendered(Arc::unwrap_or_clone(img)), Some(QuickSource::Cached)); } } let mut embedded_fallback = None; if let Some((path, loader, edge)) = &self.embedded && let Some(image) = loader(path, *edge) { if self.minimum_embedded_edge.is_none_or(|minimum| image.width.max(image.height) >= minimum) { return done(rendered(image), Some(QuickSource::Embedded)); } embedded_fallback = Some(image); } if let Some(job) = self.small { let r = job.run(); if r.rendered.is_ok() || embedded_fallback.is_none() { return RenderResult { request_id, key, quick: Some(QuickSource::Small), ..r }; } } if let Some(image) = embedded_fallback { return done(rendered(image), Some(QuickSource::Embedded)); } done(Err("no quick preview".into()), None) } } /// What identifies a photo's pixels for caching: its content hash, else its source. /// Which decode of a photo a loaded source is: its file's content and, when it has one, its database lens profile /// ([`lens_key`]; the decode fits the starting colour through it). A job's source is kept only when this still /// matches when the job finishes ([`super::Session::accept`]), so a decode started before a profile change never /// returns to the cache after `cmd::lens_refresh` has dropped the photo's sources. pub fn source_identity(p: &Photo) -> Hash128 { let mut k = Hasher128::new(); k.str(&content_key(p)); let lens = lens_key(p); if lens != 0 { k.u64(lens); } k.finish() } pub fn content_key(p: &Photo) -> String { match (&p.content_hash, &p.source) { (Some(h), _) => h.clone(), (None, Source::Demo { scene }) => format!("demo:{scene}"), (None, Source::File { path }) => format!("file:{path}:{}", p.file_size), } } /// What a photo's lens profile adds to its render and thumbnail keys: a database profile (0 without one), with the /// correction on or off: a raw's starting colour is fitted through the profile's vignetting either way /// (`camera_preview::starting_colour`), so the decoded picture depends on it. When a database update gives the /// photo another profile, its cached renders and thumbnails no longer match. (The file's own correction changes only with the file, which the content /// key covers, so it adds nothing and existing thumbnails stay valid.) pub fn lens_key(p: &Photo) -> u64 { match &p.embedded_lens { Some(l) if l.source == lightcraft_develop::LensSource::Database => { let k = Hasher128::new().str("lens").str(&format!("{l:?}")).finish(); (k.0 as u64) ^ ((k.0 >> 64) as u64) | 1 } _ => 0, } } pub fn source_info(p: &Photo) -> SourceInfo { // Procedural demo scenes are scene-referred HDR (like raw files): use the filmic tone map. // (They are drawn afresh at every size, so they have no original size.) if matches!(p.source, Source::Demo { .. }) { return SourceInfo { raw: true, ..Default::default() }; } // the thumbnail and preview levels are smaller copies: noise reduction works in the original's pixels (#960) let native_long = Some(p.width.max(p.height) as usize).filter(|&n| n > 0); SourceInfo { native_long, ..header_info(p) } } fn header_info(p: &Photo) -> SourceInfo { // A raw shown from its embedded preview is a rendered (display-referred) JPEG: relative white // balance and the display tone curve, like any other rendered file. if p.develops_raw() { let relative = p.relative_wb(); let (temp, tint) = if relative { (6500.0, 0.0) } else { p.as_shot_wb.unwrap_or((5500.0, 0.0)) }; SourceInfo { raw: true, as_shot_temp: temp, as_shot_tint: tint, lens: p.embedded_lens, relative_wb: relative, // (a fitted-look raw edited in Kelvin: its camera's matrices give the scale, see `files::WbScaleOf`) legacy_relative_wb: p.fitted_look() && !relative, ..Default::default() } } else { SourceInfo::default() } }