Created
February 28, 2016 17:19
-
-
Save ben0x539/616e5e568d24edffefb8 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extern crate gif; | |
fn make_file() { | |
// http://www.piston.rs/image/gif/index.html#encoding-gif-files | |
use gif::{Frame, Encoder, Repeat, SetParameter}; | |
use std::fs::File; | |
use std::borrow::Cow; | |
let color_map = &[0xFF, 0xFF, 0xFF, 0, 0, 0]; | |
let (width, height) = (6, 6); | |
let mut beacon_states = [[ | |
0, 0, 0, 0, 0, 0, | |
0, 1, 1, 0, 0, 0, | |
0, 1, 1, 0, 0, 0, | |
0, 0, 0, 1, 1, 0, | |
0, 0, 0, 1, 1, 0, | |
0, 0, 0, 0, 0, 0, | |
], [ | |
0, 0, 0, 0, 0, 0, | |
0, 1, 1, 0, 0, 0, | |
0, 1, 0, 0, 0, 0, | |
0, 0, 0, 0, 1, 0, | |
0, 0, 0, 1, 1, 0, | |
0, 0, 0, 0, 0, 0, | |
]]; | |
let mut image = File::create("target/beacon.gif").unwrap();; | |
let mut encoder = Encoder::new(&mut image, width, height, color_map).unwrap(); | |
encoder.set(Repeat::Infinite).unwrap(); | |
for state in &beacon_states { | |
let mut frame = Frame::default(); | |
frame.width = width; | |
frame.height = height; | |
frame.buffer = Cow::Borrowed(&*state); | |
encoder.write_frame(&frame).unwrap(); | |
} | |
} | |
fn read_file() { | |
use std::fs::File; | |
use gif::Decoder; | |
let image = File::open("target/beacon.gif").unwrap(); | |
let mut decoder = gif::Decoder::new(image).read_info().unwrap(); | |
while let Some(frame) = decoder.read_next_frame().unwrap() { | |
println!("{:?}", frame); | |
} | |
} | |
fn main() { | |
make_file(); | |
read_file(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment