Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

fast-bmp

image-js96.8kMIT3.0.0TypeScript support: included

A bmp image encoder

readme

fast-bmp

Zakodium logo

Maintained by Zakodium

NPM version Test coverage npm download

A library for encoding and decoding bmp image file format. References:

Supported features

This library only supports V5 headers.

  • binary (1-bit per pixel)
  • greyscale (8-bits per pixel)
  • RGB (24-bits per pixel)
  • RGBA (32-bits per pixel)

Usage

Encoding

import { encode } from 'fast-bmp';

// 0 0 0 0 0
// 0 1 1 1 0
// 0 1 0 1 0
// 0 1 1 1 0
// 0 0 0 0 0
const data = new Uint8Array([
  0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 1, 0, 1, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0,
]);
const imageData = {
  width: 5,
  height: 5,
  data,
  bitsPerPixel: 1,
  components: 1,
  channels: 1,
};
// Encode returns a Uint8Array.
const encoded = encode(imageData);
fs.writeFileSync('image.bmp', encoded);

Decoding

import { decode } from 'fast-bmp';

// 0 0 0 0 0
// 0 1 1 1 0
// 0 1 0 1 0
// 0 1 1 1 0
// 0 0 0 0 0
const buffer = fs.writeFileSync('image.bmp');
const imageData = decode(buffer);
/* Returns object:
{
width: 5,
height: 5,
data: new Uint8Array([
    0, 0, 0, 0, 0, 
    0, 1, 1, 1, 0, 
    0, 1, 0, 1, 0, 
    0, 1, 1, 1, 0, 
    0, 0, 0, 0, 0,
  ]),
bitsPerPixel: 1,
components: 1,
channels: 1,
colorMasks: [0x00ff0000, 0x0000ff00, 0x000000ff],
compression: 0,
xPixelsPerMeter: 2835,
yPixelsPerMeter: 2835,
}
*/

changelog

Changelog

3.0.0 (2025-04-15)

⚠ BREAKING CHANGES

  • renamed bitDepth to bitsPerPixel
  • In decoder's output data and encoder's input data, each pixel is now stored as 1 byte per pixel instead of 1 bit per pixel.

Features

  • add 1-bit image decoder (0607b6b)
  • add support for Greyscale, RGB and RGBA images (3345bce)
  • encode and decode resolution fields xPixelsPerMeter and yPixelsPerMeter (f239678)

2.0.1 (2022-09-29)

Bug Fixes

  • move eslint-config-cheminfo to dev dependencies (5b673b5)

2.0.0 (2022-07-31)

⚠ BREAKING CHANGES

  • always return a Uint8Array instead of Buffer (#5)

Miscellaneous Chores

  • always return a Uint8Array instead of Buffer (#5) (e8e7a36)

1.0.2 (2022-07-31)

Bug Fixes

1.0.0 (2016-12-15)

Bug Fixes

  • encode: fix an edge case (1eac38c)
  • encode: return a Buffer if node.js, Uint8Array otherwise (d7ea165)