From 4108a7523c7554fce7421001cf216bc4c9098dae Mon Sep 17 00:00:00 2001 From: Emil Ernerfeldt Date: Mon, 26 Feb 2024 11:08:55 +0100 Subject: [PATCH 1/2] Replace `zune-jpeg` with the default `image` crate --- Cargo.lock | 17 ------ Cargo.toml | 2 - Cranky.toml | 1 + crates/re_log/src/lib.rs | 2 - crates/re_types/Cargo.toml | 8 +-- .../re_types/src/datatypes/tensor_data_ext.rs | 12 ++-- crates/re_types/src/tensor_data.rs | 56 ++++--------------- 7 files changed, 23 insertions(+), 75 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index e10343637ee7..d9e4a2e751e3 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4939,8 +4939,6 @@ dependencies = [ "smallvec", "thiserror", "uuid", - "zune-core", - "zune-jpeg", ] [[package]] @@ -7736,21 +7734,6 @@ dependencies = [ "flate2", ] -[[package]] -name = "zune-core" -version = "0.4.11" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "aae122f32a8e2a653acb430c9af5e79e7056c519fa8bac46e51e670868e5c0f2" - -[[package]] -name = "zune-jpeg" -version = "0.4.11" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ec866b44a2a1fd6133d363f073ca1b179f438f99e7e5bfb1e33f7181facfe448" -dependencies = [ - "zune-core", -] - [[package]] name = "zvariant" version = "3.15.0" diff --git a/Cargo.toml b/Cargo.toml index 3791985ab52e..df65de9ef055 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -254,8 +254,6 @@ wgpu = { version = "0.19.1", default-features = false, features = [ wgpu-core = "0.19.0" xshell = "0.2" zip = { version = "0.6", default-features = false } -zune-core = "0.4" -zune-jpeg = "0.4" [profile.dev] diff --git a/Cranky.toml b/Cranky.toml index a9438d5b41c4..aa4ca15c2ba9 100644 --- a/Cranky.toml +++ b/Cranky.toml @@ -155,6 +155,7 @@ allow = [ "clippy::let_underscore_untyped", "clippy::missing_assert_message", "clippy::missing_errors_doc", + "clippy::use_self", "trivial_casts", "unused_qualifications", diff --git a/crates/re_log/src/lib.rs b/crates/re_log/src/lib.rs index 05dd6022f006..5e5a6a790d6a 100644 --- a/crates/re_log/src/lib.rs +++ b/crates/re_log/src/lib.rs @@ -51,8 +51,6 @@ pub mod external { /// Never log anything less serious than a `ERROR` from these crates. const CRATES_AT_ERROR_LEVEL: &[&str] = &[ - // Waiting for https://github.com/etemesi254/zune-image/issues/131 to be released - "zune_jpeg", // silence rustls in release mode: https://github.com/rerun-io/rerun/issues/3104 #[cfg(not(debug_assertions))] "rustls", diff --git a/crates/re_types/Cargo.toml b/crates/re_types/Cargo.toml index 6d32e54cfbc8..1a1deb5704d6 100644 --- a/crates/re_types/Cargo.toml +++ b/crates/re_types/Cargo.toml @@ -39,7 +39,7 @@ egui_plot = ["dep:egui_plot"] glam = ["dep:glam"] ## Integration with the [`image`](https://crates.io/crates/image/) crate, plus JPEG support. -image = ["dep:ecolor", "dep:image", "dep:zune-core", "dep:zune-jpeg"] +image = ["dep:ecolor", "dep:image"] ## Enable (de)serialization using serde. serde = ["dep:serde"] @@ -81,12 +81,12 @@ uuid = { workspace = true, features = ["serde", "v4", "js"] } ecolor = { workspace = true, optional = true } egui_plot = { workspace = true, optional = true } glam = { workspace = true, optional = true } -image = { workspace = true, optional = true, default-features = false } +image = { workspace = true, optional = true, default-features = false, features = [ + "jpeg", +] } mint = { workspace = true, optional = true } rand = { workspace = true, optional = true, features = ["std", "std_rng"] } serde = { workspace = true, optional = true, features = ["derive", "rc"] } -zune-core = { workspace = true, optional = true } -zune-jpeg = { workspace = true, optional = true } [dev-dependencies] diff --git a/crates/re_types/src/datatypes/tensor_data_ext.rs b/crates/re_types/src/datatypes/tensor_data_ext.rs index 479838ac1075..25ae101053a5 100644 --- a/crates/re_types/src/datatypes/tensor_data_ext.rs +++ b/crates/re_types/src/datatypes/tensor_data_ext.rs @@ -625,17 +625,17 @@ impl TensorData { pub fn from_jpeg_bytes(jpeg_bytes: Vec) -> Result { re_tracing::profile_function!(); - use zune_jpeg::JpegDecoder; - - let mut decoder = JpegDecoder::new(&jpeg_bytes); - decoder.decode_headers()?; - let (w, h) = decoder.dimensions().unwrap(); // Can't fail after a successful decode_headers + // Parse JPEG header: + use image::ImageDecoder as _; + let jpeg = image::codecs::jpeg::JpegDecoder::new(std::io::Cursor::new(&jpeg_bytes))?; + let (w, h) = jpeg.dimensions(); + let depth = jpeg.color_type().channel_count(); Ok(Self { shape: vec![ TensorDimension::height(h as _), TensorDimension::width(w as _), - TensorDimension::depth(3), + TensorDimension::depth(depth as _), ], buffer: TensorBuffer::Jpeg(jpeg_bytes.into()), }) diff --git a/crates/re_types/src/tensor_data.rs b/crates/re_types/src/tensor_data.rs index 8d7ea54d7ad1..6c93bbca92ed 100644 --- a/crates/re_types/src/tensor_data.rs +++ b/crates/re_types/src/tensor_data.rs @@ -44,9 +44,6 @@ pub enum TensorImageLoadError { #[error("The encoded tensor shape did not match its metadata {expected:?} != {found:?}")] InvalidMetaData { expected: Vec, found: Vec }, - - #[error(transparent)] - JpegDecode(#[from] zune_jpeg::errors::DecodeErrors), } #[cfg(feature = "image")] @@ -549,59 +546,30 @@ impl DecodedTensor { } pub fn decode_jpeg_bytes( - jpeg_bytes: &::re_types_core::ArrowBuffer, + jpeg_bytes: &[u8], [expected_height, expected_width, expected_channels]: [u64; 3], ) -> Result { re_tracing::profile_function!(format!("{expected_width}x{expected_height}")); - use zune_core::colorspace::ColorSpace; - use zune_core::options::DecoderOptions; - use zune_jpeg::JpegDecoder; - - let mut options = DecoderOptions::default(); - - let depth = if expected_channels == 1 { - options = options.jpeg_set_out_colorspace(ColorSpace::Luma); - 1 - } else { - // We decode to RGBA directly so we don't need to pad to four bytes later when uploading to GPU. - options = options.jpeg_set_out_colorspace(ColorSpace::RGBA); - 4 + use image::io::Reader as ImageReader; + let mut reader = ImageReader::new(std::io::Cursor::new(jpeg_bytes)); + reader.set_format(image::ImageFormat::Jpeg); + let img = { + re_tracing::profile_scope!("decode_jpeg"); + reader.decode()? }; - let mut decoder = JpegDecoder::new_with_options(jpeg_bytes.as_slice(), options); - let pixels = decoder.decode()?; - let (w, h) = decoder.dimensions().unwrap(); // Can't fail after a successful decode - - let (w, h) = (w as u64, h as u64); + let (w, h) = (img.width() as u64, img.height() as u64); + let channels = img.color().channel_count() as u64; - if w != expected_width || h != expected_height { + if (w, h, channels) != (expected_width, expected_height, expected_channels) { return Err(TensorImageLoadError::InvalidMetaData { expected: [expected_height, expected_width, expected_channels].into(), - found: [h, w, depth].into(), + found: [h, w, channels].into(), }); } - if pixels.len() as u64 != w * h * depth { - return Err(zune_jpeg::errors::DecodeErrors::Format(format!( - "Bug in zune-jpeg: Expected {w}x{h}x{depth}={} bytes, got {}", - w * h * depth, - pixels.len() - )) - .into()); - } - - let tensor = TensorData { - shape: vec![ - TensorDimension::height(h), - TensorDimension::width(w), - TensorDimension::depth(depth), - ], - buffer: TensorBuffer::U8(pixels.into()), - }; - let decoded_tensor = DecodedTensor(tensor); - - Ok(decoded_tensor) + Self::from_image(img) } } From b3f27fd55f6147e54c37864b5af2275228e00b95 Mon Sep 17 00:00:00 2001 From: Emil Ernerfeldt Date: Mon, 11 Mar 2024 20:32:25 +0100 Subject: [PATCH 2/2] Update to image 0.25 --- Cargo.lock | 55 +++++++++++++++++++++++++++++++++++++++++------------- Cargo.toml | 2 +- 2 files changed, 43 insertions(+), 14 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index d9e4a2e751e3..39dde1ebbcff 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -242,7 +242,7 @@ checksum = "1faa3c733d9a3dd6fbaf85da5d162a2e03b2e0033a90dceb0e2a90fdd1e5380a" dependencies = [ "clipboard-win", "core-graphics", - "image", + "image 0.24.6", "log", "objc", "objc-foundation", @@ -1586,7 +1586,7 @@ dependencies = [ "egui-wgpu", "egui-winit", "egui_glow", - "image", + "image 0.24.6", "js-sys", "log", "objc", @@ -1679,7 +1679,7 @@ dependencies = [ "egui", "ehttp", "enum-map", - "image", + "image 0.24.6", "log", "mime_guess2", "puffin", @@ -2290,7 +2290,7 @@ dependencies = [ "base64 0.13.1", "byteorder", "gltf-json", - "image", + "image 0.24.6", "lazy_static", "urlencoding", ] @@ -2617,6 +2617,20 @@ dependencies = [ "tiff", ] +[[package]] +name = "image" +version = "0.25.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a9b4f005360d32e9325029b38ba47ebd7a56f3316df09249368939562d518645" +dependencies = [ + "bytemuck", + "byteorder", + "num-traits", + "png", + "zune-core", + "zune-jpeg", +] + [[package]] name = "indent" version = "0.1.1" @@ -4259,7 +4273,7 @@ version = "0.15.0-alpha.2" dependencies = [ "ahash", "anyhow", - "image", + "image 0.25.0", "itertools 0.12.0", "once_cell", "parking_lot", @@ -4316,7 +4330,7 @@ dependencies = [ "egui", "egui_extras", "egui_plot", - "image", + "image 0.25.0", "itertools 0.12.0", "re_data_store", "re_entity_db", @@ -4597,7 +4611,7 @@ dependencies = [ "bytemuck", "console_error_panic_hook", "glam", - "image", + "image 0.25.0", "itertools 0.12.0", "macaw", "pollster", @@ -4917,7 +4931,7 @@ dependencies = [ "egui_plot", "glam", "half 2.3.1", - "image", + "image 0.25.0", "infer", "itertools 0.12.0", "linked-hash-map", @@ -5023,7 +5037,7 @@ dependencies = [ "egui_plot", "egui_tiles", "ehttp", - "image", + "image 0.25.0", "itertools 0.12.0", "once_cell", "poll-promise", @@ -5121,7 +5135,7 @@ dependencies = [ "egui", "egui_tiles", "glam", - "image", + "image 0.25.0", "itertools 0.12.0", "nohash-hasher", "once_cell", @@ -5477,7 +5491,7 @@ version = "0.15.0-alpha.2" dependencies = [ "anyhow", "clap", - "image", + "image 0.25.0", "re_log", "rerun", ] @@ -5499,7 +5513,7 @@ dependencies = [ "anyhow", "clap", "half 2.3.1", - "image", + "image 0.25.0", "ndarray", "re_log", "rerun", @@ -5561,7 +5575,7 @@ version = "0.15.0-alpha.2" dependencies = [ "anyhow", "clap", - "image", + "image 0.25.0", "re_log", "rerun", ] @@ -7734,6 +7748,21 @@ dependencies = [ "flate2", ] +[[package]] +name = "zune-core" +version = "0.4.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3f423a2c17029964870cfaabb1f13dfab7d092a62a29a89264f4d36990ca414a" + +[[package]] +name = "zune-jpeg" +version = "0.4.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ec866b44a2a1fd6133d363f073ca1b179f438f99e7e5bfb1e33f7181facfe448" +dependencies = [ + "zune-core", +] + [[package]] name = "zvariant" version = "3.15.0" diff --git a/Cargo.toml b/Cargo.toml index df65de9ef055..6ddf59953acb 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -140,7 +140,7 @@ glob = "0.3" gltf = "1.1" half = "2.3.1" hyper = "0.14" -image = { version = "0.24", default-features = false } +image = { version = "0.25", default-features = false } indent = "0.1" indicatif = "0.17.7" # Progress bar infer = "0.15" # infer MIME type by checking the magic number signaturefer MIME type by checking the magic number signature