Skip to content

Commit cbb884c

Browse files
Expose Image conversion functions (fixes #5452) (#5527)
## Solution Exposes the image <-> "texture" as methods on `Image`. ## Extra I'm wondering if `image_texture_conversion.rs` should be renamed to `image_conversion.rs`. That or the file be deleted altogether in favour of putting the code alongside the rest of the `Image` impl. Its kind-of weird to refer to the `Image` as a texture. Also `Image::convert` is a public method so I didn't want to edit its signature, but it might be nice to have the function consume the image instead of just passing a reference to it because it would eliminate a clone. ## Changelog > Rename `image_to_texture` to `Image::from_dynamic` > Rename `texture_to_image` to `Image::try_into_dynamic` > `Image::try_into_dynamic` now returns a `Result` (this is to make it easier for users who didn't read that only a few conversions are supported to figure it out.)
1 parent 6a54683 commit cbb884c

File tree

2 files changed

+221
-180
lines changed

2 files changed

+221
-180
lines changed

crates/bevy_render/src/texture/image.rs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ use super::dds::*;
55
#[cfg(feature = "ktx2")]
66
use super::ktx2::*;
77

8-
use super::image_texture_conversion::image_to_texture;
98
use crate::{
109
render_asset::{PrepareAssetError, RenderAsset},
1110
render_resource::{Sampler, Texture, TextureView},
@@ -342,13 +341,18 @@ impl Image {
342341
});
343342
}
344343

345-
/// Convert a texture from a format to another
346-
/// Only a few formats are supported as input and output:
344+
/// Convert a texture from a format to another. Only a few formats are
345+
/// supported as input and output:
347346
/// - `TextureFormat::R8Unorm`
348347
/// - `TextureFormat::Rg8Unorm`
349348
/// - `TextureFormat::Rgba8UnormSrgb`
349+
///
350+
/// To get [`Image`] as a [`image::DynamicImage`] see:
351+
/// [`Image::try_into_dynamic`].
350352
pub fn convert(&self, new_format: TextureFormat) -> Option<Self> {
351-
super::image_texture_conversion::texture_to_image(self)
353+
self.clone()
354+
.try_into_dynamic()
355+
.ok()
352356
.and_then(|img| match new_format {
353357
TextureFormat::R8Unorm => {
354358
Some((image::DynamicImage::ImageLuma8(img.into_luma8()), false))
@@ -362,9 +366,7 @@ impl Image {
362366
}
363367
_ => None,
364368
})
365-
.map(|(dyn_img, is_srgb)| {
366-
super::image_texture_conversion::image_to_texture(dyn_img, is_srgb)
367-
})
369+
.map(|(dyn_img, is_srgb)| Self::from_dynamic(dyn_img, is_srgb))
368370
}
369371

370372
/// Load a bytes buffer in a [`Image`], according to type `image_type`, using the `image`
@@ -402,7 +404,7 @@ impl Image {
402404
reader.set_format(image_crate_format);
403405
reader.no_limits();
404406
let dyn_img = reader.decode()?;
405-
Ok(image_to_texture(dyn_img, is_srgb))
407+
Ok(Self::from_dynamic(dyn_img, is_srgb))
406408
}
407409
}
408410
}

0 commit comments

Comments
 (0)