-
Notifications
You must be signed in to change notification settings - Fork 101
/
Copy pathimage.rs
53 lines (42 loc) · 1.69 KB
/
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
use minifb::{Key, Window, WindowOptions};
use png::{Decoder, Transformations};
use std::fs::File;
fn main() {
let mut decoder = Decoder::new(File::open("examples/resources/planet.png").unwrap());
// Reading the image in RGBA format.
decoder.set_transformations(Transformations::ALPHA);
let mut reader = decoder.read_info().unwrap();
let mut buffer = vec![0u32; reader.output_buffer_size() / 4];
// View of pixels as individual subpixels (avoids allocating a second pixel buffer).
let mut u8_buffer = unsafe {
std::slice::from_raw_parts_mut(
buffer.as_mut_ptr() as *mut u8,
buffer.len() * std::mem::size_of::<u32>(),
)
};
// Read the next frame. Currently this function should only be called once.
reader.next_frame(&mut u8_buffer).unwrap();
// convert RGBA buffer read by the reader to an ARGB buffer as expected by minifb.
for (rgba, argb) in u8_buffer.chunks_mut(4).zip(buffer.iter_mut()) {
// extracting the subpixels
let r = rgba[0] as u32;
let g = rgba[1] as u32;
let b = rgba[2] as u32;
let a = rgba[3] as u32;
// merging the subpixels in ARGB format.
*argb = a << 24 | r << 16 | g << 8 | b;
}
let width = reader.info().width as usize;
let height = reader.info().height as usize;
let mut window = Window::new(
"Image background example - Press ESC to exit",
width,
height,
WindowOptions::default(),
)
.expect("Unable to create the window");
window.set_target_fps(60);
while window.is_open() && !window.is_key_down(Key::Escape) {
window.update_with_buffer(&buffer, width, height).unwrap();
}
}