Coder Social home page Coder Social logo

image-gif's Introduction

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();

image-gif's People

Contributors

heroickatora avatar nwin avatar bvssvni avatar kornelski avatar shnatsel avatar rukai avatar fintelia avatar andylizi avatar foopub avatar okaneco avatar simon-frankau avatar philipc avatar veykril avatar johntitor avatar tirz avatar stoneg avatar mattico avatar martinlindhe avatar larsbergstrom avatar jdm avatar ignatenkobrain avatar francesca64 avatar fliegendewurst avatar eddyb avatar ben0x539 avatar amarok1412 avatar atouchet avatar adetaylor avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.