Comparing version 1.1.0 to 1.2.0
@@ -202,9 +202,9 @@ 'use strict'; | ||
if (this._buffers && this._buffers.length > 0 && this._buffers[0] == null) { | ||
if (this._buffers && this._buffers.length > 0 && this._buffers[0] === null) { | ||
this._end(); | ||
} | ||
} | ||
catch(ex) { | ||
catch (ex) { | ||
this.emit('error', ex); | ||
} | ||
}; |
@@ -15,7 +15,21 @@ 'use strict'; | ||
COLOR_PALETTE: 1, | ||
COLOR_COLOR: 2, | ||
COLOR_ALPHA: 4, | ||
// color-type bits | ||
COLORTYPE_GRAYSCALE: 0, | ||
COLORTYPE_PALETTE: 1, | ||
COLORTYPE_COLOR: 2, | ||
COLORTYPE_ALPHA: 4, // e.g. grayscale and alpha | ||
// color-type combinations | ||
COLORTYPE_PALETTE_COLOR: 3, | ||
COLORTYPE_COLOR_ALPHA: 6, | ||
COLORTYPE_TO_BPP_MAP: { | ||
0: 1, | ||
2: 3, | ||
3: 1, | ||
4: 2, | ||
6: 4 | ||
}, | ||
GAMMA_DIVISION: 100000 | ||
}; |
@@ -6,3 +6,2 @@ 'use strict'; | ||
function filterNone(pxData, pxPos, byteWidth, rawData, rawPos) { | ||
pxData.copy(rawData, rawPos, pxPos, pxPos + byteWidth); | ||
@@ -22,7 +21,7 @@ } | ||
function filterSub(pxData, pxPos, byteWidth, rawData, rawPos) { | ||
function filterSub(pxData, pxPos, byteWidth, rawData, rawPos, bpp) { | ||
for (var x = 0; x < byteWidth; x++) { | ||
var left = x >= 4 ? pxData[pxPos + x - 4] : 0; | ||
var left = x >= bpp ? pxData[pxPos + x - bpp] : 0; | ||
var val = pxData[pxPos + x] - left; | ||
@@ -34,3 +33,3 @@ | ||
function filterSumSub(pxData, pxPos, byteWidth) { | ||
function filterSumSub(pxData, pxPos, byteWidth, bpp) { | ||
@@ -40,3 +39,3 @@ var sum = 0; | ||
var left = x >= 4 ? pxData[pxPos + x - 4] : 0; | ||
var left = x >= bpp ? pxData[pxPos + x - bpp] : 0; | ||
var val = pxData[pxPos + x] - left; | ||
@@ -76,7 +75,7 @@ | ||
function filterAvg(pxData, pxPos, byteWidth, rawData, rawPos) { | ||
function filterAvg(pxData, pxPos, byteWidth, rawData, rawPos, bpp) { | ||
for (var x = 0; x < byteWidth; x++) { | ||
var left = x >= 4 ? pxData[pxPos + x - 4] : 0; | ||
var left = x >= bpp ? pxData[pxPos + x - bpp] : 0; | ||
var up = pxPos > 0 ? pxData[pxPos + x - byteWidth] : 0; | ||
@@ -89,3 +88,3 @@ var val = pxData[pxPos + x] - ((left + up) >> 1); | ||
function filterSumAvg(pxData, pxPos, byteWidth) { | ||
function filterSumAvg(pxData, pxPos, byteWidth, bpp) { | ||
@@ -95,3 +94,3 @@ var sum = 0; | ||
var left = x >= 4 ? pxData[pxPos + x - 4] : 0; | ||
var left = x >= bpp ? pxData[pxPos + x - bpp] : 0; | ||
var up = pxPos > 0 ? pxData[pxPos + x - byteWidth] : 0; | ||
@@ -106,9 +105,9 @@ var val = pxData[pxPos + x] - ((left + up) >> 1); | ||
function filterPaeth(pxData, pxPos, byteWidth, rawData, rawPos) { | ||
function filterPaeth(pxData, pxPos, byteWidth, rawData, rawPos, bpp) { | ||
for (var x = 0; x < byteWidth; x++) { | ||
var left = x >= 4 ? pxData[pxPos + x - 4] : 0; | ||
var left = x >= bpp ? pxData[pxPos + x - bpp] : 0; | ||
var up = pxPos > 0 ? pxData[pxPos + x - byteWidth] : 0; | ||
var upleft = pxPos > 0 && x >= 4 ? pxData[pxPos + x - (byteWidth + 4)] : 0; | ||
var upleft = pxPos > 0 && x >= bpp ? pxData[pxPos + x - (byteWidth + bpp)] : 0; | ||
var val = pxData[pxPos + x] - paethPredictor(left, up, upleft); | ||
@@ -120,10 +119,9 @@ | ||
function filterSumPaeth(pxData, pxPos, byteWidth) { | ||
function filterSumPaeth(pxData, pxPos, byteWidth, bpp) { | ||
var sum = 0; | ||
for (var x = 0; x < byteWidth; x++) { | ||
var left = x >= 4 ? pxData[pxPos + x - 4] : 0; | ||
var left = x >= bpp ? pxData[pxPos + x - bpp] : 0; | ||
var up = pxPos > 0 ? pxData[pxPos + x - byteWidth] : 0; | ||
var upleft = pxPos > 0 && x >= 4 ? pxData[pxPos + x - (byteWidth + 4)] : 0; | ||
var upleft = pxPos > 0 && x >= bpp ? pxData[pxPos + x - (byteWidth + bpp)] : 0; | ||
var val = pxData[pxPos + x] - paethPredictor(left, up, upleft); | ||
@@ -153,3 +151,3 @@ | ||
module.exports = function(pxData, width, height, options) { | ||
module.exports = function(pxData, width, height, options, bpp) { | ||
@@ -167,3 +165,3 @@ var filterTypes; | ||
var byteWidth = width << 2; | ||
var byteWidth = width * bpp; | ||
var rawPos = 0; | ||
@@ -181,4 +179,3 @@ var pxPos = 0; | ||
for (var i = 0; i < filterTypes.length; i++) { | ||
var sum = filterSums[filterTypes[i]](pxData, pxPos, byteWidth); | ||
var sum = filterSums[filterTypes[i]](pxData, pxPos, byteWidth, bpp); | ||
if (sum < min) { | ||
@@ -193,3 +190,3 @@ sel = filterTypes[i]; | ||
rawPos++; | ||
filters[sel](pxData, pxPos, byteWidth, rawData, rawPos); | ||
filters[sel](pxData, pxPos, byteWidth, rawData, rawPos, bpp); | ||
rawPos += byteWidth; | ||
@@ -199,2 +196,2 @@ pxPos += byteWidth; | ||
return rawData; | ||
}; | ||
}; |
@@ -0,0 +0,0 @@ 'use strict'; |
'use strict'; | ||
var util = require('util'); | ||
@@ -10,4 +9,4 @@ var Stream = require('stream'); | ||
var constants = require('./constants'); | ||
var bitPacker = require('./bitpacker'); | ||
var Packer = module.exports = function(options) { | ||
@@ -21,4 +20,14 @@ Stream.call(this); | ||
options.deflateStrategy = options.deflateStrategy != null ? options.deflateStrategy : 3; | ||
options.inputHasAlpha = options.inputHasAlpha != null ? options.inputHasAlpha : true; | ||
options.deflateFactory = options.deflateFactory || zlib.createDeflate; | ||
options.bitDepth = options.bitDepth || 8; | ||
options.colorType = (typeof options.colorType === 'number') ? options.colorType : constants.COLORTYPE_COLOR_ALPHA; | ||
if (options.colorType !== constants.COLORTYPE_COLOR && options.colorType !== constants.COLORTYPE_COLOR_ALPHA) { | ||
throw new Error('option color type:' + options.colorType + ' is not supported at present'); | ||
} | ||
if (options.bitDepth !== 8) { | ||
throw new Error('option bit depth:' + options.bitDepth + ' is not supported at present'); | ||
} | ||
this.readable = true; | ||
@@ -30,6 +39,5 @@ }; | ||
Packer.prototype.pack = function(data, width, height, gamma) { | ||
// Signature | ||
this.emit('data', new Buffer(constants.PNG_SIGNATURE)); | ||
this.emit('data', this._packIHDR(width, height)); | ||
this.emit('data', this._packIHDR(width, height, this._options.bitDepth, this._options.colorType)); | ||
@@ -40,4 +48,8 @@ if (gamma) { | ||
// convert to correct format for filtering (e.g. right bpp and bit depth) | ||
var packedData = bitPacker(data, width, height, this._options); | ||
// filter pixel data | ||
var filteredData = filter(data, width, height, this._options); | ||
var bpp = constants.COLORTYPE_TO_BPP_MAP[this._options.colorType]; | ||
var filteredData = filter(packedData, width, height, this._options, bpp); | ||
@@ -86,3 +98,3 @@ // compress it | ||
Packer.prototype._packIHDR = function(width, height) { | ||
Packer.prototype._packIHDR = function(width, height, bitDepth, colorType) { | ||
@@ -92,4 +104,4 @@ var buf = new Buffer(13); | ||
buf.writeUInt32BE(height, 4); | ||
buf[8] = 8; | ||
buf[9] = 6; // colorType | ||
buf[8] = bitDepth; // Bit depth | ||
buf[9] = colorType; // colorType | ||
buf[10] = 0; // compression | ||
@@ -96,0 +108,0 @@ buf[11] = 0; // filter |
@@ -39,10 +39,2 @@ 'use strict'; | ||
var colorTypeToBppMap = { | ||
0: 1, | ||
2: 3, | ||
3: 1, | ||
4: 2, | ||
6: 4 | ||
}; | ||
Parser.prototype.start = function() { | ||
@@ -154,3 +146,3 @@ this.read(constants.PNG_SIGNATURE.length, | ||
} | ||
if (!(colorType in colorTypeToBppMap)) { | ||
if (!(colorType in constants.COLORTYPE_TO_BPP_MAP)) { | ||
this.error(new Error('Unsupported color type')); | ||
@@ -174,3 +166,3 @@ return; | ||
var bpp = colorTypeToBppMap[this._colorType]; | ||
var bpp = constants.COLORTYPE_TO_BPP_MAP[this._colorType]; | ||
@@ -184,5 +176,5 @@ this._hasIHDR = true; | ||
interlace: Boolean(interlace), | ||
palette: Boolean(colorType & constants.COLOR_PALETTE), | ||
color: Boolean(colorType & constants.COLOR_COLOR), | ||
alpha: Boolean(colorType & constants.COLOR_ALPHA), | ||
palette: Boolean(colorType & constants.COLORTYPE_PALETTE), | ||
color: Boolean(colorType & constants.COLORTYPE_COLOR), | ||
alpha: Boolean(colorType & constants.COLORTYPE_ALPHA), | ||
bpp: bpp, | ||
@@ -228,3 +220,3 @@ colorType: colorType | ||
// palette | ||
if (this._colorType === 3) { | ||
if (this._colorType === constants.COLORTYPE_PALETTE_COLOR) { | ||
if (this._palette.length === 0) { | ||
@@ -246,7 +238,7 @@ this.error(new Error('Transparency chunk must be after palette')); | ||
// there might be one gray/color defined as transparent | ||
if (this._colorType === 0) { | ||
if (this._colorType === constants.COLORTYPE_GRAYSCALE) { | ||
// grey, 2 bytes | ||
this.transColor([data.readUInt16BE(0)]); | ||
} | ||
if (this._colorType === 2) { | ||
if (this._colorType === constants.COLORTYPE_COLOR) { | ||
this.transColor([data.readUInt16BE(0), data.readUInt16BE(2), data.readUInt16BE(4)]); | ||
@@ -276,3 +268,3 @@ } | ||
if (this._colorType === 3 && this._palette.length === 0) { | ||
if (this._colorType === constants.COLORTYPE_PALETTE_COLOR && this._palette.length === 0) { | ||
throw new Error('Expected palette not found'); | ||
@@ -279,0 +271,0 @@ } |
@@ -18,4 +18,4 @@ 'use strict'; | ||
this.data = this.width > 0 && this.height > 0 | ||
? new Buffer(4 * this.width * this.height) : null; | ||
this.data = this.width > 0 && this.height > 0 ? | ||
new Buffer(4 * this.width * this.height) : null; | ||
@@ -164,2 +164,2 @@ if (options.fill && this.data) { | ||
PNG.adjustGamma(this); | ||
}; | ||
}; |
{ | ||
"name": "pngjs2", | ||
"version": "1.1.0", | ||
"version": "1.2.0", | ||
"description": "PNG encoder/decoder in pure JS, supporting any bit size & interlace, async & sync with full test suite.", | ||
@@ -12,3 +12,4 @@ "contributors": [ | ||
"Steven Sojka", | ||
"liangzeng" | ||
"liangzeng", | ||
"Michael Vogt" | ||
], | ||
@@ -15,0 +16,0 @@ "homepage": "https://github.com/lukeapage/pngjs2", |
@@ -12,2 +12,3 @@ [![Build Status](https://travis-ci.org/lukeapage/pngjs2.svg?branch=master)](https://travis-ci.org/lukeapage/pngjs2) [![Build status](https://ci.appveyor.com/api/projects/status/tb8418jql1trkntd/branch/master?svg=true)](https://ci.appveyor.com/project/lukeapage/pngjs2/branch/master) [![Coverage Status](https://coveralls.io/repos/lukeapage/pngjs2/badge.svg?branch=master&service=github)](https://coveralls.io/github/lukeapage/pngjs2?branch=master) [![npm version](https://badge.fury.io/js/pngjs2.svg)](http://badge.fury.io/js/pngjs2) | ||
* Support for reading `tTRNS` transparent colours | ||
* Support for writing colortype 2 (RGB) and colortype 6 (RGBA) | ||
* Sync interface as well as async | ||
@@ -19,3 +20,3 @@ * API compatible with pngjs and node-pngjs | ||
* Extended PNG e.g. Animation | ||
* Writing in different formats | ||
* Writing in different formats, colortype 0 (greyscale), colortype 3 (indexed color), colortype 4 (greyscale with alpha) | ||
* Synchronous write | ||
@@ -117,2 +118,4 @@ | ||
- `filterType` - png filtering method for scanlines (default: -1 => auto, accepts array of numbers 0-4) | ||
- `colorType` - the output colorType - see constants. 2 = color, no alpha, 6 = color & alpha. Default currently 6, but in the future may calculate best mode. | ||
- `inputHasAlpha` - whether the input bitmap has 4 bits per pixel (rgb and alpha) or 3 (rgb - no alpha). | ||
@@ -169,7 +172,5 @@ | ||
```js | ||
var dst = new PNG({filterType: -1, width: 100, height: 50}); | ||
var dst = new PNG({width: 100, height: 50}); | ||
fs.createReadStream('in.png') | ||
.pipe(new PNG({ | ||
filterType: -1 | ||
})) | ||
.pipe(new PNG()) | ||
.on('parsed', function() { | ||
@@ -190,5 +191,3 @@ this.bitblt(dst, 0, 0, 100, 50, 0, 0); | ||
fs.createReadStream('in.png') | ||
.pipe(new PNG({ | ||
filterType: -1 | ||
})) | ||
.pipe(new PNG()) | ||
.on('parsed', function() { | ||
@@ -242,2 +241,14 @@ this.adjustGamma(); | ||
### 1.2.0 - 13/09/2015 | ||
- support passing colorType to write PNG's and writing bitmaps without alpha information | ||
### 1.1.0 - 07/09/2015 | ||
- support passing a deflate factory for controlled compression | ||
### 1.0.2 - 22/08/2015 | ||
- Expose all PNG creation info | ||
### 1.0.1 - 21/08/2015 | ||
- Fix non square interlaced files | ||
### 1.0.0 - 08/08/2015 | ||
@@ -244,0 +255,0 @@ - More tests |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
707457
78
2299
288