BlueGit

timfa questward

Files

questward / server / src / assets / pngRgba.js

pngRgba.js

feat: add sprite average colours and an editor minimap
timfa project_85689633_bot_760517ee9267c86ff1d6c3a0237abf7d · pushed by timfa · 6 weeks ago
8a24eb51 History
pngRgba.js 3.5 KiB Download Raw
1import { inflateSync } from "node:zlib";
2
3const PNG_SIGNATURE = Buffer.from([137, 80, 78, 71, 13, 10, 26, 10]);
4
5/**
6 * Decode an 8-bit RGB or RGBA PNG (no interlacing) into straight RGBA.
7 * @param {Buffer} buffer
8 * @param {string} [label]
9 * @returns {{ width: number, height: number, rgba: Uint8ClampedArray }}
10 */
11export function decodePngRgba (buffer, label = "png")
12{
13 if (!Buffer.isBuffer(buffer) || buffer.length < 8 || !buffer.subarray(0, 8).equals(PNG_SIGNATURE))
14 { throw new Error(`${label}: not a PNG`); }
15
16 let offset = 8;
17 let width = 0;
18 let height = 0;
19 let colorType = -1;
20 const idat = [];
21 while (offset + 8 <= buffer.length)
22 {
23 const length = buffer.readUInt32BE(offset);
24 const type = buffer.toString("ascii", offset + 4, offset + 8);
25 const dataStart = offset + 8;
26 const dataEnd = dataStart + length;
27 if (dataEnd + 4 > buffer.length)
28 { throw new Error(`${label}: truncated PNG chunk`); }
29 const data = buffer.subarray(dataStart, dataEnd);
30 if (type === "IHDR")
31 {
32 width = data.readUInt32BE(0);
33 height = data.readUInt32BE(4);
34 const bitDepth = data[8];
35 colorType = data[9];
36 const interlace = data[12];
37 if (bitDepth !== 8 || (colorType !== 2 && colorType !== 6) || interlace !== 0)
38 {
39 throw new Error(
40 `${label}: expected 8-bit RGB or RGBA without interlacing (bit ${bitDepth}, color ${colorType}, interlace ${interlace})`
41 );
42 }
43 }
44 else if (type === "IDAT")
45 { idat.push(data); }
46 else if (type === "IEND")
47 { break; }
48 offset = dataEnd + 4;
49 }
50 if (width <= 0 || height <= 0 || colorType < 0)
51 { throw new Error(`${label}: missing IHDR`); }
52 if (idat.length === 0)
53 { throw new Error(`${label}: missing IDAT`); }
54
55 const raw = inflateSync(Buffer.concat(idat));
56 const channels = colorType === 6 ? 4 : 3;
57 const stride = width * channels;
58 const expected = (stride + 1) * height;
59 if (raw.length < expected)
60 { throw new Error(`${label}: inflated PNG is short (${raw.length} < ${expected})`); }
61
62 const decoded = Buffer.allocUnsafe(stride * height);
63 const previous = Buffer.alloc(stride);
64 const current = Buffer.alloc(stride);
65 let source = 0;
66 for (let y = 0; y < height; y++)
67 {
68 const filter = raw[source++];
69 raw.copy(current, 0, source, source + stride);
70 source += stride;
71 for (let i = 0; i < stride; i++)
72 {
73 const left = i >= channels ? current[i - channels] : 0;
74 const up = previous[i];
75 const upLeft = i >= channels ? previous[i - channels] : 0;
76 let value = current[i];
77 if (filter === 1)
78 { value = (value + left) & 255; }
79 else if (filter === 2)
80 { value = (value + up) & 255; }
81 else if (filter === 3)
82 { value = (value + ((left + up) >> 1)) & 255; }
83 else if (filter === 4)
84 {
85 const estimate = left + up - upLeft;
86 const pa = Math.abs(estimate - left);
87 const pb = Math.abs(estimate - up);
88 const pc = Math.abs(estimate - upLeft);
89 const predictor = (pa <= pb && pa <= pc) ? left : (pb <= pc ? up : upLeft);
90 value = (value + predictor) & 255;
91 }
92 else if (filter !== 0)
93 { throw new Error(`${label}: unsupported PNG filter ${filter}`); }
94 current[i] = value;
95 }
96 current.copy(decoded, y * stride);
97 current.copy(previous);
98 }
99
100 const rgba = new Uint8ClampedArray(width * height * 4);
101 if (channels === 4)
102 { rgba.set(decoded); }
103 else
104 {
105 for (let i = 0, out = 0; i < width * height; i++)
106 {
107 const sourceOffset = i * 3;
108 rgba[out++] = decoded[sourceOffset];
109 rgba[out++] = decoded[sourceOffset + 1];
110 rgba[out++] = decoded[sourceOffset + 2];
111 rgba[out++] = 255;
112 }
113 }
114 return { width, height, rgba };
115}