Skip to content

Commit

Permalink
Parse missing mime types (bevyengine#12028)
Browse files Browse the repository at this point in the history
# Objective

This PR adds some missing mime types to the
`ImageFormat::from_mime_type` method. As discussed [in this comment on
the Discord Bevy
community](https://discord.com/channels/691052431525675048/691052431974465548/1209904290227949729):

> It's strange that Bevy supports parsing `ImageFormat::WebP` from a
.webp str extension in the method below, but not from the mime type.
> 
> In comparison, the image crate does parse it:
https://github.com/image-rs/image/blob/master/src/image.rs#L170

# Solution

For each of the missing mime types, I added them based on the
`ImageFormat::from_mime_type` of the image crate:
https://github.com/image-rs/image/blob/master/src/image.rs#L209, except
for `ImageFormat::Basis` and `ImageFormat::Ktx2` which are not present
in the image crate, and I ignore if they have a mime type or not*

\* apparently nowadays there is an official mime type: `image/ktx2`
https://www.iana.org/assignments/media-types/image/ktx2

Any feedback is welcome! I thought of refactoring a bit more and
delegating the mime type parsing to the image crate (and possibly the
same for extensions), let me know if that's desired 🙂
  • Loading branch information
Maximetinu authored and msvbg committed Feb 26, 2024
1 parent ffe3e53 commit 10320ee
Showing 1 changed file with 10 additions and 0 deletions.
10 changes: 10 additions & 0 deletions crates/bevy_render/src/texture/image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,23 @@ pub enum ImageFormat {
impl ImageFormat {
pub fn from_mime_type(mime_type: &str) -> Option<Self> {
Some(match mime_type.to_ascii_lowercase().as_str() {
"image/avif" => ImageFormat::Avif,
"image/bmp" | "image/x-bmp" => ImageFormat::Bmp,
"image/vnd-ms.dds" => ImageFormat::Dds,
"image/vnd.radiance" => ImageFormat::Hdr,
"image/gif" => ImageFormat::Gif,
"image/x-icon" => ImageFormat::Ico,
"image/jpeg" => ImageFormat::Jpeg,
"image/ktx2" => ImageFormat::Ktx2,
"image/png" => ImageFormat::Png,
"image/x-exr" => ImageFormat::OpenExr,
"image/x-portable-bitmap"
| "image/x-portable-graymap"
| "image/x-portable-pixmap"
| "image/x-portable-anymap" => ImageFormat::Pnm,
"image/x-targa" | "image/x-tga" => ImageFormat::Tga,
"image/tiff" => ImageFormat::Tiff,
"image/webp" => ImageFormat::WebP,
_ => return None,
})
}
Expand Down

0 comments on commit 10320ee

Please sign in to comment.