2023-08-12 03:40:30 +00:00
|
|
|
// @ts-check
|
|
|
|
|
|
|
|
import { GifNodeType } from "./constants.js";
|
|
|
|
/** @typedef {import("../../types/gif.d.ts").GifNode} GifNode */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {Uint8Array} bytes
|
2023-08-16 15:34:09 +00:00
|
|
|
* @returns {null | GifNode} The root node of the GIF tree, or null if the GIF is invalid.
|
2023-08-12 03:40:30 +00:00
|
|
|
*/
|
|
|
|
export default (bytes) => {
|
|
|
|
/** @type {GifNode[]} */
|
|
|
|
const children = [
|
|
|
|
{
|
|
|
|
type: GifNodeType.header,
|
|
|
|
bytes: bytes.subarray(0, 6),
|
|
|
|
children: [
|
|
|
|
{
|
|
|
|
type: GifNodeType.headerSignature,
|
|
|
|
bytes: bytes.subarray(0, 3),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
type: GifNodeType.headerVersion,
|
2023-08-16 15:46:31 +00:00
|
|
|
bytes: bytes.subarray(3, 3 + 3),
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
{
|
|
|
|
type: GifNodeType.logicalScreenDescriptor,
|
|
|
|
bytes: bytes.subarray(6, 6 + 6),
|
|
|
|
children: [
|
|
|
|
{
|
|
|
|
type: GifNodeType.logicalScreenWidth,
|
|
|
|
bytes: bytes.subarray(6, 6 + 2),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
type: GifNodeType.logicalScreenHeight,
|
|
|
|
bytes: bytes.subarray(8, 8 + 2),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
type: GifNodeType.logicalScreenDescriptorPackedFields,
|
|
|
|
bytes: bytes.subarray(10, 10 + 1),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
type: GifNodeType.logicalScreenBackgroundColorIndex,
|
|
|
|
bytes: bytes.subarray(11, 11 + 1),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
type: GifNodeType.logicalScreenPixelAspectRatio,
|
|
|
|
bytes: bytes.subarray(12, 12 + 1),
|
2023-08-12 03:40:30 +00:00
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
];
|
|
|
|
|
|
|
|
return { type: GifNodeType.root, bytes, children };
|
|
|
|
};
|