Expand description
Writing pictures to disk: still frames in any common format, and animated GIFs.
The encoding is done by the image crate, so a frame can be saved as
PNG, JPEG, BMP, TGA or GIF just by choosing the file extension, and a run
of frames can be written as one looping GIF with GifRecorder.
Everything here works on plain RGBA8 buffers with the first row at the
top, so it needs no window and no GPU. Frames read back from OpenGL come
bottom row first; flip_rows turns them the right way up.
use proof_engine::export::{save_rgba, GifRecorder};
// A 64x32 horizontal ramp, saved as a PNG...
let (w, h) = (64u32, 32u32);
let ramp: Vec<u8> = (0..w * h)
.flat_map(|i| { let v = ((i % w) * 4) as u8; [v, v, 255 - v, 255] })
.collect();
save_rgba(dir.join("ramp.png"), w, h, &ramp).unwrap();
// ...and as a three-frame looping GIF at 20 frames per second.
let mut gif = GifRecorder::create(dir.join("ramp.gif"), 1.0 / 20.0).unwrap();
for _ in 0..3 {
gif.push(w, h, &ramp).unwrap();
}
assert_eq!(gif.finish().unwrap(), 3);Structs§
- GifRecorder
- Writes frames into one looping animated GIF.
Functions§
- flip_
rows - Reverse the row order of an RGBA8 buffer in place.
- format_
for - The format a path’s extension asks for, if the
imagecrate can write it. - load_
rgba - Decode any supported image file into an RGBA8 buffer, top row first.
- save_
opaque - Save an RGBA8 buffer as an opaque image: the alpha channel is ignored.
- save_
rgba - Save an RGBA8 buffer (top row first) to
path. - scale_
to_ width - Scale an RGBA8 buffer to
widthpixels wide, keeping the aspect ratio.