diff --git a/docs/changelog.md b/docs/changelog.md index 174937ca..cf448cae 100644 --- a/docs/changelog.md +++ b/docs/changelog.md @@ -4,6 +4,11 @@ Requires libvips v8.11.3 +### v0.29.1 - TBD + +* Ensure correct PNG bitdepth is set based on number of colours. + [#2855](https://github.com/lovell/sharp/issues/2855) + ### v0.29.0 - 17th August 2021 * Drop support for Node.js 10, now requires Node.js >= 12.13.0. diff --git a/lib/constructor.js b/lib/constructor.js index 02872bd0..c76f365f 100644 --- a/lib/constructor.js +++ b/lib/constructor.js @@ -232,7 +232,7 @@ const Sharp = function (input, options) { pngAdaptiveFiltering: false, pngPalette: false, pngQuality: 100, - pngColours: 256, + pngBitdepth: 8, pngDither: 1, webpQuality: 80, webpAlphaQuality: 100, diff --git a/lib/output.js b/lib/output.js index 6ec1b125..e0b2610c 100644 --- a/lib/output.js +++ b/lib/output.js @@ -405,7 +405,7 @@ function png (options) { const colours = options.colours || options.colors; if (is.defined(colours)) { if (is.integer(colours) && is.inRange(colours, 2, 256)) { - this.options.pngColours = colours; + this.options.pngBitdepth = 1 << 31 - Math.clz32(Math.ceil(Math.log2(colours))); } else { throw is.invalidParameterError('colours', 'integer between 2 and 256', colours); } diff --git a/src/pipeline.cc b/src/pipeline.cc index 35eff31b..e11de97f 100644 --- a/src/pipeline.cc +++ b/src/pipeline.cc @@ -799,7 +799,7 @@ class PipelineWorker : public Napi::AsyncWorker { ->set("filter", baton->pngAdaptiveFiltering ? VIPS_FOREIGN_PNG_FILTER_ALL : VIPS_FOREIGN_PNG_FILTER_NONE) ->set("palette", baton->pngPalette) ->set("Q", baton->pngQuality) - ->set("colours", baton->pngColours) + ->set("bitdepth", baton->pngBitdepth) ->set("dither", baton->pngDither))); baton->bufferOut = static_cast(area->data); baton->bufferOutLength = area->length; @@ -955,7 +955,7 @@ class PipelineWorker : public Napi::AsyncWorker { ->set("filter", baton->pngAdaptiveFiltering ? VIPS_FOREIGN_PNG_FILTER_ALL : VIPS_FOREIGN_PNG_FILTER_NONE) ->set("palette", baton->pngPalette) ->set("Q", baton->pngQuality) - ->set("colours", baton->pngColours) + ->set("bitdepth", baton->pngBitdepth) ->set("dither", baton->pngDither)); baton->formatOut = "png"; } else if (baton->formatOut == "webp" || (mightMatchInput && isWebp) || @@ -1429,7 +1429,7 @@ Napi::Value pipeline(const Napi::CallbackInfo& info) { baton->pngAdaptiveFiltering = sharp::AttrAsBool(options, "pngAdaptiveFiltering"); baton->pngPalette = sharp::AttrAsBool(options, "pngPalette"); baton->pngQuality = sharp::AttrAsUint32(options, "pngQuality"); - baton->pngColours = sharp::AttrAsUint32(options, "pngColours"); + baton->pngBitdepth = sharp::AttrAsUint32(options, "pngBitdepth"); baton->pngDither = sharp::AttrAsDouble(options, "pngDither"); baton->webpQuality = sharp::AttrAsUint32(options, "webpQuality"); baton->webpAlphaQuality = sharp::AttrAsUint32(options, "webpAlphaQuality"); diff --git a/src/pipeline.h b/src/pipeline.h index d934ded2..3beb9713 100644 --- a/src/pipeline.h +++ b/src/pipeline.h @@ -146,7 +146,7 @@ struct PipelineBaton { bool pngAdaptiveFiltering; bool pngPalette; int pngQuality; - int pngColours; + int pngBitdepth; double pngDither; int webpQuality; int webpAlphaQuality; @@ -276,7 +276,7 @@ struct PipelineBaton { pngAdaptiveFiltering(false), pngPalette(false), pngQuality(100), - pngColours(256), + pngBitdepth(8), pngDither(1.0), webpQuality(80), webpAlphaQuality(100),