Skip to content

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

Merge PR #677

Claude4ebb934c56ca
diff --cc crates/engine/src/media.rs
index ef569960f,619d52fa2..6f6f0359c
--- a/crates/engine/src/media.rs
+++ b/crates/engine/src/media.rs
@@@ -896,22 -845,17 +897,38 @@@ impl RenderJob
/// the CPU, reusing `stages` either way. Both produce the same image within 1–3 LSB (see
/// `docs/gpu-pipeline.md`); `LIGHTCRAFT_GPU=0` or [`lightcraft_gpu::set_enabled`] forces the CPU.
pub fn develop(src: &Arc<Rgb32f>, info: &SourceInfo, s: &DevelopSettings, req: &RenderRequest, stages: Option<&StageCache>, gpu: bool) -> Rendered {
- let mut rendered = develop_image(src, info, s, req, stages, gpu);
+ develop_reporting(src, info, s, req, stages, gpu).0
+}
+
+/// [`develop`], also saying why the render fell back from the GPU to the CPU when it did (`None`:
+/// it rendered on the GPU, or the GPU was not tried). The reason is this render's own, not the
+/// process-wide [`lightcraft_gpu::last_fallback`].
+pub fn develop_reporting(
+ src: &Arc<Rgb32f>,
+ info: &SourceInfo,
+ s: &DevelopSettings,
+ req: &RenderRequest,
+ stages: Option<&StageCache>,
+ gpu: bool,
++) -> (Rendered, Option<String>) {
++ let (mut rendered, fallback) = develop_image(src, info, s, req, stages, gpu);
+ // an HDR edit's preview is its SDR rendition: its histogram shows the HDR range instead
+ if let Some(h) = lightcraft_pipeline::hdr_histogram(src, info, s, req) {
+ rendered.histogram = h;
+ }
- rendered
++ (rendered, fallback)
+ }
+
-fn develop_image(src: &Arc<Rgb32f>, info: &SourceInfo, s: &DevelopSettings, req: &RenderRequest, stages: Option<&StageCache>, gpu: bool) -> Rendered {
++fn develop_image(
++ src: &Arc<Rgb32f>,
++ info: &SourceInfo,
++ s: &DevelopSettings,
++ req: &RenderRequest,
++ stages: Option<&StageCache>,
++ gpu: bool,
+) -> (Rendered, Option<String>) {
// LUT profiles have no GPU stage: they render on the CPU
- if gpu
+ let fallback = if gpu
&& !lightcraft_pipeline::lut::is_lut_profile(&s.profile.id)
&& !s.masks.iter().any(|m| m.visible && m.refine > 0.0)
&& req.proof.is_none()
diff --cc crates/pipeline/src/lib.rs
index 88e39778d,a613b2be4..6fbb156e8
--- a/crates/pipeline/src/lib.rs
+++ b/crates/pipeline/src/lib.rs
@@@ -468,6 -459,36 +468,37 @@@ pub fn settings_for<'a>(s: &'a DevelopS
if req.depth == OutputDepth::F32Hdr { std::borrow::Cow::Borrowed(s) } else { s.sdr_rendition() }
}
+ /// Long edge (pixels) of the small HDR render [`hdr_histogram`] measures.
+ pub const HDR_HISTOGRAM_EDGE: usize = 384;
+
+ /// The histogram of an HDR edit's HDR rendition for a preview request `req`: a small float HDR
+ /// render of the same framing ([`HDR_HISTOGRAM_EDGE`]), binned with SDR on the left half and the
+ /// whole HDR range ([`Hdr::MAX_EV_LIMIT`](lightcraft_develop::Hdr::MAX_EV_LIMIT) stops above SDR
+ /// white, whatever the headroom limit, so the scale stays put) on the right ([`Histogram::of_hdr`]). A preview of
+ /// an HDR edit shows its SDR rendition; this says how far its highlights go. `None` for SDR edits,
+ /// high-bit-depth, windowed and proofed requests (their histogram stays the image's).
+ pub fn hdr_histogram(src: &Rgb32f, info: &SourceInfo, s: &DevelopSettings, req: &RenderRequest) -> Option<Histogram> {
- if !s.hdr.enabled || req.depth != OutputDepth::U8 || req.window.is_some() || req.proof.is_some() {
++ // a drag's drafts keep their own histogram: a second render per tick would slow them down
++ if !s.hdr.enabled || req.depth != OutputDepth::U8 || req.window.is_some() || req.proof.is_some() || req.quality == Quality::Draft {
+ return None;
+ }
+ let k = (HDR_HISTOGRAM_EDGE as f64 / req.max_w.max(req.max_h).max(1) as f64).min(1.0);
+ let size = |v: usize| ((v as f64 * k).round() as usize).max(1);
+ let hreq = RenderRequest {
+ max_w: size(req.max_w),
+ max_h: size(req.max_h),
+ depth: OutputDepth::F32Hdr,
+ overlay: Overlay::None,
+ proof: None,
+ window: None,
+ display: None,
+ ..*req
+ };
+ let deep = render(src, info, s, &hreq).deep?;
+ let DeepSamples::F32(v) = &deep.samples else { return None };
+ Some(Histogram::of_hdr(v.as_chunks::<3>().0, lightcraft_develop::Hdr::MAX_EV_LIMIT as f32))
+ }
+
/// Render `src` with settings `s`.
pub fn render(src: &Rgb32f, info: &SourceInfo, s: &DevelopSettings, req: &RenderRequest) -> Rendered {
render_impl(Src::Borrowed(src), info, s, req, None)
diff --cc crates/raster/src/histogram.rs
index 300fa273b,4aa47d381..75667bd1c
--- a/crates/raster/src/histogram.rs
+++ b/crates/raster/src/histogram.rs
@@@ -11,6 -15,34 +15,37 @@@ pub struct Histogram
pub b: Vec<u32>,
pub luma: Vec<u32>,
pub total: u32,
+ /// HDR histograms: the first bin above SDR white, and the stops above SDR white the last bin
+ /// reaches. `None` for SDR histograms (every bin is SDR).
+ #[serde(skip_serializing_if = "Option::is_none")]
+ pub hdr: Option<HdrBins>,
+ }
+
+ /// The HDR part of a [`Histogram`]: bins `from..256` cover `0..stops` stops above SDR white.
+ #[derive(Clone, Copy, Debug, PartialEq, Serialize)]
+ #[serde(rename_all = "camelCase")]
+ pub struct HdrBins {
+ pub from: usize,
+ pub stops: f32,
+ }
+
+ impl HdrBins {
+ /// The bin of a linear value (1 = SDR white; `NaN` and negatives count as black).
+ pub fn bin(self, v: f32) -> usize {
+ let last = Histogram::BINS - 1;
++ // a split outside 2..=last would leave one side without bins (or underflow)
++ let from = self.from.clamp(2, last);
+ if v.is_nan() || v <= 0.0 {
+ return 0;
+ }
+ if v <= 1.0 {
+ let e = lightcraft_color::transfer::linear_to_srgb(v);
- return ((e * (self.from - 1) as f32 + 0.5) as usize).min(self.from - 1);
++ return ((e * (from - 1) as f32 + 0.5) as usize).min(from - 1);
+ }
- let t = (v.log2() / self.stops.max(1e-3)).clamp(0.0, 1.0);
- (self.from + (t * (last - self.from) as f32 + 0.5) as usize).min(last)
++ let stops = if self.stops.is_finite() { self.stops.max(1e-3) } else { 1e-3 };
++ let t = (v.log2() / stops).clamp(0.0, 1.0);
++ (from + (t * (last - from) as f32 + 0.5) as usize).min(last)
+ }
}
impl Histogram {
@@@ -83,5 -193,41 +196,53 @@@ mod tests
assert!((h.mean_luma() - 0.5).abs() < 1e-3);
assert_eq!(h.percentile(0.25), 0.0);
assert_eq!(h.percentile(0.75), 1.0);
+ assert_eq!(h.above_sdr(), 0.0);
+ }
+
+ #[test]
+ fn hdr_bins_split_sdr_and_stops() {
+ let px = [[0.0, 0.0, 0.0], [1.0, 1.0, 1.0], [4.0, 4.0, 4.0], [16.0, 16.0, 16.0], [f32::NAN, -1.0, f32::INFINITY]];
+ let h = Histogram::of_hdr(&px, 4.0);
+ let b = h.hdr.unwrap();
+ assert_eq!(h.total, 5);
+ assert_eq!(b.bin(1.0), b.from - 1, "SDR white is the last SDR bin");
+ assert_eq!(b.bin(16.0), 255);
+ assert_eq!(b.bin(1e9), 255);
+ let two = b.bin(4.0);
+ assert!(two > b.from && two < 255, "{two}");
+ assert_eq!(h.r[0], 2, "black and NaN");
+ assert_eq!(h.b[255], 2, "16 and +inf");
+ assert!((h.above_sdr() - 0.6).abs() < 1e-6, "{}", h.above_sdr());
+ assert_eq!(Histogram::of_hdr(&[], 4.0).above_sdr(), 0.0);
+ }
+
+ #[test]
+ fn words_round_trip_and_reject_garbage() {
+ let img = Rgba8::from_fn(8, 8, |x, y| [(x * 30) as u8, (y * 30) as u8, 9, 255]);
+ let sdr = Histogram::of_srgb8(&img);
+ assert_eq!(Histogram::from_words(&sdr.to_words()), Some(sdr.clone()));
+ let hdr = Histogram::of_hdr(&[[0.5, 2.0, 9.0]], 4.0);
+ assert_eq!(Histogram::from_words(&hdr.to_words()), Some(hdr));
+ // the older layout (no HDR words) is an SDR histogram
+ assert_eq!(Histogram::from_words(&sdr.to_words()[..1025]), Some(sdr.clone()));
+ assert_eq!(Histogram::from_words(&[1, 2, 3]), None);
+ let mut bad = sdr.to_words();
+ bad[1025] = 9999; // first HDR bin out of range: SDR
+ bad[1026] = f32::NAN.to_bits();
+ assert_eq!(Histogram::from_words(&bad).unwrap().hdr, None);
+ bad[1025] = 100; // NaN stops fall back to something drawable
+ assert_eq!(Histogram::from_words(&bad).unwrap().hdr.unwrap().stops, 1.0);
+ }
++ /// A hand-built split outside the bins (any `from`, any `stops`) never panics and stays in range.
++ #[test]
++ fn hdr_bins_with_any_split_stay_in_range() {
++ for from in [0, 1, 2, 128, 255, 256, usize::MAX] {
++ for stops in [0.0, 3.0, f32::NAN, f32::INFINITY] {
++ let b = HdrBins { from, stops };
++ for v in [f32::NAN, -1.0, 0.0, 0.5, 1.0, 2.0, 1e9] {
++ assert!(b.bin(v) < Histogram::BINS, "{from} {stops} {v}");
++ }
++ }
++ }
+ }
}
diff --cc docs/parity-checklist.md
index 26a8f3964,ce2eddccd..6e3679685
--- a/docs/parity-checklist.md
+++ b/docs/parity-checklist.md
@@@ -362,11 -360,11 +362,11 @@@ differences and missing bindings are i
| Id | Feature | Tier | Status | Evidence | Missing / notes |
|---|---|---|---|---|---|
-| LR-HDR-EDIT | HDR editing | P2 | 🟑 | `cmd:develop.hdr`, `crates/pipeline/src/tone.rs` (`ToneMap::hdr`), `crates/pipeline/src/tests_hdr.rs` | HDR tone map (SDR curve through the midtones, highlights up to the headroom limit) and float HDR render, CPU only; no on-screen HDR display yet (LR-VIEW-HDR-DISPLAY); HDR settings not yet read from crs XMP |
+| LR-HDR-EDIT | HDR editing | P2 | 🟑 | `cmd:develop.hdr`, `crates/pipeline/src/tone.rs` (`ToneMap::hdr`), `crates/pipeline/src/tests_hdr.rs` | HDR tone map (SDR curve through the midtones, highlights up to the headroom limit) and float HDR render, CPU only; no on-screen HDR display yet (LR-VIEW-HDR-DISPLAY); `crs:HDREditMode` from XMP / Lightroom catalogs turns HDR editing on or off (the headroom limit and SDR rendition aren't read from crs yet) |
| LR-HDR-SDRPREVIEW | SDR preview of HDR | P2 | 🟑 | `ctl:hdr.sdr*` | SDR rendition sliders (brightness, contrast, highlights, shadows, whites, clarity) used by every SDR view and export; no separate SDR-preview toggle until HDR display lands |
| LR-HDR-VISUALIZE | Visualize HDR range | P2 | βœ… | `cmd:view.visualizeHdr`, `crates/pipeline/src/visualize.rs` (`hdr_range`) | grey below SDR white, four colour bands by stops above it |
- | LR-HDR-LIMIT | HDR headroom limit | P2 | βœ… | `ctl:hdr.maxEv`, `cmd:develop.hdr` | 0 to 5 stops |
+ | LR-HDR-LIMIT | HDR headroom limit | P2 | βœ… | `ctl:hdr.maxEv`, `cmd:develop.hdr` | 0 to 5 stops; marked on the HDR histogram (whose scale stays at 5 stops) |
-| LR-HDR-EXPORT | HDR export | P2 | 🟑 | `cmd:app.export`, `crates/codecs/src/gainmap.rs`, `crates/engine/src/export.rs` (`encode_gain_map_jpeg`) | `hdr` export option: ISO 21496-1 gain map JPEG (plus Adobe hdrgm and Apple XMP), 10-bit Rec. 2020 PQ AVIF, 32-bit float TIFF; no HLG yet; no HEIC (no permissive HEVC encoder) |
+| LR-HDR-EXPORT | HDR export | P2 | 🟑 | `cmd:app.export`, `crates/codecs/src/gainmap.rs`, `crates/engine/src/export.rs` (`encode_gain_map_jpeg`) | `hdr` export option: ISO 21496-1 gain map JPEG (plus Adobe hdrgm and Apple XMP), 10-bit Rec. 2020 PQ AVIF, 16-bit Rec. 2020 PQ PNG (`cICP` + `cLLI`; `crates/codecs/src/encode.rs` `encode_png_pq`), 16-bit Rec. 2020 PQ JPEG XL, 32-bit float TIFF; no HLG yet; no HEIC (no permissive HEVC encoder) |
## R. Video (VID)