Truevision TGA (.tga) image header encoding and decoding.

A TGA file opens with an 18-byte header, immediately followed by a variable-length image ID field:

Offset  Size  Field
------  ----  ------------------------------------------
     0     1  ID Length
     1     1  Color Map Type
     2     1  Image Type
     3     2  Color Map First Entry Index (LE)
     5     2  Color Map Length (LE)
     7     1  Color Map Entry Size
     8     2  X-Origin (LE)
    10     2  Y-Origin (LE)
    12     2  Width (LE)
    14     2  Height (LE)
    16     1  Pixel Depth
    17     1  Image Descriptor
    18     N  Image ID (N = ID Length)

Every multi-byte field is little-endian. idLength gives the size in bytes of the imageId field that immediately follows the fixed header — a value of 0 means no image ID is present.

This module covers the header (and the image ID field it sizes) only. The optional color map, the pixel data, and the optional TGA 2.0 footer / extension area are all out of scope for v0.0.1 — shallow, header-level parsing is the goal, not a full TGA codec.

Examples

Round-trip a header with no image ID

import { assertEquals } from "@std/assert";
import { tgaHeader, TGA_IMAGE_TYPE } from "@binstruct/tga";

const coder = tgaHeader();
const header = {
  idLength: 0,
  colorMapType: 0,
  imageType: TGA_IMAGE_TYPE.trueColor,
  colorMapFirstEntryIndex: 0,
  colorMapLength: 0,
  colorMapEntrySize: 0,
  xOrigin: 0,
  yOrigin: 0,
  width: 64,
  height: 32,
  pixelDepth: 24,
  imageDescriptor: 0,
  imageId: new Uint8Array(0),
};

const buffer = new Uint8Array(18);
const written = coder.encode(header, buffer);
const [decoded, read] = coder.decode(buffer);

assertEquals(written, 18);
assertEquals(read, 18);
assertEquals(decoded.width, 64);
assertEquals(decoded.height, 32);
assertEquals(decoded.imageType, TGA_IMAGE_TYPE.trueColor);