Code Monkey home page Code Monkey logo

bitmap-js's Introduction

Bitmap.js

Build Status

Read and create bitmaps in Node.js

Read a bitmap file

const { readBitmapFile } = require('bitmap-js');

const bitmapFile = await m.readBitmapFile("filename.bmp");
// { fileHeader: { filesize: 70, imageDataOffset: 62 },
//   dibHeader:
//    { headerLength: 40,
//      headerType: 'BITMAPINFOHEADER',
//      width: 2,
//      height: 2,
//      bitsPerPixel: 1,
//      compressionType: 0,
//      bitmapDataSize: 8,
//      numberOfColorsInPalette: 2,
//      numberOfImportantColors: 0 },
//   imageData: <Buffer 80 00 00 00 40 00 00 00>,
//   colorTable: <Buffer ff 00 ff 00 00 ff ff 00> }

Read a bitmap file to access header data, image pixel data, and the color table.

The image data is a binary representation stored in a buffer. The format of the data depends on the bits per pixel of the bitmap and any compression algorithm applied to it.

The color table is a binary representation of the colors used in the bitmap. Each color occupies four bytes in the format BLUE GREEN RED 0x00

Create a 1 bit-per-pixel bitmap file

createBitmapFile() creates a bitmap file at a given filename with a few required arguments.

imageData is a binary Buffer representing the pixel array, the format of which varies depending on the number of bits-per-pixel (bpp). In a 1 bpp bitmap, each bit represents whether a pixel is the first color or the second color in the palette.

imageData must include padding at the end of each pixel row. Use the padImageData() function to avoid adding the padding manually.

const { padImageData, createBitmapFile } = require('bitmap-js');

const width = 8;
const height = 6;
const colorTable = Buffer.from([
  0xFF, 0x00, 0xFF, 0x00,
  0x00, 0xFF, 0xFF, 0x00
]);

const imageData = padImageData({
  unpaddedImageData: Buffer.from([
    0b00000000,
    0b00111100,
    0b01000010,
    0b00000000,
    0b00100100,
    0b00000000
  ]),
  width,
  height
});

await createBitmapFile({
  filename: "smiley.bmp",
  imageData,
  width,
  height,
  bitsPerPixel: 1,
  colorTable
});

The above code generates a 6x8 bitmap like this:

smiley

Create a 24 bit-per-pixel bitmap file

In a 24 bit-per-pixel bitmap file, each pixel occupies three bytes of space: one for the green, blue, and red value of the pixel.

const { padImageData, createBitmapFile } = require('bitmap-js');

const width = 2;
const height = 2;

const imageData = padImageData({
  unpaddedImageData: Buffer.from([
    0xFF, 0x00, 0xFF, 0xFF, 0xFF, 0xFF,
    0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0xFF
  ]),
  width,
  height
});

await createBitmapFile({
  filename: "checkers.bmp",
  imageData,
  width,
  height,
  bitsPerPixel: 24
});

The above code generates a 2x2 bitmap like this:

smiley

bitmap-js's People

Contributors

ericandrewlewis avatar

Stargazers

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

Watchers

 avatar  avatar  avatar

bitmap-js's Issues

feature request: read bitmap file from ArrayBuffer, for browsers

I'm perusing for tools to read bitmap file content, and stumbled on this. Neat!

In the web, reading a bitmap file can yield an ArrayBuffer with the binary data, like this:

const response = await fetch('/path/to/file.bmp')
const data = response.arrayBuffer()

I've no experience with bitmaps (other than starting to peruse how to read the data just now). I've tried steps in this guide with no luck: https://www.i-programmer.info/projects/36-web/6234-reading-a-bmp-file-in-javascript.html

But when I send the data to the GPU (WebGL), I get a black texture, so I'm doing something wrong somewhere.

Anyway, it'd be neat if this lib would detect a browser environment, and in that case either accept an ArrayBuffer object, or a URL and fetch it to an ArrayBuffer, and finally create the same sort of meta object with the same content (but ArrayBuffers or UInt8Arrays inside the object instead of Buffers).

Feature Request: createBitmapBuffer

createBitmapBuffer if the same createBitmapFile, but it returns the buffer instead of writing the bitmp to a a file, I need it to return a buffer.

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.