refactor - shorten method param count

This commit is contained in:
Luke Page 2015-08-08 06:42:22 +01:00
parent 3ddd3f11a3
commit 3ccd332f5a
7 changed files with 78 additions and 84 deletions

View file

@ -135,7 +135,13 @@ function mapImageCustomBit(image, pxData, getPxPos, bpp, bits, maxBit) { // esli
} }
} }
exports.dataToBitMap = function(data, width, height, bpp, depth, interlace) { exports.dataToBitMap = function(data, bitmapInfo) {
var width = bitmapInfo.width;
var height = bitmapInfo.height;
var depth = bitmapInfo.depth;
var bpp = bitmapInfo.bpp;
var interlace = bitmapInfo.interlace;
if (depth !== 8) { if (depth !== 8) {
var bits = bitRetriever(data, depth); var bits = bitRetriever(data, depth);

View file

@ -5,18 +5,18 @@ var ChunkStream = require('./chunkstream');
var Filter = require('./filter-parse'); var Filter = require('./filter-parse');
var FilterAsync = module.exports = function(width, height, Bpp, depth, interlace) { var FilterAsync = module.exports = function(bitmapInfo) {
ChunkStream.call(this); ChunkStream.call(this);
var buffers = []; var buffers = [];
var that = this; var that = this;
this._filter = new Filter(width, height, Bpp, depth, interlace, { this._filter = new Filter(bitmapInfo, {
read: this.read.bind(this), read: this.read.bind(this),
complete: function() {
that.emit('complete', Buffer.concat(buffers), width, height);
},
write: function(buffer) { write: function(buffer) {
buffers.push(buffer); buffers.push(buffer);
},
complete: function() {
that.emit('complete', Buffer.concat(buffers));
} }
}); });

View file

@ -4,11 +4,11 @@ var SyncReader = require('./sync-reader');
var Filter = require('./filter-parse'); var Filter = require('./filter-parse');
exports.process = function(inBuffer, width, height, Bpp, depth, interlace) { exports.process = function(inBuffer, bitmapInfo) {
var outBuffers = []; var outBuffers = [];
var reader = new SyncReader(inBuffer); var reader = new SyncReader(inBuffer);
var filter = new Filter(width, height, Bpp, depth, interlace, { var filter = new Filter(bitmapInfo, {
read: reader.read.bind(reader), read: reader.read.bind(reader),
write: function(bufferPart) { write: function(bufferPart) {
outBuffers.push(bufferPart); outBuffers.push(bufferPart);

View file

@ -11,12 +11,13 @@ function getByteWidth(width, bpp, depth) {
return byteWidth; return byteWidth;
} }
var Filter = module.exports = function(width, height, Bpp, depth, interlace, dependencies) { var Filter = module.exports = function(bitmapInfo, dependencies) {
this._width = width; var width = bitmapInfo.width;
this._height = height; var height = bitmapInfo.height;
this._Bpp = Bpp; //TODO rename var interlace = bitmapInfo.interlace;
this._depth = depth; var bpp = bitmapInfo.bpp;
var depth = bitmapInfo.depth;
this.read = dependencies.read; this.read = dependencies.read;
this.write = dependencies.write; this.write = dependencies.write;
@ -28,7 +29,7 @@ var Filter = module.exports = function(width, height, Bpp, depth, interlace, dep
var passes = interlaceUtils.getImagePasses(width, height); var passes = interlaceUtils.getImagePasses(width, height);
for (var i = 0; i < passes.length; i++) { for (var i = 0; i < passes.length; i++) {
this._images.push({ this._images.push({
byteWidth: getByteWidth(passes[i].width, Bpp, depth), byteWidth: getByteWidth(passes[i].width, bpp, depth),
height: passes[i].height, height: passes[i].height,
lineIndex: 0 lineIndex: 0
}); });
@ -36,7 +37,7 @@ var Filter = module.exports = function(width, height, Bpp, depth, interlace, dep
} }
else { else {
this._images.push({ this._images.push({
byteWidth: getByteWidth(width, Bpp, depth), byteWidth: getByteWidth(width, bpp, depth),
height: height, height: height,
lineIndex: 0 lineIndex: 0
}); });
@ -47,10 +48,10 @@ var Filter = module.exports = function(width, height, Bpp, depth, interlace, dep
// so if the depth is byte compatible (8 or 16) we subtract the bpp in order to compare back // 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. // a pixel rather than just a different byte part. However if we are sub byte, we ignore.
if (depth === 8) { if (depth === 8) {
this._xComparison = Bpp; this._xComparison = bpp;
} }
else if (depth === 16) { else if (depth === 16) {
this._xComparison = this._Bpp * 2; this._xComparison = bpp * 2;
} }
else { else {
this._xComparison = 1; this._xComparison = 1;
@ -164,6 +165,7 @@ Filter.prototype._reverseFilterLine = function(rawData) {
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._lastLine = null;
this.complete();
} }
}; };

View file

@ -14,11 +14,12 @@ 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._handleMetaData.bind(this),
gamma: this.emit.bind(this, 'gamma'), gamma: this.emit.bind(this, 'gamma'),
palette: this._handlePalette.bind(this),
transColor: this._handleTransColor.bind(this),
finished: this._finished.bind(this), finished: this._finished.bind(this),
inflateData: this._inflateData.bind(this), inflateData: this._inflateData.bind(this)
bitmapInfo: this._handleBitmapInfo.bind(this)
}); });
this._options = options; this._options = options;
this.writable = true; this.writable = true;
@ -55,20 +56,24 @@ ParserAsync.prototype._inflateData = function(data) {
this._inflate.write(data); this._inflate.write(data);
}; };
ParserAsync.prototype._handleBitmapInfo = function(width, height, bpp, depth, interlace) { ParserAsync.prototype._handleMetaData = function(metaData) {
this._bpp = bpp; this.emit('metadata', metaData);
this._depth = depth;
this._interlace = interlace;
this._filter = new FilterAsync( this._bitmapInfo = Object.create(metaData);
width, height,
bpp, this._filter = new FilterAsync(this._bitmapInfo);
depth,
interlace
);
}; };
ParserAsync.prototype._handleTransColor = function(transColor) {
this._bitmapInfo.transColor = transColor;
};
ParserAsync.prototype._handlePalette = function(palette) {
this._bitmapInfo.palette = palette;
};
ParserAsync.prototype._finished = function() { ParserAsync.prototype._finished = function() {
if (this.errord) { if (this.errord) {
return; return;
@ -84,31 +89,22 @@ ParserAsync.prototype._finished = function() {
this.destroySoon(); this.destroySoon();
}; };
ParserAsync.prototype._complete = function(filteredData, width, height) { ParserAsync.prototype._complete = function(filteredData) {
if (this.errord) { if (this.errord) {
return; return;
} }
try { try {
var bitmapData = bitmapper.dataToBitMap(filteredData, width, height, var bitmapData = bitmapper.dataToBitMap(filteredData, this._bitmapInfo);
this._bpp,
this._depth,
this._interlace);
// todo not bitmap data any more var normalisedBitmapData = formatNormaliser(bitmapData, this._bitmapInfo);
bitmapData = formatNormaliser(bitmapData, { bitmapData = null;
depth: this._depth, // TODO always store in this format
width: width,
height: height,
colorType: this._parser._colorType,
palette: this._parser._palette,
transColor: this._parser._transColor });
} }
catch (ex) { catch (ex) {
this._handleError(ex); this._handleError(ex);
return; return;
} }
this.emit('parsed', bitmapData); this.emit('parsed', normalisedBitmapData);
}; };

View file

@ -16,19 +16,18 @@ module.exports = function(buffer, options) {
err = _err_; err = _err_;
} }
var bpp, width, height, depth, interlace; var metaData, bitmapInfo;
function handleBitmapInfo(_width_, _height_, _bpp_, _depth_, _interlace_) {
bpp = _bpp_;
width = _width_;
height = _height_;
depth = _depth_;
interlace = _interlace_;
}
var metaData;
function handleMetaData(_metaData_) { function handleMetaData(_metaData_) {
metaData = _metaData_; metaData = _metaData_;
bitmapInfo = Object.create(metaData);
}
function handleTransColor(transColor) {
bitmapInfo.transColor = transColor;
}
function handlePalette(palette) {
bitmapInfo.palette = palette;
} }
var gamma; var gamma;
@ -48,8 +47,9 @@ module.exports = function(buffer, options) {
error: handleError, error: handleError,
metadata: handleMetaData, metadata: handleMetaData,
gamma: handleGamma, gamma: handleGamma,
inflateData: handleInflateData, palette: handlePalette,
bitmapInfo: handleBitmapInfo transColor: handleTransColor,
inflateData: handleInflateData
}); });
parser.start(); parser.start();
@ -70,29 +70,13 @@ module.exports = function(buffer, options) {
throw new Error('bad png - invalid inflate data response'); throw new Error('bad png - invalid inflate data response');
} }
var unfilteredData = FilterSync.process( var unfilteredData = FilterSync.process(inflatedData, bitmapInfo);
inflatedData,
width,
height,
bpp,
depth,
interlace
);
inflateData = null; inflateData = null;
var bitmapData = bitmapper.dataToBitMap(unfilteredData, width, height, var bitmapData = bitmapper.dataToBitMap(unfilteredData, bitmapInfo);
bpp,
depth,
interlace);
unfilteredData = null; unfilteredData = null;
var normalisedBitmapData = formatNormaliser(bitmapData, { var normalisedBitmapData = formatNormaliser(bitmapData, bitmapInfo);
depth: depth, // TODO always store in this format
width: width,
height: height,
colorType: parser._colorType, //TODO event this
palette: parser._palette,
transColor: parser._transColor });
metaData.data = normalisedBitmapData; metaData.data = normalisedBitmapData;
metaData.gamma = gamma || 0; metaData.gamma = gamma || 0;

View file

@ -28,8 +28,10 @@ var Parser = module.exports = function(options, dependencies) {
this.error = dependencies.error; this.error = dependencies.error;
this.metadata = dependencies.metadata; this.metadata = dependencies.metadata;
this.gamma = dependencies.gamma; this.gamma = dependencies.gamma;
this.transColor = dependencies.transColor;
this.palette = dependencies.palette;
this.parsed = dependencies.parsed; this.parsed = dependencies.parsed;
this.bitmapInfo = dependencies.bitmapInfo; this.inflateData = dependencies.inflateData;
this.inflateData = dependencies.inflateData; this.inflateData = dependencies.inflateData;
this.finished = dependencies.finished; this.finished = dependencies.finished;
}; };
@ -168,7 +170,6 @@ Parser.prototype._parseIHDR = function(data) {
this._colorType = colorType; this._colorType = colorType;
var bpp = colorTypeToBppMap[this._colorType]; var bpp = colorTypeToBppMap[this._colorType];
this.bitmapInfo(width, height, bpp, depth, interlace, colorType);
this._hasIHDR = true; this._hasIHDR = true;
@ -179,7 +180,9 @@ Parser.prototype._parseIHDR = function(data) {
interlace: Boolean(interlace), interlace: Boolean(interlace),
palette: Boolean(colorType & constants.COLOR_PALETTE), palette: Boolean(colorType & constants.COLOR_PALETTE),
color: Boolean(colorType & constants.COLOR_COLOR), color: Boolean(colorType & constants.COLOR_COLOR),
alpha: Boolean(colorType & constants.COLOR_ALPHA) alpha: Boolean(colorType & constants.COLOR_ALPHA),
bpp: bpp,
colorType: colorType
}); });
this._handleChunkEnd(); this._handleChunkEnd();
@ -205,6 +208,8 @@ Parser.prototype._parsePLTE = function(data) {
]); ]);
} }
this.palette(this._palette);
this._handleChunkEnd(); this._handleChunkEnd();
}; };
@ -225,19 +230,20 @@ Parser.prototype._parseTRNS = function(data) {
this.error(new Error('More transparent colors than palette size')); this.error(new Error('More transparent colors than palette size'));
return; return;
} }
for (var i = 0; i < this._palette.length; i++) { for (var i = 0; i < data.length; i++) {
this._palette[i][3] = i < data.length ? data[i] : 0xff; this._palette[i][3] = data[i];
} }
this.palette(this._palette);
} }
// for colorType 0 (grayscale) and 2 (rgb) // for colorType 0 (grayscale) and 2 (rgb)
// there might be one gray/color defined as transparent // there might be one gray/color defined as transparent
if (this._colorType === 0) { if (this._colorType === 0) {
// grey, 2 bytes // grey, 2 bytes
this._transColor = [data.readUInt16BE(0)]; this.transColor([data.readUInt16BE(0)]);
} }
if (this._colorType === 2) { if (this._colorType === 2) {
this._transColor = [data.readUInt16BE(0), data.readUInt16BE(2), data.readUInt16BE(4)]; this.transColor([data.readUInt16BE(0), data.readUInt16BE(2), data.readUInt16BE(4)]);
} }
this._handleChunkEnd(); this._handleChunkEnd();