/** * Remap opaque pixels in a PNG into the magenta tint family (R≈B, G */ 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)) ]); } function toMagenta (r, g, b, a) { if (a < 8) { return [0, 0, 0, 0]; } const L = Math.max(1, Math.max(r, g, b)); const minC = Math.min(r, g, b); const chroma = (L - minC) / 255; const gMag = Math.min(L - 1, Math.round(L * (0.08 + (1 - chroma) * 0.22))); return [L, Math.max(0, gMag), L, 255]; } function magentaize ({ width, height, rgba }) { const out = new Uint8Array(rgba.length); for (let i = 0; i < rgba.length; i += 4) { const [r, g, b, a] = toMagenta(rgba[i], rgba[i + 1], rgba[i + 2], rgba[i + 3]); out[i] = r; out[i + 1] = g; out[i + 2] = b; out[i + 3] = a; } return { width, height, rgba: out }; } const [inputPath, outputPath] = process.argv.slice(2); if (!inputPath || !outputPath) { console.error("Usage: node magentaize-png.mjs "); process.exit(1); } const image = magentaize(decodePngRgba(readFileSync(inputPath))); mkdirSync(dirname(outputPath), { recursive: true }); writeFileSync(outputPath, encodePng(image)); console.log(`Wrote ${outputPath} (${image.width}×${image.height})`);