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 @@ -16,6 +16,7 @@
- Fix `heightOfString` drawing the measured text into the page when it is called after a `text` call with `continued: true`, because it measured with the line wrapper that call had left behind, which draws its lines. A measurement that passes `continued: true` itself still uses that wrapper and draws as before. Fixes #1284
- Render SVG quadratic curves with equivalent cubic control points, so reversing a path preserves its shape. Fixes #550
- Fix the visual test renderer rejecting its standard-font directory on Windows because its path ends in a backslash.
- Fix PNG images with a palette of fewer than 8 bits per index and transparency in a `tRNS` chunk coming out mostly transparent: their soft mask was built from the packed bytes of the indices, one alpha value per byte instead of per pixel, so most pixels got the alpha of a wrong index or none
- Fix the underline, strike-through, link and `goTo` rectangle of text drawn with a `baseline` other than the default sitting an ascent too low, because they were placed from the top of the line while the glyphs had moved up with the baseline. Fixes #994, #1245
- Fix links to a named destination going nowhere in readers that look the name up in sorted order, such as Chrome (PDFium) and qpdf, because name tree keys were sorted by locale with `localeCompare` instead of by their bytes: with destinations named `appendix` and `Chapter1`, `Chapter1` was written second and could not be found. The keys of the `EmbeddedFiles` and `JavaScript` name trees are sorted the same way
- Escape document information in XMP metadata and omit characters XML 1.0 cannot represent, without changing the PDF Info dictionary. Fixes #1817
Expand Down
61 changes: 61 additions & 0 deletions lib/image/png.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,17 @@
import zlib from '#zlib';
import PNG from 'png-js';

function paeth(left, up, upLeft) {
const p = left + up - upLeft;
const pLeft = Math.abs(p - left);
const pUp = Math.abs(p - up);
const pUpLeft = Math.abs(p - upLeft);
if (pLeft <= pUp && pLeft <= pUpLeft) {
return left;
}
return pUp <= pUpLeft ? up : upLeft;
}

class PNGImage {
constructor(data, label) {
this.label = label;
Expand Down Expand Up @@ -148,6 +159,15 @@ class PNGImage {
loadIndexedAlphaChannel() {
const transparency = this.image.transparency.indexed;
const isInterlaced = this.image.interlaceMethod === 1;
if (this.image.bits < 8 && !isInterlaced) {
const indices = this.decodePackedIndices();
const alphaChannel = new Uint8Array(indices.length);
for (let i = 0; i < indices.length; i++) {
alphaChannel[i] = transparency[indices[i]];
}
this.alphaChannel = zlib.deflateSync(alphaChannel);
return;
}
const pixels = this.image.decodePixelsSync();

const alphaChannel = new Uint8Array(this.width * this.height);
Expand All @@ -165,6 +185,47 @@ class PNGImage {
this.alphaChannel = zlib.deflateSync(alphaChannel);
}

// Palette indices of fewer than 8 bits are packed into bytes, which png-js
// leaves as they are, and it unfilters their rows in steps of less than a
// byte. PNG filters such rows a byte at a time, so unfilter and unpack the
// indices here.
decodePackedIndices() {
const { width, height } = this;
const { bits } = this.image;
const rowLength = Math.ceil((width * bits) / 8);
const data = zlib.inflateSync(new Uint8Array(this.imgData));
const indices = new Uint8Array(width * height);
let previous = new Uint8Array(rowLength);
let row = new Uint8Array(rowLength);
let pos = 0;
for (let y = 0; y < height; y++) {
const filter = data[pos++];
for (let x = 0; x < rowLength; x++) {
const left = x > 0 ? row[x - 1] : 0;
const up = previous[x];
const upLeft = x > 0 ? previous[x - 1] : 0;
let predictor = 0;
if (filter === 1) {
predictor = left;
} else if (filter === 2) {
predictor = up;
} else if (filter === 3) {
predictor = (left + up) >> 1;
} else if (filter === 4) {
predictor = paeth(left, up, upLeft);
}
row[x] = (data[pos++] + predictor) & 0xff;
}
for (let x = 0; x < width; x++) {
const bit = x * bits;
indices[y * width + x] =
(row[bit >> 3] >> (8 - bits - (bit & 7))) & ((1 << bits) - 1);
}
[previous, row] = [row, previous];
}
return indices;
}

decodeData() {
const pixels = this.image.decodePixelsSync();
this.imgData = zlib.deflateSync(pixels);
Expand Down
3 changes: 2 additions & 1 deletion lib/zlib/browser.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { zlibSync } from 'fflate';
import { unzlibSync, zlibSync } from 'fflate';

export default {
deflateSync: (data) => zlibSync(data),
inflateSync: (data) => unzlibSync(data),
};
1 change: 1 addition & 0 deletions lib/zlib/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ import zlib from 'zlib';

export default {
deflateSync: (data) => zlib.deflateSync(data),
inflateSync: (data) => zlib.inflateSync(data),
};
16 changes: 16 additions & 0 deletions tests/unit/png.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import PDFDocument from '../../lib/document';
import PDFReference from '../../lib/reference';
import PNGImage from '../../lib/image/png';
import fs from 'fs';
import zlib from 'zlib';

describe('PNGImage', () => {
let document;
Expand Down Expand Up @@ -305,6 +306,21 @@ describe('PNGImage', () => {
});
});

test('Pallete indexed transparency 1bit alpha channel', () => {
const img = new PNGImage(
fs.readFileSync('./tests/images/pallete-transparent-white-1bit.png'),
'I1',
);
img.embed(document);

// One alpha value per pixel; the pixels of the transparent palette entry
// are transparent, and only those
const alpha = zlib.inflateSync(img.alphaChannel);
expect(alpha.length).toBe(290 * 50);
expect(alpha.filter((value) => value === 0).length).toBe(6900);
expect(alpha.filter((value) => value === 255).length).toBe(7600);
});

test('Grayscale', () => {
// ImageWidth = 428
// ImageHeight = 320
Expand Down