| 1 | /** |
| 2 | * Remap opaque pixels in a PNG into the magenta tint family (R≈B, G<R). |
| 3 | * Usage: node client/scripts/magentaize-png.mjs <input.png> <output.png> |
| 4 | */ |
| 5 | import { readFileSync, mkdirSync, writeFileSync } from "node:fs"; |
| 6 | import { dirname } from "node:path"; |
| 7 | import { deflateSync, inflateSync } from "node:zlib"; |
| 8 | |
| 9 | function crcTable () |
| 10 | { |
| 11 | const table = new Uint32Array(256); |
| 12 | for (let n = 0; n < 256; n++) |
| 13 | { |
| 14 | let c = n; |
| 15 | for (let k = 0; k < 8; k++) |
| 16 | { c = (c & 1) ? (0xedb88320 ^ (c >>> 1)) : (c >>> 1); } |
| 17 | table[n] = c >>> 0; |
| 18 | } |
| 19 | return table; |
| 20 | } |
| 21 | |
| 22 | const CRC_TABLE = crcTable(); |
| 23 | |
| 24 | function crc32 (buf) |
| 25 | { |
| 26 | let c = 0xffffffff; |
| 27 | for (let i = 0; i < buf.length; i++) |
| 28 | { c = CRC_TABLE[(c ^ buf[i]) & 0xff] ^ (c >>> 8); } |
| 29 | return (c ^ 0xffffffff) >>> 0; |
| 30 | } |
| 31 | |
| 32 | function chunk (type, data) |
| 33 | { |
| 34 | const len = Buffer.alloc(4); |
| 35 | len.writeUInt32BE(data.length); |
| 36 | const typeBuf = Buffer.from(type, "ascii"); |
| 37 | const crcBuf = Buffer.alloc(4); |
| 38 | crcBuf.writeUInt32BE(crc32(Buffer.concat([typeBuf, data]))); |
| 39 | return Buffer.concat([len, typeBuf, data, crcBuf]); |
| 40 | } |
| 41 | |
| 42 | function decodePngRgba (buf) |
| 43 | { |
| 44 | let off = 8; |
| 45 | let w = 0; |
| 46 | let h = 0; |
| 47 | const idat = []; |
| 48 | while (off < buf.length) |
| 49 | { |
| 50 | const len = buf.readUInt32BE(off); |
| 51 | const type = buf.slice(off + 4, off + 8).toString("ascii"); |
| 52 | const data = buf.slice(off + 8, off + 8 + len); |
| 53 | if (type === "IHDR") |
| 54 | { |
| 55 | w = data.readUInt32BE(0); |
| 56 | h = data.readUInt32BE(4); |
| 57 | } |
| 58 | else if (type === "IDAT") |
| 59 | { idat.push(data); } |
| 60 | else if (type === "IEND") |
| 61 | { break; } |
| 62 | off += 12 + len; |
| 63 | } |
| 64 | const raw = inflateSync(Buffer.concat(idat)); |
| 65 | const stride = 1 + w * 4; |
| 66 | const rgba = new Uint8Array(w * h * 4); |
| 67 | for (let y = 0; y < h; y++) |
| 68 | { |
| 69 | for (let x = 0; x < w; x++) |
| 70 | { |
| 71 | const i = y * stride + 1 + x * 4; |
| 72 | const o = (y * w + x) * 4; |
| 73 | rgba[o] = raw[i]; |
| 74 | rgba[o + 1] = raw[i + 1]; |
| 75 | rgba[o + 2] = raw[i + 2]; |
| 76 | rgba[o + 3] = raw[i + 3]; |
| 77 | } |
| 78 | } |
| 79 | return { width: w, height: h, rgba }; |
| 80 | } |
| 81 | |
| 82 | function encodePng ({ width, height, rgba }) |
| 83 | { |
| 84 | const raw = Buffer.alloc((width * 4 + 1) * height); |
| 85 | for (let y = 0; y < height; y++) |
| 86 | { |
| 87 | const row = y * (width * 4 + 1); |
| 88 | raw[row] = 0; |
| 89 | for (let x = 0; x < width; x++) |
| 90 | { |
| 91 | const i = (y * width + x) * 4; |
| 92 | const o = row + 1 + x * 4; |
| 93 | raw[o] = rgba[i]; |
| 94 | raw[o + 1] = rgba[i + 1]; |
| 95 | raw[o + 2] = rgba[i + 2]; |
| 96 | raw[o + 3] = rgba[i + 3]; |
| 97 | } |
| 98 | } |
| 99 | const ihdr = Buffer.alloc(13); |
| 100 | ihdr.writeUInt32BE(width, 0); |
| 101 | ihdr.writeUInt32BE(height, 4); |
| 102 | ihdr[8] = 8; |
| 103 | ihdr[9] = 6; |
| 104 | return Buffer.concat([ |
| 105 | Buffer.from([137, 80, 78, 71, 13, 10, 26, 10]), |
| 106 | chunk("IHDR", ihdr), |
| 107 | chunk("IDAT", deflateSync(raw, { level: 9 })), |
| 108 | chunk("IEND", Buffer.alloc(0)) |
| 109 | ]); |
| 110 | } |
| 111 | |
| 112 | function toMagenta (r, g, b, a) |
| 113 | { |
| 114 | if (a < 8) |
| 115 | { return [0, 0, 0, 0]; } |
| 116 | const L = Math.max(1, Math.max(r, g, b)); |
| 117 | const minC = Math.min(r, g, b); |
| 118 | const chroma = (L - minC) / 255; |
| 119 | const gMag = Math.min(L - 1, Math.round(L * (0.08 + (1 - chroma) * 0.22))); |
| 120 | return [L, Math.max(0, gMag), L, 255]; |
| 121 | } |
| 122 | |
| 123 | function magentaize ({ width, height, rgba }) |
| 124 | { |
| 125 | const out = new Uint8Array(rgba.length); |
| 126 | for (let i = 0; i < rgba.length; i += 4) |
| 127 | { |
| 128 | const [r, g, b, a] = toMagenta(rgba[i], rgba[i + 1], rgba[i + 2], rgba[i + 3]); |
| 129 | out[i] = r; |
| 130 | out[i + 1] = g; |
| 131 | out[i + 2] = b; |
| 132 | out[i + 3] = a; |
| 133 | } |
| 134 | return { width, height, rgba: out }; |
| 135 | } |
| 136 | |
| 137 | const [inputPath, outputPath] = process.argv.slice(2); |
| 138 | if (!inputPath || !outputPath) |
| 139 | { |
| 140 | console.error("Usage: node magentaize-png.mjs <input.png> <output.png>"); |
| 141 | process.exit(1); |
| 142 | } |
| 143 | |
| 144 | const image = magentaize(decodePngRgba(readFileSync(inputPath))); |
| 145 | mkdirSync(dirname(outputPath), { recursive: true }); |
| 146 | writeFileSync(outputPath, encodePng(image)); |
| 147 | console.log(`Wrote ${outputPath} (${image.width}×${image.height})`); |