25 lines
1.3 KiB
TypeScript
25 lines
1.3 KiB
TypeScript
// Tiny PNG / JPEG headers for the image checks (imageInfo reads only the header). Shared by the tests.
|
|
|
|
/** PNG header imageInfo reads: signature + IHDR (+ optional tRNS) + IEND, padded to `pad` bytes. */
|
|
export function png(w: number, h: number, o: { colorType?: number; depth?: number; trns?: boolean; pad?: number } = {}): Uint8Array {
|
|
const chunks = 8 + 25 + (o.trns ? 14 : 0) + 12;
|
|
const b = new Uint8Array(Math.max(chunks, o.pad ?? 0));
|
|
b.set([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a]);
|
|
const dv = new DataView(b.buffer);
|
|
dv.setUint32(8, 13); b.set([0x49, 0x48, 0x44, 0x52], 12); dv.setUint32(16, w); dv.setUint32(20, h);
|
|
b[24] = o.depth ?? 8; b[25] = o.colorType ?? 2;
|
|
let p = 33;
|
|
if (o.trns) { dv.setUint32(p, 2); b.set([0x74, 0x52, 0x4e, 0x53], p + 4); p += 14; }
|
|
dv.setUint32(p, 0); b.set([0x49, 0x45, 0x4e, 0x44], p + 4);
|
|
return b;
|
|
}
|
|
|
|
/** JPEG with an APP0 segment and a baseline SOF0 frame header. */
|
|
export function jpeg(w: number, h: number, pad = 0): Uint8Array {
|
|
const head = [0xff, 0xd8, 0xff, 0xe0, 0, 16, 0x4a, 0x46, 0x49, 0x46, 0, 1, 1, 0, 0, 1, 0, 1, 0, 0,
|
|
0xff, 0xc0, 0, 17, 8, h >> 8, h & 255, w >> 8, w & 255, 3, 1, 0x22, 0, 2, 0x11, 1, 3, 0x11, 1, 0xff, 0xd9];
|
|
const b = new Uint8Array(Math.max(head.length, pad));
|
|
b.set(head);
|
|
return b;
|
|
}
|
|
|