Skip to content

LightCraft: An open-source non-destructive photo editor written by scratch in pure Rust

M5.1: full-resolution source level for exports larger than a preview

Brandon Thomas452521a16f3b
diff --git a/crates/engine/examples/render_bench.rs b/crates/engine/examples/render_bench.rs
index b2bb11ec0..2a94b9fa3 100644
--- a/crates/engine/examples/render_bench.rs
+++ b/crates/engine/examples/render_bench.rs
@@ -5,7 +5,8 @@
//!
//! Without a file argument a procedural 6000×4000 source is used. Scenarios: a ~2.5 MP loupe render
//! of the 2560 px preview (cold, and with a warm stage cache while a slider is dragged), a draft,
-//! and a full-size render + JPEG encode (export). Prints minimum wall-clock and minimum process CPU
+//! and a full-size render + JPEG encode (export); `ONLY=batch` with several files times a full-size
+//! export of each (decode + render + encode). Prints minimum wall-clock and minimum process CPU
//! time: on a shared machine the CPU time shows the work done, wall-clock also the wait for cores.
use std::time::Instant;
@@ -131,6 +132,23 @@ fn main() {
});
println!("loupe draft NR drag (warm): {ms}");
}
+ if run("batch") && args.len() > 1 {
+ // full-size JPEG export of all the files given (decode + render + encode)
+ use lightcraft_engine::export::export_photo;
+ let mut session = lightcraft_engine::Session::new().with_fs();
+ let r = session.execute("library.import", &serde_json::json!({"paths": args})).expect("import");
+ let ids: Vec<_> = r["imported"].as_array().expect("ids").iter().filter_map(|v| v.as_u64()).map(lightcraft_engine::catalog::PhotoId).collect();
+ let items: Vec<_> = ids.iter().enumerate().map(|(i, id)| (*id, i + 1)).collect();
+ let o = ExportOptions::default();
+ let ms = best(n.min(3), || {
+ for &(id, seq) in &items {
+ drop(export_photo(&mut session, id, &o, seq).expect("export"));
+ session.media.forget(id); // as in a batch: every original is decoded once
+ }
+ });
+ println!("batch export of {} files (decode+render+JPEG): {ms}", items.len());
+ return;
+ }
if run("export") {
let big = RenderRequest::fit(full.width, full.height);
let ne = n.min(3);
diff --git a/crates/engine/src/media.rs b/crates/engine/src/media.rs
index cac58cc29..8eb66332c 100644
--- a/crates/engine/src/media.rs
+++ b/crates/engine/src/media.rs
@@ -1,7 +1,8 @@
//! Source proxies and render jobs.
//!
//! Sources are decoded (or generated) once per level and cached in memory LRUs: `Thumb` (≤ 512 px
-//! long edge) for the grid and filmstrip, `Preview` (≤ 2560 px) for the loupe. A [`RenderJob`] is
+//! long edge) for the grid and filmstrip, `Preview` (≤ 2560 px) for the loupe, `Full` (native
+//! resolution, the last one only) for exports larger than a preview. A [`RenderJob`] is
//! self-contained and `Send`: frontends run it on a worker thread; if the source wasn't cached yet
//! the job loads it and hands it back in the [`RenderResult`] so the cache can keep it.
//!
@@ -36,6 +37,8 @@ pub const DISK_CACHE_BYTES: u64 = 2 << 30;
pub enum SourceLevel {
Thumb,
Preview,
+ /// The original at its native resolution (exports larger than a preview).
+ Full,
}
impl SourceLevel {
@@ -43,11 +46,16 @@ impl SourceLevel {
match self {
SourceLevel::Thumb => 512,
SourceLevel::Preview => 2560,
+ SourceLevel::Full => usize::MAX,
}
}
/// Smallest level that can serve an output of `long_edge` pixels.
pub fn for_size(long_edge: usize) -> SourceLevel {
- if long_edge <= 520 { SourceLevel::Thumb } else { SourceLevel::Preview }
+ match long_edge {
+ 0..=520 => SourceLevel::Thumb,
+ 521..=2560 => SourceLevel::Preview,
+ _ => SourceLevel::Full,
+ }
}
}
@@ -78,6 +86,8 @@ pub struct MediaCache {
/// Decoded thumbnail-level sources (LRU by bytes).
thumbs: Lru<PhotoId, Arc<Rgb32f>>,
previews: Vec<(PhotoId, Arc<Rgb32f>)>,
+ /// The last full-resolution original (exports; one at a time: ~300 MB at 24 MP).
+ full: Option<(PhotoId, Arc<Rgb32f>)>,
/// How many previews to keep (LRU).
pub preview_capacity: usize,
pub file_loader: Option<FileLoader>,
@@ -92,6 +102,7 @@ impl Default for MediaCache {
MediaCache {
thumbs: Lru::new(THUMB_SOURCE_BYTES),
previews: Vec::new(),
+ full: None,
preview_capacity: 0,
file_loader: None,
file_probe: None,
@@ -111,6 +122,7 @@ impl MediaCache {
match level {
SourceLevel::Thumb => self.thumbs.get(&id).cloned(),
SourceLevel::Preview => self.previews.iter().find(|(p, _)| *p == id).map(|(_, a)| a.clone()),
+ SourceLevel::Full => self.full.as_ref().filter(|(p, _)| *p == id).map(|(_, a)| a.clone()),
}
}
@@ -128,6 +140,7 @@ impl MediaCache {
self.previews.remove(0);
}
}
+ SourceLevel::Full => self.full = Some((id, img)),
}
}
@@ -135,6 +148,9 @@ impl MediaCache {
pub fn forget(&mut self, id: PhotoId) {
self.thumbs.remove(&id);
self.previews.retain(|(p, _)| *p != id);
+ if self.full.as_ref().is_some_and(|(p, _)| *p == id) {
+ self.full = None;
+ }
}
/// (decoded thumbnail sources, bytes).
@@ -146,17 +162,22 @@ impl MediaCache {
if let Some(a) = self.get(p.id, level) {
return SourceRef::Loaded(a);
}
+ // Procedural scenes have a nominal size: "full" is that size, not unbounded.
+ let max_edge = level.max_edge().min(match (&p.source, level) {
+ (Source::Demo { .. }, SourceLevel::Full) => p.width.max(p.height).max(1) as usize,
+ _ => usize::MAX,
+ });
match &p.source {
Source::Demo { scene } => {
if self.scenes.is_empty() {
self.scenes = lightcraft_scenes::demo_library();
}
match self.scenes.iter().find(|s| s.id == *scene) {
- Some(s) => SourceRef::Demo { scene: Box::new(s.clone()), max_edge: level.max_edge() },
- None => SourceRef::File { path: format!("demo:{scene}"), max_edge: level.max_edge(), loader: None },
+ Some(s) => SourceRef::Demo { scene: Box::new(s.clone()), max_edge },
+ None => SourceRef::File { path: format!("demo:{scene}"), max_edge, loader: None },
}
}
- Source::File { path } => SourceRef::File { path: path.clone(), max_edge: level.max_edge(), loader: self.file_loader.clone() },
+ Source::File { path } => SourceRef::File { path: path.clone(), max_edge, loader: self.file_loader.clone() },
}
}
}
@@ -273,11 +294,7 @@ impl crate::Session {
let source = self.media.source_ref(&p, level);
let settings = if before { Arc::new(lightcraft_pipeline::before_settings(&p.develop)) } else { p.develop.clone() };
let request = RenderRequest { max_w, max_h, quality: Quality::Full, apply_crop };
- let key = settings.hash64()
- ^ ((max_w as u64) << 40)
- ^ ((max_h as u64) << 20)
- ^ (apply_crop as u64)
- ^ ((level == SourceLevel::Preview) as u64) << 60;
+ let key = settings.hash64() ^ ((max_w as u64) << 40) ^ ((max_h as u64) << 20) ^ (apply_crop as u64) ^ (level as u64) << 60;
let cache = thumb_bucket.map(|b| {
let k = Hasher128::new().str(&content_key(&p)).u64(settings.hash64()).u64(b as u64).u64(RENDER_CACHE_VERSION).finish();
(self.media.rendered.clone(), k)
@@ -325,3 +342,29 @@ pub struct ProbeInfo {
}
pub type FileProbe = Arc<dyn Fn(&str) -> Result<ProbeInfo, String> + Send + Sync>;
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ #[test]
+ fn levels_cover_sizes_and_large_exports_use_the_original() {
+ assert_eq!(SourceLevel::for_size(256), SourceLevel::Thumb);
+ assert_eq!(SourceLevel::for_size(2560), SourceLevel::Preview);
+ assert_eq!(SourceLevel::for_size(2561), SourceLevel::Full);
+ let mut s = crate::Session::with_demo();
+ let p = s.catalog.photos().next().unwrap().clone();
+ let long = p.width.max(p.height) as usize;
+ assert!(long > 2560, "demo photos are camera-sized");
+ // a full-size export reads the source at its native size, not an upscaled preview
+ let job = s.render_job(p.id, long, long, false, true).unwrap();
+ assert_eq!(job.level, SourceLevel::Full);
+ match &job.source {
+ SourceRef::Demo { max_edge, .. } => assert_eq!(*max_edge, long),
+ _ => panic!("demo source expected"),
+ }
+ let loupe = s.render_job(p.id, 1600, 1600, false, true).unwrap();
+ assert_eq!(loupe.level, SourceLevel::Preview);
+ assert_ne!(loupe.key, s.render_job(p.id, 1600, 1600, false, true).unwrap().draft().key);
+ }
+}