Skip to content

Commit 2deced0

Browse files
gforgelovell
authored andcommitted
Ensure 16-bit PNG output uses correct bitdepth #2958
1 parent 2a0077c commit 2deced0

2 files changed

Lines changed: 14 additions & 2 deletions

File tree

src/pipeline.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -824,7 +824,7 @@ class PipelineWorker : public Napi::AsyncWorker {
824824
->set("filter", baton->pngAdaptiveFiltering ? VIPS_FOREIGN_PNG_FILTER_ALL : VIPS_FOREIGN_PNG_FILTER_NONE)
825825
->set("palette", baton->pngPalette)
826826
->set("Q", baton->pngQuality)
827-
->set("bitdepth", baton->pngBitdepth)
827+
->set("bitdepth", sharp::Is16Bit(image.interpretation()) ? 16 : baton->pngBitdepth)
828828
->set("dither", baton->pngDither)));
829829
baton->bufferOut = static_cast<char*>(area->data);
830830
baton->bufferOutLength = area->length;
@@ -993,7 +993,7 @@ class PipelineWorker : public Napi::AsyncWorker {
993993
->set("filter", baton->pngAdaptiveFiltering ? VIPS_FOREIGN_PNG_FILTER_ALL : VIPS_FOREIGN_PNG_FILTER_NONE)
994994
->set("palette", baton->pngPalette)
995995
->set("Q", baton->pngQuality)
996-
->set("bitdepth", baton->pngBitdepth)
996+
->set("bitdepth", sharp::Is16Bit(image.interpretation()) ? 16 : baton->pngBitdepth)
997997
->set("dither", baton->pngDither));
998998
baton->formatOut = "png";
999999
} else if (baton->formatOut == "webp" || (mightMatchInput && isWebp) ||

test/unit/png.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,18 @@ describe('PNG', function () {
112112
});
113113
});
114114

115+
it('16-bit grey+alpha PNG roundtrip', async () => {
116+
const after = await sharp(fixtures.inputPng16BitGreyAlpha)
117+
.toColourspace('grey16')
118+
.toBuffer();
119+
120+
const [statsBefore, statsAfter] = await Promise.all([
121+
sharp(fixtures.inputPng16BitGreyAlpha).stats(),
122+
sharp(after).stats()
123+
]);
124+
assert.deepStrictEqual(statsAfter.channels[1], statsBefore.channels[1]);
125+
});
126+
115127
it('Valid PNG libimagequant palette value does not throw error', function () {
116128
assert.doesNotThrow(function () {
117129
sharp().png({ palette: false });

0 commit comments

Comments
 (0)