diff --git a/crates/engine/src/cmd/clipboard/flavours.rs b/crates/engine/src/cmd/clipboard/flavours.rs index 5aba3707a8..4db9135014 100644 --- a/crates/engine/src/cmd/clipboard/flavours.rs +++ b/crates/engine/src/cmd/clipboard/flavours.rs @@ -453,11 +453,14 @@ pub fn read_image(bytes: &[u8]) -> Result { /// [`read_image`] for a bitmap another app put on the clipboard as decoded pixels, on any thread: /// the pixels are encoded once as the PNG the document keeps, and kept as the decoded image the /// renderers share, so neither this read nor the first frame that draws the image decodes them. -/// An error when the image is empty or too large to draw. +/// When the renderers' store refuses the pixels (over its budget, or no room beside the images +/// being drawn), they are dropped, and the renderers decode the PNG as they decode any other image, +/// under the same memory limits. An error when the image is empty or too large to draw. pub fn read_pixels(img: image::RgbaImage) -> Result { let img = image::DynamicImage::ImageRgba8(img); let read = fileio::pixels_png(&img).map_err(|e| bad("clipboard.importImage", e.to_string()))?; - vectorcraft_render::keep_decoded_image(&read.blob, img); + // Refused pixels are not held anywhere else, so the store's budget stays the only one. + let _kept = vectorcraft_render::keep_decoded_image(&read.blob, img); Ok(ImageRead(read)) } diff --git a/crates/render/src/tests.rs b/crates/render/src/tests.rs index eddc139ebb..a6a93e484f 100644 --- a/crates/render/src/tests.rs +++ b/crates/render/src/tests.rs @@ -1729,6 +1729,33 @@ fn a_kept_decoded_image_is_drawn_without_decoding_it() { assert!(!image_cached(&cmyk)); } +/// Pixels [`keep_decoded_image`] refuses (here, pixels past the decode allowance) are not +/// kept anywhere: the first render that draws the image decodes it, as for any image. +#[test] +fn a_refused_decoded_image_is_decoded_when_first_drawn() { + use vectorcraft_doc::{ImageBlob, ImageObject, NodeKind}; + // Pixels no other test uses, since every test shares the store. + let img = image::RgbaImage::from_fn(29, 17, |x, y| image::Rgba([(x * 5) as u8, (y * 13) as u8, 41, 255])); + let mut png = vec![]; + img.write_to(&mut std::io::Cursor::new(&mut png), image::ImageFormat::Png).unwrap(); + let blob = ImageBlob::new("image/png", png.clone()); + paint::TEST_ALLOWANCE.with(|c| c.set(Some(64))); + let kept = keep_decoded_image(&blob, image::load_from_memory(&png).unwrap()); + paint::TEST_ALLOWANCE.with(|c| c.set(None)); + assert!(!kept, "past the allowance: the pixels are refused"); + assert!(!image_cached(&blob)); + let mut d = Document::new(29.0, 17.0); + d.images.insert("refused".into(), blob); + let id = d.alloc_id(); + let im = + ImageObject { key: "refused".into(), width: 29, height: 17, xf: Affine::IDENTITY, link: None, placement: Default::default(), pixels: None }; + d.insert(Some(d.layers[0].id), 0, Node::new(id, NodeKind::Image(Box::new(im)))).unwrap(); + let decodes = image_decodes(); + let r = Renderer::new().render(&d, 29, 17, Affine::IDENTITY, &RenderOptions::default()); + assert_eq!(image_decodes(), decodes + 1, "the first draw decodes the image"); + assert!(r.pixel(14, 8)[3] > 0, "the image is drawn"); +} + /// A CMYK image's inks and the pixels shown from them are admitted against the decode allowance /// before either is made: past it, the image is read as other images are (reduced), not whole. #[test] diff --git a/crates/ui-egui/src/sysclip.rs b/crates/ui-egui/src/sysclip.rs index 080c0ac0e7..036ca60da2 100644 --- a/crates/ui-egui/src/sysclip.rs +++ b/crates/ui-egui/src/sysclip.rs @@ -62,6 +62,15 @@ fn deferred_flavour(f: Flavor) -> Option { engine_flavour(f) } +/// Do the pixels of a raw bitmap ([`RGBA8`]) fill the size its header gives, as [`rgba8_image`] +/// requires? The pixels are not copied. +fn rgba8_fits(data: &[u8]) -> bool { + let Some(([w0, w1, w2, w3, h0, h1, h2, h3], pixels)) = data.split_first_chunk::<8>() else { return false }; + let (width, height) = (u32::from_le_bytes([*w0, *w1, *w2, *w3]), u32::from_le_bytes([*h0, *h1, *h2, *h3])); + let needed = u64::from(width).checked_mul(u64::from(height)).and_then(|n| n.checked_mul(4)); + needed.is_some_and(|n| u64::try_from(pixels.len()).is_ok_and(|len| n <= len)) +} + /// The bitmap of `f`, a flavour that imports as an image ([`imports_as_image`]), read as /// `clipboard.importImage` reads it, and the type `clipboard.importImage` is given for it. A raw /// bitmap ([`RGBA8`]) is read from its pixels ([`read_pixels`]); other types are decoded from their @@ -598,7 +607,10 @@ impl VectorcraftApp { Some(Box::new(move || { let f = deferred_flavour(first()?)?; match picture { - Some(picture) if is_picture_stand_in(&f) => picture().and_then(deferred_flavour).or(Some(f)), + // A raw bitmap cut short gives way to the text, as a picture that can't be read does. + Some(picture) if is_picture_stand_in(&f) => { + picture().and_then(deferred_flavour).filter(|p| p.mime != RGBA8 || rgba8_fits(&p.data)).or(Some(f)) + } _ => Some(f), } })) diff --git a/crates/ui-egui/src/tests/sysclip.rs b/crates/ui-egui/src/tests/sysclip.rs index b97f140ad6..4c1c3c93e3 100644 --- a/crates/ui-egui/src/tests/sysclip.rs +++ b/crates/ui-egui/src/tests/sysclip.rs @@ -991,6 +991,34 @@ fn a_bitmap_read_off_the_ui_thread_is_not_decoded_again() { assert_eq!((im.width, im.height), (3, 2)); let blob = &app.session.active().unwrap().doc.images[&im.key]; assert_eq!(image::load_from_memory(&blob.bytes).unwrap().to_rgba8(), img, "the document keeps the copied pixels"); + // The first draw paints the image from the pixels the read kept. + let center = im.xf * Point::new(1.5, 1.0); + let decodes = vectorcraft_render::image_decodes(); + let doc = &app.session.active().unwrap().doc; + let opts = vectorcraft_render::RenderOptions::default(); + let r = vectorcraft_render::Renderer::new().render(doc, 800, 800, vectorcraft_geom::Affine::IDENTITY, &opts); + assert_eq!(vectorcraft_render::image_decodes(), decodes, "the first draw decoded the pasted image"); + assert!(r.pixel(center.x as u32, center.y as u32)[3] > 0, "the pasted image is drawn at {center:?}"); +} + +/// A raw bitmap read off the UI thread whose pixels don't fill its size is not taken: the text +/// beside it is pasted instead. +#[test] +fn a_cut_short_bitmap_read_off_the_ui_thread_gives_way_to_its_text() { + let (mut app, board) = app(); + assert!(RGBA8.starts_with("image/"), "the fake clipboard finds a raw bitmap as any bitmap"); + // 3 by 2 pixels need 24 bytes after the header; 20 are there. + let mut raw = [3u32.to_le_bytes(), 2u32.to_le_bytes()].concat(); + raw.extend_from_slice(&[255; 20]); + copy_elsewhere(&board, vec![(TEXT, b"https://example.com/images/cat.png".to_vec()), (RGBA8, raw.clone())]); + let make = app.adopt_system_clipboard_or_defer(true).unwrap().expect("the picture is read later"); + let f = make().expect("the text"); + assert_eq!((f.mime, f.data.as_slice()), (TEXT, &b"https://example.com/images/cat.png"[..])); + // With no text beside it, the bitmap is read and its error reported, not dropped. + copy_elsewhere(&board, vec![(RGBA8, raw)]); + let make = app.adopt_system_clipboard_or_defer(true).unwrap().expect("the picture is read later"); + let (_, read) = crate::sysclip::read_bitmap(make().expect("the bitmap")); + assert_eq!(read.err().as_deref(), Some("the copied bitmap is cut short")); } #[test]