• Stars
    star
    193
  • Rank 201,081 (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 nodejs C++ module that given a buffer with RGB or RGBA values creates a PNG image (in memory).

node-png

This is a node.js module, writen in C++, that uses libpng to produce a PNG image (in memory) from RGB or RGBA buffers.

The module exports three objects: Png, FixedPngStack and DynamicPngStack.

The Png object is for creating PNG images from an RGB, RGBA, or Grayscale buffer. The FixedPngStack is for joining a number of PNGs together (stacking them together) on a transparent blackground. The DynamicPngStack is for joining a number of PNGs together in the most space efficient way (so that the canvas border matches the leftmost upper corner of some PNG and the rightmost bottom corner of some PNG).

Png

The Png object takes 5 arguments in its constructor:

var png = new Png(buffer, width, height, buffer_type, bits_per_pixel);

The first argument, buffer, is a node.js Buffer filled with RGB(A) values. The second argument is integer width of the image. The third argument is integer height of the image. The fourth argument is 'rgb', 'bgr', 'rgba', 'bgra', or 'gray'. Defaults to 'rgb'. The fifth argument is valid only when buffer_type='gray'. Valid arguments are 8 (default) and 16.

The constructed png object has the encode method that's asynchronous in nature. You give it a callback and it will call your function with a node.js Buffer object containing the encoded PNG data when it's done:

png.encode(function (png_image) {
    // ...
});

The constructed png object also has encodeSync method that does the encoding synchronously and returns Buffer with PNG image data:

var png_image = png.encode();

You can either send the png_image to the browser, or write to a file, or do something else with it. See examples/ directory for some examples.

FixedPngStack

The FixedPngStack object takes 3 arguments in its constructor:

var fixed_png = new FixedPngStack(width, height, buffer_type);

The first argument is integer width of the canvas image. The second argument is integer height of the canvas image. The third argument is 'rgb', 'bgr', 'rgba or 'bgra'. Defaults to 'rgb'.

Now you can use the push method of fixed_png object to push buffers to the canvas. The push method takes 5 arguments:

fixed_png.push(buffer, x, y, w, h);

It pushes an RGB(A) image in buffer of width w and height h to the canvas position (x, y). You can push as many buffers to canvas as you want. After that you should call encode method or encodeSync method that will join all the pushed RGB(A) buffers together and return a single PNG.

All the regions that did not get covered will be transparent.

DynamicPngStack

The DynamicPngStack object doesn't take any dimension arguments because its width and height is dynamically computed. To create it, do:

var dynamic_png = new DynamicPngStack(buffer_type);

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

It provides four methods - push, encode, encodeSync, and dimensions. The push and encode methods are the same as in FixedPngStack. You push each of the RGB(A) buffers to the stack and after that you call encode or encodeSync.

The encode asynchronous method receives one more argument than others - it receives the dimensions object with x, y, width and height of the dynamic PNG. See the next paragraph for what the dimensions are.

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 PNGs together. One with width 100x40 at position (5, 10) and the other with width 20x20 at position (2, 210). First you create the DynamicPngStack object:

var dynamic_png = new DynamicPngStack();

Next you push the RGB(A) buffers of the two PNGs to it:

dynamic_png.push(png1_buf, 5, 10, 100, 40);
dynamic_png.push(png2_buf, 2, 210, 20, 20);

Now you can call encode to produce the final PNG:

var png = dynamic_png.encodeSync();

Now let's see what the dimensions are,

var dims = dynamic_png.dimensions();

Same asynchronously:

dynamic_png.encode(function (png, dims) {
    // png is the PNG image (in a node.js Buffer)
    // dims are its dimensions
});

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

How to compile?

To get the node-png module compiled, you need to have libpng and node.js installed. Then just run:

    node-gyp configure build

to build node-png module. It will be called png.node. To use it, make sure it's in NODE_PATH.

See also http://github.com/pkrumins/node-jpeg module that produces JPEG images. And also http://github.com/pkrumins/node-gif for producing GIF images.

If you wish to stream PNGs over a websocket or xhr-multipart, you'll have to base64 encode it. Use my http://github.com/pkrumins/node-base64 module to do that.

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-gif

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

node-video

A node.js module for streaming and recording HTML5 Theora videos
C++
202
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