Crate gif

Source
Expand description

§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 mut decoder = gif::DecodeOptions::new();
// Configure the decoder such that it will expand the image to RGBA.
decoder.set_color_output(gif::ColorOutput::RGBA);
// Read the file header
let file = File::open("tests/samples/sample_1.gif").unwrap();
let mut decoder = decoder.read_info(file).unwrap();
while let Some(frame) = decoder.read_next_frame().unwrap() {
    // Process every frame
}

§Encoding GIF files

The encoder can be used so 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 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("tests/samples/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();

Modules§

streaming_decoder
Low-level, advanced decoder. Prefer Decoder instead, which can stream frames too.

Structs§

AnyExtension
A newtype wrapper around an arbitrary extension ID.
DecodeOptions
Options for opening a GIF decoder. DecodeOptions::read_info will start the decoder.
Decoder
GIF decoder. Create DecodeOptions to get started, and call DecodeOptions::read_info.
DecodingFormatError
An error returned in the case of the image not being formatted properly.
Encoder
GIF encoder.
Frame
A GIF frame

Enums§

ColorOutput
Output mode for the image data
DecodingError
Decoding error.
DisposalMethod
Disposal method
EncodingError
Encoding error.
EncodingFormatError
The image has incorrect properties, making it impossible to encode as a gif.
Extension
Known GIF extension labels.
ExtensionData
Extension data.
MemoryLimit
The maximum amount of memory the decoder is allowed to use for each frame
Repeat
Number of repetitions
Version
One version number of the GIF standard.