default
BMP/DIB image file format encoding and decoding utilities using binary structures.
Round-trip a tiny 2×2 24bpp BMP
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);
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(): number
width: number,
height: number,
bpp: number
Computes the total pixel-data size for a BITMAPINFOHEADER, in bytes.
f
rowStride(): number
width: number,
bpp: number
Computes the BMP row stride in bytes for a given pixel width and bit depth.
I
BitmapFileHeader
BITMAPFILEHEADER — the fixed 14-byte preamble of a BMP/DIB file.
I
BitmapInfoHeader
BITMAPINFOHEADER — the 40-byte DIB header used by virtually all modern BMP files.
I
BmpFile
Complete BMP file consisting of a BitmapFileHeader, a BitmapInfoHeader, and raw pixel data.