• Stars
    star
    212
  • Rank 186,122 (Top 4 %)
  • Language
    C++
  • Created over 14 years ago
  • Updated over 9 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A node.js C++ module for creating GIF images and animated GIFs from RGB or RGBA buffers.

This is a node.js module, writen in C++, that uses giflib to produce GIF images from RGB, BGR, RGBA or BGRA buffers.

This module exports Gif, DynamicGifStack, AnimatedGif and AsyncAnimatedGif objects.

Gif

The Gif object is for creating simple GIF images. Gif's constructor takes takes 5 arguments:

var gif = new Gif(buffer, width, height, quality, buffer_type);

The first argument, buffer, is a node.js Buffer that is filled with RGB, BGR, RGBA or BGRA values. The second argument is integer width of the image. The third argument is integer height of the image. The fourth argument is the quality of output image. The fifth argument is buffer type, 'rgb', 'bgr', 'rgba' or 'bgra'.

You can set the transparent color for the image by using:

gif.setTransparencyColor(red, green, blue);

Once you have constructed Gif object, call encode method to encode and produce GIF image. encode returns a node.js Buffer.

var image = gif.encode();

See tests/gif.js for a concrete example.

DynamicGifStack

The DynamicGifStack is for creating space efficient stacked GIF images. This
object doesn't take any dimension arguments because its width and height is dynamically computed. To create it, do:

var dynamic_gif = new DynamicGifStack(buffer_type);

The buffer_type again is 'rgb', 'bgr', 'rgba' or 'bgra', depending on what type of buffers you're gonna push to dynamic_gif.

It provides several methods - push, encode, dimensions, setTransparencyColor.

The push method pushes the buffer to position x, y with width, height.

The encode method produces the final GIF image.

The dimensions method is more interesting. It must be called only after encode as its values are calculated upon encoding the image. It returns an object with width, height, x and y properties. The width and height properties show the width and the height of the final image. The x and y propreties show the position of the leftmost upper PNG.

Here is an example that illustrates it. Suppose you wish to join two GIFs together. One with width 100x40 at position (5, 10) and the other with width 20x20 at position (2, 210). First you create the DynamicGifStack object:

var dynamic_gif = new DynamicGifStack('rgb');

Next you push the RGB buffers of the two GIFs to it:

dynamic_gif.push(gif1_buf, 5, 10, 100, 40);
dynamic_gif.push(gif2_buf, 2, 210, 20, 20);

Now you can call encode to produce the final GIF:

var image = dynamic_gif.encode();

Now let's see what the dimensions are,

var dims = dynamic_gif.dimensions();

The x position dims.x is 2 because the 2nd GIF is closer to the left. The y position dims.y is 10 because the 1st GIF is closer to the top. The width dims.width is 103 because the first GIF stretches from x=5 to x=105, but the 2nd GIF starts only at x=2, so the first two pixels are not necessary and the width is 105-2=103. The height dims.height is 220 because the 2nd GIF is located at 210 and its height is 20, so it stretches to position 230, but the first GIF starts at 10, so the upper 10 pixels are not necessary and height becomes 230-10=220.

See tests/dynamic-gif-stack.js for a concrete example.

AnimatedGif

Use this object to create animated gifs. The whole idea is to use push and endPush methods to separate frames. The push method is used for stacking, you can stack many updates in the frame. Then when you call endPush the data you had pushed will be taken as a whole and a new frame will be produced.

Once you're done call getGif to get the final gif (in memory).

You can also make AnimatedGif to write the final animated gif to file. Call setOutputFile method to set the output file.

There are two examples of animated gifs in tests/animated-gif directory. Take a look if you're interested:

* animated-gif.js shows how to produce an animated gif in memory and then write
                  it to a file yourself (this is not recommended as the files can grow
                  pretty big).
* animated-gif-file-writer.js shows how to produce an animated gif to a file.

AsyncAnimatedGif

This object makes the animated gif creating asynchronous. When you push a fragment to AsyncAnimatedGif, it writes the fragment to a file asynchronously, and then when you're done, it takes all these files and merges them, producing an animated gif.

You must specify the temporary directory where AsyncAnimatedGif will put the files to. Do it this way:

var animated = new AsyncAnimatedGif(width, height);
animated.setTmpDir('/tmp');

You can only write the animated gifs to files with this object. Don't forget to set the output file via setOutputFile:

animated.setOutputFile('animation.gif');

Now you can push fragments to it and separate frames by endPush. After you're done with frames, call encode to produce the final gif.

The encode method takes a single argument - function that gets called when the final gif is produced. The function takes two arguments - status which will be true or false, and error which will be the error message in case status is false, or undefined if status is true:

animated.encode(function (status, error) {
    if (status) {
        console.log('animated gif successful');
    }
    else {
        console.log('animated gif unsuccessful: ' + error);
    }
});

Take a look at tests/animated-gif/animated-gif-async.js file to see how it works in a real example.

How to Install?

To compile the module, make sure you have giflib 1 and run:

node-waf configure build

This will produce gif.node object file. Don't forget to point NODE_PATH to node-gif directory to use it.

Another way to get it installed is to use node.js package manager npm [2]. To get node-gif installed via npm, run:

npm install gif

This will take care of everything and you don't need to worry about NODE_PATH.

Wondering about PNG or JPEG?

Wonder no more, I also wrote modules to produce PNG and JPEG images. Here they are:

http://github.com/pkrumins/node-png
http://github.com/pkrumins/node-jpeg

More Repositories

1

the-little-schemer

All the Scheme code examples from the book "The Little Schemer"
Scheme
662
star
2

node-lazy

lazy lists for node.js
JavaScript
490
star
3

nodejs-proxy

A HTTP proxy server written in node.js
JavaScript
416
star
4

node-tree-kill

kill trees of processes
JavaScript
330
star
5

bash-redirections-cheat-sheet

Bash redirections cheat sheet
309
star
6

perl1line.txt

collection of handy perl one-liner scripts
241
star
7

xgoogle

Python library to Google services (google search, google sets, google translate, sponsored links)
Python
217
star
8

node-video

A node.js module for streaming and recording HTML5 Theora videos
C++
202
star
9

node-png

A nodejs C++ module that given a buffer with RGB or RGBA values creates a PNG image (in memory).
C
193
star
10

stackvm

Configure, network, and interact with virtual machines entirely over the web
JavaScript
141
star
11

hacker-top

A top-like program for monitoring hacker news from the console
Python
133
star
12

node-iptables

basic iptables control via nodejs
JavaScript
112
star
13

node-image

Unifies node-png, node-jpeg and node-gif (for great good)
JavaScript
99
star
14

the-seasoned-schemer

All the Scheme code examples from the book "The Seasoned Schemer"
Scheme
97
star
15

bash-vi-editing-mode-cheat-sheet

Bash has two input modes - emacs and vi. This is vi input/editing mode keyboard shortcut cheat sheet.
91
star
16

the-little-mler

All the ML code examples from the book "The Little MLer"
OCaml
84
star
17

awk-cheat-sheet

This is AWK programming language cheat sheet.
76
star
18

bithacks.h

bithacks.h is a C header file containing useful bit manipulation macros
C
73
star
19

node-jpeg

A nodejs C++ module that given a buffer with RGB or RGBA values creates a JPEG image in memory.
C++
65
star
20

node-base64

A base64 encoding and decoding C++ module for node.js that actually works! (node now has it's own base64 encoding, see docs!)
C
64
star
21

perl-tcp-proxy

A simple TCP proxy written in Perl. Uses IO::Socket::INET and IO::Select for multiplexing.
Perl
60
star
22

node-jsmin

javascript minimizer for node.js
JavaScript
52
star
23

reddit-top

A top-like program for monitoring reddit from the console
Python
52
star
24

bash-history-cheat-sheet

This is the bash history cheat sheet. It summarizes everything there is to know about working efficiently with command line history in bash.
52
star
25

the-reasoned-schemer

All the logic programming code examples from the book "The Reasoned Schemer"
Scheme
49
star
26

catonmat.net

The new catonmat.net website.
Python
46
star
27

node-browser

Provides a Browser for easy web browsing from node.js
JavaScript
44
star
28

sed-cheat-sheet

This is sed (unix stream editor) cheat sheet.
44
star
29

node-supermarket

A key/value store based on sqlite for node.js that actually works.
JavaScript
43
star
30

screen-cheat-sheet

This is the screen terminal emulator cheat sheet. It lists the default keyboard shortcuts for working with screen.
42
star
31

bash-emacs-editing-mode-cheat-sheet

Bash has two input modes - emacs and vi. This is emacs input/editing mode keyboard shortcut cheat sheet.
33
star
32

set-operations-in-unix-shell

This is an implementation of 14 set operations by using only Unix utilities such as sort, uniq, diff, comm, cat, head, tail, awk, and others.
32
star
33

social-scraper

Social scraper is a Perl program that scrapes reddit, digg, stumbleupon, delicious, furl, flickr, simpy, boingboing, wired for content that matches the given patterns.
Perl
29
star
34

node-async

An example async library for node.js
C++
27
star
35

bash-one-liners

Bash one-liners
26
star
36

perl-tcp-proxy2

Program for my "A TCP Proxy in Perl" article
Perl
24
star
37

the-little-prover

All code examples from "The Little Prover" book
Scheme
22
star
38

adns

Asynchronous DNS resultion in Python by using adns C library.
21
star
39

busy-beaver

Implementation of a Turing Machine that runs the Busy Beaver programs.
C++
19
star
40

social-submitter

Submit your stories to Reddit, Hacker News, Twitter, Plurk, Identi.ca, Facebook at once!
JavaScript
19
star
41

youtube-uploader

A Perl program that uploads videos to YouTube without any APIs.
Perl
18
star
42

invoice

generate pdf invoices from latex via pdflatex
JavaScript
17
star
43

node-passwd

Node.js module to manage /etc/passwd
JavaScript
17
star
44

gnu-awk-youtube-downloader

A program written in GNU Awk that downloads YouTube Videos. Proof of concept that AWK can do binary IO and networking effectively.
17
star
45

ed-cheat-sheet

This is ed (the unix text editor) cheat sheet. It lists all the commands and how to do line addressing.
16
star
46

gnu-coreutils-cheat-sheet

Gnu Coreutils Cheat Sheet
15
star
47

load-status-server

Windows load status server that returns cpu load, memory usage and disk usage through JSON
C++
15
star
48

util-linux-cheat-sheet

Util-Linux Cheat Sheet
15
star
49

node-chess

Node chess - Node.js knockout competition
JavaScript
12
star
50

ssh-key-manager

manage ssh keys on the server side (can be used with ssh-key-widget)
JavaScript
12
star
51

node-des

A C++ node.js module that does DES encryption and actually works (node's crypto module didn't work.)
C++
12
star
52

node-rfb

implements the client-side of the rfb protocol that vnc uses
JavaScript
12
star
53

perl-youtube-downloader-one-liner

This is a Perl one-liner that downloads YouTube videos.
12
star
54

node-bufferdiff

A C++ module for node-js to test if two buffers are equal, fast (could add diff later).
C++
12
star
55

vbscript-youtube-downloader

A program written in VBScript that downloads YouTube videos.
Visual Basic
12
star
56

supermarket-cart

Connect session store using supermarket
JavaScript
11
star
57

keyboard

provides english keyboard (used as a widget for browserling)
JavaScript
11
star
58

speak-text-files-to-wav

Speaks text files to wav using Microsoft Speech API
C++
10
star
59

perl-predefined-variable-cheat-sheet

This is Perl special variable (predefined variable) cheat sheet. It lists all the Perl variables.
10
star
60

dnode

Simple asynchronous remote method invocation for node.js
JavaScript
10
star
61

node-time

This module provides some time functions for node.js (i forgot about Date() so this module is useless)
C++
8
star
62

reddit-comment-finder

A program that finds all the comments a given reddit user has made.
Python
8
star
63

dotfiles

Vim Script
8
star
64

bitly

shorten urls with bitly without api
Perl
7
star
65

node-quine

A node.js module that exports a function that prints itself
7
star
66

firefox-update-dialog-killer

a simple win32 program that detects and closes the firefox update dialog
C++
6
star
67

node-number-range

number ranges
JavaScript
6
star
68

node-png-sync

sync part of node-png that works on windows with node-gyp on node 0.6
C
6
star
69

youtube-video-downloader-in-perl

Wrote this real quick as I needed to get some vids
Perl
6
star
70

chrome-dialog-killer

Kills the nasty "your profile can not be used because it is from a newer version of Google Chrome" dialog by clicking OK button
C++
5
star
71

hwnd-finder

find hwnds easily
C++
5
star
72

rfb-protocols

A node.js module for various RFB encoding (RRE, Hextile, Tight, etc.) conversion to RGB buffers.
C++
5
star
73

TextToWav

Converts text files to wav using Loquendo text-to-speech sdk
C
5
star
74

perl-pack-unpack-printf-sprintf-cheat-sheet

This is Perl pack/unpack template parameter and printf/sprintf format specifier cheat sheet
4
star
75

quick-cd

Quick cd is a utility for bash that keeps track of your most often used directories and allows you to cd to them with as few keystrokes as possible.
4
star
76

feedburner-graph-generator

Current feedburner graphs suck. I wrote this Perl program to generate the nice graphs they used to have in 2008.
4
star
77

install-computer

Shell
3
star
78

plurk-command-line-plurker

A Perl program for plurking from command line
Perl
3
star
79

node-bufferlist

Create linked lists of Buffer objects
JavaScript
3
star
80

webdev-template

Web development template with reset.css
3
star
81

reddit-river

The old reddit river website for mobile devices
3
star
82

http-async-retry

HTTP::Async with retry
Perl
2
star
83

quick-history

Better interface to bash's (and other shell) CTRL+R for reverse-search-history
2
star
84

dockerfiles

Dockerfile
2
star
85

node-parse-users-exe-output

parses output from users.exe on windows
JavaScript
2
star
86

rackspace-tools

some tools to manage rackspace cloud servers
JavaScript
2
star
87

codinghorror-keyword-analyzer

A Perl program that parses public statcounter data for codinghorror.com blog and stores the search keywords in an SQLite database.
2
star
88

node-rdesktop

Client side of the RDP protocol that Windows uses for remote deskop
JavaScript
2
star
89

node-crashing-async-buf

for ry - a crashing Buffer::New example in eio_custom.
C++
2
star
90

stacked-linux

A small linux distribution for use on routers
2
star
91

utf8-length

return the number of bytes in a unicode string
JavaScript
2
star
92

lulzbot

An IRC bot for node.js
JavaScript
2
star
93

latex2html

convert latex to html (works for me)
Perl
2
star
94

utf8-bytes

return an array of bytes from a unicode string
JavaScript
2
star
95

lwp-protocol-http-socketeer

http protocol implementation for LWP that uses a proxy
Perl
1
star
96

startupsupper.github.com

Recipes for Bootstrappers & Hungry Hackers
JavaScript
1
star
97

testling-ci-badge-checker

This perl program checks github repos for testling-ci badges
Perl
1
star
98

testling-ci-test-example

testling-ci-test-example
JavaScript
1
star
99

picurls.com

This is repository of picurls.com website. picurls: picture buzz! buzziest pictures on the net!
1
star
100

digpicz

The old digpicz.com website
1
star