PhotoCraft: An open-source, photo editor written by scratch in pure Rust
Address CodeRabbit review: budget the Bitmap conversion's working buffers
David Mann901e19d30b6c
diff --git a/crates/engine/src/image_cmds.rs b/crates/engine/src/image_cmds.rsindex 08c165322..839733a57 100644--- a/crates/engine/src/image_cmds.rs+++ b/crates/engine/src/image_cmds.rs@@ -272,6 +272,19 @@ pub(crate) fn check_resample_budget(cmd: &str, doc: &Document, nw: u32, nh: u32,Err(bad(cmd, format!("{nw} x {nh} px is {mp} megapixels; resampling at this bit depth is limited to {max_mp} megapixels. Choose a smaller size{hint}")))}+/// Like [`check_resample_budget`] for a command that, besides resampling the document to+/// `nw` x `nh` px, holds `work_bytes_per_px` more bytes per pixel of full-canvas working buffers+/// (a composite, a mask, an output layer) at once.+pub(crate) fn check_working_budget(cmd: &str, doc: &Document, nw: u32, nh: u32, work_bytes_per_px: u64, hint: &str) -> Result<()> {+ let bpp = (doc.pixel_format().bytes_per_pixel().max(1) as u64).saturating_add(work_bytes_per_px);+ let px = u64::from(nw).saturating_mul(u64::from(nh));+ if px.saturating_mul(bpp) <= MAX_RESAMPLE_BYTES {+ return Ok(());+ }+ let max_mp = MAX_RESAMPLE_BYTES / bpp / 1_000_000;+ Err(bad(cmd, format!("{nw} x {nh} px is {} megapixels; this conversion is limited to {max_mp} megapixels. Choose a smaller size{hint}", px / 1_000_000)))+}+/// Resamples the whole document (pixels, masks, channels, selection, effects, vectors, guides) to/// `nw` x `nh` px with `filter`: the body of Image Size, shared by the Crop tool's W x H x Resolution/// mode. Callers check [`check_resample_budget`] first.diff --git a/crates/engine/src/mode_cmds.rs b/crates/engine/src/mode_cmds.rsindex ce66f63d3..8e658c833 100644--- a/crates/engine/src/mode_cmds.rs+++ b/crates/engine/src/mode_cmds.rs@@ -396,6 +396,10 @@ fn color_table(s: &mut Session, p: &Value) -> Result<Value> {/// Bitmap's Output resolution range (ppi): what a new document may have.const BITMAP_RESOLUTION: std::ops::RangeInclusive<f64> = 1.0..=crate::document_preset_cmds::MAX_RESOLUTION;+/// Bytes per pixel of the Bitmap conversion's full-canvas working buffers at their peak (see+/// `bitmap`), on top of the document itself.+const BITMAP_WORK_BYTES_PER_PX: u64 = 40;+/// Bitmap's Custom Pattern: a pattern's threshold map (see/// [`quantize::custom_pattern_thresholds`]) with its width and height.fn custom_pattern_map(s: &Session, cmd: &str, p: &Value) -> Result<(Vec<f32>, usize, usize)> {@@ -431,7 +435,10 @@ fn bitmap(s: &mut Session, p: &Value) -> Result<Value> {(side(ow), side(oh))};if (nw, nh) != (ow, oh) {- crate::image_cmds::check_resample_budget(CMD, &d.doc, nw, nh, ", or a lower Output resolution")?;+ // Besides the resampled document, the conversion holds an RGBA f32 composite (16 B/px; 32+ // while it is laid over white), the gray plane (4) and the RGBA f32 output (16) at the new+ // size: budget them all before allocating, not just the resample.+ crate::image_cmds::check_working_budget(CMD, &d.doc, nw, nh, BITMAP_WORK_BYTES_PER_PX, ", or a lower Output resolution")?;}let dpi = output as f32;let mut custom = None;diff --git a/crates/engine/src/mode_cmds/tests.rs b/crates/engine/src/mode_cmds/tests.rsindex 84b188bb6..ecea70e55 100644--- a/crates/engine/src/mode_cmds/tests.rs+++ b/crates/engine/src/mode_cmds/tests.rs@@ -470,6 +470,27 @@ fn bitmap_rejects_bad_resolutions_and_patterns_without_editing_the_document() {assert_eq!((doc(&s).size.width, doc(&s).size.height, doc(&s).resolution_dpi), (1, 1, 1.0));}+#[test]+fn bitmap_output_resolution_budgets_the_whole_conversion_not_just_the_resample() {+ // 100 x 100 px at 1 ppi to 200 ppi is 20000 x 20000 px: 400 MP. Resampling an 8/16-bit gray+ // document to it fits the resample budget (under 2 GB), but the conversion's f32 working+ // buffers at that size (~40 B/px, 16 GB) don't: it is refused before anything is allocated.+ for depth in [8, 16] {+ let mut s = ramp(100, 100, depth, 1.0);+ let d = &s.active().unwrap().doc;+ assert!(crate::image_cmds::check_resample_budget("t", d, 20_000, 20_000, "").is_ok(), "{depth}");+ let before = s.active().unwrap().history.past_len();+ let e = s.execute("image.mode.bitmap", json!({"method": "diffusion", "resolution": 200})).unwrap_err().to_string();+ assert!(e.contains("megapixels") && e.contains("Output resolution"), "{depth}: {e}");+ assert_eq!(s.active().unwrap().history.past_len(), before, "{depth}");+ let d = doc(&s);+ assert_eq!((d.mode, d.size.width, d.resolution_dpi), (ColorMode::Grayscale, 100, 1.0), "{depth}");+ // A size that fits is still converted.+ s.execute("image.mode.bitmap", json!({"method": "diffusion", "resolution": 3})).unwrap();+ assert_eq!((doc(&s).size.width, doc(&s).mode), (300, ColorMode::Bitmap), "{depth}");+ }+}+#[test]fn duotone_inks_and_display() {let mut s = session(16, 8, 8, "gray");