Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/image-rs/image-gif
GIF en- and decoder
https://github.com/image-rs/image-gif
hacktoberfest
Last synced: 3 days ago
JSON representation
GIF en- and decoder
- Host: GitHub
- URL: https://github.com/image-rs/image-gif
- Owner: image-rs
- License: apache-2.0
- Created: 2015-05-29T14:18:52.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2024-04-10T15:31:30.000Z (7 months ago)
- Last Synced: 2024-10-09T17:51:51.957Z (about 1 month ago)
- Topics: hacktoberfest
- Language: Rust
- Homepage:
- Size: 11.9 MB
- Stars: 150
- Watchers: 45
- Forks: 42
- Open Issues: 8
-
Metadata Files:
- Readme: README.md
- Changelog: Changes.md
- License: LICENSE-APACHE
Awesome Lists containing this project
README
# GIF en- and decoding library [![Build Status](https://github.com/image-rs/image-gif/workflows/Rust%20CI/badge.svg)](https://github.com/image-rs/image-gif/actions)
GIF en- and decoder written in Rust ([API Documentation](https://docs.rs/gif/)).
# 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`](https://docs.rs/gif/*/gif/struct.Encoder.html) and [`Decoder`](https://docs.rs/gif/*/gif/struct.Decoder.html).### Decoding GIF files
```rust
// 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:
```rust
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_*`](https://docs.rs/gif/*/gif/struct.Frame.html) can be used to convert a true color image to a paletted
image with a maximum of 256 colors:```rust
use std::fs::File;// Get pixel data from some source
let mut pixels: Vec = 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();
```