BMP Encoder/Decoder for Node.js

0.1.0 · abandoned · verified Sun Apr 19

bmp-js is a pure JavaScript library for Node.js designed to encode and decode BMP image files. Currently at version 0.1.0, it supports decoding a wide range of BMP formats including 1-bit, 4-bit, 8-bit, 16-bit (555 and 565), 24-bit, and 32-bit images. Encoding, however, is limited to 24-bit BMPs without compression. Given its last recorded release was 0.1.0, the package appears to be in an unmaintained or abandoned state, lacking modern feature updates or active bug fixes. Its key differentiator is being a standalone, pure JavaScript solution without native dependencies, making it easy to integrate into Node.js projects requiring basic BMP manipulation. The project has not seen significant updates or major version releases, suggesting a stable but feature-limited and potentially outdated tool for image processing tasks.

Common errors

Warnings

Install

Imports

Quickstart

This example demonstrates how to read a BMP file, decode it using `bmp-js`, access and modify its raw pixel data (ABGR format), and then re-encode the modified data back into a new 24-bit BMP file.

const fs = require('fs');
const bmp = require('bmp-js');

// To run this example, ensure you have a 'bit24.bmp' file
// in the same directory, or create a dummy one.

async function processBmp() {
  try {
    // 1. Decode a BMP file
    const inputBuffer = fs.readFileSync('bit24.bmp');
    const bmpData = bmp.decode(inputBuffer);

    console.log('Decoded BMP properties:');
    console.log(`Width: ${bmpData.width}, Height: ${bmpData.height}`);
    console.log(`Bits Per Pixel: ${bmpData.bitPP}`);
    console.log(`Pixel Data Length: ${bmpData.data.length} bytes`);

    // 2. Modify pixel data (e.g., set top-left pixel to solid red)
    // The `data` property is a byte array ordered by ABGR ABGR ABGR.
    if (bmpData.data.length >= 4) {
      bmpData.data[0] = 0x00; // Alpha (ignored in most BMPs, set to 0)
      bmpData.data[1] = 0x00; // Blue
      bmpData.data[2] = 0x00; // Green
      bmpData.data[3] = 0xFF; // Red
    }

    // 3. Encode modified data back into a 24-bit BMP
    // Note: bmp-js currently only encodes to 24-bit BMP format.
    const outputBmpBuffer = bmp.encode({
      data: bmpData.data,
      width: bmpData.width,
      height: bmpData.height
    });

    fs.writeFileSync('output_modified.bmp', outputBmpBuffer.data);
    console.log('BMP decoded, modified top-left pixel, and re-encoded to output_modified.bmp');

  } catch (error) {
    console.error('Error processing BMP:', error.message);
    if (error.code === 'ENOENT' && error.message.includes('bit24.bmp')) {
      console.warn("Please ensure 'bit24.bmp' exists in the current directory to run this example.");
      console.warn("You can create a dummy 24-bit BMP or download one for testing.");
    }
  }
}

processBmp();

view raw JSON →