PNG compression level range is 0-9 #95

This commit is contained in:
Lovell Fuller 2014-10-03 14:50:15 +01:00
parent fb0769a327
commit 7f8f38f666
3 changed files with 24 additions and 3 deletions

View File

@ -378,7 +378,7 @@ Include all metadata (ICC, EXIF, XMP) from the input image in the output image.
An advanced setting for the _zlib_ compression level of the lossless PNG output format. The default level is `6`.
`compressionLevel` is a Number between -1 and 9.
`compressionLevel` is a Number between 0 and 9.
### Output methods

View File

@ -63,6 +63,7 @@ util.inherits(Sharp, stream.Duplex);
Handle incoming chunk on Writable Stream
*/
Sharp.prototype._write = function(chunk, encoding, callback) {
/*jslint unused: false */
if (this.options.streamIn) {
if (typeof chunk === 'object' || chunk instanceof Buffer) {
if (typeof this.options.bufferIn === 'undefined') {
@ -229,10 +230,10 @@ Sharp.prototype.quality = function(quality) {
};
Sharp.prototype.compressionLevel = function(compressionLevel) {
if (!Number.isNaN(compressionLevel) && compressionLevel >= -1 && compressionLevel <= 9) {
if (!Number.isNaN(compressionLevel) && compressionLevel >= 0 && compressionLevel <= 9) {
this.options.compressionLevel = compressionLevel;
} else {
throw new Error('Invalid compressionLevel (-1 to 9) ' + compressionLevel);
throw new Error('Invalid compressionLevel (0 to 9) ' + compressionLevel);
}
return this;
};

View File

@ -815,6 +815,26 @@ async.series([
done();
});
},
// PNG compression level - valid
function(done) {
var isValid = false;
try {
sharp().compressionLevel(0);
isValid = true;
} catch (e) {}
assert(isValid);
done();
},
// PNG compression level - invalid
function(done) {
var isValid = false;
try {
sharp().compressionLevel(-1);
isValid = true;
} catch (e) {}
assert(!isValid);
done();
},
// Verify internal counters
function(done) {
var counters = sharp.counters();