-
Notifications
You must be signed in to change notification settings - Fork 19
/
decode.test.ts
103 lines (92 loc) · 2.74 KB
/
decode.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import assert from 'assert';
import { readFileSync } from 'fs';
import { join } from 'path';
import { decode, PngDecoderOptions, DecodedPng } from '../index';
describe('decode', () => {
it('BW2x2', () => {
const img = loadAndDecode('BW2x2.png');
check(img, {
width: 2,
height: 2,
depth: 8,
channels: 2,
});
expect(img.data).toBeInstanceOf(Uint8Array);
expect(img.data).toStrictEqual(
new Uint8Array([0, 255, 255, 255, 255, 255, 0, 255]),
);
});
it('interlaced', () => {
expect(() => loadAndDecode('interlaced.png')).toThrow(
'Interlace method 1 not supported',
);
});
it('ColorGrid5x5', () => {
const img = loadAndDecode('ColorGrid5x5.png');
check(img, {
width: 10,
height: 10,
depth: 8,
channels: 4,
});
expect(img.data).toBeInstanceOf(Uint8Array);
expect(img.data).toHaveLength(10 * 10 * 4);
});
it('palette', () => {
const img = loadAndDecode('palette.png');
check(img, {
width: 150,
height: 200,
depth: 8,
channels: 1,
});
expect(img.palette).toBeInstanceOf(Array);
expect(img.palette).toHaveLength(256);
// @ts-expect-error Palette should not be undefined
expect(img.palette[0]).toStrictEqual([124, 124, 124]);
});
it('palette with tRNS', () => {
const img = loadAndDecode('palette_trns.png');
check(img, {
width: 1300,
height: 1300,
depth: 8,
channels: 1,
});
expect(img.palette).toBeInstanceOf(Array);
expect(img.palette).toHaveLength(256);
// @ts-expect-error Palette should not be undefined
expect(img.palette[0]).toStrictEqual([71, 112, 76, 0]);
// @ts-expect-error Palette should not be undefined
expect(img.palette[255]).toStrictEqual([98, 185, 201, 255]);
});
it('should not throw when CRC is correct', () => {
loadAndDecode('palette.png', { checkCrc: true });
});
it('should throw with a non-png', () => {
expect(() => decode(new Uint8Array(20))).toThrow(
'wrong PNG signature. Byte at 0 should be 137.',
);
});
it('ICC Embeded Profile', () => {
const img = loadAndDecode('icc_profile.png');
check(img, {
width: 512,
height: 512,
depth: 8,
channels: 3,
});
assert(img.iccEmbeddedProfile);
expect(img.iccEmbeddedProfile.name).toBe('ICC profile');
expect(img.iccEmbeddedProfile.profile).toHaveLength(672);
});
});
function loadAndDecode(img: string, options?: PngDecoderOptions): DecodedPng {
return decode(readFileSync(join(__dirname, '../../img', img)), options);
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
function check(img: any, values: any): void {
for (const prop in values) {
expect(img[prop]).toBe(values[prop]);
}
}