Native iCloud apps for Windows and Linux, built in Rust. Notes, Reminders, Calendar, Contacts, Drive and Photos, offline and in sync with your iPhone.
Rust apple-notesapple-reminderscloudkitcrdtdesktop-appicloudicloud-calendaricloud-driveicloud-notesicloud-photosiphonelinuxlinux-desktoplocal-firstoffline-firstprivacyrusttauritauri-appwindows
Photos: Copy sets a PNG and a bitmap natively on Windows; Linux answers only image/png
Yashwanth Kamireddic12e5b2e4fc1
diff --git a/crates/quince-media/src/lib.rs b/crates/quince-media/src/lib.rsindex ec2190e..7f63651 100644--- a/crates/quince-media/src/lib.rs+++ b/crates/quince-media/src/lib.rs@@ -153,23 +153,42 @@ fn heic_rgb(bytes: &[u8]) -> Result<image::RgbImage, String> {/// # Errors/// When the bytes are not a picture this can decode.pub fn to_png(bytes: &[u8]) -> Result<Vec<u8>, String> {- use image::codecs::png::{CompressionType, FilterType, PngEncoder};+ png_of(&upright(bytes)?)+}++/// A photo for Windows' clipboard: the PNG [`to_png`] makes, which browsers, chat apps and+/// Office take, and the same picture as a device-independent bitmap (`CF_DIB`), the picture+/// type every Windows app takes (Paint, WordPad, older apps). Decoded once for both.+///+/// # Errors+/// When the bytes are not a picture this can decode.+pub fn to_png_and_dib(bytes: &[u8]) -> Result<(Vec<u8>, Vec<u8>), String> {+ let picture = upright(bytes)?;+ Ok((png_of(&picture)?, dib_of(&picture.to_rgb8())))+}++/// A photo decoded and turned upright: a HEIC by libheif (which turns it as the file says),+/// anything else the way its camera wrote it.+fn upright(bytes: &[u8]) -> Result<image::DynamicImage, String> {use image::ImageDecoder;- let picture = if is_heif(bytes) {- image::DynamicImage::ImageRgb8(heic_rgb(bytes)?)- } else {- let mut decoder = image::ImageReader::new(std::io::Cursor::new(bytes))- .with_guessed_format()- .map_err(|e| e.to_string())?- .into_decoder()- .map_err(|e| e.to_string())?;- let orientation = decoder- .orientation()- .unwrap_or(image::metadata::Orientation::NoTransforms);- let mut picture = image::DynamicImage::from_decoder(decoder).map_err(|e| e.to_string())?;- picture.apply_orientation(orientation);- picture- };+ if is_heif(bytes) {+ return Ok(image::DynamicImage::ImageRgb8(heic_rgb(bytes)?));+ }+ let mut decoder = image::ImageReader::new(std::io::Cursor::new(bytes))+ .with_guessed_format()+ .map_err(|e| e.to_string())?+ .into_decoder()+ .map_err(|e| e.to_string())?;+ let orientation = decoder+ .orientation()+ .unwrap_or(image::metadata::Orientation::NoTransforms);+ let mut picture = image::DynamicImage::from_decoder(decoder).map_err(|e| e.to_string())?;+ picture.apply_orientation(orientation);+ Ok(picture)+}++fn png_of(picture: &image::DynamicImage) -> Result<Vec<u8>, String> {+ use image::codecs::png::{CompressionType, FilterType, PngEncoder};let mut out = Vec::new();picture.write_with_encoder(PngEncoder::new_with_quality(@@ -181,6 +200,36 @@ pub fn to_png(bytes: &[u8]) -> Result<Vec<u8>, String> {Ok(out)}+/// A picture as Windows' `CF_DIB`: a 40-byte `BITMAPINFOHEADER`, then 24-bit rows, blue first,+/// bottom row first, each padded to a multiple of four bytes. 24-bit because a photo has no+/// transparency, and every app reads it alike (the PNG alongside keeps any there is).+fn dib_of(rgb: &image::RgbImage) -> Vec<u8> {+ let (w, h) = rgb.dimensions();+ let row = (w as usize * 3).div_ceil(4) * 4;+ let image_size = row * h as usize;+ let mut out = Vec::with_capacity(40 + image_size);+ out.extend_from_slice(&40u32.to_le_bytes()); // biSize+ out.extend_from_slice(&w.to_le_bytes()); // biWidth+ out.extend_from_slice(&h.to_le_bytes()); // biHeight: positive, bottom row first+ out.extend_from_slice(&1u16.to_le_bytes()); // biPlanes+ out.extend_from_slice(&24u16.to_le_bytes()); // biBitCount+ out.extend_from_slice(&0u32.to_le_bytes()); // biCompression: BI_RGB+ out.extend_from_slice(&u32::try_from(image_size).unwrap_or(0).to_le_bytes()); // biSizeImage+ out.extend_from_slice(&2835u32.to_le_bytes()); // biXPelsPerMeter: 72 dpi+ out.extend_from_slice(&2835u32.to_le_bytes()); // biYPelsPerMeter+ out.extend_from_slice(&0u32.to_le_bytes()); // biClrUsed+ out.extend_from_slice(&0u32.to_le_bytes()); // biClrImportant+ for y in (0..h).rev() {+ let start = out.len();+ for x in 0..w {+ let [r, g, b] = rgb.get_pixel(x, y).0;+ out.extend_from_slice(&[b, g, r]);+ }+ out.resize(start + row, 0);+ }+ out+}+/// RGB rows (`stride` bytes apart) made a JPEG, as heic_to_jpeg makes one.////// # Errors@@ -776,6 +825,38 @@ mod tests {assert!(super::to_png(b"not a picture").is_err());}+ /// Windows' copy of a photo: the PNG, and the same picture as a `CF_DIB` laid out as+ /// Windows reads one — header, bottom row first, blue first, rows padded to four bytes.+ #[test]+ fn a_photo_is_copied_for_windows_as_a_png_and_a_bitmap() {+ // 3×2: the top row red, the bottom row blue (3 pixels = 9 bytes, padded to 12).+ let img = image::RgbImage::from_fn(3, 2, |_, y| {+ if y == 0 {+ image::Rgb([255, 0, 0])+ } else {+ image::Rgb([0, 0, 255])+ }+ });+ let mut png = Vec::new();+ image::DynamicImage::ImageRgb8(img)+ .write_to(&mut std::io::Cursor::new(&mut png), image::ImageFormat::Png)+ .unwrap();+ let (out_png, dib) = super::to_png_and_dib(&png).unwrap();+ assert_eq!(&out_png[..8], b"\x89PNG\r\n\x1a\n");+ let u32_at = |i: usize| u32::from_le_bytes(dib[i..i + 4].try_into().unwrap());+ let u16_at = |i: usize| u16::from_le_bytes(dib[i..i + 2].try_into().unwrap());+ assert_eq!(u32_at(0), 40, "BITMAPINFOHEADER");+ assert_eq!((u32_at(4), u32_at(8)), (3, 2), "width, height (bottom-up)");+ assert_eq!((u16_at(12), u16_at(14)), (1, 24), "planes, bits per pixel");+ assert_eq!(u32_at(16), 0, "BI_RGB");+ assert_eq!(u32_at(20), 24, "two rows of 12 bytes");+ assert_eq!(dib.len(), 40 + 24);+ // The bottom (blue) row first, each pixel blue-green-red, then the padding.+ assert_eq!(&dib[40..52], &[255, 0, 0, 255, 0, 0, 255, 0, 0, 0, 0, 0]);+ assert_eq!(&dib[52..64], &[0, 0, 255, 0, 0, 255, 0, 0, 255, 0, 0, 0]);+ assert!(super::to_png_and_dib(b"not a picture").is_err());+ }+use super::*;/// A drawing's white goes, its ink stays; in dark, black ink turns white and a mid-tonediff --git a/src-tauri/Cargo.toml b/src-tauri/Cargo.tomlindex 7826e87..e269fa6 100644--- a/src-tauri/Cargo.toml+++ b/src-tauri/Cargo.toml@@ -60,6 +60,9 @@ windows = { version = "0.62", features = ["Win32_Storage_EnhancedStorage","Win32_System_Com","Win32_System_Com_StructuredStorage",+ "Win32_System_DataExchange",+ "Win32_System_Memory",+ "Win32_System_Ole","Win32_System_Variant","Win32_UI_Shell","Win32_UI_Shell_Common",diff --git a/src-tauri/src/photos.rs b/src-tauri/src/photos.rsindex cc7389c..c544b93 100644--- a/src-tauri/src/photos.rs+++ b/src-tauri/src/photos.rs@@ -2069,9 +2069,10 @@ pub async fn photos_counts(app: AppHandle, data: State<'_, Data>) -> UiResult<Ve/// picture itself. Decoded and made a PNG off the window's thread.////// On Linux it is offered on the clipboard here, through GTK — the window's own toolkit, which-/// speaks Wayland's clipboard and X11's alike — and nothing comes back. Elsewhere the PNG comes-/// back for the window to put there (the webview's own clipboard, which on Windows and macOS-/// takes a PNG as the system's picture type).+/// speaks Wayland's clipboard and X11's alike — and nothing comes back; on Windows it is set+/// natively, as a PNG and a bitmap (`win_clipboard`). Elsewhere the PNG comes back for the+/// window to put there (the webview's own clipboard, which on macOS takes a PNG as the+/// system's picture type).#[tauri::command]pub async fn photo_copy(id: String,@@ -2095,7 +2096,7 @@ pub async fn photo_copy(let png_of = |path: PathBuf| async move {crate::photo_cache::hold(&path);let bytes = tokio::fs::read(&path).await.map_err(|e| e.to_string())?;- crate::cpu::run(move || quince_media::to_png(&bytes)).await?+ crate::cpu::run(move || picture_for_clipboard(&bytes)).await?};if tokio::fs::metadata(&full).await.is_err() && tokio::fs::metadata(&original).await.is_err() {// Neither here yet: the whole picture, as the viewer would fetch it (a HEIC's comes with@@ -2117,10 +2118,109 @@ pub async fn photo_copy(offer_png(&app, png).await?;Ok(tauri::ipc::Response::new(Vec::new()))}- #[cfg(not(target_os = "linux"))]+ #[cfg(windows)]+ {+ let _ = app;+ crate::cpu::run(move || win_clipboard::put(&png.0, &png.1)).await??;+ Ok(tauri::ipc::Response::new(Vec::new()))+ }+ #[cfg(not(any(target_os = "linux", windows)))]Ok(tauri::ipc::Response::new(png))}+/// What Copy puts on the clipboard, made from a photo's bytes: a PNG; on Windows the same+/// picture as a bitmap besides (`CF_DIB`), the type every Windows app pastes.+#[cfg(not(windows))]+fn picture_for_clipboard(bytes: &[u8]) -> Result<Vec<u8>, String> {+ quince_media::to_png(bytes)+}+#[cfg(windows)]+fn picture_for_clipboard(bytes: &[u8]) -> Result<(Vec<u8>, Vec<u8>), String> {+ quince_media::to_png_and_dib(bytes)+}++/// The one type Copy offers on Linux: the PNG, as a picture. No text type is offered, so an+/// app that pastes text gets nothing rather than the picture's bytes as characters.+#[cfg(any(target_os = "linux", test))]+const PNG_TARGET: &str = "image/png";++/// The bytes to answer a paste that asked for `target`: the PNG only when it asked for one.+#[cfg(any(target_os = "linux", test))]+fn answer_for<'a>(target: &str, png: &'a [u8]) -> Option<&'a [u8]> {+ (target == PNG_TARGET).then_some(png)+}++/// Windows' clipboard, set from here rather than by the window: the PNG under the registered+/// "PNG" format (browsers, chat apps, Office) and the same picture as `CF_DIB` (everything+/// else; Windows makes `CF_BITMAP` and `CF_DIBV5` from it for apps that ask for those). Set+/// natively, it needs no permission from the webview and is there however long the photo took+/// to convert.+#[cfg(windows)]+mod win_clipboard {+ use windows::core::w;+ use windows::Win32::Foundation::{GlobalFree, HANDLE, HGLOBAL};+ use windows::Win32::System::DataExchange::{+ CloseClipboard, EmptyClipboard, OpenClipboard, RegisterClipboardFormatW, SetClipboardData,+ };+ use windows::Win32::System::Memory::{GlobalAlloc, GlobalLock, GlobalUnlock, GMEM_MOVEABLE};+ use windows::Win32::System::Ole::CF_DIB;++ const FAILED: &str = "The clipboard didn’t take the photo.";++ /// `bytes` in memory the clipboard can own.+ fn global(bytes: &[u8]) -> Result<HGLOBAL, String> {+ unsafe {+ let mem = GlobalAlloc(GMEM_MOVEABLE, bytes.len()).map_err(|_| FAILED)?;+ let at = GlobalLock(mem).cast::<u8>();+ if at.is_null() {+ let _ = GlobalFree(Some(mem));+ return Err(FAILED.into());+ }+ std::ptr::copy_nonoverlapping(bytes.as_ptr(), at, bytes.len());+ let _ = GlobalUnlock(mem);+ Ok(mem)+ }+ }++ /// One format set; the memory is the clipboard's once it is taken, freed here if not.+ fn set(format: u32, bytes: &[u8]) -> Result<(), String> {+ let mem = global(bytes)?;+ unsafe {+ if SetClipboardData(format, Some(HANDLE(mem.0))).is_err() {+ let _ = GlobalFree(Some(mem));+ return Err(FAILED.into());+ }+ }+ Ok(())+ }++ pub fn put(png: &[u8], dib: &[u8]) -> Result<(), String> {+ unsafe {+ // Another app may hold the clipboard for a moment: tried for half a second.+ let mut opened = false;+ for _ in 0..20 {+ if OpenClipboard(None).is_ok() {+ opened = true;+ break;+ }+ std::thread::sleep(std::time::Duration::from_millis(25));+ }+ if !opened {+ return Err(FAILED.into());+ }+ let result = EmptyClipboard()+ .map_err(|_| FAILED.to_string())+ .and_then(|()| set(u32::from(CF_DIB.0), dib))+ .and_then(|()| match RegisterClipboardFormatW(w!("PNG")) {+ 0 => Err(FAILED.into()),+ png_format => set(png_format, png),+ });+ let _ = CloseClipboard();+ result+ }+ }+}+/// A PNG offered on the desktop's clipboard through GTK, on the thread GTK lives on. Held by/// the clipboard until something else is copied; a clipboard manager keeps it past that.#[cfg(target_os = "linux")]@@ -2129,12 +2229,14 @@ async fn offer_png(app: &AppHandle, png: Vec<u8>) -> Result<(), String> {app.run_on_main_thread(move || {let clipboard = gtk::Clipboard::get(&gdk::SELECTION_CLIPBOARD);let targets = [gtk::TargetEntry::new(- "image/png",+ PNG_TARGET,gtk::TargetFlags::empty(),0,)];let offered = clipboard.set_with_data(&targets, move |_, selection, _| {- selection.set(&gdk::Atom::intern("image/png"), 8, &png);+ if let Some(bytes) = answer_for(&selection.target().name(), &png) {+ selection.set(&gdk::Atom::intern(PNG_TARGET), 8, bytes);+ }});let _ = tx.send(offered);})@@ -2927,6 +3029,26 @@ mod link_tests {mod tests {use super::*;+ /// Copy on Linux answers a paste only with the PNG, and only when a picture was asked+ /// for: an app asking for text (a terminal, a text field) gets nothing, never the+ /// picture's bytes as characters.+ #[test]+ fn copy_answers_only_a_paste_that_asks_for_a_picture() {+ let png = b"\x89PNG\r\n\x1a\nrest";+ assert_eq!(answer_for("image/png", png), Some(&png[..]));+ for text in [+ "text/plain",+ "text/plain;charset=utf-8",+ "UTF8_STRING",+ "STRING",+ "TEXT",+ "text/uri-list",+ "text/html",+ ] {+ assert_eq!(answer_for(text, png), None, "{text}");+ }+ }+/// A record id becomes one name in the cache's folder, whatever it holds.#[test]fn a_record_id_is_one_name_in_its_folder() {diff --git a/src/lib/ipc.ts b/src/lib/ipc.tsindex 240e822..bc2f4c3 100644--- a/src/lib/ipc.ts+++ b/src/lib/ipc.ts@@ -236,8 +236,8 @@ export const api = {* the most compatible. Answers where it was saved. */photoDownload: (id: string, want?: "originals" | "highestQuality" | "mostCompatible") =>invoke<string>("photo_download", { id, want: want ?? null }),- /** A photo on the clipboard as a PNG. On Linux it is put there by the app and nothing comes- * back; elsewhere the PNG comes back for the window to put there. */+ /** A photo on the clipboard as a picture. On Linux and Windows it is put there by the app+ * and nothing comes back; elsewhere the PNG comes back for the window to put there. */photoCopy: (id: string) => invoke<ArrayBuffer>("photo_copy", { id }),/** Title and caption together, as Info saves them. */photoSetText: (id: string, title: string, caption: string) => invoke<Photo>("photo_set_text", { id, title, caption }),diff --git a/src/screens/PhotosView.tsx b/src/screens/PhotosView.tsxindex 975836c..5f53a7b 100644--- a/src/screens/PhotosView.tsx+++ b/src/screens/PhotosView.tsx@@ -2459,8 +2459,8 @@ export function PhotosView() {}, 300);try {const png = await api.photoCopy(picked.id);- // Not on Linux, where the app has put it there itself: the webview's own clipboard, which- // takes a PNG as the system's picture.+ // Not on Linux or Windows, where the app has put it there itself: the webview's own+ // clipboard, which takes a PNG as the system's picture.if (png.byteLength) await navigator.clipboard.write([new ClipboardItem({ "image/png": new Blob([png], { type: "image/png" }) })]);if (mine === copying.current) toast({ message: ids.length > 1 ? "Copied the first photo" : "Copied", tone: "done" });} catch (e) {