-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathasset_image.rs
90 lines (75 loc) · 2.44 KB
/
asset_image.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
use std::path::PathBuf;
use bevy_asset::{AssetPath, LoadContext, RenderAssetUsages};
use bevy_image::prelude::*;
use image::{DynamicImage, GenericImage as _, GenericImageView as _, Rgba};
use thiserror::Error;
/// Errors that can occur when loading an image.
#[non_exhaustive]
#[derive(Debug, Error)]
pub enum LoadImageError {
/// An error occurred while loading the image.
#[error("could not load image: {dependency}")]
Error { dependency: AssetPath<'static> },
}
/// Loads an image from `path` as an [`Image`] and returns it.
///
/// If `color_key` is provided, pixels in the image with the same color as the
/// key are made transparent.
///
/// If `flip_x` is `true`, the image is flipped horizontally.
///
/// If `flip_y` is `true`, the image is flipped vertically.
pub(crate) async fn load_image(
load_context: &mut LoadContext<'_>,
path: &str,
color_key: Option<(u8, u8, u8)>,
flip_x: bool,
flip_y: bool,
) -> Result<Image, LoadImageError> {
let path: PathBuf = path.into();
let loaded = load_context
.loader()
.immediate()
.load::<Image>(path.clone())
.await
.map_err(|_| LoadImageError::Error {
dependency: path.clone().into(),
})?;
let img = loaded.get();
let mut dyn_img = img
.clone()
.try_into_dynamic()
.map_err(|_| LoadImageError::Error {
dependency: path.clone().into(),
})?;
dyn_img = process_image(dyn_img, color_key, flip_x, flip_y);
Ok(Image::from_dynamic(
dyn_img,
true,
RenderAssetUsages::default(),
))
}
fn process_image(
src_img: DynamicImage,
color_key: Option<(u8, u8, u8)>,
flip_x: bool,
flip_y: bool,
) -> DynamicImage {
let (width, height) = src_img.dimensions();
let mut dest_img = DynamicImage::new_rgba8(width, height);
for y in 0..height {
for x in 0..width {
let target_x = if flip_x { width - 1 - x } else { x };
let target_y = if flip_y { height - 1 - y } else { y };
let mut pixel = src_img.get_pixel(x, y);
// Apply color keying if a color key is provided.
if let Some((r, g, b)) = color_key {
if pixel[0] == r && pixel[1] == g && pixel[2] == b {
pixel = Rgba([0, 0, 0, 0]); // make pixel transparent
}
}
dest_img.put_pixel(target_x, target_y, pixel);
}
}
dest_img
}