PhotoCraft: An open-source, photo editor written by scratch in pure Rust
Address CodeRabbit review: refuse a Bitmap Output size past 300,000 px
David Manncd407f1a05ee
diff --git a/crates/engine/src/mode_cmds.rs b/crates/engine/src/mode_cmds.rsindex 8e658c833..02c1f8b78 100644--- a/crates/engine/src/mode_cmds.rs+++ b/crates/engine/src/mode_cmds.rs@@ -431,8 +431,17 @@ fn bitmap(s: &mut Session, p: &Value) -> Result<Value> {let (nw, nh) = if (output - input).abs() < 1e-6 {(ow, oh)} else {- let side = |v: u32| (f64::from(v) * output / input).round().clamp(1.0, 300_000.0) as u32;- (side(ow), side(oh))+ // Keeping the print size can ask for more pixels than a document may have: refuse that+ // rather than clamp, which would change the print size.+ let max = crate::document_preset_cmds::MAX_DIMENSION;+ let side = |v: u32| {+ let n = (f64::from(v) * output / input).round().max(1.0);+ if n > f64::from(max) {+ return Err(bad(CMD, format!("{n} px across at {output} pixels/inch is more than {max} px; choose a lower Output resolution")));+ }+ Ok(n as u32)+ };+ (side(ow)?, side(oh)?)};if (nw, nh) != (ow, oh) {// Besides the resampled document, the conversion holds an RGBA f32 composite (16 B/px; 32diff --git a/crates/engine/src/mode_cmds/tests.rs b/crates/engine/src/mode_cmds/tests.rsindex ecea70e55..84d816a34 100644--- a/crates/engine/src/mode_cmds/tests.rs+++ b/crates/engine/src/mode_cmds/tests.rs@@ -485,6 +485,16 @@ fn bitmap_output_resolution_budgets_the_whole_conversion_not_just_the_resample()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 side past the 300,000 px document limit is refused, not clamped (that would change+ // the print size): 20000 px at 72 ppi is 333,333 px at 1200 ppi.+ let mut wide = ramp(20_000, 1, depth, 72.0);+ let e = wide.execute("image.mode.bitmap", json!({"method": "threshold", "resolution": 1200})).unwrap_err().to_string();+ assert!(e.contains("300000 px"), "{depth}: {e}");+ assert_eq!((doc(&wide).mode, doc(&wide).size.width), (ColorMode::Grayscale, 20_000), "{depth}");+ // 18000 px to 300,000 px exactly is allowed.+ let mut edge = ramp(18_000, 1, depth, 72.0);+ edge.execute("image.mode.bitmap", json!({"method": "threshold", "resolution": 1200})).unwrap();+ assert_eq!(doc(&edge).size.width, 300_000, "{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}");