-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feature request: bmp format image support #806
Comments
Hello, please see #543 |
Thanks I used jimp to handle bmp format images. |
here is the link: https://github.com/oliver-moran/jimp
|
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:
$ npm install @vingle/bmp-js --save
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! |
I added an adapter to bmp-js for sharp, it's easy to use. See: https://github.com/ssnangua/sharp-bmp
npm install sharp-bmp
const bmp = require("sharp-bmp");
bmp.sharpFromBmp("input.bmp", {
// sharp constructor options
}) // returns an instance of sharp
.toFile("output.png");
const sharp = require("sharp");
const bmp = require("sharp-bmp");
const image = sharp("input.jpg");
bmp.sharpToBmp(image, "output.bmp")
.then((info) => {
console.log(info); // { size, width, height }
})
.catch((err) => {
console.error(err);
}); Hope this helps :-) |
I used https://github.com/mooyoul/bmp-js already has the option to convert the BRG to RGBA
see more in shaozilee/bmp-js#19 |
Hi @mooyoul, thanks for your code snippet! Can you also share another version that accepts stream instead of buffer? Is it possible that the method accepts a stream, and detects if it's BMP type? If so, then use BMP-js to decode and pipe to another stream. If not then pipe to the other stream directly without using bmp-js? Thanks for your sharing! |
Great work man. |
this is a feature request,
would this lovely project support bmp format image manipulation?
The text was updated successfully, but these errors were encountered: