// A placeholder image (architecture, "A placeholder image is a kernel library too"): a real PNG of the size asked for, in // one flat colour drawn from a seed, labeled in a tEXt chunk as the twin's, for generative packs that run no model. // Importing it does nothing. // // The image data is a zlib stream (RFC 1950) of one fixed-Huffman deflate block (RFC 1951 §3.2.6): each row is the Sub // filter (PNG §9.2, type 1), its first pixel as literals and the rest zeros, the zeros as a literal and then copies of // distance 1. A flat image of any size is a few kilobytes. import { sha256 } from './signing.ts'; const SIGNATURE = [0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a]; const CRC = Array.from({ length: 256 }, (_, n) => { let c = n; for (let k = 0; k < 8; k++) c = c & 1 ? 0xedb88320 ^ (c >>> 1) : c >>> 1; return c >>> 0; }); const crc32 = (b: Uint8Array): number => { let c = 0xffffffff; for (const x of b) c = CRC[(c ^ x) & 0xff]! ^ (c >>> 8); return (c ^ 0xffffffff) >>> 0; }; function chunk(type: string, data: Uint8Array): Uint8Array { const out = new Uint8Array(12 + data.length); const v = new DataView(out.buffer); v.setUint32(0, data.length); out.set([...type].map((c) => c.charCodeAt(0)), 4); out.set(data, 8); v.setUint32(8 + data.length, crc32(out.subarray(4, 8 + data.length))); return out; } /** Deflate's bits, least significant first; a Huffman code is written most significant bit first. */ class Bits { private out: number[] = []; private acc = 0; private n = 0; put(value: number, count: number): void { for (let i = 0; i < count; i++) { this.acc |= ((value >> i) & 1) << this.n; if (++this.n === 8) { this.out.push(this.acc); this.acc = 0; this.n = 0; } } } code(code: number, length: number): void { for (let i = length - 1; i >= 0; i--) this.put((code >> i) & 1, 1); } bytes(): number[] { return this.n ? [...this.out, this.acc] : this.out; } } /** A literal byte in the fixed code: 0–143 are 8 bits from 0x30, 144–255 are 9 bits from 0x190. */ const literal = (w: Bits, b: number): void => (b < 144 ? w.code(0x30 + b, 8) : w.code(0x190 + b - 144, 9)); /** A copy of `length` (3–258) bytes at distance 1: length codes 257–285 (7 bits from 0 for 256–279, 8 bits from 0xc0 for * 280–287) with their extra bits, then distance code 0 (5 bits, distance 1). */ const LENGTH_BASE = [3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31, 35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258]; const LENGTH_EXTRA = [0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 0]; function copy(w: Bits, length: number): void { let i = LENGTH_BASE.length - 1; while (LENGTH_BASE[i]! > length) i--; const symbol = 257 + i; if (symbol < 280) w.code(symbol - 256, 7); else w.code(0xc0 + symbol - 280, 8); w.put(length - LENGTH_BASE[i]!, LENGTH_EXTRA[i]!); w.code(0, 5); } function flatIdat(width: number, height: number, rgb: [number, number, number]): Uint8Array { const w = new Bits(); w.put(1, 1); w.put(1, 2); // the last block, fixed Huffman const zeros = (width - 1) * 3; for (let y = 0; y < height; y++) { literal(w, 1); for (const c of rgb) literal(w, c); if (zeros > 0) { literal(w, 0); let left = zeros - 1; while (left > 0) { const n = left > 258 ? (left - 258 < 3 ? left - 3 : 258) : left; if (n < 3) { for (let k = 0; k < n; k++) literal(w, 0); left -= n; continue; } copy(w, n); left -= n; } } } w.code(0, 7); // end of block (256) // Adler-32 of the raw rows: a zero byte leaves a as it is and adds a to b let a = 1; let b = 0; for (let y = 0; y < height; y++) { for (const byte of [1, ...rgb]) { a = (a + byte) % 65521; b = (b + a) % 65521; } b = (b + a * zeros) % 65521; } return Uint8Array.from([0x78, 0x01, ...w.bytes(), (b >> 8) & 0xff, b & 0xff, (a >> 8) & 0xff, a & 0xff]); } /** A real PNG of `size`, one flat colour drawn from `seed`, its tEXt Comment `[twin-stub]