/* ===================================================================== EIN PNG SCHREIBEN (03.10.2026) Das Gegenstueck zu helfer-png.mjs, das eines LIEST. Gebraucht fuer die Eventkarten-Pruefung: Ob der Titel auf einer Buehne lesbar bleibt, haengt davon ab, was fuer ein Banner darunter liegt -- und der interessante Fall ist nicht das dunkle, sondern das HELLE, das jemand spaeter hochlaedt. Ein solches Bild muss die Pruefung also selbst herstellen koennen. WARUM SELBST GESCHRIEBEN: Dieselbe Begruendung wie bei helfer-png.mjs -- das Projekt hat kein Bildmodul, und fuer ein Testbild eines nachzuinstallieren waere eine Abhaengigkeit, die jemand pflegen muss. Node bringt zlib mit; ein unkomprimiertes RGB-PNG ist danach nur noch Pruefsumme und Zeilenfilter. ===================================================================== */ import { deflateSync } from "node:zlib"; const tabelle = (() => { const t = new Int32Array(256); for (let n = 0; n < 256; n++) { let c = n; for (let k = 0; k < 8; k++) c = (c & 1) ? (0xedb88320 ^ (c >>> 1)) : (c >>> 1); t[n] = c; } return t; })(); function crc32(buf) { let c = 0xffffffff; for (let i = 0; i < buf.length; i++) c = tabelle[(c ^ buf[i]) & 0xff] ^ (c >>> 8); return (c ^ 0xffffffff) >>> 0; } function teil(name, inhalt) { const kopf = Buffer.alloc(8); kopf.writeUInt32BE(inhalt.length, 0); kopf.write(name, 4, "ascii"); const pruef = Buffer.alloc(4); pruef.writeUInt32BE(crc32(Buffer.concat([Buffer.from(name, "ascii"), inhalt])), 0); return Buffer.concat([kopf, inhalt, pruef]); } /** male(x, y, b, h) -> [r,g,b] */ export function pngBauen(b, h, male) { const roh = Buffer.alloc((b * 3 + 1) * h); let p = 0; for (let y = 0; y < h; y++) { roh[p++] = 0; for (let x = 0; x < b; x++) { const f = male(x, y, b, h); roh[p++] = f[0]; roh[p++] = f[1]; roh[p++] = f[2]; } } const ihdr = Buffer.alloc(13); ihdr.writeUInt32BE(b, 0); ihdr.writeUInt32BE(h, 4); ihdr[8] = 8; ihdr[9] = 2; ihdr[10] = 0; ihdr[11] = 0; ihdr[12] = 0; return Buffer.concat([ Buffer.from([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a]), teil("IHDR", ihdr), teil("IDAT", deflateSync(roh, { level: 9 })), teil("IEND", Buffer.alloc(0)), ]); }