/** * Normalize already-dual-channel tint masks: red-dominant → (R,0,0), blue-dominant → (0,0,B). * For first-time conversion from magenta/brown art, use convert-dual-channel-png.mjs instead. * * Usage: node client/scripts/purify-dual-channel-png.mjs [output.png] * When output is omitted, overwrites input. */ import { readFileSync, mkdirSync, writeFileSync } from "node:fs"; import { dirname } from "node:path"; import { deflateSync, inflateSync } from "node:zlib"; function crcTable () { const table = new Uint32Array(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); } table[n] = c >>> 0; } return table; } const CRC_TABLE = crcTable(); function crc32 (buf) { let c = 0xffffffff; for (let i = 0; i < buf.length; i++) { c = CRC_TABLE[(c ^ buf[i]) & 0xff] ^ (c >>> 8); } return (c ^ 0xffffffff) >>> 0; } function chunk (type, data) { const len = Buffer.alloc(4); len.writeUInt32BE(data.length); const typeBuf = Buffer.from(type, "ascii"); const crcBuf = Buffer.alloc(4); crcBuf.writeUInt32BE(crc32(Buffer.concat([typeBuf, data]))); return Buffer.concat([len, typeBuf, data, crcBuf]); } function decodePngRgba (buf) { let off = 8; let w = 0; let h = 0; const idat = []; while (off < buf.length) { const len = buf.readUInt32BE(off); const type = buf.slice(off + 4, off + 8).toString("ascii"); const data = buf.slice(off + 8, off + 8 + len); if (type === "IHDR") { w = data.readUInt32BE(0); h = data.readUInt32BE(4); } else if (type === "IDAT") { idat.push(data); } else if (type === "IEND") { break; } off += 12 + len; } const raw = inflateSync(Buffer.concat(idat)); const stride = 1 + w * 4; const rgba = new Uint8Array(w * h * 4); for (let y = 0; y < h; y++) { for (let x = 0; x < w; x++) { const i = y * stride + 1 + x * 4; const o = (y * w + x) * 4; rgba[o] = raw[i]; rgba[o + 1] = raw[i + 1]; rgba[o + 2] = raw[i + 2]; rgba[o + 3] = raw[i + 3]; } } return { width: w, height: h, rgba }; } function encodePng ({ width, height, rgba }) { const raw = Buffer.alloc((width * 4 + 1) * height); for (let y = 0; y < height; y++) { const row = y * (width * 4 + 1); raw[row] = 0; for (let x = 0; x < width; x++) { const i = (y * width + x) * 4; const o = row + 1 + x * 4; raw[o] = rgba[i]; raw[o + 1] = rgba[i + 1]; raw[o + 2] = rgba[i + 2]; raw[o + 3] = rgba[i + 3]; } } const ihdr = Buffer.alloc(13); ihdr.writeUInt32BE(width, 0); ihdr.writeUInt32BE(height, 4); ihdr[8] = 8; ihdr[9] = 6; return Buffer.concat([ Buffer.from([137, 80, 78, 71, 13, 10, 26, 10]), chunk("IHDR", ihdr), chunk("IDAT", deflateSync(raw, { level: 9 })), chunk("IEND", Buffer.alloc(0)) ]); } /** @returns {{ redFixed: number, blueFixed: number }} */ function purifyDualChannel ({ width, height, rgba }) { let redFixed = 0; let blueFixed = 0; for (let i = 0; i < rgba.length; i += 4) { const a = rgba[i + 3]; if (a < 8) { continue; } const r = rgba[i]; const g = rgba[i + 1]; const b = rgba[i + 2]; if (r > g && r > b && r >= 8) { if (g !== 0 || b !== 0) { rgba[i] = r; rgba[i + 1] = 0; rgba[i + 2] = 0; redFixed++; } } else if (b > r && b > g && b >= 8) { if (r !== 0 || g !== 0) { rgba[i] = 0; rgba[i + 1] = 0; rgba[i + 2] = b; blueFixed++; } } } return { redFixed, blueFixed }; } const [inputPath, outputPathArg] = process.argv.slice(2); if (!inputPath) { console.error("Usage: node purify-dual-channel-png.mjs [output.png]"); process.exit(1); } const outputPath = outputPathArg ?? inputPath; const image = decodePngRgba(readFileSync(inputPath)); const stats = purifyDualChannel(image); mkdirSync(dirname(outputPath), { recursive: true }); writeFileSync(outputPath, encodePng(image)); console.log(`Wrote ${outputPath} (${image.width}×${image.height}) — fixed ${stats.redFixed} red, ${stats.blueFixed} blue pixels`);