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

Creates a refiner for tRNS (transparency) chunks.

Converts between raw binary tRNS chunks and structured TrnsChunk representations. The refiner handles all three transparency formats (grayscale, RGB, indexed) by storing raw byte values that can be interpreted based on the image's color type.

Examples

Refining a grayscale transparency chunk

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: 2,
  type: new Uint8Array([116, 82, 78, 83]),
  data: new Uint8Array([128, 0]), // Gray level 32768 (0x8000) as u16be
  crc: 0xAABBCCDD,
};

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

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

Round-trip encoding and decoding

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

const refiner = trnsChunkRefiner();
const encodeContext = createContext("encode");
const decodeContext = createContext("decode");

const original: TrnsChunk = {
  length: 3,
  type: "tRNS",
  data: {
    values: [0, 128, 255], // Alpha values for indexed color
  },
  crc: 0x12345678,
};

const unrefined = refiner.unrefine(original, encodeContext);
const refined = refiner.refine(unrefined, decodeContext);

assertEquals(refined.type, original.type);
assertEquals(refined.data.values, original.data.values);

Return Type

A refiner that converts between PngChunkUnknown and TrnsChunk

Usage

import { trnsChunkRefiner } from ".";