Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

macOS: switch to png for image #164

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
107 changes: 2 additions & 105 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 2 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@ rust-version = "1.67.1"

[features]
default = ["image-data"]
image-data = ["core-graphics", "image", "windows-sys"]
image-data = ["image", "windows-sys"]
wayland-data-control = ["wl-clipboard-rs"]

[dependencies]
image = { version = "0.25", optional = true, default-features = false, features = ["png"] }

[dev-dependencies]
env_logger = "0.10.2"
Expand All @@ -30,21 +31,17 @@ windows-sys = { version = "0.48.0", optional = true, features = [
]}
clipboard-win = "5.3.1"
log = "0.4"
image = { version = "0.25", optional = true, default-features = false, features = ["png"] }

[target.'cfg(target_os = "macos")'.dependencies]
# Use `relax-void-encoding`, as that allows us to pass `c_void` instead of implementing `Encode` correctly for `&CGImageRef`
objc2 = { version = "0.5.1", features = ["relax-void-encoding"] }
objc2-foundation = { version = "0.2.0", features = ["NSArray", "NSString", "NSEnumerator", "NSGeometry"] }
objc2-app-kit = { version = "0.2.0", features = ["NSPasteboard", "NSPasteboardItem", "NSImage"] }
core-graphics = { version = "0.23", optional = true }
image = { version = "0.25", optional = true, default-features = false, features = ["tiff"] }

[target.'cfg(all(unix, not(any(target_os="macos", target_os="android", target_os="emscripten"))))'.dependencies]
log = "0.4"
x11rb = { version = "0.13" }
wl-clipboard-rs = { version = "0.8", optional = true }
image = { version = "0.25", optional = true, default-features = false, features = ["png"] }
parking_lot = "0.12"

[[example]]
Expand Down
21 changes: 21 additions & 0 deletions src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,27 @@ impl<'a> ImageData<'a> {
bytes: self.bytes.clone().into_owned().into(),
}
}

pub(crate) fn encode_as_png(&self) -> Result<Vec<u8>, Error> {
use image::ImageEncoder as _;

if self.bytes.is_empty() || self.width == 0 || self.height == 0 {
return Err(Error::ConversionFailure);
}

let mut png_bytes = Vec::new();
let encoder = image::codecs::png::PngEncoder::new(&mut png_bytes);
encoder
.write_image(
self.bytes.as_ref(),
self.width as u32,
self.height as u32,
image::ExtendedColorType::Rgba8,
)
.map_err(|_| Error::ConversionFailure)?;

Ok(png_bytes)
}
}

#[cfg(any(windows, all(unix, not(target_os = "macos"))))]
Expand Down
22 changes: 0 additions & 22 deletions src/platform/linux/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,28 +16,6 @@ fn into_unknown<E: std::fmt::Display>(error: E) -> Error {
Error::Unknown { description: error.to_string() }
}

#[cfg(feature = "image-data")]
fn encode_as_png(image: &ImageData) -> Result<Vec<u8>, Error> {
use image::ImageEncoder as _;

if image.bytes.is_empty() || image.width == 0 || image.height == 0 {
return Err(Error::ConversionFailure);
}

let mut png_bytes = Vec::new();
let encoder = image::codecs::png::PngEncoder::new(&mut png_bytes);
encoder
.write_image(
image.bytes.as_ref(),
image.width as u32,
image.height as u32,
image::ExtendedColorType::Rgba8,
)
.map_err(|_| Error::ConversionFailure)?;

Ok(png_bytes)
}

/// Clipboard selection
///
/// Linux has a concept of clipboard "selections" which tend to be used in different contexts. This
Expand Down
6 changes: 2 additions & 4 deletions src/platform/linux/wayland.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,10 @@ use wl_clipboard_rs::{
utils::is_primary_selection_supported,
};

#[cfg(feature = "image-data")]
use super::encode_as_png;
use super::{into_unknown, LinuxClipboardKind, WaitConfig};
use crate::common::Error;
#[cfg(feature = "image-data")]
use crate::common::ImageData;
use crate::ImageData;

#[cfg(feature = "image-data")]
const MIME_PNG: &str = "image/png";
Expand Down Expand Up @@ -164,7 +162,7 @@ impl Clipboard {
selection: LinuxClipboardKind,
wait: WaitConfig,
) -> Result<(), Error> {
let image = encode_as_png(&image)?;
let image = image.encode_as_png()?;
let mut opts = Options::new();
opts.foreground(matches!(wait, WaitConfig::Forever));
opts.clipboard(selection.try_into()?);
Expand Down
4 changes: 1 addition & 3 deletions src/platform/linux/x11.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,6 @@ use x11rb::{
COPY_DEPTH_FROM_PARENT, COPY_FROM_PARENT, NONE,
};

#[cfg(feature = "image-data")]
use super::encode_as_png;
use super::{into_unknown, LinuxClipboardKind, WaitConfig};
#[cfg(feature = "image-data")]
use crate::ImageData;
Expand Down Expand Up @@ -931,7 +929,7 @@ impl Clipboard {
selection: LinuxClipboardKind,
wait: WaitConfig,
) -> Result<()> {
let encoded = encode_as_png(&image)?;
let encoded = image.encode_as_png()?;
let data = vec![ClipboardData { bytes: encoded, format: self.inner.atoms.PNG_MIME }];
self.inner.write(data, selection, wait)
}
Expand Down
Loading