Skip to content

PhotoCraft: An open-source, photo editor written by scratch in pure Rust

Camera Raw: the full-resolution preview of a raw open shows the raw-stage sliders

Unemars8909e1c9483a
diff --git a/crates/raw/src/tone.rs b/crates/raw/src/tone.rs
index 80a80bfdd..2dc594b68 100644
--- a/crates/raw/src/tone.rs
+++ b/crates/raw/src/tone.rs
@@ -555,23 +555,39 @@ pub fn retone(px: &mut [[f32; 4]], w: usize, h: usize, curve: &[[f32; 2]], tone:
let i = table.partition_point(|t| *t < e).clamp(1, N);
let (a, b) = (table[i - 1], table[i]);
let f = if b > a { ((e - a) / (b - a)).clamp(0.0, 1.0) } else { 0.0 };
- ((i - 1) as f32 + f) / N as f32
+ (((i - 1) as f32 + f) / N as f32).powi(2)
};
- let mut lin: Vec<[f32; 3]> = px.iter().map(|p| [0, 1, 2].map(|k| inverse(p[k]).powi(2))).collect();
- if let Some(haze) = Haze::new(w, h, |i| lin[i], tone.dehaze) {
- for (i, l) in lin.iter_mut().enumerate() {
- *l = haze.apply(i % w, i / w, *l);
- }
- }
+ let linear = |p: &[f32; 4]| [inverse(p[0]), inverse(p[1]), inverse(p[2])];
+ // Full-size images (the detail preview develops 16–45 MP): rows in parallel, and the linear
+ // colours recomputed where needed instead of kept as a second copy of the image.
+ let haze = Haze::new(w, h, |i| linear(&px[i]), tone.dehaze);
let y = crate::color::rgb_to_xyz(crate::color::ROMM, crate::color::D50_XY)[1].map(|v| v as f32);
- let lum = |c: &[f32; 3]| y[0] * c[0] + y[1] * c[1] + y[2] * c[2];
- let map = ToneMap::new(w, h, |i| lum(&lin[i]), tone, 0.0);
- for (i, (p, l)) in px.iter_mut().zip(&lin).enumerate() {
- let g = map.as_ref().map_or(1.0, |m| m.gain(i % w, i / w, lum(l)));
- for k in 0..3 {
- p[k] = forward(l[k] * g);
+ let lum_of = |c: &[f32; 3]| y[0] * c[0] + y[1] * c[1] + y[2] * c[2];
+ let dehazed = |i: usize| {
+ let l = linear(&px[i]);
+ haze.as_ref().map_or(l, |hz| hz.apply(i % w, i / w, l))
+ };
+ let rows = crate::par::band_rows(w);
+ let lum: Vec<f32> = crate::par::map(h.div_ceil(rows), |band| {
+ let (y0, y1) = (band * rows, ((band + 1) * rows).min(h));
+ (y0 * w..y1 * w).map(|i| lum_of(&dehazed(i))).collect::<Vec<_>>()
+ })
+ .concat();
+ let map = ToneMap::new(w, h, |i| lum[i], tone, 0.0);
+ let haze = haze.as_ref();
+ crate::par::chunks_mut(&mut px[..w * h], rows * w, |band, chunk| {
+ for (j, p) in chunk.iter_mut().enumerate() {
+ let i = band * rows * w + j;
+ let mut l = [inverse(p[0]), inverse(p[1]), inverse(p[2])];
+ if let Some(hz) = haze {
+ l = hz.apply(i % w, i / w, l);
+ }
+ let g = map.as_ref().map_or(1.0, |m| m.gain(i % w, i / w, lum[i]));
+ for k in 0..3 {
+ p[k] = forward(l[k] * g);
+ }
}
- }
+ });
}
#[cfg(test)]
diff --git a/crates/ui-egui/src/camera_raw_detail_ui.rs b/crates/ui-egui/src/camera_raw_detail_ui.rs
index 7b445f177..48b09c43f 100644
--- a/crates/ui-egui/src/camera_raw_detail_ui.rs
+++ b/crates/ui-egui/src/camera_raw_detail_ui.rs
@@ -22,6 +22,9 @@ type OverlayKey = (u64, bool, PixelRect, crate::camera_raw_scope_ui::ClippingMod
pub(crate) struct DetailPreview {
source: Surface,
+ /// The open-time dialog's camera tone curve: Shadows, Highlights, Clarity, Texture and Dehaze
+ /// then act on the raw data, in linear light before it (see [`develop`]).
+ pub(crate) raw_curve: Option<Arc<Vec<[f32; 2]>>>,
area: PixelRect,
coverage: Coverage,
result: Option<(u64, Surface)>,
@@ -46,6 +49,7 @@ impl DetailPreview {
pub(crate) fn new(source: Surface, area: PixelRect, coverage: Coverage) -> Self {
Self {
source,
+ raw_curve: None,
area,
coverage,
result: None,
@@ -105,6 +109,7 @@ impl DetailPreview {
return;
}
let source = self.source.clone();
+ let raw_curve = self.raw_curve.clone();
let area = self.area;
let coverage = self.coverage.clone();
let mut params = params.clone();
@@ -116,9 +121,10 @@ impl DetailPreview {
if cancel.load(Ordering::Relaxed) {
return;
}
- let result = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| develop(&source, area, &params, &coverage)))
- .map(|surface| (revision, surface))
- .map_err(|_| "Camera Raw detail preview failed; the proxy remains available".into());
+ let result =
+ std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| develop(&source, raw_curve.as_deref().map(Vec::as_slice), area, &params, &coverage)))
+ .map(|surface| (revision, surface))
+ .map_err(|_| "Camera Raw detail preview failed; the proxy remains available".into());
if !cancel.load(Ordering::Relaxed) {
let _ = tx.send(result);
ctx.request_repaint();
@@ -291,8 +297,43 @@ impl DetailPreview {
}
}
-fn develop(source: &Surface, area: PixelRect, params: &CameraRaw, coverage: &Coverage) -> Surface {
- let mut result = photocraft_engine::lens_cmds::camera_raw_surface(source, area, params);
+/// The full-size result of `params` over `area`. With a raw file's tone `curve`, the raw-stage
+/// sliders are applied first to the whole area, from the whole frame's maps as Open computes them
+/// ([`photocraft_io::raw::retone`]), then the rest of the pipeline: at 1:1 the preview shows what
+/// Open will make.
+fn develop(source: &Surface, curve: Option<&[[f32; 2]]>, area: PixelRect, params: &CameraRaw, coverage: &Coverage) -> Surface {
+ let tone = photocraft_io::raw::Tone {
+ shadows: params.shadows,
+ highlights: params.highlights,
+ clarity: params.clarity,
+ texture: params.texture,
+ dehaze: params.dehaze,
+ };
+ let (retoned, params) = match curve {
+ Some(curve) if !tone.is_neutral() => {
+ let (w, h) = (area.width() as usize, area.height() as usize);
+ let mut px = vec![[0.0f32; 4]; w * h];
+ source.read_rgba_into(area, &mut px);
+ photocraft_io::raw::retone(&mut px, w, h, curve, tone);
+ let fmt = source.format();
+ let n = fmt.channels();
+ let mut data = source.read_region(area);
+ for (out, q) in data.chunks_exact_mut(n).zip(&px) {
+ let alpha = if fmt.alpha { out.get(n - 1).copied() } else { None };
+ photocraft_raster::from_rgba_into(&fmt, *q, out);
+ if let (Some(a), Some(o)) = (alpha, out.get_mut(n - 1)) {
+ *o = a;
+ }
+ }
+ let mut out = source.clone();
+ out.write_region(area, &data);
+ let rest = CameraRaw { shadows: 0.0, highlights: 0.0, clarity: 0.0, texture: 0.0, dehaze: 0.0, ..params.clone() };
+ (Some(out), rest)
+ }
+ _ => (None, params.clone()),
+ };
+ let base = retoned.as_ref().unwrap_or(source);
+ let mut result = photocraft_engine::lens_cmds::camera_raw_surface(base, area, &params);
if matches!(coverage, Coverage::None) {
return result;
}
@@ -403,10 +444,47 @@ mod tests {
source.write_region(area, &values);
let params = CameraRaw { exposure: 0.5, texture: 15.0, grain_amount: 30.0, ..Default::default() };
let expected = photocraft_engine::lens_cmds::camera_raw_surface(&source, area, &params);
- assert_eq!(develop(&source, area, &params, &Coverage::None), expected);
+ assert_eq!(develop(&source, None, area, &params, &Coverage::None), expected);
let mask = Surface::with_default(PixelFormat::GRAY8, &[0.0]);
- assert_eq!(develop(&source, area, &params, &Coverage::Selection(mask)), source);
+ assert_eq!(develop(&source, None, area, &params, &Coverage::Selection(mask)), source);
+ }
+ #[test]
+ fn a_raw_open_refines_raw_stage_sliders_from_the_whole_frame() {
+ // Shadows on a raw being opened: the full-size refinement retones the whole frame (as
+ // Open does) and runs the rest of the pipeline after, instead of the filter's Shadows.
+ let area = PixelRect::new(0, 0, 96, 64);
+ let mut source = Surface::new(PixelFormat::RGBA16);
+ let mut values = vec![0.0; 96 * 64 * 4];
+ for (i, p) in values.as_chunks_mut::<4>().0.iter_mut().enumerate() {
+ let v = if (i % 96) < 48 { 0.12 } else { 0.7 };
+ p.copy_from_slice(&[v, v * 0.9, v * 0.8, 1.0]);
+ }
+ source.write_region(area, &values);
+ let curve = [[0.01, 0.01], [0.05, 0.12], [0.3, 0.6], [1.0, 1.0]];
+ let params = CameraRaw { shadows: 80.0, exposure: 0.25, ..Default::default() };
+ let refined = develop(&source, Some(&curve), area, &params, &Coverage::None);
+ let mut px = vec![[0.0f32; 4]; 96 * 64];
+ source.read_rgba_into(area, &mut px);
+ let tone = photocraft_io::raw::Tone { shadows: 80.0, ..Default::default() };
+ photocraft_io::raw::retone(&mut px, 96, 64, &curve, tone);
+ let mut retoned = source.clone();
+ let flat: Vec<f32> = px.iter().flatten().copied().collect();
+ retoned.write_region(area, &flat);
+ let expected = photocraft_engine::lens_cmds::camera_raw_surface(&retoned, area, &CameraRaw { exposure: 0.25, ..Default::default() });
+ let (a, b) = (refined.rgba(10, 30), expected.rgba(10, 30));
+ assert!(a.iter().zip(&b).all(|(x, y)| (x - y).abs() < 2e-4), "{a:?} vs {b:?}");
+ assert!(
+ refined.rgba(10, 30)[0] > source.rgba(10, 30)[0] + 0.02,
+ "the shadow side is lifted: {:?} from {:?}, retoned {:?}",
+ refined.rgba(10, 30),
+ source.rgba(10, 30),
+ retoned.rgba(10, 30)
+ );
+ // Without a raw curve Shadows is the filter's.
+ let filter = develop(&source, None, area, &params, &Coverage::None);
+ assert_eq!(filter, photocraft_engine::lens_cmds::camera_raw_surface(&source, area, &params));
}
+
#[test]
fn stale_refinements_and_failed_old_revisions_do_not_replace_new_settings() {
let source = Surface::with_default(PixelFormat::RGBA8, &[0.2, 0.4, 0.6, 1.0]);
@@ -446,7 +524,7 @@ mod tests {
}
source.write_region(area, &pixels);
let params = CameraRaw { exposure: 0.5, sharpen_amount: 20.0, grain_amount: 30.0, ..Default::default() };
- let full = develop(&source, area, &params, &Coverage::None);
+ let full = develop(&source, None, area, &params, &Coverage::None);
let expected = photocraft_engine::lens_cmds::camera_raw_surface(&source, area, &params);
assert_eq!(full, expected);
let mut preview = DetailPreview::new(source, area, Coverage::None);
diff --git a/crates/ui-egui/src/camera_raw_ui.rs b/crates/ui-egui/src/camera_raw_ui.rs
index d2f78a35c..f49bb5bc3 100644
--- a/crates/ui-egui/src/camera_raw_ui.rs
+++ b/crates/ui-egui/src/camera_raw_ui.rs
@@ -245,6 +245,8 @@ fn open_raw(app: &mut PhotocraftApp, ctx: &egui::Context, raw: RawOpen) -> Resul
};
if let Some(d) = app.camera_raw.as_mut() {
d.layer_name = raw.name.clone();
+ // The full-size preview retones the whole frame with it, as Open will.
+ d.detail.raw_curve = curve.clone().map(std::sync::Arc::new);
d.raw_curve = curve;
d.raw_open = Some(raw);
}