interface TrnsChunk
extends Omit<PngChunkUnknown, "type" | "data">

PNG tRNS (transparency) chunk structure.

The tRNS chunk specifies transparency information for images without a full alpha channel. The interpretation of the data depends on the image's color type (from IHDR chunk):

  • Color type 0 (Grayscale): 2 bytes representing a single gray level value (u16be). Pixels matching this gray level are fully transparent, all others are opaque.

  • Color type 2 (RGB): 6 bytes representing RGB values (3x u16be). Pixels matching this RGB color are fully transparent, all others are opaque.

  • Color type 3 (Indexed): N bytes (1-256) representing alpha values for palette entries. Each byte is a u8 alpha value: 0 = fully transparent, 255 = fully opaque. Missing entries default to fully opaque.

The tRNS chunk must appear after PLTE (if present) and before the first IDAT chunk. It is prohibited for color types 4 and 6, which already have full alpha channels.

Examples

Decoding indexed color transparency

import { assertEquals } from "@std/assert";
import { createContext } from "@hertzg/binstruct";
import type { PngChunkUnknown } from "../mod.ts";
import { trnsChunkRefiner } from "./trns.ts";

const refiner = trnsChunkRefiner();
const context = createContext("decode");

const unknownChunk: PngChunkUnknown = {
  length: 3,
  type: new Uint8Array([116, 82, 78, 83]), // "tRNS"
  data: new Uint8Array([0, 128, 255]), // 3 alpha values
  crc: 0x12345678,
};

const refined = refiner.refine(unknownChunk, context);

assertEquals(refined.type, "tRNS");
assertEquals(refined.data.values, [0, 128, 255]);

Properties

type: "tRNS"

Chunk type identifier, always "tRNS"

data: { values: number[]; }

Transparency data

Usage

import { type TrnsChunk } from ".";