diff --git a/README.md b/README.md index ceb7b0c..03803ac 100644 --- a/README.md +++ b/README.md @@ -59,7 +59,8 @@ As input any color type is accepted (grayscale, rgb, palette, grayscale with alp - `checkCRC` - whether parser should be strict about checksums in source stream (default: `true`) - `deflateChunkSize` - chunk size used for deflating data chunks, this should be power of 2 and must not be less than 256 and more than 32*1024 (default: 32 kB) - `deflateLevel` - compression level for delate (default: 9) -- `filterType` - png filtering method for scanlines (default: -1 => auto) +- `deflateStrategy` - compression strategy for delate (default: 3) +- `filterType` - png filtering method for scanlines (default: -1 => auto, accepts array of numbers 0-4) ### Event "metadata" @@ -120,6 +121,10 @@ Gamma of image (0 if not specified) Changelog ============ +### 0.4.0-alpha - 29 Nov 2012 + - added zlib deflateStrategy option, default to Z_RLE (by pdpotter) + - added possibility to use multiple filters (by pdpotter, modified by niegowski) + ### 0.3.0-alpha - 23 Aug 2012 - Processing data as Streams, not complete Buffers of data diff --git a/lib/filter.js b/lib/filter.js index e245205..fbe60d2 100755 --- a/lib/filter.js +++ b/lib/filter.js @@ -36,29 +36,20 @@ var Filter = module.exports = function(width, height, Bpp, data, options) { this._line = 0; - 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: - } + if (!('filterType' in options) || options.filterType == -1) { + options.filterType = [0, 1, 2, 3, 4]; + } else if (typeof options.filterType == 'number') { + options.filterType = [options.filterType]; } + 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) + }; + this.read(this._width * Bpp + 1, this._reverseFilterLine.bind(this)); }; util.inherits(Filter, ChunkStream); @@ -98,7 +89,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; @@ -190,13 +181,14 @@ Filter.prototype.filter = function() { for (var y = 0; y < this._height; y++) { // find best filter for this line (with lowest sum of values) - var min = Infinity, + var filterTypes = this._options.filterType, + min = Infinity, sel = 0; - for (var f in this._filters) { - var sum = this._filters[f](pxData, y, null); + for (var i = 0; i < filterTypes.length; i++) { + var sum = this._filters[filterTypes[i]](pxData, y, null); if (sum < min) { - sel = f; + sel = filterTypes[i]; min = sum; } }