Resolve remaining style issues

This commit is contained in:
Luke Page 2015-08-04 23:00:10 +01:00
parent bf5ae3c54e
commit 99eae1c711
12 changed files with 207 additions and 143 deletions

View file

@ -107,7 +107,7 @@
"arrow-spacing": 1, "arrow-spacing": 1,
"accessor-pairs": 1, "accessor-pairs": 1,
"block-scoped-var": 0, "block-scoped-var": 0,
"brace-style": [1, "1tbs"], "brace-style": [1, "stroustrup"],
"callback-return": [2, ["callback", "cb", "next"]], "callback-return": [2, ["callback", "cb", "next"]],
"camelcase": [2, {"properties": "always"}], "camelcase": [2, {"properties": "always"}],
"comma-dangle": [2, "never"], "comma-dangle": [2, "never"],
@ -129,7 +129,7 @@
"generator-star-spacing": 0, "generator-star-spacing": 0,
"guard-for-in": 1, "guard-for-in": 1,
"handle-callback-err": 2, "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}], "indent": [1, 2, {"SwitchCase": 1}],
"init-declarations": 0, "init-declarations": 0,
"key-spacing": [1, { "beforeColon": false, "afterColon": true }], "key-spacing": [1, { "beforeColon": false, "afterColon": true }],

View file

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

View file

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

View file

@ -29,10 +29,8 @@ module.exports = {
TYPE_IEND: 0x49454e44, TYPE_IEND: 0x49454e44,
TYPE_IDAT: 0x49444154, TYPE_IDAT: 0x49444154,
TYPE_PLTE: 0x504c5445, TYPE_PLTE: 0x504c5445,
/*eslint camelcase: 0*/ TYPE_tRNS: 0x74524e53, // eslint-disable-line camelcase
TYPE_tRNS: 0x74524e53, TYPE_gAMA: 0x67414d41, // eslint-disable-line camelcase
TYPE_gAMA: 0x67414d41,
/*eslint camelcase: 1*/
COLOR_PALETTE: 1, COLOR_PALETTE: 1,
COLOR_COLOR: 2, COLOR_COLOR: 2,

View file

@ -28,7 +28,8 @@ var crcTable = [];
for (var j = 0; j < 8; j++) { for (var j = 0; j < 8; j++) {
if (currentCrc & 1) { if (currentCrc & 1) {
currentCrc = 0xedb88320 ^ (currentCrc >>> 1); currentCrc = 0xedb88320 ^ (currentCrc >>> 1);
} else { }
else {
currentCrc = currentCrc >>> 1; currentCrc = currentCrc >>> 1;
} }
} }

View file

@ -33,7 +33,7 @@ var FilterAsync = module.exports = function(width, height, Bpp, depth, interlace
this._filter = new Filter(width, height, Bpp, depth, interlace, options, { this._filter = new Filter(width, height, Bpp, depth, interlace, options, {
read: this.read.bind(this), read: this.read.bind(this),
complete: function() { complete: function() {
that.emit('complete', Buffer.concat(buffers), width, height) that.emit('complete', Buffer.concat(buffers), width, height);
}, },
write: function(buffer) { write: function(buffer) {
buffers.push(buffer); buffers.push(buffer);

View file

@ -30,12 +30,12 @@ function getByteWidth(width, bpp, depth) {
return byteWidth; return byteWidth;
} }
function PaethPredictor(left, above, upLeft) { function paethPredictor(left, above, upLeft) {
var p = left + above - upLeft, var paeth = left + above - upLeft;
pLeft = Math.abs(p - left), var pLeft = Math.abs(paeth - left);
pAbove = Math.abs(p - above), var pAbove = Math.abs(paeth - above);
pUpLeft = Math.abs(p - upLeft); var pUpLeft = Math.abs(paeth - upLeft);
if (pLeft <= pAbove && pLeft <= pUpLeft) { if (pLeft <= pAbove && pLeft <= pUpLeft) {
return left; return left;
@ -51,13 +51,14 @@ var Filter = module.exports = function(width, height, Bpp, depth, interlace, opt
this._width = width; this._width = width;
this._height = height; this._height = height;
this._Bpp = Bpp; this._Bpp = Bpp; //TODO rename
this._depth = depth; this._depth = depth;
this._options = options; this._options = options;
if (!('filterType' in options) || options.filterType == -1) { if (!('filterType' in options) || options.filterType === -1) {
options.filterType = [0, 1, 2, 3, 4]; options.filterType = [0, 1, 2, 3, 4];
} else if (typeof options.filterType == 'number') { }
else if (typeof options.filterType === 'number') {
options.filterType = [options.filterType]; options.filterType = [options.filterType];
} }
@ -84,7 +85,8 @@ var Filter = module.exports = function(width, height, Bpp, depth, interlace, opt
lineIndex: 0 lineIndex: 0
}); });
} }
} else { }
else {
this._images.push({ this._images.push({
byteWidth: getByteWidth(width, Bpp, depth), byteWidth: getByteWidth(width, Bpp, depth),
height: height, height: height,
@ -104,7 +106,20 @@ Filter.prototype._reverseFilterLine = function(rawData) {
var filter = rawData[0]; 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; var xBiggerThan = xComparison - 1;
for (var x = 0; x < currentImage.byteWidth; x++) { for (var x = 0; x < currentImage.byteWidth; x++) {
@ -114,27 +129,29 @@ Filter.prototype._reverseFilterLine = function(rawData) {
line[x] = rawByte; line[x] = rawByte;
break; break;
case 1: case 1:
var f1_left = x > xBiggerThan ? line[x - xComparison] : 0; var f1Left = x > xBiggerThan ? line[x - xComparison] : 0;
line[x] = rawByte + f1_left; line[x] = rawByte + f1Left;
break; break;
case 2: case 2:
var f2_up = this._lastLine ? this._lastLine[x] : 0; var f2Up = this._lastLine ? this._lastLine[x] : 0;
line[x] = rawByte + f2_up; line[x] = rawByte + f2Up;
break; break;
case 3: case 3:
var f3_up = this._lastLine ? this._lastLine[x] : 0; var f3Up = this._lastLine ? this._lastLine[x] : 0;
var f3_left = x > xBiggerThan ? line[x - xComparison] : 0; var f3Left = x > xBiggerThan ? line[x - xComparison] : 0;
var f3_add = Math.floor((f3_left + f3_up) / 2); var f3Add = Math.floor((f3Left + f3Up) / 2);
line[x] = rawByte + f3_add; line[x] = rawByte + f3Add;
break; break;
case 4: case 4:
var f4_up = this._lastLine ? this._lastLine[x] : 0; var f4Up = this._lastLine ? this._lastLine[x] : 0;
var f4_left = x > xBiggerThan ? line[x - xComparison] : 0; var f4Left = x > xBiggerThan ? line[x - xComparison] : 0;
var f4_upLeft = x > xBiggerThan && this._lastLine var f4UpLeft = x > xBiggerThan && this._lastLine
? this._lastLine[x - xComparison] : 0; ? this._lastLine[x - xComparison] : 0;
var f4_add = PaethPredictor(f4_left, f4_up, f4_upLeft); var f4Add = paethPredictor(f4Left, f4Up, f4UpLeft);
line[x] = rawByte + f4_add; line[x] = rawByte + f4Add;
break; break;
default:
throw new Error('Unrecognised filter type - ' + filter);
} }
//if (x === 5) { //if (x === 5) {
@ -149,18 +166,20 @@ Filter.prototype._reverseFilterLine = function(rawData) {
this._lastLine = null; this._lastLine = null;
this._imageIndex++; this._imageIndex++;
currentImage = this._images[this._imageIndex]; currentImage = this._images[this._imageIndex];
} else { }
else {
this._lastLine = line; this._lastLine = line;
} }
if (currentImage) { if (currentImage) {
this.read(currentImage.byteWidth + 1, this._reverseFilterLine.bind(this)); this.read(currentImage.byteWidth + 1, this._reverseFilterLine.bind(this));
} else { }
else {
this.complete(this._width, this._height); this.complete(this._width, this._height);
} }
}; };
//TODO pull out
Filter.prototype.filter = function(pxData) { Filter.prototype.filter = function(pxData) {
var rawData = new Buffer(((this._width << 2) + 1) * this._height); var rawData = new Buffer(((this._width << 2) + 1) * this._height);
@ -168,9 +187,9 @@ Filter.prototype.filter = function(pxData) {
for (var y = 0; y < this._height; y++) { for (var y = 0; y < this._height; y++) {
// find best filter for this line (with lowest sum of values) // find best filter for this line (with lowest sum of values)
var filterTypes = this._options.filterType, var filterTypes = this._options.filterType;
min = Infinity, var min = Infinity;
sel = 0; var sel = 0;
for (var i = 0; i < filterTypes.length; i++) { for (var i = 0; i < filterTypes.length; i++) {
var sum = this._filters[filterTypes[i]](pxData, y, null); var sum = this._filters[filterTypes[i]](pxData, y, null);
@ -187,15 +206,17 @@ Filter.prototype.filter = function(pxData) {
Filter.prototype._filterNone = function(pxData, y, rawData) { Filter.prototype._filterNone = function(pxData, y, rawData) {
var pxRowLength = this._width << 2, var pxRowLength = this._width << 2;
rawRowLength = pxRowLength + 1, var rawRowLength = pxRowLength + 1;
sum = 0; var sum = 0;
if (!rawData) { if (!rawData) {
for (var x = 0; x < pxRowLength; x++) for (var x = 0; x < pxRowLength; x++) {
sum += Math.abs(pxData[y * pxRowLength + x]); sum += Math.abs(pxData[y * pxRowLength + x]);
}
} else { }
else {
rawData[y * rawRowLength] = 0; rawData[y * rawRowLength] = 0;
pxData.copy(rawData, rawRowLength * y + 1, pxRowLength * y, pxRowLength * (y + 1)); pxData.copy(rawData, rawRowLength * y + 1, pxRowLength * y, pxRowLength * (y + 1));
} }
@ -205,83 +226,103 @@ Filter.prototype._filterNone = function(pxData, y, rawData) {
Filter.prototype._filterSub = function(pxData, y, rawData) { Filter.prototype._filterSub = function(pxData, y, rawData) {
var pxRowLength = this._width << 2, var pxRowLength = this._width << 2;
rawRowLength = pxRowLength + 1, var rawRowLength = pxRowLength + 1;
sum = 0; var sum = 0;
if (rawData) if (rawData) {
rawData[y * rawRowLength] = 1; rawData[y * rawRowLength] = 1;
}
for (var x = 0; x < pxRowLength; x++) { for (var x = 0; x < pxRowLength; x++) {
var left = x >= 4 ? pxData[y * pxRowLength + x - 4] : 0, var left = x >= 4 ? pxData[y * pxRowLength + x - 4] : 0;
val = pxData[y * pxRowLength + x] - left; var val = pxData[y * pxRowLength + x] - left;
if (!rawData) sum += Math.abs(val); if (!rawData) {
else rawData[y * rawRowLength + 1 + x] = val; sum += Math.abs(val);
}
else {
rawData[y * rawRowLength + 1 + x] = val;
}
} }
return sum; return sum;
}; };
Filter.prototype._filterUp = function(pxData, y, rawData) { Filter.prototype._filterUp = function(pxData, y, rawData) {
var pxRowLength = this._width << 2, var pxRowLength = this._width << 2;
rawRowLength = pxRowLength + 1, var rawRowLength = pxRowLength + 1;
sum = 0; var sum = 0;
if (rawData) if (rawData) {
rawData[y * rawRowLength] = 2; rawData[y * rawRowLength] = 2;
}
for (var x = 0; x < pxRowLength; x++) { for (var x = 0; x < pxRowLength; x++) {
var up = y > 0 ? pxData[(y - 1) * pxRowLength + x] : 0, var up = y > 0 ? pxData[(y - 1) * pxRowLength + x] : 0;
val = pxData[y * pxRowLength + x] - up; var val = pxData[y * pxRowLength + x] - up;
if (!rawData) sum += Math.abs(val); if (!rawData) {
else rawData[y * rawRowLength + 1 + x] = val; sum += Math.abs(val);
}
else {
rawData[y * rawRowLength + 1 + x] = val;
}
} }
return sum; return sum;
}; };
Filter.prototype._filterAvg = function(pxData, y, rawData) { Filter.prototype._filterAvg = function(pxData, y, rawData) {
var pxRowLength = this._width << 2, var pxRowLength = this._width << 2;
rawRowLength = pxRowLength + 1, var rawRowLength = pxRowLength + 1;
sum = 0; var sum = 0;
if (rawData) if (rawData) {
rawData[y * rawRowLength] = 3; rawData[y * rawRowLength] = 3;
}
for (var x = 0; x < pxRowLength; x++) { for (var x = 0; x < pxRowLength; x++) {
var left = x >= 4 ? pxData[y * pxRowLength + x - 4] : 0, var left = x >= 4 ? pxData[y * pxRowLength + x - 4] : 0;
up = y > 0 ? pxData[(y - 1) * pxRowLength + x] : 0, var up = y > 0 ? pxData[(y - 1) * pxRowLength + x] : 0;
val = pxData[y * pxRowLength + x] - ((left + up) >> 1); var val = pxData[y * pxRowLength + x] - ((left + up) >> 1);
if (!rawData) sum += Math.abs(val); if (!rawData) {
else rawData[y * rawRowLength + 1 + x] = val; sum += Math.abs(val);
}
else {
rawData[y * rawRowLength + 1 + x] = val;
}
} }
return sum; return sum;
}; };
Filter.prototype._filterPaeth = function(pxData, y, rawData) { Filter.prototype._filterPaeth = function(pxData, y, rawData) {
var pxRowLength = this._width << 2, var pxRowLength = this._width << 2;
rawRowLength = pxRowLength + 1, var rawRowLength = pxRowLength + 1;
sum = 0; var sum = 0;
if (rawData) if (rawData) {
rawData[y * rawRowLength] = 4; rawData[y * rawRowLength] = 4;
}
for (var x = 0; x < pxRowLength; x++) { for (var x = 0; x < pxRowLength; x++) {
var left = x >= 4 ? pxData[y * pxRowLength + x - 4] : 0, var left = x >= 4 ? pxData[y * pxRowLength + x - 4] : 0;
up = y > 0 ? pxData[(y - 1) * pxRowLength + x] : 0, var up = y > 0 ? pxData[(y - 1) * pxRowLength + x] : 0;
upLeft = x >= 4 && y > 0 ? pxData[(y - 1) * pxRowLength + x - 4] : 0, var upLeft = x >= 4 && y > 0 ? pxData[(y - 1) * pxRowLength + x - 4] : 0;
val = pxData[y * pxRowLength + x] - PaethPredictor(left, up, upLeft); var val = pxData[y * pxRowLength + x] - paethPredictor(left, up, upLeft);
if (!rawData) sum += Math.abs(val); if (!rawData) {
else rawData[y * rawRowLength + 1 + x] = val; sum += Math.abs(val);
}
else {
rawData[y * rawRowLength + 1 + x] = val;
}
} }
return sum; return sum;
}; };

View file

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

View file

@ -76,13 +76,15 @@ Packer.prototype.pack = function(data, width, height) {
Packer.prototype._packChunk = function(type, data) { Packer.prototype._packChunk = function(type, data) {
var len = (data ? data.length : 0), var len = (data ? data.length : 0);
buf = new Buffer(len + 12); var buf = new Buffer(len + 12);
buf.writeUInt32BE(len, 0); buf.writeUInt32BE(len, 0);
buf.writeUInt32BE(type, 4); 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); buf.writeInt32BE(CrcStream.crc32(buf.slice(4, buf.length - 4)), buf.length - 4);
return buf; return buf;

View file

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

View file

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

View file

@ -53,7 +53,8 @@ SyncReader.prototype.process = function() {
read.func.call(this, buf.slice(0, read.length)); read.func.call(this, buf.slice(0, read.length));
} else { }
else {
break; break;
} }