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,6 +293,9 @@ function png (options) {
}
if (is.defined(options.palette)) {
this._setBooleanOption('pngPalette', options.palette);
} else if (is.defined(options.quality) || is.defined(options.colours || options.colors) || is.defined(options.dither)) {
this._setBooleanOption('pngPalette', true);
}
if (this.options.pngPalette) {
if (is.defined(options.quality)) {
if (is.integer(options.quality) && is.inRange(options.quality, 0, 100)) {
@ -318,7 +321,6 @@ function png (options) {
}
}
}
}
return this._updateFormatOut('png', options);
}

View File

@ -66,7 +66,8 @@
"Paul Neave <paul.neave@gmail.com>",
"Brendan Kennedy <brenwken@gmail.com>",
"Brychan Bennett-Odlum <git@brychan.io>",
"Edward Silverton <e.silverton@gmail.com>"
"Edward Silverton <e.silverton@gmail.com>",
"Roman Malieiev <aromaleev@gmail.com>"
],
"scripts": {
"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 () {
const inputPngBuffer = fs.readFileSync(fixtures.inputPng);
return Promise.all([
sharp(inputPngBuffer).resize(10).png({ palette: true, quality: 80 }).toBuffer(),
sharp(inputPngBuffer).resize(10).png({ palette: true, quality: 100 }).toBuffer()
sharp(inputPngBuffer).resize(10).png({ quality: 80 }).toBuffer(),
sharp(inputPngBuffer).resize(10).png({ quality: 100 }).toBuffer()
]).then(function (data) {
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 () {
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 () {
const inputPngBuffer = fs.readFileSync(fixtures.inputPng);
return Promise.all([
sharp(inputPngBuffer).resize(10).png({ palette: true, colours: 100 }).toBuffer(),
sharp(inputPngBuffer).resize(10).png({ palette: true, colours: 200 }).toBuffer()
sharp(inputPngBuffer).resize(10).png({ colours: 100 }).toBuffer(),
sharp(inputPngBuffer).resize(10).png({ colours: 200 }).toBuffer()
]).then(function (data) {
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 () {
assert.throws(function () {
sharp().png({ palette: true, colours: -1 });
sharp().png({ colours: -1 });
});
});
it('Invalid PNG libimagequant colors value throws error', 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 () {
const inputPngBuffer = fs.readFileSync(fixtures.inputPng);
return Promise.all([
sharp(inputPngBuffer).resize(10).png({ palette: true, dither: 0.1 }).toBuffer(),
sharp(inputPngBuffer).resize(10).png({ palette: true, dither: 0.9 }).toBuffer()
sharp(inputPngBuffer).resize(10).png({ dither: 0.1 }).toBuffer(),
sharp(inputPngBuffer).resize(10).png({ dither: 0.9 }).toBuffer()
]).then(function (data) {
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 () {
assert.throws(function () {
sharp().png({ palette: true, dither: 'fail' });
sharp().png({ dither: 'fail' });
});
});
});