// @ts-check import { GifNodeType } from "./constants.js"; /** @typedef {import("../../types/gif.d.ts").GifNode} GifNode */ /** * @param {Uint8Array} bytes * @returns {null | GifNode} The root node of the GIF tree, or null if the GIF is invalid. */ 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, 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), }, ], }, ]; return { type: GifNodeType.root, bytes, children }; };