Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add webp support to texture loader #2600

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,18 @@ current changes on git with [previous release tags][git_tag_comparison].

[git_tag_comparison]: https://github.com/bevyengine/bevy/compare/v0.5.0...main

## Unreleased

### Added

- [Add webp as a supported texture format (C compiler required)][2600]

### Changed

### Fixed

[2600]: https://github.com/bevyengine/bevy/pull/2600

## Version 0.5.0 (2021-04-06)

### Added
Expand Down
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ dds = ["bevy_internal/dds"]
tga = ["bevy_internal/tga"]
jpeg = ["bevy_internal/jpeg"]
bmp = ["bevy_internal/bmp"]
webp = ["bevy_internal/webp"]

# Audio format support (MP3 is enabled by default)
flac = ["bevy_internal/flac"]
Expand Down
1 change: 1 addition & 0 deletions crates/bevy_internal/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ dds = ["bevy_render/dds"]
tga = ["bevy_render/tga"]
jpeg = ["bevy_render/jpeg"]
bmp = ["bevy_render/bmp"]
webp = ["bevy_render/webp"]

# Audio format support (MP3 is enabled by default)
flac = ["bevy_audio/flac"]
Expand Down
4 changes: 3 additions & 1 deletion crates/bevy_render/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ bevy_window = { path = "../bevy_window", version = "0.5.0" }
bevy_utils = { path = "../bevy_utils", version = "0.5.0" }

# rendering
image = { version = "0.23.12", default-features = false }
image = { version = "0.23.14", default-features = false }
webp_lib = { package = "webp", version = "0.1.3", optional = true}

# misc
serde = { version = "1", features = ["derive"] }
Expand Down Expand Up @@ -55,3 +56,4 @@ dds = ["image/dds"]
tga = ["image/tga"]
jpeg = ["image/jpeg"]
bmp = ["image/bmp"]
webp = ["image/webp", "webp_lib"]
6 changes: 4 additions & 2 deletions crates/bevy_render/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ use texture::HdrTextureLoader;
feature = "dds",
feature = "tga",
feature = "jpeg",
feature = "bmp"
feature = "bmp",
feature = "webp"
))]
use texture::ImageTextureLoader;

Expand Down Expand Up @@ -108,7 +109,8 @@ impl Plugin for RenderPlugin {
feature = "dds",
feature = "tga",
feature = "jpeg",
feature = "bmp"
feature = "bmp",
feature = "webp"
))]
{
app.init_asset_loader::<ImageTextureLoader>();
Expand Down
2 changes: 2 additions & 0 deletions crates/bevy_render/src/texture/image_texture_loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ const FILE_EXTENSIONS: &[&str] = &[
"jpeg",
#[cfg(feature = "bmp")]
"bmp",
#[cfg(feature = "webp")]
"webp",
];

impl AssetLoader for ImageTextureLoader {
Expand Down
17 changes: 16 additions & 1 deletion crates/bevy_render/src/texture/texture.rs
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,8 @@ impl Texture {
"image/jpeg" => Ok(image::ImageFormat::Jpeg),
"image/bmp" => Ok(image::ImageFormat::Bmp),
"image/x-bmp" => Ok(image::ImageFormat::Bmp),
#[cfg(feature = "webp")]
"image/webp" => Ok(image::ImageFormat::WebP),
_ => Err(TextureError::InvalidImageMimeType(mime_type.to_string())),
},
ImageType::Extension(extension) => image::ImageFormat::from_extension(extension)
Expand All @@ -242,7 +244,20 @@ impl Texture {
// needs to be added, so the image data needs to be converted in those
// cases.

let dyn_img = image::load_from_memory_with_format(buffer, format)?;
let dyn_img = match format {
#[cfg(feature = "webp")]
image::ImageFormat::WebP => webp_lib::Decoder::new(buffer)
.decode()
.ok_or_else(|| {
TextureError::ImageError(image::ImageError::Decoding(
image::error::DecodingError::from_format_hint(
image::error::ImageFormatHint::Exact(format),
),
))
})?
.to_image(),
_ => image::load_from_memory_with_format(buffer, format)?,
};
Ok(dyn_img.into())
}
}
Expand Down
1 change: 1 addition & 0 deletions docs/cargo_features.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
|tga|TGA picture format support.|
|jpeg|JPEG picture format support.|
|bmp|BMP picture format support.|
|webp|WEBP picture format support. (C compiler required)|
|flac|FLAC audio format support. It's included in bevy_audio feature.|
|wav|WAV audio format support.|
|vorbis|Vorbis audio format support.|
Expand Down