optimizations for a project
Go to file
lifning 312227220e allow LZW to be done at Frame time rather than Encoder time 2022-07-01 22:36:12 -07:00
.github/workflows Add std as required feature for tests 2021-01-14 01:56:28 +01:00
benches A bad sample image 2021-10-19 01:36:19 +01:00
examples Change check example to be permissive 2021-09-18 01:12:11 +02:00
fuzz Add fuzz code for decode 2021-12-23 14:23:16 +01:00
gif-afl refactor: change try! with ? 2019-05-07 04:21:30 +02:00
gif-c Fixup the worst of ripping 2020-09-12 17:15:27 +02:00
src allow LZW to be done at Frame time rather than Encoder time 2022-07-01 22:36:12 -07:00
tests Make tests work on Windows 2022-02-10 14:38:46 +08:00
.gitignore Add Github Actions CI specification 2021-01-14 01:20:48 +01:00
Cargo.toml Release notes for version 0.11.4 2022-06-26 14:20:05 +02:00
Changes.md Release notes for version 0.11.4 2022-06-26 14:20:05 +02:00
LICENSE-APACHE Change the license from MIT to MIT/APACHE-2. Closes #15. 2016-02-28 19:44:40 +01:00
LICENSE-MIT Change the license from MIT to MIT/APACHE-2. Closes #15. 2016-02-28 19:44:40 +01:00
README.md Replace Travis badge with GitHub Actions and update doc links 2021-03-21 10:59:33 -07:00

README.md

GIF en- and decoding library Build Status

GIF en- and decoder written in Rust (API Documentation).

GIF encoding and decoding library

This library provides all functions necessary to de- and encode GIF files.

High level interface

The high level interface consists of the two types Encoder and Decoder.

Decoding GIF files

// Open the file
use std::fs::File;
let input = File::open("tests/samples/sample_1.gif").unwrap();
// Configure the decoder such that it will expand the image to RGBA.
let mut options = gif::DecodeOptions::new();
options.set_color_output(gif::ColorOutput::RGBA);
// Read the file header
let mut decoder = options.read_info(input).unwrap();
while let Some(frame) = decoder.read_next_frame().unwrap() {
    // Process every frame
}

Encoding GIF files

The encoder can be used to save simple computer generated images:

use gif::{Frame, Encoder, Repeat};
use std::fs::File;
use std::borrow::Cow;

let color_map = &[0xFF, 0xFF, 0xFF, 0, 0, 0];
let (width, height) = (6, 6);
let 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(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();
}

Frame::from_* can be used to convert a true color image to a paletted image with a maximum of 256 colors:

use std::fs::File;

// Get pixel data from some source
let mut pixels: Vec<u8> = vec![0; 30_000];
// Create frame from data
let frame = gif::Frame::from_rgb(100, 100, &mut *pixels);
// Create encoder
let mut image = File::create("target/indexed_color.gif").unwrap();
let mut encoder = gif::Encoder::new(&mut image, frame.width, frame.height, &[]).unwrap();
// Write frame to file
encoder.write_frame(&frame).unwrap();