bmp(): Coder<BmpFile>
Creates a coder for a complete BMP file with a BITMAPINFOHEADER and no palette.
The pixel-data length is derived from the DIB header's width, height,
and bpp (see rowStride); the imageSize field is ignored, since
BI_RGB encoders commonly leave it at zero.
Use this for typical 16/24/32-bpp uncompressed images. For palette-indexed
images or non-zero compression values, compose bitmapFileHeader,
bitmapInfoHeader, and your own pixel/palette coders.
Round-trip a 2×2 32bpp top-down image
Round-trip a 2×2 32bpp top-down image
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, // top-down planes: 1, bpp: 32, compression: 0, imageSize: 16, xPixelsPerMeter: 0, yPixelsPerMeter: 0, colorsUsed: 0, importantColors: 0, }, // deno-fmt-ignore pixelData: new Uint8Array([ 0x00, 0x00, 0xff, 0xff, 0xff, 0x00, 0x00, 0xff, 0x00, 0xff, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, ]), }; 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.dib.height, -2); assertEquals(decoded.pixelData, image.pixelData);