Skip to content

Commit b359f92

Browse files
committed
PngParser: Add unit test for PLTE chunk
1 parent 193cafa commit b359f92

File tree

3 files changed

+19
-4
lines changed

3 files changed

+19
-4
lines changed

image/parsers/png.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import { ByteStream } from '../../io/bytestream.js';
1414
// https://en.wikipedia.org/wiki/PNG#File_format
1515
// https://www.w3.org/TR/2003/REC-PNG-20031110
1616

17+
let DEBUG = false;
1718
const SIG = new Uint8Array([0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A]);
1819

1920
/** @enum {string} */
@@ -220,8 +221,9 @@ export class PngParser extends EventTarget {
220221
});
221222
}
222223

224+
/** @type {PngPalette} */
223225
const palette = {
224-
paletteEntries,
226+
entries: paletteEntries,
225227
};
226228

227229
this.dispatchEvent(new PngPaletteEvent(palette));
@@ -240,7 +242,7 @@ export class PngParser extends EventTarget {
240242
break;
241243

242244
default:
243-
console.log(`Found an unhandled chunk: ${chunk.chunkType}`);
245+
if (DEBUG) console.log(`Found an unhandled chunk: ${chunk.chunkType}`);
244246
break;
245247
}
246248
} while (chunk.chunkType !== 'IEND');
@@ -295,4 +297,4 @@ async function main() {
295297
}
296298
}
297299

298-
main();
300+
// main();

tests/image-parsers-png.spec.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@ import * as fs from 'node:fs';
22
import 'mocha';
33
import { expect } from 'chai';
44
import { PngColorType, PngInterlaceMethod, PngParser } from '../image/parsers/png.js';
5-
import { fail } from 'node:assert';
65

76
/** @typedef {import('../image/parsers/png.js').PngImageHeader} PngImageHeader */
87
/** @typedef {import('../image/parsers/png.js').PngImageData} PngImageData */
8+
/** @typedef {import('../image/parsers/png.js').PngPalette} PngPalette */
99

1010
function getPngParser(fileName) {
1111
const nodeBuf = fs.readFileSync(fileName);
@@ -47,6 +47,19 @@ describe('bitjs.image.parsers.PngParser', () => {
4747
});
4848
});
4949

50+
it('extracts PLTE', async () => {
51+
/** @type {PngPalette} */
52+
let palette;
53+
await getPngParser('tests/image-testfiles/tbbn3p08.png')
54+
.onPalette(evt => palette = evt.palette)
55+
.start();
56+
expect(palette.entries.length).equals(246);
57+
const entry = palette.entries[1];
58+
expect(entry.red).equals(128);
59+
expect(entry.green).equals(86);
60+
expect(entry.blue).equals(86);
61+
});
62+
5063
it('extracts IDAT', async () => {
5164
/** @type {PngImageData} */
5265
let data;

tests/image-testfiles/tbbn3p08.png

1.46 KB
Loading

0 commit comments

Comments
 (0)