GIF 89a Encoder/Decoder

1.0.10 · abandoned · verified Sun Apr 19

omggif is a JavaScript library designed for low-level encoding and decoding of GIF 89a format images. It provides explicit `GifReader` and `GifWriter` classes that operate directly on byte buffers, typically `Uint8Array`s or Node.js `Buffer`s. This gives developers granular control over every aspect of GIF generation, including frame dimensions, color palettes, delays, and other GIF specification details. First released in 2013 and last updated to version 1.0.10 in June 2017, the package is considered abandoned and has received no further maintenance or feature updates. While historically useful for both browser and Node.js environments due to its lightweight and pure JavaScript implementation, modern projects might find it lacks contemporary features, active community support, or the high-level abstractions found in more actively maintained GIF manipulation libraries.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to use `omggif` to encode a simple two-frame animated GIF from raw pixel data and then how to initialize a `GifReader` for a given GIF byte buffer to extract metadata like dimensions and frame count. It highlights the low-level byte buffer manipulation required by the library.

const { GifWriter, GifReader } = require('omggif');

// --- Encoding a simple GIF ---
const width = 100;
const height = 100;
const frameCount = 2;

// A generous buffer size for a 2-frame, 100x100 GIF with a palette
// Realistically, calculate based on (width * height * numFrames) + header/metadata overhead
const outputBuffer = new Uint8Array((width * height * frameCount * 2) + 768); 

const writer = new GifWriter(outputBuffer, width, height, { loop: 0 });

// Define a simple palette (red, blue)
const palette = [
  0xFF0000, // Red
  0x0000FF  // Blue
];

// Frame 1: All red pixels (index 0 in palette)
const pixels1 = new Uint8Array(width * height).fill(0);
writer.addFrame(0, 0, width, height, pixels1, { palette, delay: 20 }); // 20ms delay

// Frame 2: All blue pixels (index 1 in palette)
const pixels2 = new Uint8Array(width * height).fill(1);
writer.addFrame(0, 0, width, height, pixels2, { palette, delay: 20 }); // 20ms delay

// Finalize the GIF and get the actual size
const finalSize = writer.end();
const encodedGifBuffer = outputBuffer.slice(0, finalSize);

console.log(`Generated GIF of size: ${finalSize} bytes`);
// In a Node.js environment, you could save this:
// require('fs').writeFileSync('output.gif', encodedGifBuffer);

// --- Decoding a GIF (example with a dummy buffer) ---
// For actual use, 'gifDataBuffer' would come from a file read or network request
// This is a minimal valid 1x1 GIF (header + LSD + GCT + IMG descriptor + LZW data + trailer)
const dummyGifDataBuffer = new Uint8Array([
  0x47, 0x49, 0x46, 0x38, 0x39, 0x61, // GIF89a
  0x01, 0x00, 0x01, 0x00,             // 1x1 canvas
  0x80, 0x00, 0x00,                   // GCT present, 2 colors (2^1)
  0xFF, 0xFF, 0xFF,                   // White (index 0)
  0x00, 0x00, 0x00,                   // Black (index 1)
  0x2C, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x01, 0x00, // Image Descriptor: 1x1 at 0,0
  0x02,                               // LZW Minimum Code Size
  0x02, 0x4C, 0x01,                   // LZW Data (clear, data, EOI)
  0x3B                                // GIF Trailer
]);

try {
  const reader = new GifReader(dummyGifDataBuffer);
  console.log(`Decoded GIF: ${reader.width}x${reader.height}, ${reader.numFrames()} frame(s)`);

  if (reader.numFrames() > 0) {
    const frameInfo = reader.frameInfo(0);
    console.log(`  First frame dimensions: ${frameInfo.width}x${frameInfo.height}`);
    // To get pixel data, you'd use decodeAndBlitFrameRGBA:
    // const pixelsRGBA = new Uint8Array(frameInfo.width * frameInfo.height * 4);
    // reader.decodeAndBlitFrameRGBA(0, pixelsRGBA);
  }
} catch (e) {
  console.error("Error reading GIF data:", e.message);
}

view raw JSON →