https://github.com/ssnangua/sharp-bmp
Bmp encoder and decoder for sharp base on bmp-js.
https://github.com/ssnangua/sharp-bmp
bmp convert decode encode image sharp
Last synced: 8 days ago
JSON representation
Bmp encoder and decoder for sharp base on bmp-js.
- Host: GitHub
- URL: https://github.com/ssnangua/sharp-bmp
- Owner: ssnangua
- License: mit
- Created: 2022-08-03T20:06:40.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2022-08-04T15:49:30.000Z (over 3 years ago)
- Last Synced: 2025-10-23T15:11:10.723Z (3 months ago)
- Topics: bmp, convert, decode, encode, image, sharp
- Language: JavaScript
- Homepage:
- Size: 17.6 KB
- Stars: 7
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# sharp-bmp
Bmp encoder and decoder for [sharp](https://www.npmjs.com/package/sharp) base on [bmp-js](https://www.npmjs.com/package/bmp-js).
## Install
```bash
npm install sharp-bmp
```
## Usage
### Create an instance of sharp from a BMP image
```js
const bmp = require("sharp-bmp");
bmp.sharpFromBmp("input.bmp", {
// sharp constructor options
}) // returns an instance of sharp
.toFile("output.png");
```
### Write output image data to a BMP file
```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);
});
```
### Decode BMP
```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");
```
### Encode BMP
```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
})();
```
## Change Log
### 0.1.3
- Merge alpha transparency channel with a white background
### 0.1.4
- `sharpFromBmp(input, options)` support `Buffer` input.
### 0.1.5
- `sharpFromBmp(input, options, resolveWithObject)` add the third option, if `true`, will return an object with decoding info, default by `false`
- Update index.d.ts