Code Monkey home page Code Monkey logo

pngjs's Introduction

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]
// 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 wrapup:

wrup -r PNGReader ./PNGReader.js

# or with the predefined make commands
make build-browser
make build-browser-min

pngjs's People

Contributors

arian avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.