added possibility to use multiple filters

This commit is contained in:
Pieterjan De Potter 2012-11-28 16:19:14 +01:00
parent 303e231a7e
commit 0bdf465808

View file

@ -36,15 +36,28 @@ var Filter = module.exports = function(width, height, Bpp, data, options) {
this._line = 0; this._line = 0;
options.filterType = 'filterType' in options ? options.filterType : -1; options.filterTypes = options.filterTypes || [0,1,2,3,4];
this._filters = {};
this._filters = { for (var filterType in options.filterTypes) {
0: this._filterNone.bind(this), switch (filterType) {
1: this._filterSub.bind(this), case '0':
2: this._filterUp.bind(this), this._filters[filterType] = this._filterNone.bind(this);
3: this._filterAvg.bind(this), break;
4: this._filterPaeth.bind(this) case '1':
}; this._filters[filterType] = this._filterSub.bind(this);
break;
case '2':
this._filters[filterType] = this._filterUp.bind(this);
break;
case '3':
this._filters[filterType] = this._filterAvg.bind(this);
break;
case '4':
this._filters[filterType] = this._filterPaeth.bind(this);
break;
default:
}
}
this.read(this._width * Bpp + 1, this._reverseFilterLine.bind(this)); this.read(this._width * Bpp + 1, this._reverseFilterLine.bind(this));
}; };
@ -177,21 +190,17 @@ Filter.prototype.filter = function() {
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)
if (this._options.filterType == -1) { var min = Infinity,
var min = Infinity, sel = 0;
sel = 0;
for (var f in this._filters) { for (var f in this._filters) {
var sum = this._filters[f](pxData, y, null); var sum = this._filters[f](pxData, y, null);
if (sum < min) { if (sum < min) {
sel = f; sel = f;
min = sum; min = sum;
}
} }
} else {
sel = this._options.filterType;
} }
this._filters[sel](pxData, y, rawData); this._filters[sel](pxData, y, rawData);
} }
return rawData; return rawData;