diff --git a/CHANGELOG.md b/CHANGELOG.md index 4eae7517d..78318f437 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ ### Unreleased +- Fix the color key mask of greyscale and RGB PNGs with a `tRNS` chunk. The key is now read as one 2-byte sample per color component, so 8-bit greyscale keys other than 0 and 16-bit keys are made transparent and RGB masks have one pair per component. Fixes #1864 - Fix gradients whose last stop comes before 1 stretching the stops over the whole gradient. The last color now stays solid from that stop to the end - Add a `headerRows` table option, which repeats the first rows at the top of each new page. Fixes #1657 - Add a `scaleDown` option to `doc.image()`, which works like `fit` but keeps an image smaller than the rectangle at its own size instead of enlarging it. Fixes #1347 diff --git a/lib/image/png.js b/lib/image/png.js index f3b405c38..43770418b 100644 --- a/lib/image/png.js +++ b/lib/image/png.js @@ -63,18 +63,12 @@ class PNGImage { // For PNG color types 0, 2 and 3, the transparency data is stored in // a dedicated PNG chunk. - if (image.transparency.grayscale != null) { + if (image.transparency.grayscale != null || image.transparency.rgb) { // Use Color Key Masking (spec section 4.8.5) // An array with N elements, where N is two times the number of color components. - const val = image.transparency.grayscale; - obj.data['Mask'] = [val, val]; - } else if (image.transparency.rgb) { - // Use Color Key Masking (spec section 4.8.5) - // An array with N elements, where N is two times the number of color components. - const { rgb } = image.transparency; const mask = []; - for (let x of rgb) { - mask.push(x, x); + for (const sample of this.colorKey()) { + mask.push(sample, sample); } obj.data['Mask'] = mask; @@ -175,6 +169,32 @@ class PNGImage { this.finalize(); }); } + + // For greyscale and truecolour images, the tRNS chunk holds one 2-byte + // sample per color component. png-js keeps only the first byte of the + // greyscale sample, so read the samples from the chunk itself. + colorKey() { + const { data } = this.image; + let pos = 8; + while (pos + 8 <= data.length) { + const length = + ((data[pos] << 24) | + (data[pos + 1] << 16) | + (data[pos + 2] << 8) | + data[pos + 3]) >>> + 0; + const type = String.fromCharCode(...data.slice(pos + 4, pos + 8)); + if (type === 'tRNS') { + const samples = []; + for (let i = 0; i + 1 < length; i += 2) { + samples.push((data[pos + 8 + i] << 8) | data[pos + 9 + i]); + } + return samples; + } + pos += length + 12; + } + return []; + } } export default PNGImage; diff --git a/tests/unit/png.spec.js b/tests/unit/png.spec.js index d9218fa46..883a4f2b9 100644 --- a/tests/unit/png.spec.js +++ b/tests/unit/png.spec.js @@ -92,7 +92,7 @@ describe('PNGImage', () => { Subtype: 'Image', Type: 'XObject', Width: 32, - Mask: [255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255], + Mask: [65535, 65535, 65535, 65535, 65535, 65535], DecodeParms: expect.any(PDFReference), }); @@ -359,7 +359,7 @@ describe('PNGImage', () => { Subtype: 'Image', Type: 'XObject', Width: 32, - Mask: [0, 0], + Mask: [15, 15], DecodeParms: expect.any(PDFReference), }); @@ -395,7 +395,7 @@ describe('PNGImage', () => { Subtype: 'Image', Type: 'XObject', Width: 32, - Mask: [255, 255], + Mask: [65535, 65535], DecodeParms: expect.any(PDFReference), });