| 1 | /** |
| 2 | * Convert item sprites to dual-channel tint masks from magenta + natural art. |
| 3 | * |
| 4 | * - Magenta-family pixels (R≈B, G<R) → pure blue mask (0, 0, L), L = max(R, B) |
| 5 | * - All other opaque pixels → pure red mask (L, 0, 0), L = max(R, G, B) |
| 6 | * - Near-white pixels (fletching) are left unchanged |
| 7 | * |
| 8 | * Usage: node client/scripts/convert-dual-channel-png.mjs <input.png> [output.png] |
| 9 | */ |
| 10 | import { readFileSync, mkdirSync, writeFileSync } from "node:fs"; |
| 11 | import { dirname } from "node:path"; |
| 12 | import { deflateSync, inflateSync } from "node:zlib"; |
| 13 | import { MAGENTA_CHANNEL_TOLERANCE } from "../../shared/render/tint.js"; |
| 14 | |
| 15 | function crcTable () |
| 16 | { |
| 17 | const table = new Uint32Array(256); |
| 18 | for (let n = 0; n < 256; n++) |
| 19 | { |
| 20 | let c = n; |
| 21 | for (let k = 0; k < 8; k++) |
| 22 | { c = (c & 1) ? (0xedb88320 ^ (c >>> 1)) : (c >>> 1); } |
| 23 | table[n] = c >>> 0; |
| 24 | } |
| 25 | return table; |
| 26 | } |
| 27 | |
| 28 | const CRC_TABLE = crcTable(); |
| 29 | |
| 30 | function crc32 (buf) |
| 31 | { |
| 32 | let c = 0xffffffff; |
| 33 | for (let i = 0; i < buf.length; i++) |
| 34 | { c = CRC_TABLE[(c ^ buf[i]) & 0xff] ^ (c >>> 8); } |
| 35 | return (c ^ 0xffffffff) >>> 0; |
| 36 | } |
| 37 | |
| 38 | function chunk (type, data) |
| 39 | { |
| 40 | const len = Buffer.alloc(4); |
| 41 | len.writeUInt32BE(data.length); |
| 42 | const typeBuf = Buffer.from(type, "ascii"); |
| 43 | const crcBuf = Buffer.alloc(4); |
| 44 | crcBuf.writeUInt32BE(crc32(Buffer.concat([typeBuf, data]))); |
| 45 | return Buffer.concat([len, typeBuf, data, crcBuf]); |
| 46 | } |
| 47 | |
| 48 | function decodePngRgba (buf) |
| 49 | { |
| 50 | let off = 8; |
| 51 | let w = 0; |
| 52 | let h = 0; |
| 53 | const idat = []; |
| 54 | while (off < buf.length) |
| 55 | { |
| 56 | const len = buf.readUInt32BE(off); |
| 57 | const type = buf.slice(off + 4, off + 8).toString("ascii"); |
| 58 | const data = buf.slice(off + 8, off + 8 + len); |
| 59 | if (type === "IHDR") |
| 60 | { |
| 61 | w = data.readUInt32BE(0); |
| 62 | h = data.readUInt32BE(4); |
| 63 | } |
| 64 | else if (type === "IDAT") |
| 65 | { idat.push(data); } |
| 66 | else if (type === "IEND") |
| 67 | { break; } |
| 68 | off += 12 + len; |
| 69 | } |
| 70 | const raw = inflateSync(Buffer.concat(idat)); |
| 71 | const stride = 1 + w * 4; |
| 72 | const rgba = new Uint8Array(w * h * 4); |
| 73 | for (let y = 0; y < h; y++) |
| 74 | { |
| 75 | for (let x = 0; x < w; x++) |
| 76 | { |
| 77 | const i = y * stride + 1 + x * 4; |
| 78 | const o = (y * w + x) * 4; |
| 79 | rgba[o] = raw[i]; |
| 80 | rgba[o + 1] = raw[i + 1]; |
| 81 | rgba[o + 2] = raw[i + 2]; |
| 82 | rgba[o + 3] = raw[i + 3]; |
| 83 | } |
| 84 | } |
| 85 | return { width: w, height: h, rgba }; |
| 86 | } |
| 87 | |
| 88 | function encodePng ({ width, height, rgba }) |
| 89 | { |
| 90 | const raw = Buffer.alloc((width * 4 + 1) * height); |
| 91 | for (let y = 0; y < height; y++) |
| 92 | { |
| 93 | const row = y * (width * 4 + 1); |
| 94 | raw[row] = 0; |
| 95 | for (let x = 0; x < width; x++) |
| 96 | { |
| 97 | const i = (y * width + x) * 4; |
| 98 | const o = row + 1 + x * 4; |
| 99 | raw[o] = rgba[i]; |
| 100 | raw[o + 1] = rgba[i + 1]; |
| 101 | raw[o + 2] = rgba[i + 2]; |
| 102 | raw[o + 3] = rgba[i + 3]; |
| 103 | } |
| 104 | } |
| 105 | const ihdr = Buffer.alloc(13); |
| 106 | ihdr.writeUInt32BE(width, 0); |
| 107 | ihdr.writeUInt32BE(height, 4); |
| 108 | ihdr[8] = 8; |
| 109 | ihdr[9] = 6; |
| 110 | return Buffer.concat([ |
| 111 | Buffer.from([137, 80, 78, 71, 13, 10, 26, 10]), |
| 112 | chunk("IHDR", ihdr), |
| 113 | chunk("IDAT", deflateSync(raw, { level: 9 })), |
| 114 | chunk("IEND", Buffer.alloc(0)) |
| 115 | ]); |
| 116 | } |
| 117 | |
| 118 | function isMagentaPixel (r, g, b) |
| 119 | { |
| 120 | return Math.abs(r - b) <= MAGENTA_CHANNEL_TOLERANCE && g < r; |
| 121 | } |
| 122 | |
| 123 | function isNeutralWhite (r, g, b) |
| 124 | { |
| 125 | return r >= 240 && g >= 240 && b >= 240; |
| 126 | } |
| 127 | |
| 128 | /** @returns {[number, number, number, number]} */ |
| 129 | function toDualChannelPixel (r, g, b, a) |
| 130 | { |
| 131 | if (a < 8) |
| 132 | { return [0, 0, 0, 0]; } |
| 133 | if (isNeutralWhite(r, g, b)) |
| 134 | { return [r, g, b, a]; } |
| 135 | if (isMagentaPixel(r, g, b)) |
| 136 | { |
| 137 | const L = Math.max(r, b); |
| 138 | return [0, 0, L, a]; |
| 139 | } |
| 140 | const L = Math.max(r, g, b); |
| 141 | return [L, 0, 0, a]; |
| 142 | } |
| 143 | |
| 144 | /** @returns {{ red: number, blue: number, white: number, opaque: number }} */ |
| 145 | function convertDualChannel ({ width, height, rgba }) |
| 146 | { |
| 147 | const stats = { red: 0, blue: 0, white: 0, opaque: 0 }; |
| 148 | for (let i = 0; i < rgba.length; i += 4) |
| 149 | { |
| 150 | const r = rgba[i]; |
| 151 | const g = rgba[i + 1]; |
| 152 | const b = rgba[i + 2]; |
| 153 | const a = rgba[i + 3]; |
| 154 | const next = toDualChannelPixel(r, g, b, a); |
| 155 | rgba[i] = next[0]; |
| 156 | rgba[i + 1] = next[1]; |
| 157 | rgba[i + 2] = next[2]; |
| 158 | rgba[i + 3] = next[3]; |
| 159 | if (a < 8) |
| 160 | { continue; } |
| 161 | stats.opaque++; |
| 162 | if (isNeutralWhite(r, g, b)) |
| 163 | { stats.white++; } |
| 164 | else if (isMagentaPixel(r, g, b)) |
| 165 | { stats.blue++; } |
| 166 | else |
| 167 | { stats.red++; } |
| 168 | } |
| 169 | return stats; |
| 170 | } |
| 171 | |
| 172 | const [inputPath, outputPathArg] = process.argv.slice(2); |
| 173 | if (!inputPath) |
| 174 | { |
| 175 | console.error("Usage: node convert-dual-channel-png.mjs <input.png> [output.png]"); |
| 176 | process.exit(1); |
| 177 | } |
| 178 | |
| 179 | const outputPath = outputPathArg ?? inputPath; |
| 180 | const image = decodePngRgba(readFileSync(inputPath)); |
| 181 | const stats = convertDualChannel(image); |
| 182 | mkdirSync(dirname(outputPath), { recursive: true }); |
| 183 | writeFileSync(outputPath, encodePng(image)); |
| 184 | console.log( |
| 185 | `Wrote ${outputPath} (${image.width}×${image.height}) — ` |
| 186 | + `${stats.opaque} opaque → ${stats.red} red, ${stats.blue} blue` |
| 187 | + (stats.white ? `, ${stats.white} white` : "") |
| 188 | ); |