import { inflateSync } from "node:zlib"; const PNG_SIGNATURE = Buffer.from([137, 80, 78, 71, 13, 10, 26, 10]); /** * Decode an 8-bit RGB or RGBA PNG (no interlacing) into straight RGBA. * @param {Buffer} buffer * @param {string} [label] * @returns {{ width: number, height: number, rgba: Uint8ClampedArray }} */ export function decodePngRgba (buffer, label = "png") { if (!Buffer.isBuffer(buffer) || buffer.length < 8 || !buffer.subarray(0, 8).equals(PNG_SIGNATURE)) { throw new Error(`${label}: not a PNG`); } let offset = 8; let width = 0; let height = 0; let colorType = -1; const idat = []; while (offset + 8 <= buffer.length) { const length = buffer.readUInt32BE(offset); const type = buffer.toString("ascii", offset + 4, offset + 8); const dataStart = offset + 8; const dataEnd = dataStart + length; if (dataEnd + 4 > buffer.length) { throw new Error(`${label}: truncated PNG chunk`); } const data = buffer.subarray(dataStart, dataEnd); if (type === "IHDR") { width = data.readUInt32BE(0); height = data.readUInt32BE(4); const bitDepth = data[8]; colorType = data[9]; const interlace = data[12]; if (bitDepth !== 8 || (colorType !== 2 && colorType !== 6) || interlace !== 0) { throw new Error( `${label}: expected 8-bit RGB or RGBA without interlacing (bit ${bitDepth}, color ${colorType}, interlace ${interlace})` ); } } else if (type === "IDAT") { idat.push(data); } else if (type === "IEND") { break; } offset = dataEnd + 4; } if (width <= 0 || height <= 0 || colorType < 0) { throw new Error(`${label}: missing IHDR`); } if (idat.length === 0) { throw new Error(`${label}: missing IDAT`); } const raw = inflateSync(Buffer.concat(idat)); const channels = colorType === 6 ? 4 : 3; const stride = width * channels; const expected = (stride + 1) * height; if (raw.length < expected) { throw new Error(`${label}: inflated PNG is short (${raw.length} < ${expected})`); } const decoded = Buffer.allocUnsafe(stride * height); const previous = Buffer.alloc(stride); const current = Buffer.alloc(stride); let source = 0; for (let y = 0; y < height; y++) { const filter = raw[source++]; raw.copy(current, 0, source, source + stride); source += stride; for (let i = 0; i < stride; i++) { const left = i >= channels ? current[i - channels] : 0; const up = previous[i]; const upLeft = i >= channels ? previous[i - channels] : 0; let value = current[i]; if (filter === 1) { value = (value + left) & 255; } else if (filter === 2) { value = (value + up) & 255; } else if (filter === 3) { value = (value + ((left + up) >> 1)) & 255; } else if (filter === 4) { const estimate = left + up - upLeft; const pa = Math.abs(estimate - left); const pb = Math.abs(estimate - up); const pc = Math.abs(estimate - upLeft); const predictor = (pa <= pb && pa <= pc) ? left : (pb <= pc ? up : upLeft); value = (value + predictor) & 255; } else if (filter !== 0) { throw new Error(`${label}: unsupported PNG filter ${filter}`); } current[i] = value; } current.copy(decoded, y * stride); current.copy(previous); } const rgba = new Uint8ClampedArray(width * height * 4); if (channels === 4) { rgba.set(decoded); } else { for (let i = 0, out = 0; i < width * height; i++) { const sourceOffset = i * 3; rgba[out++] = decoded[sourceOffset]; rgba[out++] = decoded[sourceOffset + 1]; rgba[out++] = decoded[sourceOffset + 2]; rgba[out++] = 255; } } return { width, height, rgba }; }