Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions contributors/people.toml
Original file line number Diff line number Diff line change
Expand Up @@ -82,3 +82,6 @@ sync_github_name = true
[people.Peaperfish]
real_name = "Yule Wang"
display_name = "Brian Wang"

[people.nkovanen]
sync_github_name = true
8 changes: 5 additions & 3 deletions crates/gpu/src/params.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,9 @@ const FIELDS: &[(&str, usize)] = &[
("VIG_POWER", 1),
("VIG_HL", 1),
("VIG_STYLE", 1),
// the window's offset in the whole output and the whole output's size (pixels)
("VIG_VIEW", 4),
// the vignette's frame (`FinishParams::vig_aff`): render pixel → frame pixel, and the frame's size
("VIG_AFF", 6),
("VIG_SIZE", 2),
("GRAIN", 1),
("GRAIN_AMT", 1),
("GRAIN_SC", 1),
Expand Down Expand Up @@ -220,7 +221,8 @@ pub fn finish_block(fp: &FinishParams, masks: &[[f32; MASK_TERMS]], present: &Pr
p.f("VIG_ASPECT_MIX", v.aspect_mix);
p.f("VIG_POWER", v.power);
p.f("VIG_HL", v.highlights);
p.fs("VIG_VIEW", &fp.view);
p.fs("VIG_AFF", &fp.vig_aff);
p.fs("VIG_SIZE", &fp.vig_size);
p.u(
"VIG_STYLE",
match v.style {
Expand Down
12 changes: 7 additions & 5 deletions crates/gpu/src/wgsl/finish.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -477,15 +477,17 @@ fn main(@builtin(global_invocation_id) gid: vec3<u32>) {

// --- vignette (display linear, post-crop)
if (pu(F_VIG) != 0u) {
// the vignette belongs to the whole output frame, not to the window being drawn
let fw = pf(F_VIG_VIEW + 2u);
let fh = pf(F_VIG_VIEW + 3u);
// the vignette belongs to its frame (the whole output, or the crop), not to the pixels drawn
let fw = pf(F_VIG_SIZE);
let fh = pf(F_VIG_SIZE + 1u);
let aspect = fw / fh;
let amount = pf(F_VIG_AMOUNT);
let mixa = pf(F_VIG_ASPECT_MIX);
let power = pf(F_VIG_POWER);
let u = (pf(F_VIG_VIEW) + f32(x) + 0.5) / fw * 2.0 - 1.0;
let vv = (pf(F_VIG_VIEW + 1u) + f32(y) + 0.5) / fh * 2.0 - 1.0;
let px = f32(x) + 0.5;
let py = f32(y) + 0.5;
let u = (pf(F_VIG_AFF) * px + pf(F_VIG_AFF + 2u) * py + pf(F_VIG_AFF + 4u)) / fw * 2.0 - 1.0;
let vv = (pf(F_VIG_AFF + 1u) * px + pf(F_VIG_AFF + 3u) * py + pf(F_VIG_AFF + 5u)) / fh * 2.0 - 1.0;
let sx = 1.0 + (aspect - 1.0) * mixa;
let sy = 1.0 + (1.0 / aspect - 1.0) * mixa;
let ax = abs(u * max(sx, 1.0) / max(sx, sy));
Expand Down
23 changes: 23 additions & 0 deletions crates/gpu/tests/it/equivalence.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1006,3 +1006,26 @@ fn soft_proofs_match() {
};
assert!(lightcraft_gpu::render(&src, &raw, &DevelopSettings::default(), &req, None).is_none(), "declined: the CPU renders it");
}

/// Issue #3484: the crop tool's uncropped view frames the post-crop vignette by the crop, on the
/// GPU as on the CPU, whole and as a zoomed window, with flips.
#[test]
fn crop_tool_vignette_matches() {
if !gpu() {
return;
}
let src = scene(2, 480, 320);
let mut s = DevelopSettings::default();
s.vignette.amount = -80.0;
s.vignette.midpoint = 20.0;
s.crop.geometry.rect = Rect { x0: 0.12, y0: 0.15, x1: 0.6, y1: 0.75 };
s.crop.geometry.angle = 5.0;
let info = SourceInfo::default();
let req = RenderRequest { apply_crop: false, ..RenderRequest::fit(480, 480) };
check("crop tool vignette", &src, &info, &s, &req);
let window = lightcraft_pipeline::PixelWindow { x: 100, y: 60, w: 240, h: 180 };
check("crop tool vignette, window", &src, &info, &s, &RenderRequest { window: Some(window), ..req });
s.crop.flip_h = true;
s.vignette.roundness = -40.0;
check("crop tool vignette, flip H", &src, &info, &s, &req);
}
20 changes: 13 additions & 7 deletions crates/pipeline/src/finish.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,8 +192,10 @@ pub struct FinishParams {
pub w: usize,
pub h: usize,
pub px_per_long: f64,
/// The window's offset and the whole output's size (pixels): `(0, 0, w, h)` unless windowed.
pub view: [f32; 4],
/// The post-crop vignette's frame ([`Frame::vignette_frame`]): an affine from this render's
/// pixels (centres at +0.5) to that frame's pixels, and the frame's size in them.
pub vig_aff: [f32; 6],
pub vig_size: [f32; 2],
/// Peak linear output relative to SDR white: 1 for SDR renders; `2^max_ev` for an HDR render
/// ([`OutputDepth::F32Hdr`] with HDR on), which keeps values above 1 instead of clipping them.
pub peak: f32,
Expand Down Expand Up @@ -235,6 +237,7 @@ impl FinishParams {
});
let calibration = s.section_enabled("calibration");
let peak = s.hdr.peak();
let (vig_aff, (vig_w, vig_h)) = frame.vignette_frame(s, w, h);
FinishParams {
peak,
calib: if calibration { crate::colorops::calibration_matrix(&s.calibration) } else { None },
Expand Down Expand Up @@ -267,7 +270,8 @@ impl FinishParams {
w,
h,
px_per_long,
view: frame.view.map_or([0.0, 0.0, w as f32, h as f32], |v| [v.x as f32, v.y as f32, v.full_w as f32, v.full_h as f32]),
vig_aff: vig_aff.0.map(|v| v as f32),
vig_size: [vig_w as f32, vig_h as f32],
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
}
}

Expand Down Expand Up @@ -381,8 +385,9 @@ pub(crate) fn finish_with<T: Copy + Default + Send>(
let terms: Vec<[f32; MASK_TERMS]> = p.masks.iter().map(|m| mask_terms(&m.adjust)).collect();
let out_to_norm = fp.out_to_norm;
let long = fp.ow.max(fp.oh);
let [vx, vy, vw, vh] = fp.view;
// the vignette belongs to the whole output frame, not to the window being drawn
// the vignette belongs to its frame (the whole output, or the crop), not to the pixels drawn
let [va, vb, vc, vd, ve, vf] = fp.vig_aff;
let [vw, vh] = fp.vig_size;
let aspect = vw / vh;

let srgb = srgb_lut();
Expand Down Expand Up @@ -437,8 +442,9 @@ pub(crate) fn finish_with<T: Copy + Default + Send>(

// --- vignette (display linear, post-crop)
if let Some(v) = vig {
let u = (vx + x as f32 + 0.5) / vw * 2.0 - 1.0;
let vv = (vy + y as f32 + 0.5) / vh * 2.0 - 1.0;
let (px, py) = (x as f32 + 0.5, y as f32 + 0.5);
let u = (va * px + vc * py + ve) / vw * 2.0 - 1.0;
let vv = (vb * px + vd * py + vf) / vh * 2.0 - 1.0;
let sx = 1.0 + (aspect - 1.0) * v.aspect_mix;
let sy = 1.0 + (1.0 / aspect - 1.0) * v.aspect_mix;
let (ax, ay) = ((u * sx.max(1.0) / sx.max(sy)).abs(), (vv * sy.max(1.0) / sx.max(sy)).abs());
Expand Down
51 changes: 50 additions & 1 deletion crates/pipeline/src/geometry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
//! lens correction and perspective, at the source's size. Everything is sampled in one resample.

use lightcraft_develop::{DevelopSettings, EmbeddedLens};
use lightcraft_geom::{Affine, CropGeometry, Homography, Orientation, Point, Rect};
use lightcraft_geom::{Affine, CropGeometry, Homography, Orientation, Point, Rect, Vec2};
use lightcraft_raster::resample::{Filter, resize};
use lightcraft_raster::{Rgb32f, par_rows};

Expand All @@ -33,6 +33,9 @@ pub struct Frame {
pub warp: Option<Warp>,
/// Set when this frame draws only a window of a larger output (see [`Frame::window`]).
pub view: Option<ViewWindow>,
/// Shows the whole photo whatever its crop (the crop tool's view). The crop is not part of
/// the frame, so dragging it reuses the rendered stages; post-crop effects still follow it.
pub uncropped: bool,
/// Mask coordinates are on the photo before the warp ([`Frame::for_mask`],
/// `lightcraft_develop::Mask::on_photo`); otherwise they are normalized coordinates of this
/// frame's transformed image, like spots and the crop.
Expand Down Expand Up @@ -81,6 +84,7 @@ impl Frame {
flip_v: s.crop.flip_v,
warp: (!warp.is_identity()).then_some(warp),
view: None,
uncropped: !apply_crop,
masks_on_photo: false,
};
if apply_crop {
Expand Down Expand Up @@ -136,6 +140,51 @@ impl Frame {
self.crop.rect = Rect::from_center(r.center(), r.width() * t, r.height() * t);
}

/// The frame the post-crop vignette belongs to, for a `w × h` render of this frame with `s`: an
/// affine from the render's pixels to that frame's pixels, and its size. It is the whole
/// output, of which a windowed render is a part, or, in the crop tool's uncropped view, the
/// crop the edit exports (#3484).
pub fn vignette_frame(&self, s: &DevelopSettings, w: usize, h: usize) -> (Affine, (f64, f64)) {
let (vx, vy, fw, fh) = self.view.map_or((0.0, 0.0, w as f64, h as f64), |v| (v.x, v.y, v.full_w, v.full_h));
let offset = Affine::translate(Vec2::new(vx, vy));
let whole = (offset, (fw, fh));
if !self.uncropped {
return whole;
}
let cropped = self.export_crop(s);
let (cw, ch) = cropped.native_size();
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
// a crop with nothing in it has no frame: the vignette stays on the whole photo
if !(cw > 0.0 && ch > 0.0 && cw.is_finite() && ch.is_finite()) {
return whole;
}
// the whole uncropped output → oriented pixels (flips included) → the crop's own pixels
let to_oriented = Frame { crop: CropGeometry::default(), ..cropped.clone() }.out_to_oriented(fw as usize, fh as usize);
let Some(to_crop) = cropped.crop.output_to_source(self.ow, self.oh, cw, ch).inverse() else { return whole };
let a = to_crop * to_oriented * offset;
if a.0.iter().any(|v| !v.is_finite()) {
return whole;
}
(a, (cw, ch))
}

/// This frame with the crop the cropped render of `s` has ([`crate::frame_for`]): bounded,
/// shrunk inside the corrected lens (and the perspective with Constrain Crop), and shrunk again
/// once the automatic lateral CA estimate, which this frame's warp already holds, is added.
pub(crate) fn export_crop(&self, s: &DevelopSettings) -> Frame {
let mut f = Frame { crop: s.crop.geometry.bounded(self.ow, self.oh), view: None, uncropped: false, ..self.clone() };
let estimated = s.optics.remove_ca && s.section_enabled("optics");
// `Frame::with_lens` constrains before the estimate is added: only the settings' CA then
if estimated && let Some(w) = f.warp.as_mut() {
w.ca = Warp::from_settings(self.ow, self.oh, s, None).ca;
}
f.constrain_to_image(s.geometry.constrain_crop);
if estimated {
f.warp.clone_from(&self.warp);
f.constrain_to_image(s.geometry.constrain_crop);
}
f
}

/// Long edge, in pixels, of the whole output this frame draws (its own size unless it is a
/// window of a larger one): what size-relative effects (noise reduction, edge refinement)
/// must scale with, so a window matches the same pixels of the whole render.
Expand Down
2 changes: 1 addition & 1 deletion crates/pipeline/src/optics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -683,4 +683,4 @@ fn min_filter(p: &Plane, r: usize, max: bool) -> Plane {
}

#[cfg(test)]
mod tests;
pub(crate) mod tests;
2 changes: 1 addition & 1 deletion crates/pipeline/src/optics/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ fn block_coverage_agrees_with_every_pixel() {
}

/// A grid of dark lines on white with lateral CA: red magnified by `ar`, blue by `ab` (about the centre).
fn ca_grid(w: usize, h: usize, ar: f64, ab: f64) -> Rgb32f {
pub(crate) fn ca_grid(w: usize, h: usize, ar: f64, ab: f64) -> Rgb32f {
let (cx, cy) = (w as f64 / 2.0, h as f64 / 2.0);
let val = |x: f64, y: f64| -> f32 {
// smooth-edged discs on a grid (edges in every direction)
Expand Down
Loading