tgaHeader(): Coder<TgaHeader>
Creates a coder for a Truevision TGA image header.
The imageId field's length is derived from idLength, so a header with
no image ID needs idLength = 0 and an empty imageId.
Nothing beyond the header and image ID is parsed — the color map, pixel
data, and TGA 2.0 footer/extension area are all left to the caller.
Nothing is validated on encode either: idLength is written exactly as
given, so it must match imageId.length.
Header carrying an image ID
Header carrying an image ID
import { assertEquals } from "@std/assert"; import { tgaHeader, TGA_HEADER_SIZE, TGA_IMAGE_TYPE } from "@binstruct/tga"; const imageId = new TextEncoder().encode("frame01"); const coder = tgaHeader(); const header = { idLength: imageId.length, colorMapType: 0, imageType: TGA_IMAGE_TYPE.grayscale, colorMapFirstEntryIndex: 0, colorMapLength: 0, colorMapEntrySize: 0, xOrigin: 0, yOrigin: 0, width: 16, height: 16, pixelDepth: 8, imageDescriptor: 0, imageId, }; const buffer = new Uint8Array(64); const written = coder.encode(header, buffer); const [decoded, read] = coder.decode(buffer.subarray(0, written)); assertEquals(written, TGA_HEADER_SIZE + imageId.length); assertEquals(read, written); assertEquals(decoded.imageId, imageId); assertEquals(decoded.idLength, imageId.length);
Known wire bytes for a color-mapped image
Known wire bytes for a color-mapped image
import { assertEquals } from "@std/assert"; import { tgaHeader, TGA_IMAGE_TYPE } from "@binstruct/tga"; // deno-fmt-ignore const wire = new Uint8Array([ 0x00, // idLength = 0 0x01, // colorMapType = 1 (present) 0x01, // imageType = colorMapped 0x00, 0x00, // colorMapFirstEntryIndex = 0 0x00, 0x01, // colorMapLength = 256 0x18, // colorMapEntrySize = 24 0x00, 0x00, // xOrigin = 0 0x00, 0x00, // yOrigin = 0 0x0a, 0x00, // width = 10 0x0a, 0x00, // height = 10 0x08, // pixelDepth = 8 0x00, // imageDescriptor = 0 ]); const [decoded, read] = tgaHeader().decode(wire); assertEquals(read, wire.length); assertEquals(decoded.imageType, TGA_IMAGE_TYPE.colorMapped); assertEquals(decoded.colorMapLength, 256); assertEquals(decoded.width, 10); assertEquals(decoded.height, 10); assertEquals(decoded.imageId.length, 0);