bevy/crates/bevy_render/src/texture/png_texture_loader.rs
Thomas Herzog 4cf0f53eae use TextureFormat for Textures
This commit also inserts debug asserts that texture data roughly respects
the format.
2020-07-26 22:08:15 +02:00

27 lines
798 B
Rust

use super::{Texture, TextureFormat};
use anyhow::Result;
use bevy_asset::AssetLoader;
use bevy_math::Vec2;
use std::path::Path;
#[derive(Clone, Default)]
pub struct PngTextureLoader;
impl AssetLoader<Texture> for PngTextureLoader {
fn from_bytes(&self, _asset_path: &Path, bytes: Vec<u8>) -> Result<Texture> {
let decoder = png::Decoder::new(bytes.as_slice());
let (info, mut reader) = decoder.read_info()?;
let mut data = vec![0; info.buffer_size()];
reader.next_frame(&mut data)?;
Ok(Texture::new(
Vec2::new(info.width as f32, info.height as f32),
data,
TextureFormat::Rgba8UnormSrgb,
))
}
fn extensions(&self) -> &[&str] {
static EXTENSIONS: &[&str] = &["png"];
EXTENSIONS
}
}