| 1 | use std::io::Cursor; |
| 2 | |
| 3 | use gpui_kit::{App, ClipboardItem, Image, ImageFormat}; |
| 4 | |
| 5 | const DIB_HEADER_SIZE: u32 = 40; |
| 6 | const DIB_BITS_PER_PIXEL: u16 = 32; |
| 7 | |
| 8 | /// Windows apps such as Paint, Office and Teams only paste `CF_DIB`, which gpui does not write. |
| 9 | pub struct ClipboardImage { |
| 10 | png: Vec<u8>, |
| 11 | #[cfg(windows)] |
| 12 | dib: Vec<u8>, |
| 13 | } |
| 14 | |
| 15 | impl ClipboardImage { |
| 16 | pub fn from_bytes(bytes: &[u8]) -> Option<Self> { |
| 17 | let decoded = image::load_from_memory(bytes).ok()?; |
| 18 | let png = if image::guess_format(bytes).ok()? == image::ImageFormat::Png { |
| 19 | bytes.to_vec() |
| 20 | } else { |
| 21 | let mut png = Vec::new(); |
| 22 | decoded |
| 23 | .write_to(&mut Cursor::new(&mut png), image::ImageFormat::Png) |
| 24 | .ok()?; |
| 25 | png |
| 26 | }; |
| 27 | Some(ClipboardImage { |
| 28 | png, |
| 29 | #[cfg(windows)] |
| 30 | dib: dib_bytes(&decoded.into_rgba8()), |
| 31 | }) |
| 32 | } |
| 33 | |
| 34 | pub fn write(self, cx: &mut App) { |
| 35 | #[cfg(windows)] |
| 36 | if windows_clipboard::write(&self.png, &self.dib).is_ok() { |
| 37 | return; |
| 38 | } |
| 39 | cx.write_to_clipboard(ClipboardItem::new_image(&Image::from_bytes( |
| 40 | ImageFormat::Png, |
| 41 | self.png, |
| 42 | ))); |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | pub fn dib_bytes(rgba: &image::RgbaImage) -> Vec<u8> { |
| 47 | let (width, height) = rgba.dimensions(); |
| 48 | let pixel_bytes = width as usize * height as usize * 4; |
| 49 | let mut dib = Vec::with_capacity(DIB_HEADER_SIZE as usize + pixel_bytes); |
| 50 | dib.extend_from_slice(&DIB_HEADER_SIZE.to_le_bytes()); |
| 51 | dib.extend_from_slice(&(width as i32).to_le_bytes()); |
| 52 | dib.extend_from_slice(&(height as i32).to_le_bytes()); |
| 53 | dib.extend_from_slice(&1u16.to_le_bytes()); |
| 54 | dib.extend_from_slice(&DIB_BITS_PER_PIXEL.to_le_bytes()); |
| 55 | dib.extend_from_slice(&0u32.to_le_bytes()); |
| 56 | dib.extend_from_slice(&(pixel_bytes as u32).to_le_bytes()); |
| 57 | dib.extend_from_slice(&[0; 16]); |
| 58 | for row in rgba.rows().rev() { |
| 59 | for pixel in row { |
| 60 | let [red, green, blue, alpha] = pixel.0; |
| 61 | let on_white = |channel: u8| { |
| 62 | let alpha = u16::from(alpha); |
| 63 | ((u16::from(channel) * alpha + 255 * (255 - alpha) + 127) / 255) as u8 |
| 64 | }; |
| 65 | dib.extend_from_slice(&[on_white(blue), on_white(green), on_white(red), 255]); |
| 66 | } |
| 67 | } |
| 68 | dib |
| 69 | } |
| 70 | |
| 71 | #[cfg(windows)] |
| 72 | mod windows_clipboard { |
| 73 | use windows::Win32::Foundation::HANDLE; |
| 74 | use windows::Win32::System::DataExchange::{ |
| 75 | CloseClipboard, EmptyClipboard, OpenClipboard, RegisterClipboardFormatW, SetClipboardData, |
| 76 | }; |
| 77 | use windows::Win32::System::Memory::{GMEM_MOVEABLE, GlobalAlloc, GlobalLock, GlobalUnlock}; |
| 78 | use windows::Win32::System::Ole::CF_DIB; |
| 79 | use windows::core::{Owned, Result, w}; |
| 80 | |
| 81 | pub fn write(png: &[u8], dib: &[u8]) -> Result<()> { |
| 82 | unsafe { |
| 83 | OpenClipboard(None)?; |
| 84 | let result = (|| { |
| 85 | EmptyClipboard()?; |
| 86 | set_bytes(png, RegisterClipboardFormatW(w!("PNG")))?; |
| 87 | set_bytes(dib, u32::from(CF_DIB.0)) |
| 88 | })(); |
| 89 | CloseClipboard().ok(); |
| 90 | result |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | unsafe fn set_bytes(data: &[u8], format: u32) -> Result<()> { |
| 95 | unsafe { |
| 96 | let global = Owned::new(GlobalAlloc(GMEM_MOVEABLE, data.len())?); |
| 97 | let pointer = GlobalLock(*global); |
| 98 | if pointer.is_null() { |
| 99 | return Err(windows::core::Error::from_thread()); |
| 100 | } |
| 101 | std::ptr::copy_nonoverlapping(data.as_ptr(), pointer.cast(), data.len()); |
| 102 | GlobalUnlock(*global).ok(); |
| 103 | SetClipboardData(format, Some(HANDLE(global.0)))?; |
| 104 | std::mem::forget(global); |
| 105 | } |
| 106 | Ok(()) |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | #[cfg(test)] |
| 111 | mod tests { |
| 112 | use super::*; |
| 113 | use image::{Rgba, RgbaImage}; |
| 114 | |
| 115 | #[test] |
| 116 | fn dib_is_a_bottom_up_bgra_bitmap_on_white_behind_a_40_byte_header() { |
| 117 | let mut rgba = RgbaImage::new(2, 2); |
| 118 | rgba.put_pixel(0, 0, Rgba([10, 20, 30, 255])); |
| 119 | rgba.put_pixel(1, 1, Rgba([40, 50, 60, 128])); |
| 120 | let dib = dib_bytes(&rgba); |
| 121 | assert_eq!(dib.len(), 40 + 16); |
| 122 | assert_eq!(&dib[0..4], &40u32.to_le_bytes()); |
| 123 | assert_eq!(&dib[4..8], &2i32.to_le_bytes()); |
| 124 | assert_eq!(&dib[8..12], &2i32.to_le_bytes()); |
| 125 | assert_eq!(&dib[14..16], &32u16.to_le_bytes()); |
| 126 | assert_eq!(&dib[44..48], &[157, 152, 147, 255]); |
| 127 | assert_eq!(&dib[48..52], &[30, 20, 10, 255]); |
| 128 | } |
| 129 | |
| 130 | #[test] |
| 131 | fn a_jpeg_is_kept_as_png_for_the_clipboard() { |
| 132 | let mut jpeg = Vec::new(); |
| 133 | image::DynamicImage::ImageRgba8(RgbaImage::from_pixel(3, 1, Rgba([1, 2, 3, 255]))) |
| 134 | .to_rgb8() |
| 135 | .write_to(&mut Cursor::new(&mut jpeg), image::ImageFormat::Jpeg) |
| 136 | .unwrap(); |
| 137 | let image = ClipboardImage::from_bytes(&jpeg).unwrap(); |
| 138 | assert_eq!( |
| 139 | image::guess_format(&image.png).unwrap(), |
| 140 | image::ImageFormat::Png |
| 141 | ); |
| 142 | } |
| 143 | } |