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;
options.filterType = 'filterType' in options ? options.filterType : -1;
this._filters = {
0: this._filterNone.bind(this),
1: this._filterSub.bind(this),
2: this._filterUp.bind(this),
3: this._filterAvg.bind(this),
4: this._filterPaeth.bind(this)
};
options.filterTypes = options.filterTypes || [0,1,2,3,4];
this._filters = {};
for (var filterType in options.filterTypes) {
switch (filterType) {
case '0':
this._filters[filterType] = this._filterNone.bind(this);
break;
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));
};
@ -177,21 +190,17 @@ Filter.prototype.filter = function() {
for (var y = 0; y < this._height; y++) {
// find best filter for this line (with lowest sum of values)
if (this._options.filterType == -1) {
var min = Infinity,
sel = 0;
var min = Infinity,
sel = 0;
for (var f in this._filters) {
var sum = this._filters[f](pxData, y, null);
if (sum < min) {
sel = f;
min = sum;
}
for (var f in this._filters) {
var sum = this._filters[f](pxData, y, null);
if (sum < min) {
sel = f;
min = sum;
}
} else {
sel = this._options.filterType;
}
this._filters[sel](pxData, y, rawData);
}
return rawData;