function pngFileChunks
pngFileChunks<
TChunk = ReturnType<pngChunkRefined> extends Coder<infer T> ? T : never
>
(chunkCoder?: Coder<TChunk>): Coder<PngFile<TChunk>>

Creates a coder for PNG files with a custom chunk coder.

This function allows you to specify how individual chunks should be encoded/decoded, enabling custom chunk handling or type-specific refinement.

Omitting the chunk coder is the common case: it defaults to pngChunkRefined, which makes pngFileChunks() behave exactly like pngFile. Pass an explicit coder when you want different chunk handling — pngChunkUnknown to keep every chunk as raw bytes, or your own refiner pipeline for custom chunk types.

Examples

Create a PNG file coder with unknown chunks

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

const coder = pngFileChunks(pngChunkUnknown());
const png = {
  signature: {
    highBitByte: 137,
    signature: "PNG",
    dosLineEnding: "\r\n",
    dosEOF: "\u001a",
    unixLineEnding: "\n",
  },
  chunks: [] as PngChunkUnknown[],
};

const buffer = new Uint8Array(8);
const bytesWritten = coder.encode(png, buffer);

assertEquals(bytesWritten, 8); // Just the signature

Round-trip using the default refined chunk coder

import { assertEquals } from "@std/assert";
import { pngFileChunks } from "@binstruct/png";

const coder = pngFileChunks();
const png = {
  signature: {
    highBitByte: 137,
    signature: "PNG",
    dosLineEnding: "\r\n",
    dosEOF: "\u001a",
    unixLineEnding: "\n",
  },
  chunks: [
    { length: 0, type: "IEND" as const, crc: 0xAE426082 },
  ],
};

const buffer = new Uint8Array(20);
const bytesWritten = coder.encode(png, buffer);
const [decoded, bytesRead] = coder.decode(buffer.subarray(0, bytesWritten));

assertEquals(bytesRead, bytesWritten);
assertEquals(decoded.chunks[0].type, "IEND");

Type Parameters

TChunk = ReturnType<pngChunkRefined> extends Coder<infer T> ? T : never

The type of chunks in the PNG file. Inferred from chunkCoder, and defaults to the refined chunk union that pngChunkRefined produces.

Parameters

optional
chunkCoder: Coder<TChunk>

The coder to use for encoding/decoding individual chunks. Defaults to pngChunkRefined, the same chunk coder pngFile passes. Override it when the refined chunk types are not what you want, for example pngChunkUnknown() to inspect or rewrite chunks at the byte level.

Return Type

Coder<PngFile<TChunk>>

A coder for PngFile structures with the specified chunk type.