Bmp encoder and decoder for sharp base on bmp-js.
npm install sharp-bmpBmp encoder and decoder for sharp base on bmp-js.
``bash`
npm install sharp-bmp
`js
const bmp = require("sharp-bmp");
bmp.sharpFromBmp("input.bmp", {
// sharp constructor options
}) // returns an instance of sharp
.toFile("output.png");
`
`js
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);
});
`
`js
const fs = require("fs");
const sharp = require("sharp");
const bmp = require("sharp-bmp");
const buffer = fs.readFileSync("input.bmp");
const bitmap = bmp.decode(buffer);
sharp(bitmap.data, {
raw: {
width: bitmap.width,
height: bitmap.height,
channels: 4,
},
})
.toFile("output.png");
`
`js
const fs = require("fs");
const sharp = require("sharp");
const bmp = require("sharp-bmp");
(async () => {
const image = sharp("input.jpg");
const { data, info } = await image
// If the image has alpha transparency channel
.flatten({ background: "#ffffff" })
// If the image has no alpha transparency channel
.ensureAlpha()
.raw()
.toBuffer({ resolveWithObject: true });
const bitmap = {
data,
width: info.width,
height: info.height,
};
const rawData = bmp.encode(bitmap);
fs.writeFileSync("output.bmp", rawData.data);
console.log(rawData.data.length); // size of output.bmp
})();
`
- Merge alpha transparency channel with a white background
- sharpFromBmp(input, options) support Buffer input.
- sharpFromBmp(input, options, resolveWithObject) add the third option, if true, will return an object with decoding info, default by false`
- Update index.d.ts