Tests

Tested using PNG Suite. We read every file into pngjs, output it in standard 8bit colour, synchronously and asynchronously, then compare the original with the newly saved images.

To run the tests, fetch the repo (tests are not distributed via npm) and install with npm i, run npm test.

The only thing not converted is gamma correction - this is because multiple vendors will do gamma correction differently, so the tests will have different results on different browsers.

In addition we use a tolerance of 3 for 16 bit images in PhantomJS because PhantomJS seems to have non-compliant rules for downscaling 16 bit images.

Installation

$ npm install pngjs --save

Browserify

Use Browserify to build a version of library that can run client-side:

$ browserify lib/png.js --standalone png > bundle.js

Then depending on your module system you can either require('bundle.js') or if you're not using module loader you can use plain old <script src="bundle.js"></script> tag and the library will be available under window.PNG or just PNG.

Example

var fs =require('fs'),

PNG=require('pngjs').PNG;

fs.createReadStream('in.png')

.pipe(newPNG({

filterType:4

}))

.on('parsed',function(){

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.

Async API

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.

png.bitblt(dst, sx, sy, w, h, dx, dy)

For example, the following code copies the top-left 100x50 px of in.png into dst and writes it to out.png:

var dst =newPNG({width:100, height:50});

fs.createReadStream('in.png')

.pipe(newPNG())

.on('parsed',function(){

this.bitblt(dst,0,0,100,50,0,0);

dst.pack().pipe(fs.createWriteStream('out.png'));

});

Property: adjustGamma()

Helper that takes data and adjusts it to be gamma corrected. Note that it is not 100% reliable with transparent colours because that requires knowing the background colour the bitmap is rendered on to.

In tests against PNG suite it compared 100% with chrome on all 8 bit and below images. On IE there were some differences.

The following example reads a file, adjusts the gamma (which sets the gamma to 0) and writes it out again, effectively removing any gamma correction from the image.

fs.createReadStream('in.png')

.pipe(newPNG())

.on('parsed',function(){

this.adjustGamma();

this.pack().pipe(fs.createWriteStream('out.png'));

});

Property: width

Width of image in pixels

Property: height

Height of image in pixels

Property: data

Property: gamma

Gamma of image (0 if not specified)

Packing a PNG and removing alpha (RGBA to RGB)

When removing the alpha channel from an image, there needs to be a background color to correctly
convert each pixel's transparency to the appropriate RGB value. By default, pngjs will flatten
the image against a white background. You can override this in the options:

var fs =require('fs'),

PNG=require('pngjs').PNG;

fs.createReadStream('in.png')

.pipe(newPNG({

colorType:2,

bgColor:{

red:0,

green:255,

blue:0

}

}))

.on('parsed',function(){

this.pack().pipe(fs.createWriteStream('out.png'));

});

Sync API

PNG.sync

PNG.sync.read(buffer)

Take a buffer and returns a PNG image. The properties on the image include the meta data and data as per the async API above.

var data = fs.readFileSync('in.png');
var png = PNG.sync.read(data);

PNG.sync.write(png)

Take a PNG image and returns a buffer. The properties on the image include the meta data and data as per the async API above.

1.0.0 - 08/08/2015

breaking change - gamma chunk is now written. previously a read then write would destroy gamma information, now it is persisted.

0.0.3 - 03/08/2015

Error handling fixes

ignore files for smaller npm footprint

0.0.2 - 02/08/2015

Bugfixes to interlacing, support for transparent colours

0.0.1 - 02/08/2015

Initial release, see pngjs for older changelog.

License

(The MIT License)

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.