Sharp: feature request: bmp format image support

Created on 13 May 2017  路  6Comments  路  Source: lovell/sharp

this is a feature request,

would this lovely project support bmp format image manipulation?

question

Most helpful comment

Unfortunately, decoded channel order of bmpjs is BGR order, which is not compatible with sharp (sharp uses RGB order - RGBA if 4 channels defined.)

So we've forked bmp-js to provide RGBA option, with improved bitmap format compatibility. (tested with 1/2/4/8/16/24/32bit bitmap image)

Forked repo: https://github.com/balmbees/bmp-js

Just follow below steps to load bitmap images:

  1. install @vingle/bmp-js package
$ npm install @vingle/bmp-js --save
  1. Change your code like this:

Old:

import * as sharp from "sharp";

async function transform(input: Buffer): Promise<Buffer> {
  const sharpInstance = sharp(input);

   // do stuff...

  return await sharpInstance.jpeg().toBuffer();
}

New:

import * as bmp from "@vingle/bmp-js";
import * as sharp from "sharp";

const BUF_BMP = Buffer.from([0x42, 0x4d]); // "BM" file signature

function isBitmap(buf: Buffer): boolean {
  return Buffer.compare(BUF_BMP, buf.slice(0, 2)) === 0;
}

async function transform(input: Buffer): Promise<Buffer> {
  const sharpInstance = (() => {
    if (isBitmap(buf)) {
      const bitmap = bmp.decode(buf, true);
      return sharp(bitmap.data, {
        raw: {
          width: bitmap.width,
          height: bitmap.height,
          channels: 4,
        },
      });
    }
    return sharp(buf);
  })();

  // do stuff..

  return await sharpInstance.jpeg().toBuffer();
}

Hope this helps!

All 6 comments

Hello, please see #543

Thanks I used jimp to handle bmp format images.

here is the link: https://github.com/oliver-moran/jimp

read a bmp file, then export it as a jpeg buffer, then send it to sharp

I believe https://github.com/shaozilee/bmp-js is the underlying library used by jimp.

It might be possible to remove the jpeg compression round-trip by using it directly with sharp, something like (untested):

const bmpDecoded = bmp.decode(bmpBuffer);

sharp(bmpDecoded.data, { raw: {
  width: bmpDecoded.width,
  height: bmpDecoded.height,
  channels: 4
}})...

Great, thanks!

Unfortunately, decoded channel order of bmpjs is BGR order, which is not compatible with sharp (sharp uses RGB order - RGBA if 4 channels defined.)

So we've forked bmp-js to provide RGBA option, with improved bitmap format compatibility. (tested with 1/2/4/8/16/24/32bit bitmap image)

Forked repo: https://github.com/balmbees/bmp-js

Just follow below steps to load bitmap images:

  1. install @vingle/bmp-js package
$ npm install @vingle/bmp-js --save
  1. Change your code like this:

Old:

import * as sharp from "sharp";

async function transform(input: Buffer): Promise<Buffer> {
  const sharpInstance = sharp(input);

   // do stuff...

  return await sharpInstance.jpeg().toBuffer();
}

New:

import * as bmp from "@vingle/bmp-js";
import * as sharp from "sharp";

const BUF_BMP = Buffer.from([0x42, 0x4d]); // "BM" file signature

function isBitmap(buf: Buffer): boolean {
  return Buffer.compare(BUF_BMP, buf.slice(0, 2)) === 0;
}

async function transform(input: Buffer): Promise<Buffer> {
  const sharpInstance = (() => {
    if (isBitmap(buf)) {
      const bitmap = bmp.decode(buf, true);
      return sharp(bitmap.data, {
        raw: {
          width: bitmap.width,
          height: bitmap.height,
          channels: 4,
        },
      });
    }
    return sharp(buf);
  })();

  // do stuff..

  return await sharpInstance.jpeg().toBuffer();
}

Hope this helps!

Was this page helpful?
0 / 5 - 0 ratings