diff --git a/crates/ui-egui/src/sysclip.rs b/crates/ui-egui/src/sysclip.rs index 036ca60da2..844d3de175 100644 --- a/crates/ui-egui/src/sysclip.rs +++ b/crates/ui-egui/src/sysclip.rs @@ -67,8 +67,17 @@ fn deferred_flavour(f: Flavor) -> Option { 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)) + rgba8_len(width, height).is_some_and(|n| n <= pixels.len()) +} + +/// The byte count of `width` × `height` RGBA8 pixels; `None` for an empty size or one that +/// overflows. +fn rgba8_len(width: u32, height: u32) -> Option { + if width == 0 || height == 0 { + return None; + } + let n = u64::from(width).checked_mul(u64::from(height))?.checked_mul(4)?; + usize::try_from(n).ok() } /// The bitmap of `f`, a flavour that imports as an image ([`imports_as_image`]), read as @@ -84,12 +93,15 @@ pub(crate) fn read_bitmap(f: Flavour) -> (&'static str, Result) -> Option { let (header, _) = data.split_first_chunk::<8>()?; let [w0, w1, w2, w3, h0, h1, h2, h3] = *header; let (width, height) = (u32::from_le_bytes([w0, w1, w2, w3]), u32::from_le_bytes([h0, h1, h2, h3])); + let len = rgba8_len(width, height)?; data.drain(..8); + data.truncate(len); image::RgbaImage::from_raw(width, height, data) } diff --git a/crates/ui-egui/src/tests/sysclip.rs b/crates/ui-egui/src/tests/sysclip.rs index 4c1c3c93e3..e64fc4c234 100644 --- a/crates/ui-egui/src/tests/sysclip.rs +++ b/crates/ui-egui/src/tests/sysclip.rs @@ -1021,6 +1021,36 @@ fn a_cut_short_bitmap_read_off_the_ui_thread_gives_way_to_its_text() { assert_eq!(read.err().as_deref(), Some("the copied bitmap is cut short")); } +/// A raw bitmap read off the UI thread with no width or no height is not taken: the text beside +/// it is pasted instead. +#[test] +fn an_empty_bitmap_read_off_the_ui_thread_gives_way_to_its_text() { + let (mut app, board) = app(); + for (w, h) in [(0u32, 2u32), (3, 0)] { + let raw = [w.to_le_bytes(), h.to_le_bytes()].concat(); + copy_elsewhere(&board, vec![(TEXT, b"https://example.com/images/cat.png".to_vec()), (RGBA8, raw)]); + 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"[..]), "{w} by {h}"); + } +} + +/// A raw bitmap read off the UI thread with bytes past its pixels pastes its pixels. +#[test] +fn a_padded_bitmap_read_off_the_ui_thread_pastes_its_pixels() { + let (mut app, board) = app(); + // 3 by 2 pixels need 24 bytes after the header; 28 are there. + let mut raw = [3u32.to_le_bytes(), 2u32.to_le_bytes()].concat(); + raw.extend_from_slice(&[255; 28]); + copy_elsewhere(&board, vec![(TEXT, b"https://example.com/images/cat.png".to_vec()), (RGBA8, raw)]); + let make = app.adopt_system_clipboard_or_defer(true).unwrap().expect("the picture is read later"); + let (mime, read) = crate::sysclip::read_bitmap(make().expect("the bitmap")); + app.adopt_image_read(mime, Some([250.0, 180.0]), read).unwrap(); + app.session.execute("edit.paste", &json!({})).unwrap(); + let NodeKind::Image(im) = &pasted(&app)[0].kind else { panic!("not an image: {:?}", pasted(&app)[0].kind) }; + assert_eq!((im.width, im.height), (3, 2)); +} + #[test] fn a_pdf_from_another_app_pastes_as_vectors_ahead_of_its_bitmap() { let (mut app, board) = app();