Merge branch 'master' of github.com:lukeapage/node-pngjs2

This commit is contained in:
Luke Page 2015-08-05 17:00:47 +01:00
commit c450597b09
18 changed files with 210 additions and 407 deletions

View file

@ -107,7 +107,7 @@
"arrow-spacing": 1,
"accessor-pairs": 1,
"block-scoped-var": 0,
"brace-style": [1, "1tbs"],
"brace-style": [1, "stroustrup"],
"callback-return": [2, ["callback", "cb", "next"]],
"camelcase": [2, {"properties": "always"}],
"comma-dangle": [2, "never"],
@ -129,7 +129,7 @@
"generator-star-spacing": 0,
"guard-for-in": 1,
"handle-callback-err": 2,
"id-length": [2, {"min": 3, "max": 20, "exceptions":["x", "y", "i", "j"]}],
"id-length": [2, {"min": 3, "max": 20, "exceptions":["x", "y", "i", "j", "ex", "up"]}],
"indent": [1, 2, {"SwitchCase": 1}],
"init-declarations": 0,
"key-spacing": [1, { "beforeColon": false, "afterColon": true }],

View file

@ -1,4 +1,5 @@
Copyright (c) 2012 Kuba Niegowski
pngjs2 original work Copyright (c) 2015 Luke Page & Original Contributors
pngjs derived work Copyright (c) 2012 Kuba Niegowski
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal

View file

@ -104,7 +104,8 @@ exports.dataToBitMap = function(data, width, height, bpp, depth, interlace) {
var pxData;
if (depth <= 8) {
pxData = new Buffer(width * height * 4);
} else {
}
else {
pxData = new Uint16Array(width * height * 4);
}
var maxBit = Math.pow(2, depth) - 1;
@ -116,7 +117,8 @@ exports.dataToBitMap = function(data, width, height, bpp, depth, interlace) {
if (interlace) {
images = interlaceUtils.getImagePasses(width, height);
getPxPos = interlaceUtils.getInterlaceIterator(width, height);
} else {
}
else {
var nonInterlacedPxPos = 0;
getPxPos = function() {
var returner = nonInterlacedPxPos;
@ -144,7 +146,8 @@ exports.dataToBitMap = function(data, width, height, bpp, depth, interlace) {
throw new Error('Ran out of data');
}
pxData[pxPos + i] = idx !== 0xff ? data[idx + rawPos] : maxBit;
} else {
}
else {
pxData[pxPos + i] = idx !== 0xff ? pixelData[idx] : maxBit;
}
}
@ -160,7 +163,8 @@ exports.dataToBitMap = function(data, width, height, bpp, depth, interlace) {
if (rawPos !== data.length) {
throw new Error('extra data found');
}
} else {
}
else {
bits.end();
}

View file

@ -1,23 +1,3 @@
// Copyright (c) 2012 Kuba Niegowski
//
// 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.
'use strict';
@ -70,7 +50,8 @@ ChunkStream.prototype.write = function(data, encoding) {
var dataBuffer;
if (Buffer.isBuffer(data)) {
dataBuffer = data;
} else {
}
else {
dataBuffer = new Buffer(data, encoding || this._encoding);
}
@ -80,7 +61,7 @@ ChunkStream.prototype.write = function(data, encoding) {
this._process();
// ok if there are no more read requests
if (this._reads && this._reads.length == 0) {
if (this._reads && this._reads.length === 0) {
this._paused = true;
}
@ -96,12 +77,15 @@ ChunkStream.prototype.end = function(data, encoding) {
this.writable = false;
// already destroyed
if (!this._buffers) return;
if (!this._buffers) {
return;
}
// enqueue or handle end
if (this._buffers.length == 0) {
if (this._buffers.length === 0) {
this._end();
} else {
}
else {
this._buffers.push(null);
this._process();
}
@ -157,7 +141,8 @@ ChunkStream.prototype._process = function() {
read.func.call(this, smallerBuf.slice(0, read.length));
} else {
}
else {
// ok this is less than maximum length so use it all
this._buffered -= smallerBuf.length;
this._buffers.shift(); // == smallerBuf
@ -165,7 +150,8 @@ ChunkStream.prototype._process = function() {
read.func.call(this, smallerBuf);
}
} else if (this._buffered >= read.length) {
}
else if (this._buffered >= read.length) {
// ok we can meet some expectations
this._reads.shift(); // == read
@ -184,19 +170,22 @@ ChunkStream.prototype._process = function() {
pos += len;
// last buffer wasn't used all so just slice it and leave
if (len != buf.length)
if (len !== buf.length) {
this._buffers[--count] = buf.slice(len);
}
}
// remove all used buffers
if (count > 0)
if (count > 0) {
this._buffers.splice(0, count);
}
this._buffered -= read.length;
read.func.call(this, data);
} else {
}
else {
// not enought data to satisfy first request in queue
// so we need to wait for more
break;

View file

@ -1,23 +1,3 @@
// Copyright (c) 2012 Kuba Niegowski
//
// 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.
'use strict';
@ -29,10 +9,8 @@ module.exports = {
TYPE_IEND: 0x49454e44,
TYPE_IDAT: 0x49444154,
TYPE_PLTE: 0x504c5445,
/*eslint camelcase: 0*/
TYPE_tRNS: 0x74524e53,
TYPE_gAMA: 0x67414d41,
/*eslint camelcase: 1*/
TYPE_tRNS: 0x74524e53, // eslint-disable-line camelcase
TYPE_gAMA: 0x67414d41, // eslint-disable-line camelcase
COLOR_PALETTE: 1,
COLOR_COLOR: 2,

View file

@ -1,23 +1,3 @@
// Copyright (c) 2012 Kuba Niegowski
//
// 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.
'use strict';
var crcTable = [];
@ -28,7 +8,8 @@ var crcTable = [];
for (var j = 0; j < 8; j++) {
if (currentCrc & 1) {
currentCrc = 0xedb88320 ^ (currentCrc >>> 1);
} else {
}
else {
currentCrc = currentCrc >>> 1;
}
}

View file

@ -1,23 +1,3 @@
// Copyright (c) 2012 Kuba Niegowski
//
// 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.
'use strict';
var util = require('util');
@ -33,7 +13,7 @@ var FilterAsync = module.exports = function(width, height, Bpp, depth, interlace
this._filter = new Filter(width, height, Bpp, depth, interlace, options, {
read: this.read.bind(this),
complete: function() {
that.emit('complete', Buffer.concat(buffers), width, height)
that.emit('complete', Buffer.concat(buffers), width, height);
},
write: function(buffer) {
buffers.push(buffer);

View file

@ -1,23 +1,3 @@
// Copyright (c) 2012 Kuba Niegowski
//
// 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.
'use strict';
var SyncReader = require('./sync-reader');

View file

@ -1,23 +1,3 @@
// Copyright (c) 2012 Kuba Niegowski
//
// 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.
'use strict';
var interlaceUtils = require('./interlace');
@ -30,12 +10,12 @@ function getByteWidth(width, bpp, depth) {
return byteWidth;
}
function PaethPredictor(left, above, upLeft) {
function paethPredictor(left, above, upLeft) {
var p = left + above - upLeft,
pLeft = Math.abs(p - left),
pAbove = Math.abs(p - above),
pUpLeft = Math.abs(p - upLeft);
var paeth = left + above - upLeft;
var pLeft = Math.abs(paeth - left);
var pAbove = Math.abs(paeth - above);
var pUpLeft = Math.abs(paeth - upLeft);
if (pLeft <= pAbove && pLeft <= pUpLeft) {
return left;
@ -51,13 +31,14 @@ var Filter = module.exports = function(width, height, Bpp, depth, interlace, opt
this._width = width;
this._height = height;
this._Bpp = Bpp;
this._Bpp = Bpp; //TODO rename
this._depth = depth;
this._options = options;
if (!('filterType' in options) || options.filterType == -1) {
if (!('filterType' in options) || options.filterType === -1) {
options.filterType = [0, 1, 2, 3, 4];
} else if (typeof options.filterType == 'number') {
}
else if (typeof options.filterType === 'number') {
options.filterType = [options.filterType];
}
@ -84,7 +65,8 @@ var Filter = module.exports = function(width, height, Bpp, depth, interlace, opt
lineIndex: 0
});
}
} else {
}
else {
this._images.push({
byteWidth: getByteWidth(width, Bpp, depth),
height: height,
@ -104,7 +86,20 @@ Filter.prototype._reverseFilterLine = function(rawData) {
var filter = rawData[0];
var xComparison = this._depth >= 8 ? ((this._depth === 16) ? this._Bpp * 2 : this._Bpp) : 1;
// when filtering the line we look at the pixel to the left
// the spec also says it is done on a byte level regardless of the number of pixels
// so if the depth is byte compatible (8 or 16) we subtract the bpp in order to compare back
// a pixel rather than just a different byte part. However if we are sub byte, we ignore.
var xComparison;
if (this._depth === 8) {
xComparison = this._Bpp;
}
else if (this._depth === 16) {
xComparison = this._Bpp * 2;
}
else {
xComparison = 1;
}
var xBiggerThan = xComparison - 1;
for (var x = 0; x < currentImage.byteWidth; x++) {
@ -114,27 +109,29 @@ Filter.prototype._reverseFilterLine = function(rawData) {
line[x] = rawByte;
break;
case 1:
var f1_left = x > xBiggerThan ? line[x - xComparison] : 0;
line[x] = rawByte + f1_left;
var f1Left = x > xBiggerThan ? line[x - xComparison] : 0;
line[x] = rawByte + f1Left;
break;
case 2:
var f2_up = this._lastLine ? this._lastLine[x] : 0;
line[x] = rawByte + f2_up;
var f2Up = this._lastLine ? this._lastLine[x] : 0;
line[x] = rawByte + f2Up;
break;
case 3:
var f3_up = this._lastLine ? this._lastLine[x] : 0;
var f3_left = x > xBiggerThan ? line[x - xComparison] : 0;
var f3_add = Math.floor((f3_left + f3_up) / 2);
line[x] = rawByte + f3_add;
var f3Up = this._lastLine ? this._lastLine[x] : 0;
var f3Left = x > xBiggerThan ? line[x - xComparison] : 0;
var f3Add = Math.floor((f3Left + f3Up) / 2);
line[x] = rawByte + f3Add;
break;
case 4:
var f4_up = this._lastLine ? this._lastLine[x] : 0;
var f4_left = x > xBiggerThan ? line[x - xComparison] : 0;
var f4_upLeft = x > xBiggerThan && this._lastLine
var f4Up = this._lastLine ? this._lastLine[x] : 0;
var f4Left = x > xBiggerThan ? line[x - xComparison] : 0;
var f4UpLeft = x > xBiggerThan && this._lastLine
? this._lastLine[x - xComparison] : 0;
var f4_add = PaethPredictor(f4_left, f4_up, f4_upLeft);
line[x] = rawByte + f4_add;
var f4Add = paethPredictor(f4Left, f4Up, f4UpLeft);
line[x] = rawByte + f4Add;
break;
default:
throw new Error('Unrecognised filter type - ' + filter);
}
//if (x === 5) {
@ -149,18 +146,20 @@ Filter.prototype._reverseFilterLine = function(rawData) {
this._lastLine = null;
this._imageIndex++;
currentImage = this._images[this._imageIndex];
} else {
}
else {
this._lastLine = line;
}
if (currentImage) {
this.read(currentImage.byteWidth + 1, this._reverseFilterLine.bind(this));
} else {
}
else {
this.complete(this._width, this._height);
}
};
//TODO pull out
Filter.prototype.filter = function(pxData) {
var rawData = new Buffer(((this._width << 2) + 1) * this._height);
@ -168,9 +167,9 @@ Filter.prototype.filter = function(pxData) {
for (var y = 0; y < this._height; y++) {
// find best filter for this line (with lowest sum of values)
var filterTypes = this._options.filterType,
min = Infinity,
sel = 0;
var filterTypes = this._options.filterType;
var min = Infinity;
var sel = 0;
for (var i = 0; i < filterTypes.length; i++) {
var sum = this._filters[filterTypes[i]](pxData, y, null);
@ -187,15 +186,17 @@ Filter.prototype.filter = function(pxData) {
Filter.prototype._filterNone = function(pxData, y, rawData) {
var pxRowLength = this._width << 2,
rawRowLength = pxRowLength + 1,
sum = 0;
var pxRowLength = this._width << 2;
var rawRowLength = pxRowLength + 1;
var sum = 0;
if (!rawData) {
for (var x = 0; x < pxRowLength; x++)
for (var x = 0; x < pxRowLength; x++) {
sum += Math.abs(pxData[y * pxRowLength + x]);
}
} else {
}
else {
rawData[y * rawRowLength] = 0;
pxData.copy(rawData, rawRowLength * y + 1, pxRowLength * y, pxRowLength * (y + 1));
}
@ -205,83 +206,103 @@ Filter.prototype._filterNone = function(pxData, y, rawData) {
Filter.prototype._filterSub = function(pxData, y, rawData) {
var pxRowLength = this._width << 2,
rawRowLength = pxRowLength + 1,
sum = 0;
var pxRowLength = this._width << 2;
var rawRowLength = pxRowLength + 1;
var sum = 0;
if (rawData)
if (rawData) {
rawData[y * rawRowLength] = 1;
}
for (var x = 0; x < pxRowLength; x++) {
var left = x >= 4 ? pxData[y * pxRowLength + x - 4] : 0,
val = pxData[y * pxRowLength + x] - left;
var left = x >= 4 ? pxData[y * pxRowLength + x - 4] : 0;
var val = pxData[y * pxRowLength + x] - left;
if (!rawData) sum += Math.abs(val);
else rawData[y * rawRowLength + 1 + x] = val;
if (!rawData) {
sum += Math.abs(val);
}
else {
rawData[y * rawRowLength + 1 + x] = val;
}
}
return sum;
};
Filter.prototype._filterUp = function(pxData, y, rawData) {
var pxRowLength = this._width << 2,
rawRowLength = pxRowLength + 1,
sum = 0;
var pxRowLength = this._width << 2;
var rawRowLength = pxRowLength + 1;
var sum = 0;
if (rawData)
if (rawData) {
rawData[y * rawRowLength] = 2;
}
for (var x = 0; x < pxRowLength; x++) {
var up = y > 0 ? pxData[(y - 1) * pxRowLength + x] : 0,
val = pxData[y * pxRowLength + x] - up;
var up = y > 0 ? pxData[(y - 1) * pxRowLength + x] : 0;
var val = pxData[y * pxRowLength + x] - up;
if (!rawData) sum += Math.abs(val);
else rawData[y * rawRowLength + 1 + x] = val;
if (!rawData) {
sum += Math.abs(val);
}
else {
rawData[y * rawRowLength + 1 + x] = val;
}
}
return sum;
};
Filter.prototype._filterAvg = function(pxData, y, rawData) {
var pxRowLength = this._width << 2,
rawRowLength = pxRowLength + 1,
sum = 0;
var pxRowLength = this._width << 2;
var rawRowLength = pxRowLength + 1;
var sum = 0;
if (rawData)
if (rawData) {
rawData[y * rawRowLength] = 3;
}
for (var x = 0; x < pxRowLength; x++) {
var left = x >= 4 ? pxData[y * pxRowLength + x - 4] : 0,
up = y > 0 ? pxData[(y - 1) * pxRowLength + x] : 0,
val = pxData[y * pxRowLength + x] - ((left + up) >> 1);
var left = x >= 4 ? pxData[y * pxRowLength + x - 4] : 0;
var up = y > 0 ? pxData[(y - 1) * pxRowLength + x] : 0;
var val = pxData[y * pxRowLength + x] - ((left + up) >> 1);
if (!rawData) sum += Math.abs(val);
else rawData[y * rawRowLength + 1 + x] = val;
if (!rawData) {
sum += Math.abs(val);
}
else {
rawData[y * rawRowLength + 1 + x] = val;
}
}
return sum;
};
Filter.prototype._filterPaeth = function(pxData, y, rawData) {
var pxRowLength = this._width << 2,
rawRowLength = pxRowLength + 1,
sum = 0;
var pxRowLength = this._width << 2;
var rawRowLength = pxRowLength + 1;
var sum = 0;
if (rawData)
if (rawData) {
rawData[y * rawRowLength] = 4;
}
for (var x = 0; x < pxRowLength; x++) {
var left = x >= 4 ? pxData[y * pxRowLength + x - 4] : 0,
up = y > 0 ? pxData[(y - 1) * pxRowLength + x] : 0,
upLeft = x >= 4 && y > 0 ? pxData[(y - 1) * pxRowLength + x - 4] : 0,
val = pxData[y * pxRowLength + x] - PaethPredictor(left, up, upLeft);
var left = x >= 4 ? pxData[y * pxRowLength + x - 4] : 0;
var up = y > 0 ? pxData[(y - 1) * pxRowLength + x] : 0;
var upLeft = x >= 4 && y > 0 ? pxData[(y - 1) * pxRowLength + x - 4] : 0;
var val = pxData[y * pxRowLength + x] - paethPredictor(left, up, upLeft);
if (!rawData) sum += Math.abs(val);
else rawData[y * rawRowLength + 1 + x] = val;
if (!rawData) {
sum += Math.abs(val);
}
else {
rawData[y * rawRowLength + 1 + x] = val;
}
}
return sum;
};

View file

@ -70,7 +70,7 @@ exports.getImagePasses = function(width, height) {
}
}
if (passWidth > 0 && passHeight > 0) {
images.push({width: passWidth, height: passHeight, index: i});
images.push({ width: passWidth, height: passHeight, index: i });
}
}
return images;

View file

@ -1,23 +1,3 @@
// Copyright (c) 2012 Kuba Niegowski
//
// 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.
'use strict';
@ -80,13 +60,15 @@ Packer.prototype.pack = function(data, width, height, gamma) {
Packer.prototype._packChunk = function(type, data) {
var len = (data ? data.length : 0),
buf = new Buffer(len + 12);
var len = (data ? data.length : 0);
var buf = new Buffer(len + 12);
buf.writeUInt32BE(len, 0);
buf.writeUInt32BE(type, 4);
if (data) data.copy(buf, 8);
if (data) {
data.copy(buf, 8);
}
buf.writeInt32BE(CrcStream.crc32(buf.slice(4, buf.length - 4)), buf.length - 4);
return buf;

View file

@ -1,23 +1,3 @@
// Copyright (c) 2012 Kuba Niegowski
//
// 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.
'use strict';
@ -35,8 +15,8 @@ var ParserAsync = module.exports = function(options) {
this._parser = new Parser(options, {
read: this.read.bind(this),
error: this._handleError.bind(this),
metadata: this.emit.bind(this, "metadata"),
gamma: this.emit.bind(this, "gamma"),
metadata: this.emit.bind(this, 'metadata'),
gamma: this.emit.bind(this, 'gamma'),
finished: this._finished.bind(this),
inflateData: this._inflateData.bind(this),
createData: this._createData.bind(this)
@ -98,31 +78,32 @@ ParserAsync.prototype._finished = function() {
if (!this._inflate) {
this.emit('error', 'No Inflate block');
} else {
}
else {
// no more data to inflate
this._inflate.end();
}
this.destroySoon();
};
ParserAsync.prototype._complete = function(data, width, height) {
ParserAsync.prototype._complete = function(filteredData, width, height) {
if (this.errord) {
return;
}
try {
data = bitmapper.dataToBitMap(data, width, height,
var bitmapData = bitmapper.dataToBitMap(filteredData, width, height,
this._bpp,
this._depth,
this._interlace);
data = this._parser.reverseFiltered(data, this._depth, width, height);
bitmapData = this._parser.reverseFiltered(bitmapData, this._depth, width, height);
}
catch (e) {
this._handleError();
catch (ex) {
this._handleError(ex);
return;
}
this.emit('parsed', data);
this.emit('parsed', bitmapData);
};

View file

@ -1,23 +1,3 @@
// Copyright (c) 2012 Kuba Niegowski
//
// 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.
'use strict';

View file

@ -1,26 +1,5 @@
// Copyright (c) 2012 Kuba Niegowski
//
// 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.
'use strict';
var constants = require('./constants');
var CrcCalculator = require('./crc');
@ -74,7 +53,7 @@ Parser.prototype._parseSignature = function(data) {
var signature = constants.PNG_SIGNATURE;
for (var i = 0; i < signature.length; i++) {
if (data[i] != signature[i]) {
if (data[i] !== signature[i]) {
this.error(new Error('Invalid file signature'));
return;
}
@ -88,8 +67,8 @@ Parser.prototype._parseChunkBegin = function(data) {
var length = data.readUInt32BE(0);
// chunk type
var type = data.readUInt32BE(4),
name = '';
var type = data.readUInt32BE(4);
var name = '';
for (var i = 4; i < 8; i++) {
name += String.fromCharCode(data[i]);
}
@ -97,11 +76,11 @@ Parser.prototype._parseChunkBegin = function(data) {
//console.log('chunk ', name, length);
// chunk flags
var ancillary = !!(data[4] & 0x20); // or critical
// priv = !!(data[5] & 0x20), // or public
// safeToCopy = !!(data[7] & 0x20); // or unsafe
var ancillary = Boolean(data[4] & 0x20); // or critical
// priv = Boolean(data[5] & 0x20), // or public
// safeToCopy = Boolean(data[7] & 0x20); // or unsafe
if (!this._hasIHDR && type != constants.TYPE_IHDR) {
if (!this._hasIHDR && type !== constants.TYPE_IHDR) {
this.error(new Error('Expected IHDR on beggining'));
return;
}
@ -131,12 +110,12 @@ Parser.prototype._handleChunkEnd = function() {
Parser.prototype._parseChunkEnd = function(data) {
var fileCrc = data.readInt32BE(0),
calcCrc = this._crc.crc32();
var fileCrc = data.readInt32BE(0);
var calcCrc = this._crc.crc32();
// check CRC
if (this._options.checkCRC && calcCrc != fileCrc) {
this.error(new Error('Crc error - ' + fileCrc + " - " + calcCrc));
if (this._options.checkCRC && calcCrc !== fileCrc) {
this.error(new Error('Crc error - ' + fileCrc + ' - ' + calcCrc));
return;
}
@ -152,13 +131,13 @@ Parser.prototype._parseIHDR = function(data) {
this._crc.write(data);
var width = data.readUInt32BE(0),
height = data.readUInt32BE(4),
depth = data[8],
colorType = data[9], // bits: 1 palette, 2 color, 4 alpha
compr = data[10],
filter = data[11],
interlace = data[12];
var width = data.readUInt32BE(0);
var height = data.readUInt32BE(4);
var depth = data[8];
var colorType = data[9]; // bits: 1 palette, 2 color, 4 alpha
var compr = data[10];
var filter = data[11];
var interlace = data[12];
// console.log(' width', width, 'height', height,
// 'depth', depth, 'colorType', colorType,
@ -173,15 +152,15 @@ Parser.prototype._parseIHDR = function(data) {
this.error(new Error('Unsupported color type'));
return;
}
if (compr != 0) {
if (compr !== 0) {
this.error(new Error('Unsupported compression method'));
return;
}
if (filter != 0) {
if (filter !== 0) {
this.error(new Error('Unsupported filter method'));
return;
}
if (interlace != 0 && interlace !== 1) {
if (interlace !== 0 && interlace !== 1) {
this.error(new Error('Unsupported interlace method'));
return;
}
@ -197,10 +176,10 @@ Parser.prototype._parseIHDR = function(data) {
width: width,
height: height,
depth: depth,
interlace: interlace,
palette: !!(colorType & constants.COLOR_PALETTE),
color: !!(colorType & constants.COLOR_COLOR),
alpha: !!(colorType & constants.COLOR_ALPHA)
interlace: Boolean(interlace),
palette: Boolean(colorType & constants.COLOR_PALETTE),
color: Boolean(colorType & constants.COLOR_COLOR),
alpha: Boolean(colorType & constants.COLOR_ALPHA)
});
this._handleChunkEnd();
@ -237,8 +216,8 @@ Parser.prototype._parseTRNS = function(data) {
this._crc.write(data);
// palette
if (this._colorType == 3) {
if (this._palette.length == 0) {
if (this._colorType === 3) {
if (this._palette.length === 0) {
this.error(new Error('Transparency chunk must be after palette'));
return;
}
@ -282,16 +261,19 @@ Parser.prototype._parseIDAT = function(length, data) {
this._crc.write(data);
if (this._colorType == 3 && this._palette.length == 0)
if (this._colorType === 3 && this._palette.length === 0) {
throw new Error('Expected palette not found');
}
this.inflateData(data);
length -= data.length;
var leftOverLength = length - data.length;
if (length > 0)
this._handleIDAT(length);
else
if (leftOverLength > 0) {
this._handleIDAT(leftOverLength);
}
else {
this._handleChunkEnd();
}
};
@ -315,7 +297,7 @@ Parser.prototype.reverseFiltered = function(indata, depth, width, height) {
var pxLineLength = width << 2;
var x, y, i, pxPos, pxRowPos;
if (this._colorType == 3) { // paletted
if (this._colorType === 3) { // paletted
//TODO abstract loop?
// use values from palette
for (y = 0; y < height; y++) {
@ -326,14 +308,16 @@ Parser.prototype.reverseFiltered = function(indata, depth, width, height) {
var color = this._palette[indata[pxPos]];
if (!color) {
throw new Error("index " + indata[pxPos] + " not in palette");
throw new Error('index ' + indata[pxPos] + ' not in palette');
}
for (i = 0; i < 4; i++)
for (i = 0; i < 4; i++) {
indata[pxPos + i] = color[i];
}
}
}
} else {
}
else {
if (this._transColor) {
for (y = 0; y < height; y++) {
pxRowPos = y * pxLineLength;
@ -346,7 +330,8 @@ Parser.prototype.reverseFiltered = function(indata, depth, width, height) {
if (this._transColor[0] === indata[pxPos]) {
makeTrans = true;
}
} else if (this._transColor[0] === indata[pxPos] && this._transColor[1] === indata[pxPos + 1] && this._transColor[2] === indata[pxPos + 2]) {
}
else if (this._transColor[0] === indata[pxPos] && this._transColor[1] === indata[pxPos + 1] && this._transColor[2] === indata[pxPos + 2]) {
makeTrans = true;
}
if (makeTrans) {
@ -370,8 +355,9 @@ Parser.prototype.reverseFiltered = function(indata, depth, width, height) {
for (x = 0; x < width; x++) {
pxPos = pxRowPos + (x << 2);
for (i = 0; i < 4; i++)
for (i = 0; i < 4; i++) {
outdata[pxPos + i] = Math.floor((indata[pxPos + i] * maxOutSample) / maxInSample + 0.5);
}
}
}
}

View file

@ -1,23 +1,3 @@
// Copyright (c) 2012 Kuba Niegowski
//
// 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.
'use strict';

View file

@ -1,26 +1,5 @@
// Copyright (c) 2012 Kuba Niegowski
//
// 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.
'use strict';
var util = require('util');
var Stream = require('stream');
var Parser = require('./parser-async');

View file

@ -1,23 +1,3 @@
// Copyright (c) 2012 Kuba Niegowski
//
// 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.
'use strict';
var SyncReader = module.exports = function(buffer) {
@ -53,7 +33,8 @@ SyncReader.prototype.process = function() {
read.func.call(this, buf.slice(0, read.length));
} else {
}
else {
break;
}

View file

@ -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",