BlueGit

timfa questward

Files

questward / client / scripts / purify-dual-channel-png.mjs

purify-dual-channel-png.mjs

lots of things
timfa Tim Falken · pushed by timfa · 2 months ago
07a95f71 History
purify-dual-channel-png.mjs 3.9 KiB Download Raw
1/**
2 * Normalize already-dual-channel tint masks: red-dominant → (R,0,0), blue-dominant → (0,0,B).
3 * For first-time conversion from magenta/brown art, use convert-dual-channel-png.mjs instead.
4 *
5 * Usage: node client/scripts/purify-dual-channel-png.mjs <input.png> [output.png]
6 * When output is omitted, overwrites input.
7 */
8import { readFileSync, mkdirSync, writeFileSync } from "node:fs";
9import { dirname } from "node:path";
10import { deflateSync, inflateSync } from "node:zlib";
11
12function crcTable ()
13{
14 const table = new Uint32Array(256);
15 for (let n = 0; n < 256; n++)
16 {
17 let c = n;
18 for (let k = 0; k < 8; k++)
19 { c = (c & 1) ? (0xedb88320 ^ (c >>> 1)) : (c >>> 1); }
20 table[n] = c >>> 0;
21 }
22 return table;
23}
24
25const CRC_TABLE = crcTable();
26
27function crc32 (buf)
28{
29 let c = 0xffffffff;
30 for (let i = 0; i < buf.length; i++)
31 { c = CRC_TABLE[(c ^ buf[i]) & 0xff] ^ (c >>> 8); }
32 return (c ^ 0xffffffff) >>> 0;
33}
34
35function chunk (type, data)
36{
37 const len = Buffer.alloc(4);
38 len.writeUInt32BE(data.length);
39 const typeBuf = Buffer.from(type, "ascii");
40 const crcBuf = Buffer.alloc(4);
41 crcBuf.writeUInt32BE(crc32(Buffer.concat([typeBuf, data])));
42 return Buffer.concat([len, typeBuf, data, crcBuf]);
43}
44
45function decodePngRgba (buf)
46{
47 let off = 8;
48 let w = 0;
49 let h = 0;
50 const idat = [];
51 while (off < buf.length)
52 {
53 const len = buf.readUInt32BE(off);
54 const type = buf.slice(off + 4, off + 8).toString("ascii");
55 const data = buf.slice(off + 8, off + 8 + len);
56 if (type === "IHDR")
57 {
58 w = data.readUInt32BE(0);
59 h = data.readUInt32BE(4);
60 }
61 else if (type === "IDAT")
62 { idat.push(data); }
63 else if (type === "IEND")
64 { break; }
65 off += 12 + len;
66 }
67 const raw = inflateSync(Buffer.concat(idat));
68 const stride = 1 + w * 4;
69 const rgba = new Uint8Array(w * h * 4);
70 for (let y = 0; y < h; y++)
71 {
72 for (let x = 0; x < w; x++)
73 {
74 const i = y * stride + 1 + x * 4;
75 const o = (y * w + x) * 4;
76 rgba[o] = raw[i];
77 rgba[o + 1] = raw[i + 1];
78 rgba[o + 2] = raw[i + 2];
79 rgba[o + 3] = raw[i + 3];
80 }
81 }
82 return { width: w, height: h, rgba };
83}
84
85function encodePng ({ width, height, rgba })
86{
87 const raw = Buffer.alloc((width * 4 + 1) * height);
88 for (let y = 0; y < height; y++)
89 {
90 const row = y * (width * 4 + 1);
91 raw[row] = 0;
92 for (let x = 0; x < width; x++)
93 {
94 const i = (y * width + x) * 4;
95 const o = row + 1 + x * 4;
96 raw[o] = rgba[i];
97 raw[o + 1] = rgba[i + 1];
98 raw[o + 2] = rgba[i + 2];
99 raw[o + 3] = rgba[i + 3];
100 }
101 }
102 const ihdr = Buffer.alloc(13);
103 ihdr.writeUInt32BE(width, 0);
104 ihdr.writeUInt32BE(height, 4);
105 ihdr[8] = 8;
106 ihdr[9] = 6;
107 return Buffer.concat([
108 Buffer.from([137, 80, 78, 71, 13, 10, 26, 10]),
109 chunk("IHDR", ihdr),
110 chunk("IDAT", deflateSync(raw, { level: 9 })),
111 chunk("IEND", Buffer.alloc(0))
112 ]);
113}
114
115/** @returns {{ redFixed: number, blueFixed: number }} */
116function purifyDualChannel ({ width, height, rgba })
117{
118 let redFixed = 0;
119 let blueFixed = 0;
120 for (let i = 0; i < rgba.length; i += 4)
121 {
122 const a = rgba[i + 3];
123 if (a < 8)
124 { continue; }
125 const r = rgba[i];
126 const g = rgba[i + 1];
127 const b = rgba[i + 2];
128 if (r > g && r > b && r >= 8)
129 {
130 if (g !== 0 || b !== 0)
131 {
132 rgba[i] = r;
133 rgba[i + 1] = 0;
134 rgba[i + 2] = 0;
135 redFixed++;
136 }
137 }
138 else if (b > r && b > g && b >= 8)
139 {
140 if (r !== 0 || g !== 0)
141 {
142 rgba[i] = 0;
143 rgba[i + 1] = 0;
144 rgba[i + 2] = b;
145 blueFixed++;
146 }
147 }
148 }
149 return { redFixed, blueFixed };
150}
151
152const [inputPath, outputPathArg] = process.argv.slice(2);
153if (!inputPath)
154{
155 console.error("Usage: node purify-dual-channel-png.mjs <input.png> [output.png]");
156 process.exit(1);
157}
158
159const outputPath = outputPathArg ?? inputPath;
160const image = decodePngRgba(readFileSync(inputPath));
161const stats = purifyDualChannel(image);
162mkdirSync(dirname(outputPath), { recursive: true });
163writeFileSync(outputPath, encodePng(image));
164console.log(`Wrote ${outputPath} (${image.width}×${image.height}) — fixed ${stats.redFixed} red, ${stats.blueFixed} blue pixels`);