image-js
Version:
Image processing and manipulation in JavaScript
60 lines • 1.8 kB
JavaScript
import { decodeApng } from 'fast-png';
import { Image } from "../../Image.js";
import { Stack } from "../../Stack.js";
/**
* Decodes APNG image into a Stack
* @param data - APNG data.
* @returns stack of frames.
*/
export function decodeStackFromApng(data) {
const decodedApng = decodeApng(data);
const images = [];
let colorModel;
switch (decodedApng.channels) {
case 1:
if (decodedApng.palette) {
colorModel = decodedApng.palette[0].length === 3 ? 'RGB' : 'RGBA';
}
else {
colorModel = 'GREY';
}
break;
case 2:
colorModel = 'GREYA';
break;
case 3:
colorModel = 'RGB';
break;
default:
colorModel = 'RGBA';
break;
}
if (decodedApng.palette) {
for (const image of decodedApng.frames) {
images.push(new Image(decodedApng.width, decodedApng.height, {
data: convertIndexedData(image.data, decodedApng.palette),
colorModel,
}));
}
}
else {
for (const image of decodedApng.frames) {
images.push(new Image(decodedApng.width, decodedApng.height, {
data: image.data,
colorModel,
}));
}
}
const stack = new Stack(images);
return stack;
}
function convertIndexedData(data, palette) {
const result = new Uint8Array(data.length * palette[0].length);
for (let i = 0; i < data.length; i++) {
for (let channel = 0; channel < palette[0].length; channel++) {
result[i * palette[0].length + channel] = palette[data[i]][channel];
}
}
return result;
}
//# sourceMappingURL=decodeApng.js.map