A simple and non-blocking PNG encoder / decoder for Node.
forked from node-png.
$ npm install png-async --save$ git clone https://github.com/kanreisa/node-png-async.git
$ cd node-png-async
$ npm install
$ npm run typings-install
$ npm run buildvar fs = require('fs');
var png = require('png-async');
fs.createReadStream('in.png')
.pipe(png.createImage({
filterType: 4
}))
.on('parsed', function () {
// Note: this is blocking. be careful.
for (var y = 0; y < this.height; y++) {
for (var x = 0; x < this.width; x++) {
var idx = (this.width * y + x) << 2;
// invert color
this.data[idx] = 255 - this.data[idx];
this.data[idx+1] = 255 - this.data[idx+1];
this.data[idx+2] = 255 - this.data[idx+2];
// and reduce opacity
this.data[idx+3] = this.data[idx+3] >> 1;
}
}
this.pack().pipe(fs.createWriteStream('out.png'));
});For more examples see examples folder.
As input any color type is accepted (grayscale, rgb, palette, grayscale with alpha, rgb with alpha) but 8 bit per sample (channel) is the only supported bit depth. Interlaced mode is not supported.
gAMA- gamma,tRNS- transparency (but only for paletted image)
Image is readable and writable Stream.
width- use this withheightif you want to create png from scratchheight- as abovecheckCRC- whether parser should be strict about checksums in source stream (default:true)deflateChunkSize- chunk size used for deflating data chunks, this should be power of 2 and must not be less than 256 and more than 32*1024 (default: 32 kB)deflateLevel- compression level for delate (default: 9)deflateStrategy- compression strategy for delate (default: 3)filterType- png filtering method for scanlines (default: -1 => auto, accepts array of numbers 0-4)
function(metadata) { }
Image's header has been parsed, metadata contains this information:
widthimage size in pixelsheightimage size in pixelspaletteimage is palettedcolorimage is not grayscalealphaimage contains alpha channel
function(data) { }
Input image has been completly parsed, data is complete and ready for modification.
function(error) { }
Parses PNG file data. Alternatively you can stream data to instance of PNG.
Optional callback is once called on error or parsed. The callback gets
two arguments (err, data).
Returns this for method chaining.
Starts converting data to PNG file Stream.
Returns this for method chaining.
Image#bitblt(dst: Image, sx: number, sy: number, w: number, h: number, dx: number, dy: number): Image
Helper for image manipulation, copies rectangle of pixels from current image (sx, sy, w, h) to dst image (at dx, dy).
Returns this for method chaining.
Width of image in pixels
Height of image in pixels
Buffer of image pixel data. Every pixel consists 4 bytes: R, G, B, A (opacity).
Gamma of image (0 if not specified)