mirror of
https://github.com/storytold/photocraft.git
synced 2026-11-21 09:26:43 +00:00
Address CodeRabbit review: the hidden-pixel resample refusal suggests Delete Cropped Pixels only when it would help
Delete Cropped Pixels clips pixel layers, fill caches and video frames only. When a layer mask, channel, the quick mask, the selection, type, a shape or a smart object holds the hidden pixels over the budget, the refusal now says so instead of pointing to an option that can't clear it. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
0b36deeca4
commit
79fee53116
2 files changed
+62
-15
No files matched your search
@@ -1,6 +1,7 @@
|
||||
//! Image menu: Image Size, Canvas Size, Crop, Trim, Mode (colour model and
|
||||
//! bit depth) conversions, Duplicate.
|
||||
|
||||
use std::collections::HashSet;
|
||||
use std::convert::Infallible;
|
||||
|
||||
use photocraft_algo::resample::{Resample, crop_surface, largest_resize_rect, resize_surface_in_canvas, scaled_rect, translate_surface};
|
||||
@@ -333,23 +334,39 @@ pub(crate) fn resample_doc(doc: &mut Document, cmd: &str, nw: u32, nh: u32, filt
|
||||
/// `sx` x `sy` with `filter` (masks, channels and the selection with Bilinear): each keeps its
|
||||
/// pixels outside the canvas (a crop that didn't delete them, a layer moved off the canvas), and
|
||||
/// those grow with the scale like the rest. A bicubic shrink also stores the image between its
|
||||
/// two passes, one axis still at its old size ([`largest_resize_rect`]).
|
||||
/// two passes, one axis still at its old size ([`largest_resize_rect`]). The refusal suggests
|
||||
/// Delete Cropped Pixels only when the surfaces over the budget are all ones it clips
|
||||
/// ([`delete_cropped_pixels`]).
|
||||
fn check_hidden_resample_budget(cmd: &str, doc: &mut Document, sx: f64, sy: f64, filter: Resample) -> Result<()> {
|
||||
let new_canvas = scaled_rect(doc.bounds(), sx, sy);
|
||||
// The largest surface by bytes: its width, height and bytes per pixel (a mask has one
|
||||
// channel, the layers the document's).
|
||||
let mut largest = (0u32, 0u32, 1u64);
|
||||
// The surfaces Delete Cropped Pixels clips; masks, channels, the selection, type, shapes and
|
||||
// smart objects keep their pixels outside the crop.
|
||||
let mut clipped: HashSet<*const Surface> = HashSet::new();
|
||||
for_each_layer(&mut doc.layers, &mut |l| {
|
||||
clipped.extend(l.fill_cache.as_ref().map(|fc| std::ptr::from_ref(&fc.surface)));
|
||||
if let Some(v) = &l.video {
|
||||
clipped.extend(v.frames.iter().chain(v.original_frames.iter().flatten()).map(std::ptr::from_ref));
|
||||
}
|
||||
if let LayerContent::Raster(s) = &l.content {
|
||||
clipped.insert(std::ptr::from_ref(s));
|
||||
}
|
||||
});
|
||||
let bytes = |(w, h, bpp): (u32, u32, u64)| u64::from(w).saturating_mul(u64::from(h)).saturating_mul(bpp);
|
||||
// The largest surface by bytes, and the largest one Delete Cropped Pixels doesn't clip: its
|
||||
// width, height and bytes per pixel (a mask has one channel, the layers the document's).
|
||||
let (mut largest, mut largest_kept) = ((0u32, 0u32, 1u64), (0u32, 0u32, 1u64));
|
||||
let mut visit = |surf: &Surface, filter: Resample| {
|
||||
let r = largest_resize_rect(surf.content_bounds(), sx, sy, filter);
|
||||
// What stays within the new canvas is already budgeted.
|
||||
if r.is_empty() || new_canvas.union(&r) == new_canvas {
|
||||
return;
|
||||
}
|
||||
let (w, h) = (r.width(), r.height());
|
||||
let bpp = surf.format().bytes_per_pixel().max(1) as u64;
|
||||
let bytes = |(w, h, bpp): (u32, u32, u64)| u64::from(w).saturating_mul(u64::from(h)).saturating_mul(bpp);
|
||||
if bytes((w, h, bpp)) > bytes(largest) {
|
||||
largest = (w, h, bpp);
|
||||
let this = (r.width(), r.height(), surf.format().bytes_per_pixel().max(1) as u64);
|
||||
if bytes(this) > bytes(largest) {
|
||||
largest = this;
|
||||
}
|
||||
if !clipped.contains(&std::ptr::from_ref(surf)) && bytes(this) > bytes(largest_kept) {
|
||||
largest_kept = this;
|
||||
}
|
||||
};
|
||||
// The surfaces `resample_doc` scales, smart object caches included.
|
||||
@@ -360,17 +377,24 @@ fn check_hidden_resample_budget(cmd: &str, doc: &mut Document, sx: f64, sy: f64,
|
||||
if let Some(sel) = &doc.selection {
|
||||
visit(sel, Resample::Bilinear);
|
||||
}
|
||||
let (w, h, bpp) = largest;
|
||||
let px = u64::from(w).saturating_mul(u64::from(h));
|
||||
if px.saturating_mul(bpp) <= MAX_RESAMPLE_BYTES {
|
||||
let (kept, (w, h, bpp)) = if bytes(largest_kept) > MAX_RESAMPLE_BYTES {
|
||||
(true, largest_kept)
|
||||
} else if bytes(largest) > MAX_RESAMPLE_BYTES {
|
||||
(false, largest)
|
||||
} else {
|
||||
return Ok(());
|
||||
}
|
||||
};
|
||||
let hint = if kept {
|
||||
"choose a smaller size (layer masks, channels, the quick mask, the selection, type, shapes and smart objects keep theirs through Crop with Delete Cropped Pixels)"
|
||||
} else {
|
||||
"choose a smaller size, or delete them first (Crop with Delete Cropped Pixels)"
|
||||
};
|
||||
let max_mp = MAX_RESAMPLE_BYTES / bpp / 1_000_000;
|
||||
Err(bad(
|
||||
cmd,
|
||||
format!(
|
||||
"{w} x {h} px is {} megapixels; resampling at this bit depth is limited to {max_mp} megapixels. The pixels outside the canvas are resampled too: choose a smaller size, or delete them first (Crop with Delete Cropped Pixels)",
|
||||
px / 1_000_000
|
||||
"{w} x {h} px is {} megapixels; resampling at this bit depth is limited to {max_mp} megapixels. The pixels outside the canvas are resampled too: {hint}",
|
||||
u64::from(w).saturating_mul(u64::from(h)) / 1_000_000
|
||||
),
|
||||
))
|
||||
}
|
||||
|
||||
@@ -855,12 +855,35 @@ fn image_size_budgets_the_hidden_pixels_it_resamples() {
|
||||
let past = s.active().unwrap().history.past_len();
|
||||
let err = s.execute("image.imageSize", json!({"width": 20000, "height": 20000})).unwrap_err().to_string();
|
||||
assert!(err.contains("200000 x 200000 px is 40000 megapixels"), "{err}");
|
||||
// The hidden pixels are a pixel layer's: Delete Cropped Pixels would clear them.
|
||||
assert!(err.contains("or delete them first (Crop with Delete Cropped Pixels)"), "{err}");
|
||||
assert_eq!((doc(&s).size, s.active().unwrap().history.past_len()), (Size::new(10, 10), past));
|
||||
// Without resampling only the resolution changes.
|
||||
s.execute("image.imageSize", json!({"resolution": 300, "resample": "none"})).unwrap();
|
||||
assert_eq!((doc(&s).size, doc(&s).resolution_dpi), (Size::new(10, 10), 300.0));
|
||||
}
|
||||
|
||||
/// Delete Cropped Pixels clips pixel layers only: an alpha channel keeps its pixels outside the
|
||||
/// crop, so the refusal doesn't send the user to an option that can't clear it.
|
||||
#[test]
|
||||
fn hidden_resample_budget_names_what_delete_cropped_pixels_keeps() {
|
||||
let mut s = Session::new();
|
||||
s.execute("file.new", json!({"width": 100, "height": 100})).unwrap();
|
||||
s.edit("channel", |doc, _| {
|
||||
let fmt = photocraft_color::PixelFormat::new(ColorMode::Grayscale, doc.pixel_format().sample, false);
|
||||
let mut ch = Surface::new(fmt);
|
||||
ch.write_region(Rect::new(0, 0, 100, 100), &[1.0; 100 * 100]);
|
||||
doc.channels.push(photocraft_doc::AlphaChannel::new("Alpha 1", ch));
|
||||
Ok(())
|
||||
})
|
||||
.unwrap();
|
||||
s.execute("image.crop", json!({"x": 0, "y": 0, "width": 10, "height": 10, "deleteCroppedPixels": true})).unwrap();
|
||||
let err = s.execute("image.imageSize", json!({"width": 20000, "height": 20000})).unwrap_err().to_string();
|
||||
assert!(err.contains("200000 x 200000 px is 40000 megapixels"), "{err}");
|
||||
assert!(err.contains("channels") && !err.contains("delete them first"), "{err}");
|
||||
assert_eq!(doc(&s).size, Size::new(10, 10));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn crop_target_rejects_bad_params_without_changing_the_document() {
|
||||
let rect = json!({"x": 0, "y": 0, "width": 10, "height": 10});
|
||||
|
||||
Reference in new issue
Block a user