177 lines
6.3 KiB
TypeScript
177 lines
6.3 KiB
TypeScript
// SPDX-FileCopyrightText: 2026 Paula Wisnievski
|
|
// SPDX-License-Identifier: AGPL-3.0-or-later
|
|
|
|
// png decoding, for textures, skins and the gui. the desktop app's client runs without a browser, so it has no image
|
|
// decoding of its own, and both clients decoding the same way keeps them alike. pixels come out as stored: no color
|
|
// profile or gamma is applied, like an editor that ignores them
|
|
import { unzlibSync } from "fflate";
|
|
|
|
// rgba, 8 bits a channel, not premultiplied, row by row from the top
|
|
export interface Pixels {
|
|
width: number;
|
|
height: number;
|
|
data: Uint8ClampedArray<ArrayBuffer>;
|
|
}
|
|
|
|
const SIGNATURE = [137, 80, 78, 71, 13, 10, 26, 10];
|
|
|
|
// by color type: gray, rgb, palette, gray and alpha, rgba
|
|
const CHANNELS: Record<number, number> = { 0: 1, 2: 3, 3: 1, 4: 2, 6: 4 };
|
|
|
|
// the 7 passes of an interlaced image: the first pixel's x and y, and the steps between pixels
|
|
const ADAM7 = [
|
|
[0, 0, 8, 8],
|
|
[4, 0, 8, 8],
|
|
[0, 4, 4, 8],
|
|
[2, 0, 4, 4],
|
|
[0, 2, 2, 4],
|
|
[1, 0, 2, 2],
|
|
[0, 1, 1, 2],
|
|
];
|
|
|
|
// throws for anything that isn't a png
|
|
export function decode_png(bytes: Uint8Array): Pixels {
|
|
if (bytes.length < 8 || SIGNATURE.some((byte, i) => bytes[i] !== byte)) throw new Error("Not a png");
|
|
const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
|
|
|
|
let width = 0;
|
|
let height = 0;
|
|
let depth = 0;
|
|
let color_type = -1;
|
|
let interlaced = false;
|
|
let palette: Uint8Array | undefined;
|
|
// the alpha of each palette entry, or the one gray or rgb value that's see-through
|
|
let transparency: Uint8Array | undefined;
|
|
const compressed: Uint8Array[] = [];
|
|
|
|
let pos = 8;
|
|
while (pos + 8 <= bytes.length) {
|
|
const length = view.getUint32(pos);
|
|
const type = String.fromCharCode(...bytes.subarray(pos + 4, pos + 8));
|
|
const start = pos + 8;
|
|
const data = bytes.subarray(start, start + length);
|
|
pos = start + length + 4;
|
|
if (type === "IHDR") {
|
|
width = view.getUint32(start);
|
|
height = view.getUint32(start + 4);
|
|
depth = data[8];
|
|
color_type = data[9];
|
|
interlaced = data[12] === 1;
|
|
} else if (type === "PLTE") {
|
|
palette = data;
|
|
} else if (type === "tRNS") {
|
|
transparency = data;
|
|
} else if (type === "IDAT") {
|
|
compressed.push(data);
|
|
} else if (type === "IEND") {
|
|
break;
|
|
}
|
|
}
|
|
const channels = CHANNELS[color_type];
|
|
if (!channels || !width || !height || ![1, 2, 4, 8, 16].includes(depth)) throw new Error("Not a png");
|
|
if (color_type === 3 && !palette) throw new Error("A palette png without a palette");
|
|
|
|
const raw = unzlibSync(concat(compressed));
|
|
const pixels = new Uint8ClampedArray(width * height * 4);
|
|
const bits_per_pixel = channels * depth;
|
|
// filters work on whole bytes, a pixel's or 1 for pixels smaller than a byte
|
|
const filter_step = Math.max(1, bits_per_pixel >> 3);
|
|
// a sample at depth, scaled to 0-255
|
|
const max = (1 << depth) - 1;
|
|
const sample = (line: Uint8Array, index: number) => {
|
|
if (depth === 8) return line[index];
|
|
if (depth === 16) return (line[index * 2] << 8) | line[index * 2 + 1];
|
|
const bit = index * depth;
|
|
return (line[bit >> 3] >> (8 - depth - (bit & 7))) & max;
|
|
};
|
|
const to_byte = (value: number) => depth === 16 ? value >> 8 : depth === 8 ? value : Math.round(value * 255 / max);
|
|
const see_through = (value: number, channel: number) =>
|
|
(transparency![channel * 2] << 8 | transparency![channel * 2 + 1]) === value;
|
|
|
|
let offset = 0;
|
|
for (const [first_x, first_y, step_x, step_y] of interlaced ? ADAM7 : [[0, 0, 1, 1]]) {
|
|
const pass_width = Math.ceil((width - first_x) / step_x);
|
|
const pass_height = Math.ceil((height - first_y) / step_y);
|
|
if (pass_width <= 0 || pass_height <= 0) continue;
|
|
const stride = Math.ceil(pass_width * bits_per_pixel / 8);
|
|
let previous = new Uint8Array(stride);
|
|
for (let y = 0; y < pass_height; y++) {
|
|
if (offset + 1 + stride > raw.length) throw new Error("A png with too little image data");
|
|
const line = raw.slice(offset + 1, offset + 1 + stride);
|
|
unfilter(raw[offset], line, previous, filter_step);
|
|
offset += 1 + stride;
|
|
previous = line;
|
|
|
|
for (let x = 0; x < pass_width; x++) {
|
|
const out = ((first_y + y * step_y) * width + first_x + x * step_x) * 4;
|
|
const s = x * channels;
|
|
if (color_type === 3) {
|
|
const entry = sample(line, x);
|
|
pixels[out] = palette![entry * 3];
|
|
pixels[out + 1] = palette![entry * 3 + 1];
|
|
pixels[out + 2] = palette![entry * 3 + 2];
|
|
pixels[out + 3] = transparency && entry < transparency.length ? transparency[entry] : 255;
|
|
} else if (color_type === 0 || color_type === 4) {
|
|
const gray = sample(line, s);
|
|
pixels[out] = pixels[out + 1] = pixels[out + 2] = to_byte(gray);
|
|
pixels[out + 3] = color_type === 4
|
|
? to_byte(sample(line, s + 1))
|
|
: transparency && see_through(gray, 0)
|
|
? 0
|
|
: 255;
|
|
} else {
|
|
const [r, g, b] = [sample(line, s), sample(line, s + 1), sample(line, s + 2)];
|
|
pixels[out] = to_byte(r);
|
|
pixels[out + 1] = to_byte(g);
|
|
pixels[out + 2] = to_byte(b);
|
|
pixels[out + 3] = color_type === 6
|
|
? to_byte(sample(line, s + 3))
|
|
: transparency && see_through(r, 0) && see_through(g, 1) && see_through(b, 2)
|
|
? 0
|
|
: 255;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return { width, height, data: pixels };
|
|
}
|
|
|
|
// undoes a line's filter in place, previous is the line above it, already undone
|
|
function unfilter(filter: number, line: Uint8Array, previous: Uint8Array, step: number) {
|
|
switch (filter) {
|
|
case 0:
|
|
return;
|
|
case 1:
|
|
for (let i = step; i < line.length; i++) line[i] += line[i - step];
|
|
return;
|
|
case 2:
|
|
for (let i = 0; i < line.length; i++) line[i] += previous[i];
|
|
return;
|
|
case 3:
|
|
for (let i = 0; i < line.length; i++) line[i] += ((i >= step ? line[i - step] : 0) + previous[i]) >> 1;
|
|
return;
|
|
case 4:
|
|
for (let i = 0; i < line.length; i++) {
|
|
const left = i >= step ? line[i - step] : 0;
|
|
const up = previous[i];
|
|
const up_left = i >= step ? previous[i - step] : 0;
|
|
const p = left + up - up_left;
|
|
const [pa, pb, pc] = [Math.abs(p - left), Math.abs(p - up), Math.abs(p - up_left)];
|
|
line[i] += pa <= pb && pa <= pc ? left : pb <= pc ? up : up_left;
|
|
}
|
|
return;
|
|
default:
|
|
throw new Error("A png with an unknown filter");
|
|
}
|
|
}
|
|
|
|
function concat(parts: Uint8Array[]) {
|
|
const all = new Uint8Array(parts.reduce((sum, part) => sum + part.length, 0));
|
|
let at = 0;
|
|
for (const part of parts) {
|
|
all.set(part, at);
|
|
at += part.length;
|
|
}
|
|
return all;
|
|
}
|