Skip to content

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

Merge main into #213 (camera_preview: main's proxy edge/size with the sensor_proxy fallback)

Brandon Thomase574839724e8
diff --cc crates/engine/src/camera_preview.rs
index b1ed392b7,f01206411..560c920d2
--- a/crates/engine/src/camera_preview.rs
+++ b/crates/engine/src/camera_preview.rs
@@@ -39,27 -73,36 +73,44 @@@ fn proxies(raw: &RawImage, bytes: &[u8]
return None;
}
// Fixed, bounded proxy: the selected look cannot depend on thumbnail/export resolution.
- let k = (crop.width.max(crop.height).div_ceil(384).max(2)).div_ceil(2) * 2;
- let sensor = sensor_proxy(raw, k)?;
- let mut sensor = fit(&sensor, 96, 96, Filter::Box);
+ let k = (crop.width.max(crop.height).div_ceil(edge as usize).max(2)).div_ceil(2) * 2;
- let sensor = raw.develop_binned(k, 0.99).ok()??;
++ let sensor = sensor_proxy(raw, k, edge as usize)?;
+ let mut sensor = fit(&sensor, size, size, Filter::Box);
let reference = fit(&reference, sensor.width, sensor.height, Filter::Box);
let gain = 2f32.powf(transform.baseline_exposure as f32);
sensor.map_in_place(|p| transform.matrix.apply_f32(std::array::from_fn(|i| p[i] * transform.wb[i] * gain)));
- let look = fit_pairs(&sensor, &reference)?;
- if lightcraft_pipeline::profiling() {
- eprintln!("[profile] {:?} camera look: {:?}, {:?}", raw.format, look.matrix.0, look.tone);
+ Some((sensor, reference))
+ }
+
+ /// Colour training pairs of one raw for a camera profile: white-balanced camera RGB (with the
+ /// baseline exposure) → its camera JPEG (linear Rec.2020). `None` for formats with their own
+ /// colour matrices, other files or unusable previews.
+ pub(crate) fn profile_pairs(raw: &RawImage, bytes: &[u8]) -> Option<Vec<([f64; 3], [f64; 3])>> {
+ if !file_local_look(raw.format) || lightcraft_raw::color::has_matrix(&raw.color) {
+ return None;
}
- Some(look)
+ let transform = lightcraft_raw::color::camera_transform(raw, lightcraft_raw::color::as_shot_white_xy(raw));
+ let (sensor, reference) = proxies(raw, bytes, &transform, PROFILE_PROXY)?;
+ let to_camera = transform.matrix.inverse()?;
+ let (pairs, _) = collect_pairs(&sensor, &reference)?;
+ Some(pairs.into_iter().map(|(x, y)| (to_camera.apply(x), y)).collect())
+ }
+
+ /// A camera profile's colour model (matrix from white-balanced camera RGB, hue/saturation table)
+ /// fitted to pairs pooled from many photos.
+ pub(crate) fn fit_profile(pairs: &[([f64; 3], [f64; 3])]) -> Option<(Mat3, Option<HsvTable>)> {
+ let matrix = fit_matrix(pairs)?;
+ Some((matrix, fit_hue_sat(pairs, &matrix)))
}
- fn sensor_proxy(raw: &RawImage, k: usize) -> Option<Rgb32f> {
++fn sensor_proxy(raw: &RawImage, k: usize, edge: usize) -> Option<Rgb32f> {
+ Some(match raw.develop_binned(k, 0.99).ok()? {
+ Some(sensor) => sensor,
- None if raw.cpp == 3 && raw.cfa.is_none() => fit(&raw.develop(lightcraft_raw::Method::Bilinear).ok()?, 384, 384, Filter::Box),
++ None if raw.cpp == 3 && raw.cfa.is_none() => fit(&raw.develop(lightcraft_raw::Method::Bilinear).ok()?, edge, edge, Filter::Box),
+ None => return None,
+ })
+}
+
/// A fit must cut the held-out squared error to below this share of the fallback's.
const MIN_IMPROVEMENT: f64 = 0.7;
/// ...and stay within this per-channel RMS of the camera JPEG (linear display values). On public
@@@ -213,35 -511,6 +519,35 @@@ fn fit_tone(mut pairs: Vec<(f64, f64)>
#[cfg(test)]
mod tests {
use super::*;
+
+ #[test]
+ fn linear_arw_gets_a_sensor_proxy_without_demosaicing() {
+ use lightcraft_raw::{BlackLevel, ColorData, OpcodeLists, Orientation, RawData, RawFormat, Rect};
+ let mut raw = RawImage {
+ format: RawFormat::Arw,
+ width: 32,
+ height: 32,
+ cpp: 3,
+ data: RawData::F32([0.2, 0.3, 0.4].repeat(32 * 32)),
+ cfa: None,
+ bits: 16,
+ black: BlackLevel::uniform(0.0),
+ white: vec![1.0],
+ active_area: Rect::new(0, 0, 32, 32),
+ crop: Rect::new(0, 0, 32, 32),
+ orientation: Orientation::from_exif(1),
+ color: ColorData::default(),
+ wb_multipliers: Some([1.0; 3]),
+ linearized: true,
+ opcodes: OpcodeLists::default(),
+ metadata: lightcraft_meta::Metadata::default(),
+ };
- let proxy = sensor_proxy(&raw, 2).unwrap();
++ let proxy = sensor_proxy(&raw, 2, 384).unwrap();
+ assert_eq!((proxy.width, proxy.height), (32, 32));
+ assert_eq!(proxy.data[0], [0.2, 0.3, 0.4]);
+ raw.data = RawData::F32(Vec::new());
- assert!(sensor_proxy(&raw, 2).is_none());
++ assert!(sensor_proxy(&raw, 2, 384).is_none());
+ }
#[test]
fn separates_nonlinear_tone_from_colour_and_keeps_sensor_headroom() {
let known = Mat3([[1.8, -0.4, -0.1], [-0.2, 1.5, -0.1], [-0.05, -0.3, 1.7]]);