A pure TypeScript BMP encoder and decoder with zero dependencies.
- 🚀 Pure TypeScript - no native dependencies
- 📦 Zero dependencies
- 🎨 Supports multiple bit depths: 1, 4, 8, 16, 24, 32 bits
- 🗜️ RLE compression support (RLE4 and RLE8)
- 🔄 Encode and decode BMP images
- 📐 Handles both bottom-up and top-down BMPs
bun add ts-bmp
# or
npm install ts-bmpimport { decode } from 'ts-bmp'
const buffer = await Bun.file('image.bmp').arrayBuffer()
const { data, width, height } = decode(new Uint8Array(buffer))
// data is RGBA pixel data (4 bytes per pixel)
console.log(`Image size: ${width}x${height}`)import { encode } from 'ts-bmp'
const imageData = {
data: new Uint8Array(width * height * 4), // RGBA pixel data
width: 100,
height: 100,
}
// Encode as 32-bit BMP (with alpha channel)
const bmpBuffer = encode(imageData, { bitsPerPixel: 32 })
await Bun.write('output.bmp', bmpBuffer)
// Or encode as 24-bit BMP (no alpha)
const bmp24 = encode(imageData, { bitsPerPixel: 24 })Decodes a BMP image buffer to RGBA pixel data.
Returns:
data: Uint8Array- RGBA pixel data (4 bytes per pixel)width: number- Image width in pixelsheight: number- Image height in pixels
Encodes RGBA pixel data to BMP format.
Options:
bitsPerPixel: 24 | 32- Bits per pixel (default: 32)
- 1-bit monochrome
- 4-bit indexed (with RLE4 compression)
- 8-bit indexed (with RLE8 compression)
- 16-bit RGB (with bit field support)
- 24-bit RGB
- 32-bit RGBA
- 24-bit RGB
- 32-bit RGBA (with alpha channel support)
MIT