mirror of
https://github.com/codedread/bitjs
synced 2025-10-03 17:49:16 +02:00
PngParser: Add unit test for PLTE chunk
This commit is contained in:
parent
193cafa5e7
commit
b359f92251
3 changed files with 19 additions and 4 deletions
|
@ -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();
|
|
@ -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;
|
||||
|
|
BIN
tests/image-testfiles/tbbn3p08.png
Normal file
BIN
tests/image-testfiles/tbbn3p08.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 1.5 KiB |
Loading…
Add table
Add a link
Reference in a new issue