Skip to content

Commit

Permalink
update image to 0.24
Browse files Browse the repository at this point in the history
  • Loading branch information
mockersf committed Mar 6, 2022
1 parent 159fe52 commit 49b9fde
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 28 deletions.
2 changes: 1 addition & 1 deletion crates/bevy_render/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ bevy_window = { path = "../bevy_window", version = "0.6.0" }
bevy_utils = { path = "../bevy_utils", version = "0.6.0" }

# rendering
image = { version = "0.23.12", default-features = false }
image = { version = "0.24", default-features = false }

# misc
wgpu = { version = "0.12.0", features = ["spirv"] }
Expand Down
2 changes: 1 addition & 1 deletion crates/bevy_render/src/texture/hdr_texture_loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
4 changes: 0 additions & 4 deletions crates/bevy_render/src/texture/image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,6 @@ impl Image {
/// - `TextureFormat::R8Unorm`
/// - `TextureFormat::Rg8Unorm`
/// - `TextureFormat::Rgba8UnormSrgb`
/// - `TextureFormat::Bgra8UnormSrgb`
pub fn convert(&self, new_format: TextureFormat) -> Option<Self> {
super::image_texture_conversion::texture_to_image(self)
.and_then(|img| match new_format {
Expand All @@ -188,9 +187,6 @@ impl Image {
TextureFormat::Rgba8UnormSrgb => {
Some(image::DynamicImage::ImageRgba8(img.into_rgba8()))
}
TextureFormat::Bgra8UnormSrgb => {
Some(image::DynamicImage::ImageBgra8(img.into_bgra8()))
}
_ => None,
})
.map(super::image_texture_conversion::image_to_texture)
Expand Down
64 changes: 42 additions & 22 deletions crates/bevy_render/src/texture/image_texture_conversion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,22 +44,6 @@ pub(crate) fn image_to_texture(dyn_img: DynamicImage) -> Image {

data = i.into_raw();
}
DynamicImage::ImageBgr8(i) => {
let i = DynamicImage::ImageBgr8(i).into_bgra8();

width = i.width();
height = i.height();
format = TextureFormat::Bgra8UnormSrgb;

data = i.into_raw();
}
DynamicImage::ImageBgra8(i) => {
width = i.width();
height = i.height();
format = TextureFormat::Bgra8UnormSrgb;

data = i.into_raw();
}
DynamicImage::ImageLuma16(i) => {
width = i.width();
height = i.height();
Expand Down Expand Up @@ -111,6 +95,48 @@ pub(crate) fn image_to_texture(dyn_img: DynamicImage) -> 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(
Expand Down Expand Up @@ -147,12 +173,6 @@ pub(crate) fn texture_to_image(texture: &Image) -> Option<DynamicImage> {
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,
}
}

0 comments on commit 49b9fde

Please sign in to comment.