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 19, 2022
1 parent fee7a26 commit a0f0f24
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 36 deletions.
2 changes: 1 addition & 1 deletion crates/bevy_render/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }
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 @@ -260,7 +260,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 @@ -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)| {
Expand Down
72 changes: 42 additions & 30 deletions crates/bevy_render/src/texture/image_texture_conversion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -171,12 +189,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 a0f0f24

Please sign in to comment.