From a0f0f24925194fd8b0c62e52b6f8962d686538cb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois?= Date: Sun, 6 Mar 2022 03:13:06 +0100 Subject: [PATCH] update image to 0.24 --- crates/bevy_render/Cargo.toml | 2 +- .../src/texture/hdr_texture_loader.rs | 2 +- crates/bevy_render/src/texture/image.rs | 4 -- .../src/texture/image_texture_conversion.rs | 72 +++++++++++-------- 4 files changed, 44 insertions(+), 36 deletions(-) diff --git a/crates/bevy_render/Cargo.toml b/crates/bevy_render/Cargo.toml index 14ba909fb3c5b..bdfceca4e6800 100644 --- a/crates/bevy_render/Cargo.toml +++ b/crates/bevy_render/Cargo.toml @@ -40,7 +40,7 @@ bevy_window = { path = "../bevy_window", version = "0.7.0-dev" } bevy_utils = { path = "../bevy_utils", version = "0.7.0-dev" } # rendering -image = { version = "0.23.12", default-features = false } +image = { version = "0.24", default-features = false } # misc wgpu = { version = "0.12.0", features = ["spirv"] } diff --git a/crates/bevy_render/src/texture/hdr_texture_loader.rs b/crates/bevy_render/src/texture/hdr_texture_loader.rs index 09890c0033a2a..81c539a061664 100644 --- a/crates/bevy_render/src/texture/hdr_texture_loader.rs +++ b/crates/bevy_render/src/texture/hdr_texture_loader.rs @@ -22,7 +22,7 @@ impl AssetLoader for HdrTextureLoader { "Format should have 32bit x 4 size" ); - let decoder = image::hdr::HdrDecoder::new(bytes)?; + let decoder = image::codecs::hdr::HdrDecoder::new(bytes)?; let info = decoder.metadata(); let rgb_data = decoder.read_image_hdr()?; let mut rgba_data = Vec::with_capacity(rgb_data.len() * format.pixel_size()); diff --git a/crates/bevy_render/src/texture/image.rs b/crates/bevy_render/src/texture/image.rs index fa4afe04924ef..3873a560a4681 100644 --- a/crates/bevy_render/src/texture/image.rs +++ b/crates/bevy_render/src/texture/image.rs @@ -260,7 +260,6 @@ impl Image { /// - `TextureFormat::R8Unorm` /// - `TextureFormat::Rg8Unorm` /// - `TextureFormat::Rgba8UnormSrgb` - /// - `TextureFormat::Bgra8UnormSrgb` pub fn convert(&self, new_format: TextureFormat) -> Option { super::image_texture_conversion::texture_to_image(self) .and_then(|img| match new_format { @@ -274,9 +273,6 @@ impl Image { TextureFormat::Rgba8UnormSrgb => { Some((image::DynamicImage::ImageRgba8(img.into_rgba8()), true)) } - TextureFormat::Bgra8UnormSrgb => { - Some((image::DynamicImage::ImageBgra8(img.into_bgra8()), true)) - } _ => None, }) .map(|(dyn_img, is_srgb)| { diff --git a/crates/bevy_render/src/texture/image_texture_conversion.rs b/crates/bevy_render/src/texture/image_texture_conversion.rs index 1c7e897555001..44611faafcddb 100644 --- a/crates/bevy_render/src/texture/image_texture_conversion.rs +++ b/crates/bevy_render/src/texture/image_texture_conversion.rs @@ -60,30 +60,6 @@ pub(crate) fn image_to_texture(dyn_img: DynamicImage, is_srgb: bool) -> Image { data = i.into_raw(); } - DynamicImage::ImageBgr8(i) => { - let i = DynamicImage::ImageBgr8(i).into_bgra8(); - - width = i.width(); - height = i.height(); - format = if is_srgb { - TextureFormat::Bgra8UnormSrgb - } else { - TextureFormat::Bgra8Unorm - }; - - data = i.into_raw(); - } - DynamicImage::ImageBgra8(i) => { - width = i.width(); - height = i.height(); - format = if is_srgb { - TextureFormat::Bgra8UnormSrgb - } else { - TextureFormat::Bgra8Unorm - }; - - data = i.into_raw(); - } DynamicImage::ImageLuma16(i) => { width = i.width(); height = i.height(); @@ -135,6 +111,48 @@ pub(crate) fn image_to_texture(dyn_img: DynamicImage, is_srgb: bool) -> Image { data = cast_slice(&raw_data).to_owned(); } + DynamicImage::ImageRgb32F(image) => { + width = image.width(); + height = image.height(); + format = TextureFormat::Rgba32Float; + + let mut local_data = + Vec::with_capacity(width as usize * height as usize * format.pixel_size()); + + for pixel in image.into_raw().chunks_exact(3) { + // TODO: use the array_chunks method once stabilised + // https://github.com/rust-lang/rust/issues/74985 + let r = pixel[0]; + let g = pixel[1]; + let b = pixel[2]; + let a = u16::max_value(); + + local_data.extend_from_slice(&r.to_ne_bytes()); + local_data.extend_from_slice(&g.to_ne_bytes()); + local_data.extend_from_slice(&b.to_ne_bytes()); + local_data.extend_from_slice(&a.to_ne_bytes()); + } + + data = local_data; + } + DynamicImage::ImageRgba32F(image) => { + width = image.width(); + height = image.height(); + format = TextureFormat::Rgba32Float; + + let raw_data = image.into_raw(); + + data = cast_slice(&raw_data).to_owned(); + } + // DynamicImage is now non exhaustive, catch future variants and convert them + _ => { + let image = dyn_img.into_rgba8(); + width = image.width(); + height = image.height(); + format = TextureFormat::Rgba8UnormSrgb; + + data = image.into_raw(); + } } Image::new( @@ -171,12 +189,6 @@ pub(crate) fn texture_to_image(texture: &Image) -> Option { texture.data.clone(), ) .map(DynamicImage::ImageRgba8), - TextureFormat::Bgra8UnormSrgb => ImageBuffer::from_raw( - texture.texture_descriptor.size.width, - texture.texture_descriptor.size.height, - texture.data.clone(), - ) - .map(DynamicImage::ImageBgra8), _ => None, } }