Ensure PNG bitdepth can be set for non-palette output #3322

This commit is contained in:
Lovell Fuller 2022-08-22 14:57:12 +01:00
parent e1bc8674fd
commit 3a44748f49
3 changed files with 28 additions and 8 deletions

View File

@ -52,6 +52,9 @@ Requires libvips v8.13.0
Emit warnings when previous calls in the same pipeline will be ignored. Emit warnings when previous calls in the same pipeline will be ignored.
[#3319](https://github.com/lovell/sharp/issues/3319) [#3319](https://github.com/lovell/sharp/issues/3319)
* Ensure PNG bitdepth can be set for non-palette output.
[#3322](https://github.com/lovell/sharp/issues/3322)
* Ensure resized image is unpremultiplied before composite. * Ensure resized image is unpremultiplied before composite.
[#3334](https://github.com/lovell/sharp/issues/3334) [#3334](https://github.com/lovell/sharp/issues/3334)

View File

@ -407,6 +407,14 @@ function png (options) {
if (is.defined(options.adaptiveFiltering)) { if (is.defined(options.adaptiveFiltering)) {
this._setBooleanOption('pngAdaptiveFiltering', options.adaptiveFiltering); this._setBooleanOption('pngAdaptiveFiltering', options.adaptiveFiltering);
} }
const colours = options.colours || options.colors;
if (is.defined(colours)) {
if (is.integer(colours) && is.inRange(colours, 2, 256)) {
this.options.pngBitdepth = bitdepthFromColourCount(colours);
} else {
throw is.invalidParameterError('colours', 'integer between 2 and 256', colours);
}
}
if (is.defined(options.palette)) { if (is.defined(options.palette)) {
this._setBooleanOption('pngPalette', options.palette); this._setBooleanOption('pngPalette', options.palette);
} else if ([options.quality, options.effort, options.colours, options.colors, options.dither].some(is.defined)) { } else if ([options.quality, options.effort, options.colours, options.colors, options.dither].some(is.defined)) {
@ -427,14 +435,6 @@ function png (options) {
throw is.invalidParameterError('effort', 'integer between 1 and 10', options.effort); throw is.invalidParameterError('effort', 'integer between 1 and 10', options.effort);
} }
} }
const colours = options.colours || options.colors;
if (is.defined(colours)) {
if (is.integer(colours) && is.inRange(colours, 2, 256)) {
this.options.pngBitdepth = bitdepthFromColourCount(colours);
} else {
throw is.invalidParameterError('colours', 'integer between 2 and 256', colours);
}
}
if (is.defined(options.dither)) { if (is.defined(options.dither)) {
if (is.number(options.dither) && is.inRange(options.dither, 0, 1)) { if (is.number(options.dither) && is.inRange(options.dither, 0, 1)) {
this.options.pngDither = options.dither; this.options.pngDither = options.dither;

View File

@ -190,6 +190,23 @@ describe('PNG', function () {
}); });
}); });
it('Can set bitdepth of PNG without palette', async () => {
const data = await sharp({
create: {
width: 8, height: 8, channels: 3, background: 'red'
}
})
.toColourspace('b-w')
.png({ colours: 2, palette: false })
.toBuffer();
const { channels, paletteBitDepth, size, space } = await sharp(data).metadata();
assert.strictEqual(channels, 1);
assert.strictEqual(paletteBitDepth, undefined);
assert.strictEqual(size, 90);
assert.strictEqual(space, 'b-w');
});
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([