Merge branch 'master' of git://github.com/pdpotter/node-pngjs into pdpotter-master

This commit is contained in:
Kuba Niegowski 2012-11-28 23:24:34 +01:00
commit b01112cf2f
2 changed files with 35 additions and 24 deletions

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));
}; };
@ -56,7 +69,7 @@ var pixelBppMap = {
0: 0, 0: 0,
1: 0, 1: 0,
2: 0, 2: 0,
3: 0xff, 3: 0xff
}, },
2: { // LA 2: { // LA
0: 0, 0: 0,
@ -85,7 +98,7 @@ Filter.prototype._reverseFilterLine = function(rawData) {
pxRowPos = this._line * pxLineLength, pxRowPos = this._line * pxLineLength,
filter = rawData[0]; filter = rawData[0];
if (filter == 0) { if (filter === 0) {
for (var x = 0; x < this._width; x++) { for (var x = 0; x < this._width; x++) {
var pxPos = pxRowPos + (x << 2), var pxPos = pxRowPos + (x << 2),
rawPos = 1 + x * this._Bpp; rawPos = 1 + x * this._Bpp;
@ -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;

View file

@ -36,6 +36,7 @@ var Packer = module.exports = function(options) {
options.deflateChunkSize = options.deflateChunkSize || 32 * 1024; options.deflateChunkSize = options.deflateChunkSize || 32 * 1024;
options.deflateLevel = options.deflateLevel || 9; options.deflateLevel = options.deflateLevel || 9;
options.deflateStrategy = options.deflateStrategy || 3;
this.readable = true; this.readable = true;
}; };
@ -55,7 +56,8 @@ Packer.prototype.pack = function(data, width, height) {
// compress it // compress it
var deflate = zlib.createDeflate({ var deflate = zlib.createDeflate({
chunkSize: this._options.deflateChunkSize, chunkSize: this._options.deflateChunkSize,
level: this._options.deflateLevel level: this._options.deflateLevel,
strategy: this._options.deflateStrategy
}); });
deflate.on('error', this.emit.bind(this, 'error')); deflate.on('error', this.emit.bind(this, 'error'));