diff --git a/image/parsers/png.js b/image/parsers/png.js index 615cc49..36883b7 100644 --- a/image/parsers/png.js +++ b/image/parsers/png.js @@ -14,6 +14,7 @@ import { ByteStream } from '../../io/bytestream.js'; // https://en.wikipedia.org/wiki/PNG#File_format // https://www.w3.org/TR/2003/REC-PNG-20031110 +let DEBUG = false; const SIG = new Uint8Array([0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A]); /** @enum {string} */ @@ -220,8 +221,9 @@ export class PngParser extends EventTarget { }); } + /** @type {PngPalette} */ const palette = { - paletteEntries, + entries: paletteEntries, }; this.dispatchEvent(new PngPaletteEvent(palette)); @@ -240,7 +242,7 @@ export class PngParser extends EventTarget { break; default: - console.log(`Found an unhandled chunk: ${chunk.chunkType}`); + if (DEBUG) console.log(`Found an unhandled chunk: ${chunk.chunkType}`); break; } } while (chunk.chunkType !== 'IEND'); @@ -295,4 +297,4 @@ async function main() { } } -main(); \ No newline at end of file +// main(); \ No newline at end of file diff --git a/tests/image-parsers-png.spec.js b/tests/image-parsers-png.spec.js index b4ae474..68714c4 100644 --- a/tests/image-parsers-png.spec.js +++ b/tests/image-parsers-png.spec.js @@ -2,10 +2,10 @@ import * as fs from 'node:fs'; import 'mocha'; import { expect } from 'chai'; import { PngColorType, PngInterlaceMethod, PngParser } from '../image/parsers/png.js'; -import { fail } from 'node:assert'; /** @typedef {import('../image/parsers/png.js').PngImageHeader} PngImageHeader */ /** @typedef {import('../image/parsers/png.js').PngImageData} PngImageData */ +/** @typedef {import('../image/parsers/png.js').PngPalette} PngPalette */ function getPngParser(fileName) { const nodeBuf = fs.readFileSync(fileName); @@ -47,6 +47,19 @@ describe('bitjs.image.parsers.PngParser', () => { }); }); + it('extracts PLTE', async () => { + /** @type {PngPalette} */ + let palette; + await getPngParser('tests/image-testfiles/tbbn3p08.png') + .onPalette(evt => palette = evt.palette) + .start(); + expect(palette.entries.length).equals(246); + const entry = palette.entries[1]; + expect(entry.red).equals(128); + expect(entry.green).equals(86); + expect(entry.blue).equals(86); + }); + it('extracts IDAT', async () => { /** @type {PngImageData} */ let data; diff --git a/tests/image-testfiles/tbbn3p08.png b/tests/image-testfiles/tbbn3p08.png new file mode 100644 index 0000000..0ede357 Binary files /dev/null and b/tests/image-testfiles/tbbn3p08.png differ