async function resizePngToUintArray(arrayBuffer, size) {
const img = UPNG.decode(arrayBuffer);
const rgba = UPNG.toRGBA8(img);
const resized = await Promise.all(
rgba.map(async (buf) => {
const imageData = new ImageData(
new Uint8ClampedArray(buf),
img.width,
img.height
);
const bitmap = await createImageBitmap(imageData, {
resizeWidth: size,
resizeHeight: size,
});
const ctx = DOM.context2d(size, size, 1);
ctx.drawImage(bitmap, 0, 0);
const { buffer } = ctx.getImageData(0, 0, size, size).data;
return buffer;
})
);
const apng = UPNG.encode(
resized,
size,
size,
img.depth,
img.frames.map((f) => f.delay)
);
return {bytes: new Uint8Array(apng), frames: img.frames.length};
}