/** * Convert item sprites to dual-channel tint masks from magenta + natural art. * * - Magenta-family pixels (R≈B, G [output.png] */ import { readFileSync, mkdirSync, writeFileSync } from "node:fs"; import { dirname } from "node:path"; import { deflateSync, inflateSync } from "node:zlib"; import { MAGENTA_CHANNEL_TOLERANCE } from "../../shared/render/tint.js"; 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 isMagentaPixel (r, g, b) { return Math.abs(r - b) <= MAGENTA_CHANNEL_TOLERANCE && g < r; } function isNeutralWhite (r, g, b) { return r >= 240 && g >= 240 && b >= 240; } /** @returns {[number, number, number, number]} */ function toDualChannelPixel (r, g, b, a) { if (a < 8) { return [0, 0, 0, 0]; } if (isNeutralWhite(r, g, b)) { return [r, g, b, a]; } if (isMagentaPixel(r, g, b)) { const L = Math.max(r, b); return [0, 0, L, a]; } const L = Math.max(r, g, b); return [L, 0, 0, a]; } /** @returns {{ red: number, blue: number, white: number, opaque: number }} */ function convertDualChannel ({ width, height, rgba }) { const stats = { red: 0, blue: 0, white: 0, opaque: 0 }; for (let i = 0; i < rgba.length; i += 4) { const r = rgba[i]; const g = rgba[i + 1]; const b = rgba[i + 2]; const a = rgba[i + 3]; const next = toDualChannelPixel(r, g, b, a); rgba[i] = next[0]; rgba[i + 1] = next[1]; rgba[i + 2] = next[2]; rgba[i + 3] = next[3]; if (a < 8) { continue; } stats.opaque++; if (isNeutralWhite(r, g, b)) { stats.white++; } else if (isMagentaPixel(r, g, b)) { stats.blue++; } else { stats.red++; } } return stats; } const [inputPath, outputPathArg] = process.argv.slice(2); if (!inputPath) { console.error("Usage: node convert-dual-channel-png.mjs [output.png]"); process.exit(1); } const outputPath = outputPathArg ?? inputPath; const image = decodePngRgba(readFileSync(inputPath)); const stats = convertDualChannel(image); mkdirSync(dirname(outputPath), { recursive: true }); writeFileSync(outputPath, encodePng(image)); console.log( `Wrote ${outputPath} (${image.width}×${image.height}) — ` + `${stats.opaque} opaque → ${stats.red} red, ${stats.blue} blue` + (stats.white ? `, ${stats.white} white` : "") );