Parsing global color table

This commit is contained in:
Evan Hahn 2023-08-16 11:59:33 -05:00
parent 775746959e
commit 476789c5a2
3 changed files with 65 additions and 5 deletions

View File

@ -14,6 +14,9 @@ export const GifNodeType = [
"logicalScreenDescriptorPackedFields",
"logicalScreenBackgroundColorIndex",
"logicalScreenPixelAspectRatio",
"globalColorTable",
"globalColorTableColor",
].reduce((result, id) => {
result[id] = id;
return result;

View File

@ -23,6 +23,12 @@ const p = (...children) => crel("p", {}, ...children);
*/
const li = (...children) => crel("li", {}, ...children);
/**
* @param {number} byte
* @returns {string}
*/
const formatByte = (byte) => byte.toString(16).padStart(2, "0");
/**
* @param {Uint8Array} bytes
* @returns {number}
@ -77,9 +83,9 @@ const NODE_UI_FNS = {
// Logical screen descriptor
[GifNodeType.logicalScreenDescriptor]: () => ({
title: "Logical Screen Descriptor",
title: "Logical Screen Descriptor block",
description: p(
"The logical screen descriptor contains information about the overall GIF, such as its dimensions.",
"The Logical Screen Descriptor contains information about the overall GIF, such as its dimensions.",
),
}),
[GifNodeType.logicalScreenWidth]: ({ bytes }) => ({
@ -134,8 +140,8 @@ const NODE_UI_FNS = {
li(
`The last three bits (${
(byte & 0b111).toString(2).padStart(3, "0")
}) encode the size of the global color table. To decode this field, you add 1 and then raise that to the power of 2, so the size of the global color table is ${
pluralize(packedField.sizeOfGlobalColorTable, "byte")
}) encode the number of colors of the global color table. To decode this field, you add 1 and then raise that to the power of 2, so the size of the global color table is ${
pluralize(packedField.sizeOfGlobalColorTable, "color")
}. `,
),
),
@ -150,6 +156,35 @@ const NODE_UI_FNS = {
title: "Pixel Aspect Ratio",
description: p("TODO"),
}),
// Global color table
[GifNodeType.globalColorTable]: ({ children }) => ({
title: "Global Color Table",
description: p(
`The Global Color Table is a list of colors. Each color is encoded as three bytes: one for red, one for green, and one for blue. The number of colors in the table is ${
pluralize(children?.length || 0, "color")
}.`,
),
}),
[GifNodeType.globalColorTableColor]: ({ bytes }) => ({
title: "Global Color Table color",
description: p(
"A color. It looks like this: ",
crel(
"span",
{
"style":
`display:inline-block;width:1em;height:1em;vertical-align:middle;color:transparent;background:rgb(${
bytes.join(",")
})`,
},
`#${formatByte(bytes[0])}${formatByte(bytes[1])}${
formatByte(bytes[2])
}`,
),
),
}),
};
/**

View File

@ -30,7 +30,6 @@ export default (bytes) => {
const widthBytes = bytes.subarray(6, 6 + 2);
const heightBytes = bytes.subarray(8, 8 + 2);
const packedFieldBytes = bytes.subarray(10, 10 + 1);
const packedFieldByte = packedFieldBytes[0];
/** @type {GifNode[]} */
const children = [
@ -76,5 +75,28 @@ export default (bytes) => {
},
];
let offset = 13;
const packedField = parseLogicalScreenDescriptorPackedField(
packedFieldBytes[0],
);
if (packedField.globalColorTableFlag) {
/** @type {GifNode[]} */ const colorNodes = [];
for (let i = 0; i < packedField.sizeOfGlobalColorTable; i++) {
colorNodes.push({
type: GifNodeType.globalColorTableColor,
bytes: bytes.subarray(13 + (i * 3), 13 + (i * 3) + 3),
});
}
const sizeOfGlobalColorTableInBytes = packedField.sizeOfGlobalColorTable *
3;
children.push({
type: GifNodeType.globalColorTable,
bytes: bytes.subarray(13, 13 + sizeOfGlobalColorTableInBytes),
children: colorNodes,
});
offset += sizeOfGlobalColorTableInBytes;
}
return { type: GifNodeType.root, bytes, children };
};