bevy icon indicating copy to clipboard operation
bevy copied to clipboard

Expose `Image` conversion functions (fixes #5452)

Open micron-mushroom opened this issue 1 year ago • 4 comments

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.)

micron-mushroom avatar Aug 01 '22 15:08 micron-mushroom

  • related: #5452

nicopap avatar Aug 01 '22 15:08 nicopap

Is this still WIP? I see commented code and builds fail.

Makes sense I think. There is a few more image formats that can be supported, I've a branch of bevy that adds four more compatible image formats. I'll upload them later and let you copy the code.

The idea of exposing the DynamicImage conversion functions is OK for me, it's already a great ergonomic win compared to the current situation, but it's not as optimal as implementing the GenericImage trait on Image (or a wrapper type): it would allow ability to modify a wider variety of Image (described in #5452) Still, we can merge this change and impl GenericImage later.

nicopap avatar Aug 01 '22 15:08 nicopap

Sorry about the messiness, I'm a hobbyist and it's the first time I've ever tried submitting code to a public repo. I tried implementing GenericImage on Image, but I got the sense I was rewriting code that has already been written. I'll go remove the commented code and run cargo fmt.

micron-mushroom avatar Aug 01 '22 15:08 micron-mushroom

I'll try the changes with some code that would benefit from this change and drop a review tomorrow.

nicopap avatar Aug 01 '22 16:08 nicopap

@IceSentry is it good to merge now?

micron-mushroom avatar Aug 04 '22 14:08 micron-mushroom

🤔 actually, the old function was pub(crate) so I guess it isn't a breaking change

IceSentry avatar Aug 04 '22 14:08 IceSentry

Change makes sense to me, and is well-documented and has basic tests.

bors r+

alice-i-cecile avatar Sep 03 '22 17:09 alice-i-cecile