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
use std::{
io::Cursor,
path::Path,
};
use glium::texture::{
RawImage2d,
Texture2d,
};
use image::{
ImageBuffer,
ImageFormat,
Rgba,
};
#[macro_export]
macro_rules! include_png {
($path:literal) => {{
let bytes = include_bytes!($path);
crate::png::image_from_bytes(bytes)
}};
}
pub fn image_from_bytes<'a>(bytes: Vec<u8>) -> RawImage2d<'a, u8> {
let cursor = Cursor::new(&bytes);
let image = image::load(cursor, ImageFormat::Png).unwrap().to_rgba8();
let image_dimensions = image.dimensions();
RawImage2d::from_raw_rgba_reversed(&image.into_raw(), image_dimensions)
}
pub fn load_png(path: &Path) -> RawImage2d<u8> {
let bytes = std::fs::read(path).expect("Could not read input image");
image_from_bytes(bytes)
}
pub fn write_png(texture: &Texture2d, path: &Path) {
let mut buffer =
ImageBuffer::new(texture.width() as u32, texture.height() as u32);
let sink: Vec<Vec<(u8, u8, u8, u8)>> = texture.read();
for (y, row) in sink.iter().rev().enumerate() {
for (x, pixel) in row.iter().enumerate() {
buffer.put_pixel(
x as u32,
y as u32,
Rgba([pixel.0, pixel.1, pixel.2, pixel.3]),
);
}
}
buffer.save(&path).expect("Could not write frame");
println!("Saved frame {}", path.display());
}