Enable PNG palette when at least one of quality, colours, colors or dither is set (#2226)

This commit is contained in:
Roman Malieiev 2020-05-28 23:21:33 +02:00 committed by GitHub
parent c76ae06fd1
commit a7b1185602
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 34 additions and 31 deletions

View File

@ -293,28 +293,30 @@ function png (options) {
} }
if (is.defined(options.palette)) { if (is.defined(options.palette)) {
this._setBooleanOption('pngPalette', options.palette); this._setBooleanOption('pngPalette', options.palette);
if (this.options.pngPalette) { } else if (is.defined(options.quality) || is.defined(options.colours || options.colors) || is.defined(options.dither)) {
if (is.defined(options.quality)) { this._setBooleanOption('pngPalette', true);
if (is.integer(options.quality) && is.inRange(options.quality, 0, 100)) { }
this.options.pngQuality = options.quality; if (this.options.pngPalette) {
} else { if (is.defined(options.quality)) {
throw is.invalidParameterError('quality', 'integer between 0 and 100', options.quality); if (is.integer(options.quality) && is.inRange(options.quality, 0, 100)) {
} this.options.pngQuality = options.quality;
} else {
throw is.invalidParameterError('quality', 'integer between 0 and 100', options.quality);
} }
const colours = options.colours || options.colors; }
if (is.defined(colours)) { const colours = options.colours || options.colors;
if (is.integer(colours) && is.inRange(colours, 2, 256)) { if (is.defined(colours)) {
this.options.pngColours = colours; if (is.integer(colours) && is.inRange(colours, 2, 256)) {
} else { this.options.pngColours = colours;
throw is.invalidParameterError('colours', 'integer between 2 and 256', colours); } else {
} throw is.invalidParameterError('colours', 'integer between 2 and 256', colours);
} }
if (is.defined(options.dither)) { }
if (is.number(options.dither) && is.inRange(options.dither, 0, 1)) { if (is.defined(options.dither)) {
this.options.pngDither = options.dither; if (is.number(options.dither) && is.inRange(options.dither, 0, 1)) {
} else { this.options.pngDither = options.dither;
throw is.invalidParameterError('dither', 'number between 0.0 and 1.0', options.dither); } else {
} throw is.invalidParameterError('dither', 'number between 0.0 and 1.0', options.dither);
} }
} }
} }

View File

@ -66,7 +66,8 @@
"Paul Neave <paul.neave@gmail.com>", "Paul Neave <paul.neave@gmail.com>",
"Brendan Kennedy <brenwken@gmail.com>", "Brendan Kennedy <brenwken@gmail.com>",
"Brychan Bennett-Odlum <git@brychan.io>", "Brychan Bennett-Odlum <git@brychan.io>",
"Edward Silverton <e.silverton@gmail.com>" "Edward Silverton <e.silverton@gmail.com>",
"Roman Malieiev <aromaleev@gmail.com>"
], ],
"scripts": { "scripts": {
"install": "(node install/libvips && node install/dll-copy && prebuild-install --runtime=napi) || (node-gyp rebuild && node install/dll-copy)", "install": "(node install/libvips && node install/dll-copy && prebuild-install --runtime=napi) || (node-gyp rebuild && node install/dll-copy)",

View File

@ -127,8 +127,8 @@ describe('PNG', function () {
it('Valid PNG libimagequant quality value produces image of same size or smaller', function () { it('Valid PNG libimagequant quality value produces image of same size or smaller', function () {
const inputPngBuffer = fs.readFileSync(fixtures.inputPng); const inputPngBuffer = fs.readFileSync(fixtures.inputPng);
return Promise.all([ return Promise.all([
sharp(inputPngBuffer).resize(10).png({ palette: true, quality: 80 }).toBuffer(), sharp(inputPngBuffer).resize(10).png({ quality: 80 }).toBuffer(),
sharp(inputPngBuffer).resize(10).png({ palette: true, quality: 100 }).toBuffer() sharp(inputPngBuffer).resize(10).png({ quality: 100 }).toBuffer()
]).then(function (data) { ]).then(function (data) {
assert.strictEqual(true, data[0].length <= data[1].length); assert.strictEqual(true, data[0].length <= data[1].length);
}); });
@ -136,15 +136,15 @@ describe('PNG', function () {
it('Invalid PNG libimagequant quality value throws error', function () { it('Invalid PNG libimagequant quality value throws error', function () {
assert.throws(function () { assert.throws(function () {
sharp().png({ palette: true, quality: 101 }); sharp().png({ quality: 101 });
}); });
}); });
it('Valid PNG libimagequant colours value produces image of same size or smaller', function () { it('Valid PNG libimagequant colours value produces image of same size or smaller', function () {
const inputPngBuffer = fs.readFileSync(fixtures.inputPng); const inputPngBuffer = fs.readFileSync(fixtures.inputPng);
return Promise.all([ return Promise.all([
sharp(inputPngBuffer).resize(10).png({ palette: true, colours: 100 }).toBuffer(), sharp(inputPngBuffer).resize(10).png({ colours: 100 }).toBuffer(),
sharp(inputPngBuffer).resize(10).png({ palette: true, colours: 200 }).toBuffer() sharp(inputPngBuffer).resize(10).png({ colours: 200 }).toBuffer()
]).then(function (data) { ]).then(function (data) {
assert.strictEqual(true, data[0].length <= data[1].length); assert.strictEqual(true, data[0].length <= data[1].length);
}); });
@ -152,21 +152,21 @@ describe('PNG', function () {
it('Invalid PNG libimagequant colours value throws error', function () { it('Invalid PNG libimagequant colours value throws error', function () {
assert.throws(function () { assert.throws(function () {
sharp().png({ palette: true, colours: -1 }); sharp().png({ colours: -1 });
}); });
}); });
it('Invalid PNG libimagequant colors value throws error', function () { it('Invalid PNG libimagequant colors value throws error', function () {
assert.throws(function () { assert.throws(function () {
sharp().png({ palette: true, colors: 0.1 }); sharp().png({ colors: 0.1 });
}); });
}); });
it('Valid PNG libimagequant dither value produces image of same size or smaller', function () { it('Valid PNG libimagequant dither value produces image of same size or smaller', function () {
const inputPngBuffer = fs.readFileSync(fixtures.inputPng); const inputPngBuffer = fs.readFileSync(fixtures.inputPng);
return Promise.all([ return Promise.all([
sharp(inputPngBuffer).resize(10).png({ palette: true, dither: 0.1 }).toBuffer(), sharp(inputPngBuffer).resize(10).png({ dither: 0.1 }).toBuffer(),
sharp(inputPngBuffer).resize(10).png({ palette: true, dither: 0.9 }).toBuffer() sharp(inputPngBuffer).resize(10).png({ dither: 0.9 }).toBuffer()
]).then(function (data) { ]).then(function (data) {
assert.strictEqual(true, data[0].length <= data[1].length); assert.strictEqual(true, data[0].length <= data[1].length);
}); });
@ -174,7 +174,7 @@ describe('PNG', function () {
it('Invalid PNG libimagequant dither value throws error', function () { it('Invalid PNG libimagequant dither value throws error', function () {
assert.throws(function () { assert.throws(function () {
sharp().png({ palette: true, dither: 'fail' }); sharp().png({ dither: 'fail' });
}); });
}); });
}); });