GithubHelp home page GithubHelp logo

bmp-js's Introduction

bmp-js

A pure javascript Bmp encoder and decoder for node.js

Supports all bits decoding(1,4,8,16,24,32) and encoding with 24bit.

Install

npm install bmp-js

How to use

Decode BMP

var bmp = require("bmp-js");
var bmpBuffer = fs.readFileSync('bit24.bmp');
var bmpData = bmp.decode(bmpBuffer);

bmpData has all properties, including:

  1. fileSize
  2. reserved
  3. offset
  4. headerSize
  5. width
  6. height
  7. planes
  8. bitPP
  9. compress
  10. rawSize
  11. hr
  12. vr
  13. colors
  14. importantColors
  15. palette
  16. data a. This is a byte array b. The bytes are ordered as follows: ABGR (alpha, blue, green, red) c. 4 bytes represent 1 pixel

Encode RGB

var bmp = require("bmp-js");
var fs = require("fs");
var bmpData = {
    data, //Buffer
    width, //Number
    height //Number
};
var rawData = bmp.encode(bmpData); //defaults to no compression
fs.WriteFileSync('./image.bmp', rawData.data);

License

You can use for free with MIT License

bmp-js's People

Contributors

chriszimmerman avatar gaubee avatar omardelarosa avatar pablodgonzalez avatar pilaas2 avatar rkorszun avatar shaozilee avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

bmp-js's Issues

Decode piped bmp

I am piping a bmp from fluent-ffmpeg through a stream to bmp-js (to decode it) but I keep getting Index out of range errors. It still outputs a bmpData object with the correct width and height though.

Also, do you know how I would go about taking bmpData.data and displaying it on an html5 canvas?

Thank you.

BMP 32 support

Please add bit32 BMP support for decoding:

BmpDecoder.prototype.bit32 = function() {
  //when height > 0
  for (var y = this.height - 1; y >= 0; y--) {
    for (var x = 0; x < this.width; x++) {
      var blue = this.buffer.readUInt8(this.pos++);
      var green = this.buffer.readUInt8(this.pos++);
      var red = this.buffer.readUInt8(this.pos++);
      var alpha = this.buffer.readUInt8(this.pos++);
      var location = y * this.width * 4 + x * 4;
      this.data[location] = red;
      this.data[location + 1] = green;
      this.data[location + 2] = blue;
      this.data[location + 3] = alpha;
    }
    //skip extra bytes
    this.pos += (this.width % 4);
  }

};

Thanks.

NPM package: --use_strict mode

Hi man, thanks for the great lib!

I just wanted to ask when the node --use_strict mode will be supported?
If you use the latest version of this lib (0.0.1 on NPM), it throws and exception:

module.exports = encode = function(imgData, quality) {
                        ^
ReferenceError: encode is not defined
    at Object.<anonymous> (/home//node_modules/bmp-js/lib/encoder.js:75:25)

I saw that recent commit (88cda18) resolves this bug, but NPM package hasn't yet been updated.

Is there going to be an NPM update solving this bug?

Doesn't properly decode bmp

Hi, I realized that bmp-js doesn't seem to handle correctly any bmp generated by Gimp.

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

const bmpBuffer = fs.readFileSync("input.bmp");
const bmpData = bmp.decode(bmpBuffer);

fs.writeFileSync("output.bmp", bmp.encode(bmpData).data);
$ file input.bmp output.bmp
input.bmp:  PC bitmap, Windows 98/2000 and newer format, 640 x 480 x 24, cbSize 921738, bits offset 138
output.bmp: PC bitmap, Windows 3.x format, 640 x -480 x 24, image size 921600, cbSize 921654, bits offset 54

Top: input.bmp, Bottom: output.bmp

diff

Steps to reproduce:

  • Export a bmp image from gimp (exporting with 24 bits seems to offset horizontally the image like in my case, using any 32 bits format seems to taint output.bmp in red or aqua on top of that)
    image
  • Run the above script

Image Size

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

const buffer = fs.readFileSync('./exampleImages/windows95.bmp')
const data = bmp.decode(buffer)

const outBuffer = bmp.encode(data)
fs.writeFileSync('test.bmp', outBuffer.data)

console.log(buffer.byteLength, data.data.byteLength, outBuffer.data.byteLength)

OUTPUT:

129078 512000 384054

almost a 3x increase!

.bpm to 1 bit image error

Not sure why but I am not able to convert the following image to 1 bit BMP. I made sure both the old and new have the same dimensions 150x150 but I am not sure what is going on. Any help is appreciated. Thanks!
image

app.get('/', (req, res) =>
 {
	const bmpBuffer = fs.readFileSync('test3.bmp');
	//var Data = bmp.decode(bmpBuffer);
	const bmpData = {
		data: Data,
		bitPP: 1,
		width: 150,
		height: 150
	};

	const rawData = bmp.encode(bmpData);
	fs.writeFileSync('test/image.bmp', rawData.data);
});

Ordering from top-bottom or bottom-top

I discovered this via Jimp, but posting here seems more directly relevant.

I didn't know this, but apparently the data in a BMP can be ordere top-to-bottom or bottom-to-top. This would be visible in the biHeight.
My MacOS doesn't seem to care either way, but when I tried displaying a BMP generated through Jimp (via bmp-js) on a low-resolution display through a C library (RaspberryPi), it failed and reported a negative image height. When I re-converted the image with XnConvert, it did work.

Console log from the C library:
BMP_cfSize:1800054
BMP_cfoffBits:54
BMP_ciSize:40
BMP_ciWidth:1200
BMP_ciHeight:-500 <---------------------
//etc.
total_length:1800000,1800000
bytesPerLine = 3600
imageSize = -1800000

When I posted on StackOverflow, GregHNZ pointed me towards the negative height in encoder.js. I changed this to positive and now the image displays through the C library on the low-res screen just fine.
I just had to flip the image in Jimp with image.flip(false, true) to maintain the right orientation.

I don't know if this is intended behaviour, and maybe this is a real edge case, but maybe good to document, and perhaps turn into a variable?

Is this repo still active?

Hi!

Is this repo still maintained? Quite a few issues and pull requests have piled up, any chance to have those looked at anytime soon? If not then is there any alternatives to your code?

Thank you in advance!

Provide more information on the BMP

Would you accept a pull request that changes the output of decode that instead of returning only the width, height and data it would return all the information that is available?

The reason I ask is that I have been using a modified version of your bmp-js to investigate BMP images and found it quite helpful and would like to be able to get at the header values in JS.

Encode input assumed to be 32bpp

Trying to encode a buffer of RGB24 data results in garbled output. BmpEncoder always skips a byte of the input buffer after handling a pixel (apparently assuming there is an alpha channel).

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.