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

PNG bKGD (background color) chunk structure.

The bKGD chunk specifies a default background color for displaying PNG images. The interpretation of the data depends on the image's color type (from IHDR chunk):

  • Color type 0, 4 (Grayscale): 2 bytes representing a gray level value (u16be). This value should be used as the background when displaying the image.

  • Color type 2, 6 (RGB): 6 bytes representing RGB values (3x u16be). These values specify the background red, green, and blue components.

  • Color type 3 (Indexed): 1 byte representing a palette index (u8). This index references an entry in the PLTE chunk to use as background.

The bKGD chunk must appear before the first IDAT chunk. For indexed color (type 3), it must also appear after the PLTE chunk.

Examples

Decoding indexed color background

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

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

const unknownChunk: PngChunkUnknown = {
  length: 1,
  type: new Uint8Array([98, 75, 71, 68]), // "bKGD"
  data: new Uint8Array([5]), // Palette index 5
  crc: 0x12345678,
};

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

assertEquals(refined.type, "bKGD");
assertEquals(refined.data.values, [5]);

Decoding RGB background

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

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

const unknownChunk: PngChunkUnknown = {
  length: 6,
  type: new Uint8Array([98, 75, 71, 68]), // "bKGD"
  data: new Uint8Array([0, 255, 0, 255, 0, 255]), // White RGB
  crc: 0xAABBCCDD,
};

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

assertEquals(refined.type, "bKGD");
assertEquals(refined.data.values.length, 6);
assertEquals(refined.data.values, [0, 255, 0, 255, 0, 255]);

Decoding grayscale background

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

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

const unknownChunk: PngChunkUnknown = {
  length: 2,
  type: new Uint8Array([98, 75, 71, 68]), // "bKGD"
  data: new Uint8Array([171, 132]), // Gray level 43908 as u16be
  crc: 0x99887766,
};

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

assertEquals(refined.type, "bKGD");
assertEquals(refined.data.values, [171, 132]);

Properties

type: "bKGD"

Chunk type identifier, always "bKGD"

data: { values: number[]; }

Background color data