mirror of
https://github.com/danbulant/pngjs
synced 2026-05-27 05:41:47 +00:00
Merge branch 'pdpotter-master'
This commit is contained in:
commit
a6406c85f2
2 changed files with 35 additions and 24 deletions
|
|
@ -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));
|
||||
};
|
||||
|
|
@ -56,7 +69,7 @@ var pixelBppMap = {
|
|||
0: 0,
|
||||
1: 0,
|
||||
2: 0,
|
||||
3: 0xff,
|
||||
3: 0xff
|
||||
},
|
||||
2: { // LA
|
||||
0: 0,
|
||||
|
|
@ -85,7 +98,7 @@ Filter.prototype._reverseFilterLine = function(rawData) {
|
|||
pxRowPos = this._line * pxLineLength,
|
||||
filter = rawData[0];
|
||||
|
||||
if (filter == 0) {
|
||||
if (filter === 0) {
|
||||
for (var x = 0; x < this._width; x++) {
|
||||
var pxPos = pxRowPos + (x << 2),
|
||||
rawPos = 1 + x * this._Bpp;
|
||||
|
|
@ -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;
|
||||
|
|
|
|||
|
|
@ -36,6 +36,7 @@ var Packer = module.exports = function(options) {
|
|||
|
||||
options.deflateChunkSize = options.deflateChunkSize || 32 * 1024;
|
||||
options.deflateLevel = options.deflateLevel || 9;
|
||||
options.deflateStrategy = options.deflateStrategy || 3;
|
||||
|
||||
this.readable = true;
|
||||
};
|
||||
|
|
@ -55,7 +56,8 @@ Packer.prototype.pack = function(data, width, height) {
|
|||
// compress it
|
||||
var deflate = zlib.createDeflate({
|
||||
chunkSize: this._options.deflateChunkSize,
|
||||
level: this._options.deflateLevel
|
||||
level: this._options.deflateLevel,
|
||||
strategy: this._options.deflateStrategy
|
||||
});
|
||||
deflate.on('error', this.emit.bind(this, 'error'));
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue