1
0
Fork 0
mirror of https://github.com/codedread/bitjs synced 2025-10-03 09:39:16 +02:00

PngParser: Add unit test for PLTE chunk

This commit is contained in:
Jeff Schiller 2024-01-16 08:00:35 -08:00
parent 193cafa5e7
commit b359f92251
3 changed files with 19 additions and 4 deletions

View file

@ -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();
// main();

View file

@ -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;

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.5 KiB