BMP Image Encoder/Decoder
bmp-ts is a pure TypeScript library designed for encoding and decoding BMP image files. It currently stands at version 1.0.9, with a release cadence focused on addressing bug fixes and minor enhancements. The library distinguishes itself by offering comprehensive support for all common BMP bit depths, including 1-bit, 4-bit, 8-bit, 16-bit, 24-bit, and 32-bit images. It provides granular control over BMP header properties during the encoding process and offers an optional `toRGBA` conversion during decoding for compatibility with other image processing libraries. While it supports various compression methods for decoding, a key limitation is the lack of compression support during encoding, meaning output files will always be uncompressed.
Common errors
-
TypeError: Cannot read properties of undefined (reading 'decode')
cause This error typically occurs in CommonJS environments when attempting to call `bmp.decode` (or `bmp.encode`) after importing `bmp-ts` without accessing the default export.fixIn CommonJS, explicitly use `.default`: `const bmp = require('bmp-ts').default;`. -
TypeError: bmp.decode is not a function
cause This can happen in ESM if `import * as bmp from 'bmp-ts';` is used, treating the module as a namespace object, when `decode` is a property of the default export, not a named export.fixUse `import bmp from 'bmp-ts';` to import the default export directly. -
Property 'data' is missing in type '{ data: Buffer; bitPP: number; width: number; height: number; }' but required in type 'BmpDataInput'.cause A TypeScript compilation error indicating that the object passed to `bmp.encode` is missing required properties or has incorrect types.fixEnsure the object passed to `bmp.encode` strictly conforms to the `BmpDataInput` interface, including `data` (Buffer), `width` (number), `height` (number), and `bitPP` (number). -
The image data is not correctly decoded and appears flipped vertically.
cause This is a known issue for bottom-up BMP images in versions prior to `v1.0.5`.fixUpgrade `bmp-ts` to version `1.0.5` or newer. If upgrading isn't possible, manual image data manipulation might be required to flip the image vertically after decoding.
Warnings
- breaking Version 1.0.0 introduced breaking changes to the API. Users upgrading from pre-1.0 versions will need to adapt their code to the new interface, which standardized the `decode` and `encode` methods.
- gotcha The `encode` function in `bmp-ts` currently does not support any compression methods. All output BMP files generated by encoding will be uncompressed, which can result in larger file sizes.
- gotcha CommonJS users must access the default export of `bmp-ts` via `.default` (e.g., `require('bmp-ts').default`). Directly using `require('bmp-ts')` will not expose the `decode` and `encode` methods directly, leading to runtime errors.
- gotcha Prior to version 1.0.5, `bmp-ts` had a bug that caused decoding errors or incorrect image data when processing bottom-up BMP images.
- gotcha Versions prior to 1.0.4 experienced issues with CommonJS module resolution, which could prevent the package from being imported correctly in certain Node.js environments.
Install
-
npm install bmp-ts -
yarn add bmp-ts -
pnpm add bmp-ts
Imports
- bmp
import { decode, encode } from 'bmp-ts';import bmp from 'bmp-ts';
- bmp (CommonJS)
const bmp = require('bmp-ts');const bmp = require('bmp-ts').default; - BmpData (Type)
import type { BmpData, BmpHeader } from 'bmp-ts';
Quickstart
import bmp from 'bmp-ts';
import * as fs from 'fs';
// --- Decoding a BMP image ---
// Ensure 'example.bmp' exists in the execution directory for this to run.
// You can replace this with any Buffer containing BMP data.
try {
const bmpBuffer = fs.readFileSync('example.bmp');
const decodedBmpData = bmp.decode(bmpBuffer, { toRGBA: true });
console.log('Decoded BMP Header:', decodedBmpData.header);
console.log('Decoded BMP Data length:', decodedBmpData.data.length);
console.log('Decoded BMP dimensions:', decodedBmpData.header.width, 'x', decodedBmpData.header.height);
} catch (error) {
console.error('Error decoding example.bmp:', error);
}
// --- Encoding a new BMP image ---
// Create a dummy image buffer (e.g., a 10x10 white image with alpha)
const width = 10;
const height = 10;
const pixelData = Buffer.alloc(width * height * 4, 255); // RGBA white pixels
const bmpDataToEncode = {
data: pixelData,
bitPP: 32, // 32-bit RGBA
width: width,
height: height
// Optional: other BMP header fields can be specified here.
};
const encodedRawData = bmp.encode(bmpDataToEncode);
fs.writeFileSync('./output.bmp', encodedRawData.data);
console.log('Encoded 10x10 32-bit white BMP saved to output.bmp');