default

PNG (Portable Network Graphics) file format support for binary structure encoding and 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);

Functions

f
bkgdChunkRefiner(): Refiner<PngChunkUnknown, BkgdChunk, []>

Creates a refiner for bKGD (background color) chunks.

f
idatChunkRefiner(): Refiner<PngChunkUnknown, IdatChunk, []>

Creates a refiner for IDAT (Image Data) chunks.

f
iendChunkRefiner(): Refiner<PngChunkUnknown, IendChunk, []>

Creates a refiner for IEND (Image End) chunks.

f
ihdrChunkRefiner(): Refiner<PngChunkUnknown, IhdrChunk, []>

Creates a refiner for IHDR (Image Header) chunks.

f
plteChunkRefiner(): Refiner<PngChunkUnknown, PlteChunk, []>

Creates a refiner for PLTE (Palette) chunks.

f
pngChunkUnknown(): Coder<PngChunkUnknown>

Creates a coder for unknown/generic PNG chunks.

f
trnsChunkRefiner(): Refiner<PngChunkUnknown, TrnsChunk, []>

Creates a refiner for tRNS (transparency) chunks.

Interfaces

I
BkgdChunk

PNG bKGD (background color) chunk structure.

I
IdatChunk

Refined IDAT (Image Data) chunk structure.

I
IendChunk

Refined IEND (Image End) chunk structure.

I
PlteChunk

Refined PLTE (Palette) chunk structure.

I
PngChunkUnknown

Generic PNG chunk structure.

I
PngFile

PNG file structure containing signature and chunks.

I
TrnsChunk

PNG tRNS (transparency) chunk structure.