From ce0a242b48a6aa8d2b688006091fbdbbff579a2f Mon Sep 17 00:00:00 2001 From: Ian Douglas Scott Date: Wed, 26 Oct 2022 13:45:14 -0700 Subject: [PATCH] Change uses of BGRA for images to RGBA and use image-rs 0.24 Is there a reason this was using BGRA? RGBA is more common, though BGR was historically popular at one point. `image` 0.24 has removed support for BGR images. `image::Handle::from_pixels()` is renamed to `image::Handle::from_rgba()` to be more descriptive, and force any users to use RGBA instead of silently interpreting their pixels incorrectly. --- Cargo.toml | 2 +- native/src/image.rs | 14 +++++++------- wgpu/Cargo.toml | 2 +- wgpu/src/image/atlas.rs | 4 ++-- wgpu/src/image/raster.rs | 8 ++++---- wgpu/src/image/vector.rs | 5 +---- 6 files changed, 16 insertions(+), 19 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index f9b441a530..c3c60feb73 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -104,7 +104,7 @@ iced_glow = { version = "0.3", path = "glow", optional = true } thiserror = "1.0" [dependencies.image_rs] -version = "0.23" +version = "0.24" package = "image" optional = true diff --git a/native/src/image.rs b/native/src/image.rs index 516eb2dba9..1ff0618a5c 100644 --- a/native/src/image.rs +++ b/native/src/image.rs @@ -21,12 +21,12 @@ impl Handle { } /// Creates an image [`Handle`] containing the image pixels directly. This - /// function expects the input data to be provided as a `Vec` of BGRA + /// function expects the input data to be provided as a `Vec` of RGBA /// pixels. /// /// This is useful if you have already decoded your image. - pub fn from_pixels(width: u32, height: u32, pixels: Vec) -> Handle { - Self::from_data(Data::Pixels { + pub fn from_rgba(width: u32, height: u32, pixels: Vec) -> Handle { + Self::from_data(Data::Rgba { width, height, pixels, @@ -88,8 +88,8 @@ pub enum Data { /// In-memory data Bytes(Vec), - /// Decoded image pixels in BGRA format. - Pixels { + /// Decoded image pixels in RGBA format. + Rgba { /// The width of the image. width: u32, /// The height of the image. @@ -104,8 +104,8 @@ impl std::fmt::Debug for Data { match self { Data::Path(path) => write!(f, "Path({:?})", path), Data::Bytes(_) => write!(f, "Bytes(...)"), - Data::Pixels { width, height, .. } => { - write!(f, "Pixels({} * {})", width, height) + Data::Rgba { width, height, .. } => { + write!(f, "Rgba({} * {})", width, height) } } } diff --git a/wgpu/Cargo.toml b/wgpu/Cargo.toml index 55eec73f72..31eec5ddbe 100644 --- a/wgpu/Cargo.toml +++ b/wgpu/Cargo.toml @@ -52,7 +52,7 @@ path = "../graphics" features = ["font-fallback", "font-icons"] [dependencies.image_rs] -version = "0.23" +version = "0.24" package = "image" default-features = false optional = true diff --git a/wgpu/src/image/atlas.rs b/wgpu/src/image/atlas.rs index 953dd4e23a..ac015d082d 100644 --- a/wgpu/src/image/atlas.rs +++ b/wgpu/src/image/atlas.rs @@ -35,7 +35,7 @@ impl Atlas { mip_level_count: 1, sample_count: 1, dimension: wgpu::TextureDimension::D2, - format: wgpu::TextureFormat::Bgra8UnormSrgb, + format: wgpu::TextureFormat::Rgba8UnormSrgb, usage: wgpu::TextureUsages::COPY_DST | wgpu::TextureUsages::COPY_SRC | wgpu::TextureUsages::TEXTURE_BINDING, @@ -336,7 +336,7 @@ impl Atlas { mip_level_count: 1, sample_count: 1, dimension: wgpu::TextureDimension::D2, - format: wgpu::TextureFormat::Bgra8UnormSrgb, + format: wgpu::TextureFormat::Rgba8UnormSrgb, usage: wgpu::TextureUsages::COPY_DST | wgpu::TextureUsages::COPY_SRC | wgpu::TextureUsages::TEXTURE_BINDING, diff --git a/wgpu/src/image/raster.rs b/wgpu/src/image/raster.rs index 2b4d4af397..9d7fe41539 100644 --- a/wgpu/src/image/raster.rs +++ b/wgpu/src/image/raster.rs @@ -6,7 +6,7 @@ use bitflags::bitflags; #[derive(Debug)] pub enum Memory { - Host(::image_rs::ImageBuffer<::image_rs::Bgra, Vec>), + Host(::image_rs::ImageBuffer<::image_rs::Rgba, Vec>), Device(atlas::Entry), NotFound, Invalid, @@ -53,7 +53,7 @@ impl Cache { }) .unwrap_or_else(Operation::empty); - Memory::Host(operation.perform(image.to_bgra8())) + Memory::Host(operation.perform(image.to_rgba8())) } else { Memory::NotFound } @@ -65,12 +65,12 @@ impl Cache { .ok() .unwrap_or_else(Operation::empty); - Memory::Host(operation.perform(image.to_bgra8())) + Memory::Host(operation.perform(image.to_rgba8())) } else { Memory::Invalid } } - image::Data::Pixels { + image::Data::Rgba { width, height, pixels, diff --git a/wgpu/src/image/vector.rs b/wgpu/src/image/vector.rs index b08a0aa287..4525b27026 100644 --- a/wgpu/src/image/vector.rs +++ b/wgpu/src/image/vector.rs @@ -122,13 +122,10 @@ impl Cache { img.as_mut(), )?; - let mut rgba = img.take(); - rgba.chunks_exact_mut(4).for_each(|rgba| rgba.swap(0, 2)); - let allocation = texture_atlas.upload( width, height, - bytemuck::cast_slice(rgba.as_slice()), + bytemuck::cast_slice(img.data()), device, encoder, )?;