PhotoCraft: An open-source, photo editor written by scratch in pure Rust
Image: Trim and merged Duplicate report an error on documents over 2^30 px (#7223)
David Mannb450baa6eb9f
diff --git a/crates/engine/src/image_cmds.rs b/crates/engine/src/image_cmds.rsindex 3e870aa68..6d4970ecc 100644--- a/crates/engine/src/image_cmds.rs+++ b/crates/engine/src/image_cmds.rs@@ -273,6 +273,17 @@ fn bad(cmd: &str, msg: impl Into<String>) -> EngineError {EngineError::BadParams { cmd: cmd.into(), msg: msg.into() }}+/// The flattened document, or an error when the canvas is over the compositor's render cap+/// ([`photocraft_compose::MAX_RENDER_PIXELS`]; `flatten` returns an empty buffer then).+fn flatten_whole(doc: &Document, cmd: &str) -> Result<photocraft_compose::Buffer> {+ let buf = photocraft_compose::flatten(doc);+ let (w, h) = (doc.size.width, doc.size.height);+ if buf.px.len() as u64 != u64::from(w) * u64::from(h) {+ return Err(bad(cmd, format!("the document ({w} x {h} px) is too large to flatten (limit {} pixels)", photocraft_compose::MAX_RENDER_PIXELS)));+ }+ Ok(buf)+}+/// Moves every surface, channel, selection and guide by `(dx, dy)`.////// Fails, before moving anything, when some pixels would land outside the i32 coordinate@@ -650,11 +661,11 @@ fn rotated_crop(s: &mut Session, r: Rect, angle: f64, delete: bool, content_awarfn trim(s: &mut Session, p: &Value) -> Result<Value> {let d = s.active().ok_or(EngineError::NoDocument)?;let canvas = d.doc.bounds();- let buf = photocraft_compose::flatten(&d.doc);+ let buf = flatten_whole(&d.doc, "image.trim")?;let based = p.get("basedOn").and_then(Value::as_str).unwrap_or("transparent");let w = canvas.width() as usize;let reference = match based {- "topLeft" => Some(buf.px[0]),+ "topLeft" => buf.px.first().copied(),"bottomRight" => buf.px.last().copied(),_ => None,};@@ -729,7 +740,7 @@ fn duplicate(s: &mut Session, p: &Value) -> Result<Value> {doc.id = photocraft_doc::DocId::fresh();doc.name = p.get("name").and_then(Value::as_str).map(str::to_string).unwrap_or_else(|| format!("{} copy", d.doc.name));if p.get("mergedOnly").and_then(Value::as_bool).unwrap_or(false) {- let buf = photocraft_compose::flatten(&doc);+ let buf = flatten_whole(&doc, "image.duplicate")?;let fmt = doc.pixel_format();let mut surf = Surface::new(fmt);let vals: Vec<f32> = buf.px.iter().flat_map(|q| photocraft_raster::from_rgba(&fmt, *q)).collect();diff --git a/crates/engine/tests/huge_doc_flatten.rs b/crates/engine/tests/huge_doc_flatten.rsnew file mode 100644index 000000000..fd83c1169--- /dev/null+++ b/crates/engine/tests/huge_doc_flatten.rs@@ -0,0 +1,30 @@+//! Trim and Duplicate › Merged Layers Only flatten the document; on a canvas over the compositor's+//! render cap the flatten comes back empty, and both must report it instead of panicking (#7223).++use photocraft_engine::Session;+use serde_json::json;++/// 1.2e9 px: within File › New's per-side limit, over `MAX_RENDER_PIXELS` (2^30).+fn huge() -> Session {+ let mut s = Session::new();+ s.execute("file.new", json!({"width": 40000, "height": 30000, "background": "transparent"})).unwrap();+ s+}++#[test]+fn trim_on_a_document_too_large_to_flatten_is_an_error() {+ for based in ["topLeft", "bottomRight", "transparent"] {+ let mut s = huge();+ let err = s.execute("image.trim", json!({"basedOn": based})).unwrap_err().to_string();+ assert!(err.contains("too large"), "{based}: {err}");+ assert_eq!(s.active().unwrap().doc.size.width, 40000, "{based}: the document is untouched");+ }+}++#[test]+fn merged_duplicate_of_a_document_too_large_to_flatten_is_an_error() {+ let mut s = huge();+ let err = s.execute("image.duplicate", json!({"mergedOnly": true})).unwrap_err().to_string();+ assert!(err.contains("too large"), "{err}");+ assert_eq!(s.documents().len(), 1, "no half-built copy is added");+}