default

BMP/DIB image file format encoding and decoding utilities using binary structures.

Examples

Round-trip a tiny 2×2 24bpp BMP

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

const coder = bmp();
const image = {
  file: {
    signature: "BM",
    fileSize: 70,
    reserved1: 0,
    reserved2: 0,
    pixelOffset: 54,
  },
  dib: {
    size: 40,
    width: 2,
    height: 2,
    planes: 1,
    bpp: 24,
    compression: 0,
    imageSize: 16,
    xPixelsPerMeter: 2835,
    yPixelsPerMeter: 2835,
    colorsUsed: 0,
    importantColors: 0,
  },
  // 2 rows × stride 8 = 16 bytes of BGR pixels with 2 bytes padding per row.
  // deno-fmt-ignore
  pixelData: new Uint8Array([
    0x00, 0x00, 0xff,  0xff, 0x00, 0x00,  0x00, 0x00,
    0x00, 0xff, 0x00,  0xff, 0xff, 0xff,  0x00, 0x00,
  ]),
};

const buffer = new Uint8Array(image.file.fileSize);
const bytesWritten = coder.encode(image, buffer);
const [decoded, bytesRead] = coder.decode(buffer);

assertEquals(bytesWritten, 70);
assertEquals(bytesRead, 70);
assertEquals(decoded.file.signature, "BM");
assertEquals(decoded.dib.bpp, 24);
assertEquals(decoded.pixelData, image.pixelData);

Functions

f
bitmapFileHeader(): Coder<BitmapFileHeader>

Creates a coder for the 14-byte BITMAPFILEHEADER.

f
bitmapInfoHeader(): Coder<BitmapInfoHeader>

Creates a coder for the 40-byte BITMAPINFOHEADER (the most common DIB header).

f
bmp(): Coder<BmpFile>

Creates a coder for a complete BMP file with a BITMAPINFOHEADER and no palette.

f
pixelDataSize(
width: number,
height: number,
bpp: number
): number

Computes the total pixel-data size for a BITMAPINFOHEADER, in bytes.

f
rowStride(
width: number,
bpp: number
): number

Computes the BMP row stride in bytes for a given pixel width and bit depth.

Interfaces