PNG (Portable Network Graphics) file format support for binary structure encoding and decoding.
Basic PNG file encoding and decoding
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);
Creates a refiner for bKGD (background color) chunks.
Calculates the CRC32 checksum for PNG chunk data.
Creates a refiner for IDAT (Image Data) chunks.
Creates a refiner for IEND (Image End) chunks.
Creates a refiner for IHDR (Image Header) chunks.
Creates a refiner for PLTE (Palette) chunks.
| IhdrChunk
| PlteChunk
| TrnsChunk
| BkgdChunk
| IdatChunk
| IendChunk
Creates a coder for PNG chunks with type-specific refinement.
Creates a coder for unknown/generic PNG chunks.
| IhdrChunk
| PlteChunk
| TrnsChunk
| BkgdChunk
| IdatChunk
| IendChunk
Creates a coder for complete PNG files with automatic chunk refinement.
Creates a coder for PNG files with a custom chunk coder.
Creates a refiner for tRNS (transparency) chunks.
PNG bKGD (background color) chunk structure.
-
data: { values: number[]; }
Background color data
-
type: "bKGD"
Chunk type identifier, always "bKGD"
Refined IDAT (Image Data) chunk structure.
-
data: ZlibUncompressedData
Decompressed zlib data structure
-
type: "IDAT"
Chunk type identifier, always "IDAT"
Refined IEND (Image End) chunk structure.
-
crc: number
CRC checksum (always the same for IEND since data is empty)
-
type: "IEND"
Chunk type identifier, always "IEND"
Refined IHDR (Image Header) chunk structure.
-
data: { width: number; height: number; bitDepth: number; colorType: number; compressionMethod: number; filterMethod: number; interlaceMethod: number; }
Parsed image header data
-
type: "IHDR"
Chunk type identifier, always "IHDR"
Refined PLTE (Palette) chunk structure.
-
data: { colors: [number, number, number][]; }
Parsed palette data
-
type: "PLTE"
Chunk type identifier, always "PLTE"
Generic PNG chunk structure.
-
crc: number
CRC (4 bytes)
-
data: Uint8Array
Data (variable length)
-
length: number
Length of the data field in bytes (4-byte big-endian)
-
type: Uint8Array
Type (4 bytes)
PNG file structure containing signature and chunks.
-
chunks: TChunk[]
Array of PNG chunks in the file
-
signature: { highBitByte: number; signature: string; dosLineEnding: string; dosEOF: string; unixLineEnding: string; }
The 8-byte PNG signature: [137, 80, 78, 71, 13, 10, 26, 10]
PNG tRNS (transparency) chunk structure.
-
data: { values: number[]; }
Transparency data
-
type: "tRNS"
Chunk type identifier, always "tRNS"