Skip to content

Commit

Permalink
Expose Image conversion functions (fixes bevyengine#5452) (bevyengi…
Browse files Browse the repository at this point in the history
…ne#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.)
  • Loading branch information
micron-mushroom authored and ItsDoot committed Feb 1, 2023
1 parent e05aa0d commit 9da5c57
Show file tree
Hide file tree
Showing 2 changed files with 221 additions and 180 deletions.
18 changes: 10 additions & 8 deletions crates/bevy_render/src/texture/image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ use super::dds::*;
#[cfg(feature = "ktx2")]
use super::ktx2::*;

use super::image_texture_conversion::image_to_texture;
use crate::{
render_asset::{PrepareAssetError, RenderAsset},
render_resource::{Sampler, Texture, TextureView},
Expand Down Expand Up @@ -342,13 +341,18 @@ impl Image {
});
}

/// Convert a texture from a format to another
/// Only a few formats are supported as input and output:
/// Convert a texture from a format to another. Only a few formats are
/// supported as input and output:
/// - `TextureFormat::R8Unorm`
/// - `TextureFormat::Rg8Unorm`
/// - `TextureFormat::Rgba8UnormSrgb`
///
/// To get [`Image`] as a [`image::DynamicImage`] see:
/// [`Image::try_into_dynamic`].
pub fn convert(&self, new_format: TextureFormat) -> Option<Self> {
super::image_texture_conversion::texture_to_image(self)
self.clone()
.try_into_dynamic()
.ok()
.and_then(|img| match new_format {
TextureFormat::R8Unorm => {
Some((image::DynamicImage::ImageLuma8(img.into_luma8()), false))
Expand All @@ -362,9 +366,7 @@ impl Image {
}
_ => None,
})
.map(|(dyn_img, is_srgb)| {
super::image_texture_conversion::image_to_texture(dyn_img, is_srgb)
})
.map(|(dyn_img, is_srgb)| Self::from_dynamic(dyn_img, is_srgb))
}

/// Load a bytes buffer in a [`Image`], according to type `image_type`, using the `image`
Expand Down Expand Up @@ -402,7 +404,7 @@ impl Image {
reader.set_format(image_crate_format);
reader.no_limits();
let dyn_img = reader.decode()?;
Ok(image_to_texture(dyn_img, is_srgb))
Ok(Self::from_dynamic(dyn_img, is_srgb))
}
}
}
Expand Down
Loading

0 comments on commit 9da5c57

Please sign in to comment.