Skip to content

Commit

Permalink
Update image crate to 0.24 (#75)
Browse files Browse the repository at this point in the history
* Update image crate to 0.24

* image::png::PngEncoder -> image::codecs::png::PngEncoder

* Switch from deprecated function

* Simplify `encode_as_png` function
  • Loading branch information
emilk committed Sep 21, 2022
1 parent 8e08a0b commit a33cea4
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 68 deletions.
65 changes: 26 additions & 39 deletions Cargo.lock

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

4 changes: 2 additions & 2 deletions Cargo.toml
Expand Up @@ -36,13 +36,13 @@ objc_id = "0.1"
objc-foundation = "0.1"
once_cell = "1"
core-graphics = { version = "0.22", optional = true }
image = { version = "0.23", optional = true, default-features = false, features = ["tiff"] }
image = { version = "0.24", 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.10" }
wl-clipboard-rs = { version = "0.6", optional = true }
image = { version = "0.23.9", optional = true, default-features = false, features = ["png"] }
image = { version = "0.24", optional = true, default-features = false, features = ["png"] }
parking_lot = "0.12"

[[example]]
Expand Down
32 changes: 5 additions & 27 deletions src/platform/linux/mod.rs
@@ -1,6 +1,4 @@
use std::borrow::Cow;
#[cfg(feature = "image-data")]
use std::{cell::RefCell, rc::Rc};

#[cfg(feature = "wayland-data-control")]
use log::{trace, warn};
Expand All @@ -20,44 +18,24 @@ fn into_unknown<E: std::fmt::Display>(error: E) -> Error {

#[cfg(feature = "image-data")]
fn encode_as_png(image: &ImageData) -> Result<Vec<u8>, Error> {
/// This is a workaround for the PNGEncoder not having a `into_inner` like function
/// which would allow us to take back our Vec after the encoder finished encoding.
/// So instead we create this wrapper around an Rc Vec which implements `io::Write`
#[derive(Clone)]
struct RcBuffer {
inner: Rc<RefCell<Vec<u8>>>,
}
impl std::io::Write for RcBuffer {
fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
self.inner.borrow_mut().extend_from_slice(buf);
Ok(buf.len())
}
fn flush(&mut self) -> std::io::Result<()> {
// Noop
Ok(())
}
}
use image::ImageEncoder as _;

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

let enc_output = RcBuffer { inner: Rc::new(RefCell::new(Vec::new())) };
let encoder = image::png::PngEncoder::new(enc_output.clone());
let mut png_bytes = Vec::new();
let encoder = image::codecs::png::PngEncoder::new(&mut png_bytes);
encoder
.encode(
.write_image(
image.bytes.as_ref(),
image.width as u32,
image.height as u32,
image::ColorType::Rgba8,
)
.map_err(|_| Error::ConversionFailure)?;

// The encoder must be destroyed by the time we get to `try_unwrap`, in order to
// be able to take the value from the `Rc`. This code is currently relying on the fact
// that the `encode` function consumes its `self` parameter.
let bytes = Rc::try_unwrap(enc_output.inner).unwrap().into_inner();
Ok(bytes)
Ok(png_bytes)
}

/// Clipboard selection
Expand Down

0 comments on commit a33cea4

Please sign in to comment.