From a7b1185602472670bdf998fda66536d434dfad18 Mon Sep 17 00:00:00 2001 From: Roman Malieiev Date: Thu, 28 May 2020 23:21:33 +0200 Subject: [PATCH] Enable PNG palette when at least one of quality, colours, colors or dither is set (#2226) --- lib/output.js | 42 ++++++++++++++++++++++-------------------- package.json | 3 ++- test/unit/png.js | 20 ++++++++++---------- 3 files changed, 34 insertions(+), 31 deletions(-) diff --git a/lib/output.js b/lib/output.js index d8cdfff4..b5fa3b3f 100644 --- a/lib/output.js +++ b/lib/output.js @@ -293,28 +293,30 @@ function png (options) { } if (is.defined(options.palette)) { this._setBooleanOption('pngPalette', options.palette); - if (this.options.pngPalette) { - if (is.defined(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); - } + } 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)) { + 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)) { - if (is.integer(colours) && is.inRange(colours, 2, 256)) { - this.options.pngColours = colours; - } else { - throw is.invalidParameterError('colours', 'integer between 2 and 256', colours); - } + } + const colours = options.colours || options.colors; + if (is.defined(colours)) { + if (is.integer(colours) && is.inRange(colours, 2, 256)) { + this.options.pngColours = 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)) { - this.options.pngDither = options.dither; - } else { - throw is.invalidParameterError('dither', 'number between 0.0 and 1.0', options.dither); - } + } + if (is.defined(options.dither)) { + if (is.number(options.dither) && is.inRange(options.dither, 0, 1)) { + this.options.pngDither = options.dither; + } else { + throw is.invalidParameterError('dither', 'number between 0.0 and 1.0', options.dither); } } } diff --git a/package.json b/package.json index 6d66cd17..925e0a4d 100644 --- a/package.json +++ b/package.json @@ -66,7 +66,8 @@ "Paul Neave ", "Brendan Kennedy ", "Brychan Bennett-Odlum ", - "Edward Silverton " + "Edward Silverton ", + "Roman Malieiev " ], "scripts": { "install": "(node install/libvips && node install/dll-copy && prebuild-install --runtime=napi) || (node-gyp rebuild && node install/dll-copy)", diff --git a/test/unit/png.js b/test/unit/png.js index 4661d414..810cb3d4 100644 --- a/test/unit/png.js +++ b/test/unit/png.js @@ -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' }); }); }); });