| 1 |
# GIF en- and decoding library [](https://github.com/image-rs/image-gif/actions) |
| 2 |
|
| 3 |
GIF en- and decoder written in Rust ([API Documentation](https://docs.rs/gif/)). |
| 4 |
|
| 5 |
# GIF encoding and decoding library |
| 6 |
|
| 7 |
This library provides all functions necessary to de- and encode GIF files. |
| 8 |
|
| 9 |
## High level interface |
| 10 |
|
| 11 |
The high level interface consists of the two types |
| 12 |
[`Encoder`](https://docs.rs/gif/*/gif/struct.Encoder.html) and [`Decoder`](https://docs.rs/gif/*/gif/struct.Decoder.html). |
| 13 |
|
| 14 |
### Decoding GIF files |
| 15 |
|
| 16 |
```rust |
| 17 |
// Open the file |
| 18 |
use std::fs::File; |
| 19 |
let input = File::open("tests/samples/sample_1.gif").unwrap(); |
| 20 |
// Configure the decoder such that it will expand the image to RGBA. |
| 21 |
let mut options = gif::DecodeOptions::new(); |
| 22 |
options.set_color_output(gif::ColorOutput::RGBA); |
| 23 |
// Read the file header |
| 24 |
let mut decoder = options.read_info(input).unwrap(); |
| 25 |
while let Some(frame) = decoder.read_next_frame().unwrap() { |
| 26 |
// Process every frame |
| 27 |
} |
| 28 |
``` |
| 29 |
|
| 30 |
### Encoding GIF files |
| 31 |
|
| 32 |
The encoder can be used to save simple computer generated images: |
| 33 |
|
| 34 |
```rust |
| 35 |
use gif::{Frame, Encoder, Repeat}; |
| 36 |
use std::fs::File; |
| 37 |
use std::borrow::Cow; |
| 38 |
|
| 39 |
let color_map = &[0xFF, 0xFF, 0xFF, 0, 0, 0]; |
| 40 |
let (width, height) = (6, 6); |
| 41 |
let beacon_states = [[ |
| 42 |
0, 0, 0, 0, 0, 0, |
| 43 |
0, 1, 1, 0, 0, 0, |
| 44 |
0, 1, 1, 0, 0, 0, |
| 45 |
0, 0, 0, 1, 1, 0, |
| 46 |
0, 0, 0, 1, 1, 0, |
| 47 |
0, 0, 0, 0, 0, 0, |
| 48 |
], [ |
| 49 |
0, 0, 0, 0, 0, 0, |
| 50 |
0, 1, 1, 0, 0, 0, |
| 51 |
0, 1, 0, 0, 0, 0, |
| 52 |
0, 0, 0, 0, 1, 0, |
| 53 |
0, 0, 0, 1, 1, 0, |
| 54 |
0, 0, 0, 0, 0, 0, |
| 55 |
]]; |
| 56 |
let mut image = File::create("target/beacon.gif").unwrap(); |
| 57 |
let mut encoder = Encoder::new(&mut image, width, height, color_map).unwrap(); |
| 58 |
encoder.set_repeat(Repeat::Infinite).unwrap(); |
| 59 |
for state in &beacon_states { |
| 60 |
let mut frame = Frame::default(); |
| 61 |
frame.width = width; |
| 62 |
frame.height = height; |
| 63 |
frame.buffer = Cow::Borrowed(&*state); |
| 64 |
encoder.write_frame(&frame).unwrap(); |
| 65 |
} |
| 66 |
``` |
| 67 |
|
| 68 |
[`Frame::from_*`](https://docs.rs/gif/*/gif/struct.Frame.html) can be used to convert a true color image to a paletted |
| 69 |
image with a maximum of 256 colors: |
| 70 |
|
| 71 |
```rust |
| 72 |
use std::fs::File; |
| 73 |
|
| 74 |
// Get pixel data from some source |
| 75 |
let mut pixels: Vec<u8> = vec![0; 30_000]; |
| 76 |
// Create frame from data |
| 77 |
let frame = gif::Frame::from_rgb(100, 100, &mut *pixels); |
| 78 |
// Create encoder |
| 79 |
let mut image = File::create("target/indexed_color.gif").unwrap(); |
| 80 |
let mut encoder = gif::Encoder::new(&mut image, frame.width, frame.height, &[]).unwrap(); |
| 81 |
// Write frame to file |
| 82 |
encoder.write_frame(&frame).unwrap(); |
| 83 |
``` |