• Stars
    star
    171
  • Rank 222,266 (Top 5 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created over 12 years ago
  • Updated almost 4 years ago

Reviews

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

Repository Details

Pure JavaScript PNG decoder

PNG.js

PNG.js is a PNG decoder fully written in JavaScript. It works in Node.js as well as in (modern) browsers.

Usage

var PNGReader = require('png.js');

var reader = new PNGReader(bytes);
reader.parse(function(err, png){
	if (err) throw err;
	console.log(png);
});

Or with options:

reader.parse({
	data: false
}, function(err, png){
	if (err) throw err;
	console.log(png);
});

Currently the only option is:

  • data (boolean) - should it read the pixel data, or only the image information.

PNG object

The PNG object is passed in the callback. It contains all the data extracted from the image.

// most importantly
png.getWidth();
png.getHeight();
png.getPixel(x, y); // [red, blue, green, alpha]
png.getRGBA8Array(); // [r1, g1, b1, a1, r2, b2, g2, a2, ... ] - Same as canvas.getImageData
// but also
png.getBitDepth();
png.getColorType();
png.getCompressionMethod();
png.getFilterMethod();
png.getInterlaceMethod();
png.getPalette();

Using PNGReader in Node.js

PNGReader accepts an Buffer object, returned by fs.readFile, for example:

fs.readFile('test.png', function(err, buffer){

	var reader = new PNGReader(buffer);
	reader.parse(function(err, png){
		if (err) throw err;
		console.log(png);
	});

});

Using PNGReader in the Browser

PNGReader accepts a byte string, array of bytes or an ArrayBuffer.

For example using FileReader with file input fields:

var reader = new FileReader();

reader.onload = function(event){
	var reader = new PNGReader(event.target.result);
	reader.parse(function(err, png){
		if (err) throw err;
		console.log(png);
	});
};

fileInputElement.onchange = function(){
	reader.readAsArrayBuffer(fileInputElement.files[0]);
	// or, but less optimal
	reader.readAsBinaryString(fileInputElement.files[0]);
};

Or instead of using input elements, XHR can also be used:

var xhr = new XMLHttpRequest();
xhr.open('GET', 'image.png', true);
xhr.responseType = 'arraybuffer';

xhr.onload = function(e){
	if (this.status == 200){
		var reader = new PNGReader(this.response);
		reader.parse(function(err, png){
			if (err) throw err;
			console.log(png);
		});
	}
};

xhr.send();

Building Browser Version

PNG.js uses CommonJS modules which can be used in browsers after building it with browserify:

browserify ./PNGReader.js -s PNGReader

More Repositories

1

CoverJS

Instrument JavaScript to determine the testing coverage
JavaScript
126
star
2

partition-bundle

A browserify plugin to partition your modules in different bundles
JavaScript
114
star
3

cubic-bezier

A small cubic bézier timing function
JavaScript
76
star
4

mootools-form-upload

Upload your multiple files with HTML5 Drag and Drop, XMLHttpRequest with graceful degradation
JavaScript
48
star
5

CSSMatrix

CSSMatrix polyfill
JavaScript
44
star
6

Array.Math

Add all kind of mathematical methods to the Array Native
JavaScript
33
star
7

Complex

Calculations with Complex Numbers in JavaScript
JavaScript
26
star
8

amd-packager-php

An CommonJS AMD Packager in PHP
JavaScript
25
star
9

MooDialog

MooDialog is a MooTools plugin to replace the native alert(), confirm() and promt() javascript functions by more stylish ones. You can use it also for other DOM elements, create an IFrame dialog or even create an Ajax Dialog.
JavaScript
23
star
10

LISP.js

Parse and run LISP code in JavaScript
JavaScript
15
star
11

DirectAdminApi

PHP DirectAdminApi is a wrapper for the DirectAdmin Api to make the usage of the Api simple as much as possible
PHP
14
star
12

graphql-kotlin-test-dsl

A Kotlin DSL to write tests for your graphql-java schema
Kotlin
13
star
13

iFrameFormRequest

iFrameFormRequest is a MooTools plug-in that can upload files the Ajax way
JavaScript
13
star
14

Rss-Parser

This is a simple PHP Rss Parser
PHP
12
star
15

classy

Brings the MooTools 1.x Class sugar to Prime
JavaScript
10
star
16

prime-util

Utilities for MooTools Prime
JavaScript
9
star
17

MooDropMenu

MooDropMenu provides a simple Drop Down menu with infinit levels for MooTools
JavaScript
7
star
18

mootools-ui-runner

A Testrunner for MooTools More in PHP
JavaScript
6
star
19

MooDocs

A simple php script to generate MooTools docs, so you can parse the MooTools 1.3 docs for yourself and you're able to help debug the Docs
PHP
5
star
20

G-pack-2.0

The Gourmand Planner, by Andrey Kolobov, Peng Dai, Mausam, and Dan Weld
C++
5
star
21

elements-util

Utilities for elements
JavaScript
5
star
22

scopup

JavaScript scope analysis for Mozilla Parser AST
JavaScript
4
star
23

Element.Delegation

This is a simple MooTools Plugin for Event Delegation, now I would suggest the MooTools More 1.3 Delegation though
JavaScript
4
star
24

MilkyTester

Simultaneous browser tester with Node.js, Socket.io and Jasmine
JavaScript
4
star
25

Supersonic

A small and supersonic flow-controll library for Node.js
JavaScript
3
star
26

docker-android-react-native

docker image for android and react-native tools
Dockerfile
3
star
27

elements-ui

ui components for elements.
JavaScript
3
star
28

Fx.Dynamic

JavaScript
3
star
29

hashmap

Educational hashmap implementation in C.
C
3
star
30

Klass

Yet Another Class
JavaScript
3
star
31

MooDeps

This is a MooTools dependency checker written in MooTools Javascript and PHP
PHP
2
star
32

grunt-wrapup

Grunt Plugin for MooTools Wrapup - Convert your node modules into web modules.
JavaScript
2
star
33

animateImage

AnimateImage is an MooTools plugin to create an animated image
JavaScript
2
star
34

es-safe-ie

Transforms obj.finally into obj['finally'] so it's safe in IE < 9
JavaScript
2
star
35

mootools-docs-node

A MooTools Documentation viewer in Node.js
JavaScript
2
star
36

Drag

Another Drag Implementation
JavaScript
2
star
37

MooResize

MooResize is a MooTools plugin to resize images and other elements in an easy way
JavaScript
2
star
38

go-qoi

QOI decoder and encoder implemented in Go
Go
2
star
39

ptrace

C
2
star
40

Element.Style.Transform

An abstracted API for CSS3 transforms
JavaScript
2
star
41

reactive

Reactive Programming in JavaScript?? (actually, I don't really know exactly what Reactive Programming is)
JavaScript
2
star
42

wrapup-webbuilder

A webbuilder for downloading wrapup'd JavaScript
JavaScript
2
star
43

npm-login-cmd

JavaScript
2
star
44

MooCss

With MooCss (MooTools plugin) you can write Css as Javascript objects (just like Element.Style) to a style tag in the document head
JavaScript
2
star
45

Locale.AutoUse

Automatically determine the right language of the document to use for Locale
JavaScript
1
star
46

dom-binding

DOM data binding, push your objects to the DOM
JavaScript
1
star
47

node-sass-cli

Better CLI for node-sass
JavaScript
1
star
48

wrapup-middleware

Wrapup middleware for express.
JavaScript
1
star
49

metac

A MetaC to C compiler using Spoofax
TypeScript
1
star
50

version.is-sources

Source repo for version-is/version.is
1
star
51

packager-nightly

Download and build from github with packager, can be used with github hooks
PHP
1
star
52

selenium-wrapper

A selenium server wrapper, including installation and chrome webdriver
JavaScript
1
star
53

JAMD

Ported AMD-Packager-PHP to Java! To build and optimize your AMD modules into single files
Java
1
star
54

procs

Stupid Markdown to HTML documentation tool
JavaScript
1
star
55

vim

just showing my vim noobness
Vim Script
1
star
56

node-testbox

Run your tests in a sandboxed iframe
JavaScript
1
star
57

envarconst

Make JavaScript const variable, using esprima to change const declarations in your code, so a minifier like UgilfyJS can remove dead code.
JavaScript
1
star