From a33cea4d762c50d9cc2451ab26217837e85d6c9f Mon Sep 17 00:00:00 2001 From: Emil Ernerfeldt Date: Wed, 21 Sep 2022 02:59:12 +0200 Subject: [PATCH] Update image crate to 0.24 (#75) * Update image crate to 0.24 * image::png::PngEncoder -> image::codecs::png::PngEncoder * Switch from deprecated function * Simplify `encode_as_png` function --- Cargo.lock | 65 ++++++++++++++++----------------------- Cargo.toml | 4 +-- src/platform/linux/mod.rs | 32 +++---------------- 3 files changed, 33 insertions(+), 68 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 1a76409..cbda385 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -189,12 +189,11 @@ dependencies = [ [[package]] name = "deflate" -version = "0.8.6" +version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "73770f8e1fe7d64df17ca66ad28994a0a623ea497fa69486e14984e715c5d174" +checksum = "c86f7e25f518f4b81808a2cf1c50996a61f5c2eb394b2393bd87f2a4780a432f" dependencies = [ "adler32", - "byteorder", ] [[package]] @@ -265,6 +264,16 @@ version = "0.4.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0ce7134b9999ecaf8bcd65542e436736ef32ddca1b3e06094cb6ec5755203b80" +[[package]] +name = "flate2" +version = "1.0.24" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f82b0f4c27ad9f8bfd1f3208d882da2b09c301bc1c828fd3a00d0216d2fbbff6" +dependencies = [ + "crc32fast", + "miniz_oxide", +] + [[package]] name = "fnv" version = "1.0.7" @@ -319,14 +328,13 @@ checksum = "9a3a5bfb195931eeb336b2a7b4d761daec841b97f947d34394601737a7bba5e4" [[package]] name = "image" -version = "0.23.14" +version = "0.24.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "24ffcb7e7244a9bf19d35bf2883b9c080c4ced3c07a9895572178cdb8f13f6a1" +checksum = "7e30ca2ecf7666107ff827a8e481de6a132a9b687ed3bb20bb1c144a36c00964" dependencies = [ "bytemuck", "byteorder", "color_quant", - "num-iter", "num-rational", "num-traits", "png", @@ -360,9 +368,9 @@ checksum = "6c8af84674fe1f223a982c933a0ee1086ac4d4052aa0fb8060c12c6ad838e754" [[package]] name = "jpeg-decoder" -version = "0.1.22" +version = "0.2.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "229d53d58899083193af11e15917b5640cd40b29ff475a1fe4ef725deb02d0f2" +checksum = "9478aa10f73e7528198d75109c8be5cd7d15fb530238040148d5f9a22d4c5b3b" [[package]] name = "lazy_static" @@ -427,21 +435,11 @@ checksum = "68354c5c6bd36d73ff3feceb05efa59b6acb7626617f4962be322a825e61f79a" [[package]] name = "miniz_oxide" -version = "0.3.7" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "791daaae1ed6889560f8c4359194f56648355540573244a5448a83ba1ecc7435" -dependencies = [ - "adler32", -] - -[[package]] -name = "miniz_oxide" -version = "0.4.4" +version = "0.5.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a92518e98c078586bc6c934028adcca4c92a53d6a958196de835170a01d84e4b" +checksum = "6f5c75688da582b8ffc1f1799e9db273f32133c49e048f614d22ec3256773ccc" dependencies = [ "adler", - "autocfg", ] [[package]] @@ -489,22 +487,11 @@ dependencies = [ "num-traits", ] -[[package]] -name = "num-iter" -version = "0.1.43" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7d03e6c028c5dc5cac6e2dec0efda81fc887605bb3d884578bb6d6bf7514e252" -dependencies = [ - "autocfg", - "num-integer", - "num-traits", -] - [[package]] name = "num-rational" -version = "0.3.2" +version = "0.4.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "12ac428b1cb17fce6f731001d307d351ec70a6d202fc2e60f7d4c5e42d8f4f07" +checksum = "0638a1c9d0a3c0914158145bc76cff373a75a627e6ecbfb71cbe6f453a5a19b0" dependencies = [ "autocfg", "num-integer", @@ -615,14 +602,14 @@ checksum = "1df8c4ec4b0627e53bdf214615ad287367e482558cf84b109250b37464dc03ae" [[package]] name = "png" -version = "0.16.8" +version = "0.17.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3c3287920cb847dee3de33d301c463fba14dda99db24214ddf93f83d3021f4c6" +checksum = "dc38c0ad57efb786dd57b9864e5b18bae478c00c824dc55a38bbc9da95dde3ba" dependencies = [ "bitflags", "crc32fast", "deflate", - "miniz_oxide 0.3.7", + "miniz_oxide", ] [[package]] @@ -780,12 +767,12 @@ dependencies = [ [[package]] name = "tiff" -version = "0.6.1" +version = "0.7.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9a53f4706d65497df0c4349241deddf35f84cee19c87ed86ea8ca590f4464437" +checksum = "7259662e32d1e219321eb309d5f9d898b779769d81b76e762c07c8e5d38fcb65" dependencies = [ + "flate2", "jpeg-decoder", - "miniz_oxide 0.4.4", "weezl", ] diff --git a/Cargo.toml b/Cargo.toml index d1e8ae4..747a914 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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]] diff --git a/src/platform/linux/mod.rs b/src/platform/linux/mod.rs index e12896a..47519fc 100644 --- a/src/platform/linux/mod.rs +++ b/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}; @@ -20,32 +18,16 @@ fn into_unknown(error: E) -> Error { #[cfg(feature = "image-data")] fn encode_as_png(image: &ImageData) -> Result, 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>>, - } - impl std::io::Write for RcBuffer { - fn write(&mut self, buf: &[u8]) -> std::io::Result { - 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, @@ -53,11 +35,7 @@ fn encode_as_png(image: &ImageData) -> Result, Error> { ) .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