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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
use crate::definition::*;
use crate::io::*;
use std::path::Path;
impl<'a> Loaded<'a> {
pub fn image<P: AsRef<Path>>(&'a self, path: P) -> Result<CPUTexture<u8>, IOError> {
use image::GenericImageView;
let img = image::load_from_memory(self.bytes(path)?)?;
let bytes = img.to_bytes();
let number_of_channels = bytes.len() / (img.width() * img.height()) as usize;
let format = match number_of_channels {
1 => Ok(Format::R),
2 => Ok(Format::RG),
3 => Ok(Format::RGB),
4 => Ok(Format::RGBA),
_ => Err(IOError::FailedToLoad {
message: format!("Could not determine the pixel format for the texture."),
}),
}?;
Ok(CPUTexture {
data: bytes,
width: img.width() as usize,
height: img.height() as usize,
format,
..Default::default()
})
}
pub fn cube_image<P: AsRef<Path>>(
&'a self,
right_path: P,
left_path: P,
top_path: P,
bottom_path: P,
front_path: P,
back_path: P,
) -> Result<CPUTexture<u8>, IOError> {
let mut right = self.image(right_path)?;
let left = self.image(left_path)?;
let top = self.image(top_path)?;
let bottom = self.image(bottom_path)?;
let front = self.image(front_path)?;
let back = self.image(back_path)?;
right.data.extend(left.data);
right.data.extend(top.data);
right.data.extend(bottom.data);
right.data.extend(front.data);
right.data.extend(back.data);
Ok(right)
}
}
#[cfg(not(target_arch = "wasm32"))]
impl Saver {
pub fn save_pixels<P: AsRef<Path>>(
path: P,
pixels: &[u8],
width: usize,
height: usize,
) -> Result<(), IOError> {
let mut pixels_out = vec![0u8; width * height * 4];
for row in 0..height {
for col in 0..width {
for i in 0..4 {
pixels_out[4 * width * (height - row - 1) + 4 * col + i] =
pixels[4 * width * row + 4 * col + i];
}
}
}
image::save_buffer(
path,
&pixels_out,
width as u32,
height as u32,
image::ColorType::Rgba8,
)?;
Ok(())
}
}