From eabc8ac0614796eb7b46add737b156b87f56d97b Mon Sep 17 00:00:00 2001 From: Luke Page Date: Tue, 4 Aug 2015 23:11:37 +0100 Subject: [PATCH 1/2] Update package.json --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 665b8ee..a1f5ec6 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "pngjs2", "version": "0.0.3", - "description": "Pure JS PNG encoder/decoder", + "description": "PNG encoder/decoder in pure JS, supporting any bit size & interlace, async & sync with full test suite.", "contributors": [ "Alexandre Paré", "Gaurav Mali", From ff9d322a3612ef287958e3ba4222de88ad4da3a9 Mon Sep 17 00:00:00 2001 From: Luke Page Date: Wed, 5 Aug 2015 17:00:37 +0100 Subject: [PATCH 2/2] add ability to write gamma chunks --- lib/packer.js | 12 +++++++++++- lib/parser.js | 2 +- lib/png.js | 2 +- test/convert-images.js | 3 +-- 4 files changed, 14 insertions(+), 5 deletions(-) diff --git a/lib/packer.js b/lib/packer.js index 85ec1ed..96f3f97 100755 --- a/lib/packer.js +++ b/lib/packer.js @@ -43,12 +43,16 @@ var Packer = module.exports = function(options) { util.inherits(Packer, Stream); -Packer.prototype.pack = function(data, width, height) { +Packer.prototype.pack = function(data, width, height, gamma) { // Signature this.emit('data', new Buffer(constants.PNG_SIGNATURE)); this.emit('data', this._packIHDR(width, height)); + if (gamma) { + this.emit('data', this._packGAMA(gamma)); + } + // filter pixel data //TODO {} var filter = new Filter(width, height, 4, 8, false, this._options, {}); @@ -88,6 +92,12 @@ Packer.prototype._packChunk = function(type, data) { return buf; }; +Packer.prototype._packGAMA = function(gamma) { + var buf = new Buffer(4); + buf.writeUInt32BE(Math.floor(gamma * 100000), 0); // TODO constant + return this._packChunk(constants.TYPE_gAMA, buf); +}; + Packer.prototype._packIHDR = function(width, height) { var buf = new Buffer(13); diff --git a/lib/parser.js b/lib/parser.js index 726fce2..6a31007 100644 --- a/lib/parser.js +++ b/lib/parser.js @@ -270,7 +270,7 @@ Parser.prototype._handleGAMA = function(length) { Parser.prototype._parseGAMA = function(data) { this._crc.write(data); - this.gamma(data.readUInt32BE(0) / 100000); + this.gamma(data.readUInt32BE(0) / 100000); //TODO constant this._handleChunkEnd(); }; diff --git a/lib/png.js b/lib/png.js index 68c90c5..c5ddf1b 100755 --- a/lib/png.js +++ b/lib/png.js @@ -71,7 +71,7 @@ PNG.sync = PNGSync; PNG.prototype.pack = function() { process.nextTick(function() { - this._packer.pack(this.data, this.width, this.height); + this._packer.pack(this.data, this.width, this.height, this.gamma); }.bind(this)); return this; diff --git a/test/convert-images.js b/test/convert-images.js index 0ec147a..cc28c56 100644 --- a/test/convert-images.js +++ b/test/convert-images.js @@ -51,7 +51,7 @@ module.exports = function(done) { } else { var outpng = new PNG(); - //PNG.adjustGamma(png); + outpng.gamma = png.gamma; outpng.data = png.data; outpng.width = png.width; outpng.height = png.height; @@ -76,7 +76,6 @@ module.exports = function(done) { if (expectedError) { console.log("Async: Error expected, parsed fine ..", file); } - //this.adjustGamma(); this.pack() .pipe(