Viewer: the eyedropper samples under the pointer in a region of interest (#284)
Effect eyedroppers (other than keyers', which pick from the effect's input) sample the composite the viewer shows; a test covers that on main. But they, and the Info panel, read the shown frame as if it covered the whole comp. With a Region of Interest, or the Extended Viewer of a 3D view, the frame covers only that area, so they read a pixel somewhere else. They now map the point through the area the frame covers (`shown_region`, `frame_pixel`). Tests: the_eyedropper_samples_under_the_pointer_in_a_region_of_interest (ui_effect_controls), eyedropper_samples_straight_colour (fx_widgets). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
cf7263d6c6
commit
065f0c91e7
@@ -185,18 +185,23 @@ pub fn layer_to_comp(l2c: &Mat3, p: [f64; 2]) -> [f64; 2] {
|
||||
[v.x, v.y]
|
||||
}
|
||||
|
||||
/// Straight RGBA (0..1) of the displayed frame at comp point `pt` (the frame may be rendered
|
||||
/// at a reduced resolution: `comp` is the comp size in pixels). `None` outside the frame.
|
||||
pub fn sample_frame(img: &egui::ColorImage, comp: [f32; 2], pt: [f64; 2]) -> Option<[f32; 4]> {
|
||||
if comp[0] <= 0.0 || comp[1] <= 0.0 || pt[0] < 0.0 || pt[1] < 0.0 {
|
||||
/// The premultiplied pixel of the displayed frame at comp point `pt`. The frame covers
|
||||
/// `region` ([x, y, w, h] in comp pixels: the whole comp, or the region of interest or Extended
|
||||
/// Viewer area it was rendered for), maybe at a reduced resolution. `None` outside it.
|
||||
pub fn frame_pixel(img: &egui::ColorImage, region: [f64; 4], pt: [f64; 2]) -> Option<Color32> {
|
||||
let [x, y, w, h] = region;
|
||||
let (fx, fy) = ((pt[0] - x) / w, (pt[1] - y) / h);
|
||||
if !(0.0..1.0).contains(&fx) || !(0.0..1.0).contains(&fy) {
|
||||
return None;
|
||||
}
|
||||
let sx = (pt[0] / comp[0] as f64 * img.size[0] as f64).floor() as usize;
|
||||
let sy = (pt[1] / comp[1] as f64 * img.size[1] as f64).floor() as usize;
|
||||
if sx >= img.size[0] || sy >= img.size[1] {
|
||||
return None;
|
||||
}
|
||||
let c = img.pixels[sy * img.size[0] + sx];
|
||||
let sx = ((fx * img.size[0] as f64).floor() as usize).min(img.size[0].saturating_sub(1));
|
||||
let sy = ((fy * img.size[1] as f64).floor() as usize).min(img.size[1].saturating_sub(1));
|
||||
img.pixels.get(sy.checked_mul(img.size[0])?.checked_add(sx)?).copied()
|
||||
}
|
||||
|
||||
/// Straight RGBA (0..1) of the displayed frame at comp point `pt` ([`frame_pixel`]).
|
||||
pub fn sample_frame(img: &egui::ColorImage, region: [f64; 4], pt: [f64; 2]) -> Option<[f32; 4]> {
|
||||
let c = frame_pixel(img, region, pt)?;
|
||||
let a = c.a() as f32 / 255.0;
|
||||
if a <= 0.0 {
|
||||
return Some([0.0, 0.0, 0.0, 0.0]);
|
||||
@@ -639,12 +644,20 @@ mod tests {
|
||||
img.pixels[4 + 3] = Color32::from_rgba_premultiplied(128, 64, 0, 128); // (3, 1), half alpha
|
||||
img.pixels[0] = Color32::from_rgb(255, 0, 0);
|
||||
// The frame is rendered at half resolution of an 8 × 4 comp.
|
||||
let c = sample_frame(&img, [8.0, 4.0], [7.5, 3.0]).unwrap();
|
||||
let comp = [0.0, 0.0, 8.0, 4.0];
|
||||
let c = sample_frame(&img, comp, [7.5, 3.0]).unwrap();
|
||||
assert!((c[0] - 1.0).abs() < 0.01 && (c[1] - 0.5).abs() < 0.01 && c[2] == 0.0, "{c:?}");
|
||||
assert!((c[3] - 128.0 / 255.0).abs() < 1e-6);
|
||||
assert_eq!(sample_frame(&img, [8.0, 4.0], [0.5, 0.5]), Some([1.0, 0.0, 0.0, 1.0]));
|
||||
assert_eq!(sample_frame(&img, [8.0, 4.0], [8.0, 1.0]), None, "outside");
|
||||
assert_eq!(sample_frame(&img, [8.0, 4.0], [-1.0, 1.0]), None);
|
||||
assert_eq!(sample_frame(&img, comp, [0.5, 0.5]), Some([1.0, 0.0, 0.0, 1.0]));
|
||||
assert_eq!(sample_frame(&img, comp, [8.0, 1.0]), None, "outside");
|
||||
assert_eq!(sample_frame(&img, comp, [-1.0, 1.0]), None);
|
||||
assert_eq!(sample_frame(&img, [0.0, 0.0, 0.0, 4.0], [0.0, 1.0]), None, "an empty region");
|
||||
// A frame of the region of interest (16, 8) to (24, 12) of a bigger comp (#284: it was
|
||||
// read as if it covered the whole comp).
|
||||
let roi = [16.0, 8.0, 8.0, 4.0];
|
||||
assert_eq!(sample_frame(&img, roi, [16.5, 8.5]), Some([1.0, 0.0, 0.0, 1.0]));
|
||||
assert!(sample_frame(&img, roi, [23.5, 11.0]).is_some_and(|c| c[3] > 0.4 && c[3] < 0.6));
|
||||
assert_eq!(sample_frame(&img, roi, [0.5, 0.5]), None, "outside the region");
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
||||
Reference in New Issue
Block a user