Skip to content

Commit 070f275

Browse files
committed
Add transparent (palette) 1 bit png for testing
1 parent cbf57a4 commit 070f275

File tree

4 files changed

+69
-1
lines changed

4 files changed

+69
-1
lines changed

examples/png.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
const PDFDocument = require('..');
2+
const fs = require('fs');
3+
4+
const doc = new PDFDocument();
5+
6+
doc.pipe(fs.createWriteStream('png.pdf'));
7+
8+
// Set the background color to highlight the transparency
9+
doc.fillColor('lightblue');
10+
doc.rect(0, 0, doc.page.width, doc.page.height).fill();
11+
12+
doc.fillColor('black');
13+
14+
doc.text('PNG with transparency (palette 8bit):', 60, 50);
15+
doc.image('../tests/images/pngsuite-palette-transparent-white.png', 60, 64);
16+
17+
doc.text('PNG with transparency (palette 1bit):', 60, 100);
18+
doc.image('../tests/images/pallete-transparent-white-1bit.png', 60, 114);
19+
20+
doc.end();

examples/png.pdf

4.17 KB
Binary file not shown.
173 Bytes
Loading

tests/unit/png.spec.js

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ describe('PNGImage', () => {
212212
});
213213
});
214214

215-
test('Pallete indexed transparency', () => {
215+
test('Pallete indexed transparency 8bit', () => {
216216
// ImageWidth = 32
217217
// ImageHeight = 32
218218
// BitDepth = 8
@@ -260,6 +260,54 @@ describe('PNGImage', () => {
260260
});
261261
});
262262

263+
test('Pallete indexed transparency 1bit', () => {
264+
// ImageWidth = 290
265+
// ImageHeight = 50
266+
// BitDepth = 1
267+
// ColorType = 3
268+
// Compression = 0
269+
// Filter = 0
270+
// Interlace = 0
271+
272+
const img = createImage(
273+
'./tests/images/pallete-transparent-white-1bit.png',
274+
);
275+
276+
expect(img.finalize).toBeCalledTimes(1);
277+
278+
expect(img.obj.data).toMatchObject({
279+
BitsPerComponent: 1,
280+
ColorSpace: ['Indexed', 'DeviceRGB', 1, expect.any(PDFReference)],
281+
Filter: 'FlateDecode',
282+
Height: 50,
283+
Length: 64,
284+
Subtype: 'Image',
285+
Type: 'XObject',
286+
Width: 290,
287+
DecodeParms: expect.any(PDFReference),
288+
SMask: expect.any(PDFReference),
289+
});
290+
291+
expect(img.obj.data.DecodeParms.data).toMatchObject({
292+
BitsPerComponent: 1,
293+
Colors: 1,
294+
Columns: 290,
295+
Predictor: 15,
296+
});
297+
298+
expect(img.obj.data.SMask.data).toMatchObject({
299+
BitsPerComponent: 8, // ????
300+
ColorSpace: 'DeviceGray',
301+
Decode: [0, 1],
302+
Filter: 'FlateDecode',
303+
Height: 50,
304+
Length: 16,
305+
Subtype: 'Image',
306+
Type: 'XObject',
307+
Width: 290,
308+
});
309+
});
310+
263311
test('Grayscale', () => {
264312
// ImageWidth = 428
265313
// ImageHeight = 320

0 commit comments

Comments
 (0)