PNG (Portable Network Graphics) file format support for binary structure encoding and decoding.

This module provides coders for PNG files and their constituent chunks, enabling efficient binary serialization and deserialization of PNG data structures.

The PNG format consists of a signature followed by a series of chunks, where each chunk contains length, type, data, and CRC fields. This module handles the complete PNG file structure as well as individual chunk encoding/decoding.

Examples

Basic PNG file encoding and decoding

import { assertEquals } from "@std/assert";
import { pngFile, type PngChunkUnknown } from "@binstruct/png";

const pngCoder = pngFile();
const testPng = {
  signature: {
    highBitByte: 137,
    signature: "PNG",
    dosEOF: "\u001a",
    dosLineEnding: "\r\n",
    unixLineEnding: "\n"
  },
  chunks: [
    {
      length: 13,
      type: new Uint8Array([73, 72, 68, 82]), // "IHDR"
      data: new Uint8Array([0, 0, 0, 1, 0, 0, 0, 1, 8, 2, 0, 0, 0]),
      crc: 0x12345678,
    } as PngChunkUnknown,
  ],
};

const buffer = new Uint8Array(100);
const bytesWritten = pngCoder.encode(testPng, buffer);
const [decodedPng, bytesRead] = pngCoder.decode(buffer);

assertEquals(bytesRead, 93);
assertEquals(decodedPng.signature, testPng.signature);
assertEquals(decodedPng.chunks.length, 6);