Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
38 changes: 29 additions & 9 deletions lib/image/png.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -175,6 +169,32 @@ class PNGImage {
this.finalize();
});
}

// For greyscale and truecolour images, the tRNS chunk holds one 2-byte

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please create a PR in https://github.com/foliojs/png.js to fix the transparency property

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Try to use an existing png file instead of a synthetic one

// 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;
6 changes: 3 additions & 3 deletions tests/unit/png.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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),
});

Expand Down Expand Up @@ -359,7 +359,7 @@ describe('PNGImage', () => {
Subtype: 'Image',
Type: 'XObject',
Width: 32,
Mask: [0, 0],
Mask: [15, 15],
DecodeParms: expect.any(PDFReference),
});

Expand Down Expand Up @@ -395,7 +395,7 @@ describe('PNGImage', () => {
Subtype: 'Image',
Type: 'XObject',
Width: 32,
Mask: [255, 255],
Mask: [65535, 65535],
DecodeParms: expect.any(PDFReference),
});

Expand Down