Skip to content

jimp-dev/gifwrap

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

86 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

gifwrap

A Jimp-compatible library for working with GIFs

Overview

gifwrap is a minimalist library for working with GIFs in Javascript, supporting both single- and multi-frame GIFs. It reads GIFs into an internal representation that's easy to work with and allows for making GIFs from scratch. The frame class is structured to make it easy to move images between Jimp and gifwrap for more sophisticated image manipulation in Jimp, but the module has no dependency on Jimp.

The library uses Dean McNamee's omggif GIF encoder/decoder by default, but it employs an abstraction that allows using other encoders and decoders as well, once suitably wrapped.

At present, the module only works in Node.js. Includes Typescript typings.

Installation

npm install gifwrap --save

or

yarn add gifwrap

Usage

You can work with either GIF files or GIF encodings, and you can create GIFs from scratch.

The GifFrame class represents a single image frame, and the library largely represents a GIF as an array of GifFrame instances. For example, here is how you create a GIF from scratch:

const { GifFrame, GifUtil, GifCodec } = require('gifwrap');
const width = 200, height = 100;
const frames = [];

let frame = new GifFrame(width, height, { delayCentisecs: 10 });
// modify the pixels at frame.bitmap.data
frames.push(frame);
frame = new GifFrame(width, height, { delayCentisecs: 15 });
// modify the pixels at frame.bitmap.data
frames.push(frame);
// add more frames as desired...

// to write to a file...
GifUtil.write("my-creation.gif", frames, { loops: 3 }).then(gif => {
    console.log("written");
});

// to get the byte encoding without writing to a file...
const codec = new GifCodec();
codec.encodeGif(frames, { loops: 3 }).then(gif => {
    // byte encoding is now in gif.buffer
});

Images are represented within a GifFrame exactly as they are in a Jimp image. In particular, each GifFrame instance has a bitmap property having the following structure:

  • frame.bitmap.width - Width of image in pixels
  • frame.bitmap.height - Height of image in pixels
  • frame.bitmap.data - A Node.js Buffer that can be accessed like an array of bytes. Every 4 adjacent bytes represents the RGBA values of a single pixel. These 4 bytes correspond to red, green, blue, and alpha, in that order. Each pixel begins at an index that is a multiple of 4.

GIFs do not support partial transparency, so within frame.bitmap.data, pixels having alpha value 0x00 are treated as transparent and pixels of non-zero alpha value are treated as opaque. The encoder ignores the RGB values of transparent pixels.

gifwrap also provides utilities for reading GIF files and for parsing raw encodings:

const { GifUtil } = require('gifwrap');
GifUtil.read("fancy.gif").then(inputGif => {

    inputGif.frames.forEach(frame => {

        const buf = frame.bitmap.data;
        frame.scanAllCoords((x, y, bi) => {

            // Halve all grays on right half of image.

            if (x > inputGif.width / 2) {
                const r = buf[bi];
                const g = buf[bi + 1];
                const b = buf[bi + 2];
                const a = buf[bi + 3];
                if (r === g && r === b && a === 0xFF) {
                    buf[bi] /= 2;
                    buf[bi + 1] /= 2;
                    buf[bi + 2] /= 2;
                }
            }
        });
    });

    // Pass inputGif to write() to preserve the original GIF's specs.
    return GifUtil.write("modified.gif", inputGif.frames, inputGif).then(outputGif => {
        console.log("modified");
    });
});
const { GifUtil, GifCodec } = require('gifwrap');
const codec = new GifCodec();
const byteEncodingBuffer = getByteEncodingForSomeGif();

codec.decodeGif(byteEncodingBuffer).then(sourceGif => {

    const edgeLength = Math.min(sourceGif.width, sourceGif.height);
    sourceGif.frames.forEach(frame => {

        // Make each frame a centered square of size edgeLength x edgeLength.
        // Note that frames may vary in size and that reframe() works even if
        // the frame's image is smaller than the square. Should this happen,
        // the space surrounding the original image will be transparent.

        const xOffset = (frame.bitmap.width - edgeLength)/2;
        const yOffset = (frame.bitmap.height - edgeLength)/2;
        frame.reframe(xOffset, yOffset, edgeLength, edgeLength);
    });

    // The encoder determines GIF size from the frames, not the provided spec (sourceGif).
    return GifUtil.write("modified.gif", sourceGif.frames, sourceGif).then(outputGif => {
        console.log("modified");
    });
});

Notice that both encoding and decoding yields a GIF object. This is an instance of class Gif, and it provides information about the GIF, such as its size and how many times it loops. Notice also that you never call the Gif constructor to create a GIF. Instead, GIFs are created by providing a GifFrame array and a specification of GIF options. That specification is a subset of the properties of a Gif, so you can pass a previously-loaded Gif as a specification when writing or encoding. The encoder only uses the properties that can't be inferred from the frames -- namely, how many times the GIF loops and how to attempt to package the color tables within the encoding.

Leveraging Jimp

This module was originally written as a wrapper around Jimp images -- hence its name -- and then with frames as subclasses of Jimp images. Neither approach worked out well. The final approach requires just a tad of legwork to use gifwrap images within Jimp.

Both Jimp images and GifFrame instances share the bitmap property. By transferring this property back and forth between Jimp images and GifFrame instances, an image can be moved back and forth between the two libraries.

You can construct a GifFrame from a Jimp image as follows:

const { BitmapImage, GifFrame } = require('gifwrap');
const Jimp = require('jimp');
const j = new Jimp(200, 100, 0xFFFFFFFF);

// create a frame clone of a Jip bitmap
const fCopied = new GifFrame(new BitmapImage(j.bitmap));

// create a frame that shares a bitmap with Jimp (one way)
const fShared1 = new GifFrame(j.bitmap);

// create a frame that shares a bitmap with Jimp (another way)
const fShared2 = new GifFrame(1, 1, 0); // any GifFrame
fShared2.bitmap = j.bitmap;

And you can construct a Jimp instance from a GifFrame image as follows:

const { BitmapImage, GifFrame } = require('gifwrap');
const Jimp = require('jimp');
const frame = new GifFrame(200, 100, 0xFFFFFFFF);

// create a Jimp containing a clone of the frame bitmap
jimpCopied = GifUtil.copyAsJimp(Jimp, frame);

// create a Jimp that shares a bitmap with the frame
jimpShared = GifUtil.shareAsJimp(Jimp, frame);

Encoders and Decoders

gifwrap provides a default GIF encoder/decoder, but it is architected to be able to work with other encoders and decoders. The encoder and decoder may even be separate implementations. Encoders and decoders have varying capabilities, performance measures, and levels of reliability.

GifCodec is the default implementation, and it's both an encoder and a decoder. It's an adapter that wraps the omggif module. omggif appears to support a broad variety of GIFs, although it cannot produce an interlaced encoding (which there is little need for anyway). Although omggif doesn't include a test suite at present, gifwrap's test suite happens to test it reasonably well by virtue of using omggif underneath.

An encoder need only implement GifCodec's encodeGif() method, and a decoder need only implement its decodeGif() method. See the descriptions of those methods for the requirement details. Although GifCodec is stateless, so that instances an be reused across multiple encodings and decodings, third party encoders and decoders need not be. However, applications that use the library with stateful encoders will need to be aware of the need to create new instances.

To use a third-party encoder or decoder with the GifUtil write() and read() functions, just pass an instance of the encoder or decoder as the last parameter to write() or read(), respectively. For example:

const { GifUtil } = require('gifwrap');
const SnazzyDecoder = require('gifwrap-snazzy-decoder');
const AwesomeEncoder = require('gifwrap-awesome-encoder');

GifUtil.read("fancy.gif", new SnazzyDecoder()).then(gif =>

    /*...*/

    return GifUtil.write("modified.gif", gif.frames, gif, new AwesomeEncoder()).then(newGif => {
        console.log("modified");
    });
});

API Reference

The Typescript typings provide an exact specification of the API and also serve as a cheat sheet. The classes and namespaces follow:

new Gif(buffer, frames, spec)

Param Type Description
buffer Buffer A Buffer containing the encoded bytes
frames Array.<GifFrame> Array of frames found in the encoding
spec object Properties of the encoding as listed above

Gif is a class representing an encoded GIF. It is intended to be a read-only representation of a byte-encoded GIF. Only encoders and decoders should be creating instances of this class.

Property Description
width width of the GIF at its widest
height height of the GIF at its highest
loops the number of times the GIF should loop before stopping; 0 => loop indefinitely
usesTransparency boolean indicating whether at least one frame contains at least one transparent pixel
colorScope the scope of the color tables as encoded within the GIF; either Gif.GlobalColorsOnly (== 1) or Gif.LocalColorsOnly (== 2).
frames a array of GifFrame instances, one for each frame of the GIF
buffer a Buffer holding the encoding's byte data

Its constructor should only ever be called by the GIF encoder or decoder.

new BitmapImage()

BitmapImage is a class that hold an RGBA (red, green, blue, alpha) representation of an image. It's shape is borrowed from the Jimp package to make it easy to transfer GIF image frames into Jimp and Jimp images into GIF image frames. Each instance has a bitmap property having the following properties:

Property Description
bitmap.width width of image in pixels
bitmap.height height of image in pixels
bitmap.data a Buffer whose every four bytes represents a pixel, each sequential byte of a pixel corresponding to the red, green, blue, and alpha values of the pixel

Its constructor supports the following signatures:

  • new BitmapImage(bitmap: { width: number, height: number, data: Buffer })
  • new BitmapImage(bitmapImage: BitmapImage)
  • new BitmapImage(width: number, height: number, buffer: Buffer)
  • new BitmapImage(width: number, height: number, backgroundRGBA?: number)

When a BitmapImage is provided, the constructed BitmapImage is a deep clone of the provided one, so that each image's pixel data can subsequently be modified without affecting each other.

backgroundRGBA is an optional parameter representing a pixel as a single number. In hex, the number is as follows: 0xRRGGBBAA, where RR is the red byte, GG the green byte, BB, the blue byte, and AA the alpha value. An AA of 0x00 is considered transparent, and all non-zero AA values are treated as opaque.

bitmapImage.blit(toImage, toX, toY, fromX, fromY)

Param Type Description
toImage BitmapImage Image into which to copy the square
toX number x-coord in toImage of upper-left corner of receiving square
toY number y-coord in toImage of upper-left corner of receiving square
fromX number x-coord in this image of upper-left corner of source square
fromY number y-coord in this image of upper-left corner of source square

Copy a square portion of this image into another image.

Returns: BitmapImage - The present image to allow for chaining.

bitmapImage.fillRGBA(rgba)

Param Type Description
rgba number Color with which to fill image, expressed as a singlenumber in the form 0xRRGGBBAA, where AA is 0x00 for transparent and any other value for opaque.

Fills the image with a single color.

Returns: BitmapImage - The present image to allow for chaining.

bitmapImage.getRGBA(x, y)

Param Type Description
x number x-coord of pixel
y number y-coord of pixel

Gets the RGBA number of the pixel at the given coordinate in the form 0xRRGGBBAA, where AA is the alpha value, with alpha 0x00 encoding to transparency in GIFs.

Returns: number - RGBA of pixel in 0xRRGGBBAA form

bitmapImage.getRGBASet()

Gets a set of all RGBA colors found within the image.

Returns: Set - Set of all RGBA colors that the image contains.

bitmapImage.greyscale()

Converts the image to greyscale using inferred Adobe metrics.

Returns: BitmapImage - The present image to allow for chaining.

bitmapImage.reframe(xOffset, yOffset, width, height, fillRGBA)

Param Type Description
xOffset number The x-coord offset of the upper-left pixel of the desired image relative to the present image.
yOffset number The y-coord offset of the upper-left pixel of the desired image relative to the present image.
width number The width of the new image after reframing
height number The height of the new image after reframing
fillRGBA number The color with which to fill space added to the image as a result of the reframing, in 0xRRGGBBAA format, where AA is 0x00 to indicate transparent and a non-zero value to indicate opaque. This parameter is only required when the reframing exceeds the original boundaries (i.e. does not simply perform a crop).

Reframes the image as if placing a frame around the original image and replacing the original image with the newly framed image. When the new frame is strictly within the boundaries of the original image, this method crops the image. When any of the new boundaries exceed those of the original image, the fillRGBA must be provided to indicate the color with which to fill the extra space added to the image.

Returns: BitmapImage - The present image to allow for chaining.

bitmapImage.scale(factor)

Param Type Description
factor number The factor by which to scale up the image. Must be an integer >= 1.

Scales the image size up by an integer factor. Each pixel of the original image becomes a square of the same color in the new image having a size of factor x factor pixels.

Returns: BitmapImage - The present image to allow for chaining.

bitmapImage.scanAllCoords(scanHandler)

See: scanAllIndexes

Param Type Description
scanHandler function A function(x: number, y: number, bi: number) to be called for each pixel of the image with that pixel's x-coord, y-coord, and index into the data buffer. The function accesses the pixel at this coordinate by accessing the this.data at index bi.

Scans all coordinates of the image, handing each in turn to the provided handler function.

bitmapImage.scanAllIndexes(scanHandler)

See: scanAllCoords

Param Type Description
scanHandler function A function(bi: number) to be called for each pixel of the image with that pixel's index into the data buffer. The pixels is found at index 'bi' within this.data.

Scans all pixels of the image, handing the index of each in turn to the provided handler function. Runs a bit faster than scanAllCoords(), should the handler not need pixel coordinates.

new GifFrame()

GifFrame is a class representing an image frame of a GIF. GIFs contain one or more instances of GifFrame.

Property Description
xOffset x-coord of position within GIF at which to render the image (defaults to 0)
yOffset y-coord of position within GIF at which to render the image (defaults to 0)
disposalMethod GIF disposal method; only relevant when the frames aren't all the same size (defaults to 2, disposing to background color)
delayCentisecs duration of the frame in hundreths of a second
interlaced boolean indicating whether the frame renders interlaced

Its constructor supports the following signatures:

  • new GifFrame(bitmap: {width: number, height: number, data: Buffer}, options?)
  • new GifFrame(bitmapImage: BitmapImage, options?)
  • new GifFrame(width: number, height: number, buffer: Buffer, options?)
  • new GifFrame(width: number, height: number, backgroundRGBA?: number, options?)
  • new GifFrame(frame: GifFrame)

See the base class BitmapImage for a discussion of all parameters but options and frame. options is an optional argument providing initial values for the above-listed GifFrame properties. Each property within option is itself optional.

Provide a frame to the constructor to create a clone of the provided frame. The new frame includes a copy of the provided frame's pixel data so that each can subsequently be modified without affecting each other.

gifFrame.getPalette()

Get a summary of the colors found within the frame. The return value is an object of the following form:

Property Description
colors An array of all the opaque colors found within the frame. Each color is given as an RGB number of the form 0xRRGGBB. The array is sorted by increasing number. Will be an empty array when the image is completely transparent.
usesTransparency boolean indicating whether there are any transparent pixels within the frame. A pixel is considered transparent if its alpha value is 0x00.
indexCount The number of color indexes required to represent this palette of colors. It is equal to the number of opaque colors plus one if the image includes transparency.

Returns: object - An object representing a color palette as described above.