Giter VIP home page Giter VIP logo

bmp-ts's Introduction

bmp-ts

A pure typescript bmp encoder and decoder.

Codecov code style: prettier

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

Install

npm install bmp-ts

Usage

Decoding

decode will return an object that includes all the header properties of the bmp image file and the data. See header definition below.

const bmp = require('bmp-ts').default;
const bmpBuffer = fs.readFileSync('bit24.bmp');
const bmpData = bmp.decode(bmpBuffer);

Options

  • toRGBA - switch the output to big endian RGBA, making it compatible with other libraries like pngjs
const bmp = require('bmp-ts').default;
const bmpBuffer = fs.readFileSync('bit24.bmp');
const bmpData = bmp.decode(bmpBuffer, { toRGBA: true });

Supported Compression Methods

Currently compression is only supported during decoding. The following methods are implemented:

  • NONE - Most common
  • BI_RLE8 - Can be used only with 8-bit/pixel bitmap
  • BI_RLE4 - Can be used only with 4-bit/pixel bitmaps
  • BI_BIT_FIELDS - Huffman 1D - BITMAPV2INFOHEADER: RGB bit field masks, BITMAPV3INFOHEADER+: RGBA
  • BI_ALPHA_BIT_FIELDS - RGBA bit field masks - only Windows CE 5.0 with .NET 4.0 or later

Encoding

To encode an image all you need is a buffer with the image data, the height and the width. You can specify the bit depth of the output image by modifying bitPP. If you do not provide a value, the output image defaults to 24-bit.

All header fields are valid options to encode and will be encoded into the header.

const bmp = require('bmp-ts').default;
const fs = require('fs');
const bmpData = {
  data, // Buffer
  bitPP: 1 | 2 | 4 | 16 | 24 | 32, // The number of bits per pixel
  width, // Number
  height, // Number
};

// Compression is not supported
const rawData = bmp.encode(bmpData);
fs.writeFileSync('./image.bmp', rawData.data);

Header

Property Type Purpose
fileSize number The size of the BMP file in bytes
reserve1 number Reserved; actual value depends on the application that creates the image
reserve2 number Reserved; actual value depends on the application that creates the image
offset number The offset, i.e. starting address, of the byte where the bitmap image data (pixel array) can be found.
headerSize number The size of this header (12 bytes)
width number The bitmap width in pixels (unsigned 16-bit)
height number The bitmap height in pixels (unsigned 16-bit)
planes number The number of color planes, must be 1
bitPP number The number of bits per pixel
compress number The compression method being used. See the supported compression methods
rawSize number The image size. This is the size of the raw bitmap data; a dummy 0 can be given for BI_RGB bitmaps.
hr number The horizontal resolution of the image. (pixel per metre, signed integer)
vr number The vertical resolution of the image. (pixel per metre, signed integer)
colors number The number of colors in the color palette, or 0 to default to 2n
importantColors number The number of important colors used, or 0 when every color is important; generally ignored
palette Color[] The colors used to render the image. only used for 1, 4, and 8 bitPP images
data Byte[] The data in ABGR

Color

The color palette is returned when decoding a 1, 4, or 8 bit image.

Color Format:

{
  "red": 255,
  "green": 255,
  "blue": 255,
  "quad": 255
}

To encode to 4 or 8 bit a color palette must be provided. 1 bit defaults to black and white but you can override this via palette.

const rawData = bmp.encode({
  data,
  bitPP: 8,
  width,
  height,
  palette: [
    { red: 255, green: 255, blue: 255, quad: 0 },
    { red: 255, green: 255, blue: 0, quad: 0 },
    { red: 255, green: 0, blue: 255, quad: 0 },
    { red: 255, green: 0, blue: 0, quad: 0 },
    { red: 0, green: 255, blue: 255, quad: 0 },
    { red: 0, green: 255, blue: 0, quad: 0 },
    { red: 0, green: 0, blue: 255, quad: 0 },
    { red: 0, green: 0, blue: 0, quad: 0 },
  ],
});

fs.writeFileSync('./image.bmp', rawData.data);

bmp-ts's People

Contributors

alecmev avatar chriszimmerman avatar dependabot[bot] avatar francoisp avatar gaubee avatar hipstersmoothie avatar mooyoul 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

Watchers

 avatar  avatar

bmp-ts's Issues

Alpha pixels are set to zero on all opaque pixel formats

For whatever reason, this library gets handling alpha all wrong.

The output data buffer seems to store the pixel data as RGBA or ABGR:

bmp-ts/src/decoder.ts

Lines 60 to 63 in 2521cce

this.locRed = this.toRGBA ? 0 : 3;
this.locGreen = this.toRGBA ? 1 : 2;
this.locBlue = this.toRGBA ? 2 : 1;
this.locAlpha = this.toRGBA ? 3 : 0;

However seems like all code that handles opaque pixel formats sets the alpha channel to zero which wasn't the behavior I expected and requires anyone trying to use this library to check whether the input BMP file has an alpha channel and handle it accordingly in their code. The alpha channel of any opaque pixels should be set to 255.

bmp-ts/src/decoder.ts

Lines 438 to 452 in 2521cce

private bit24() {
const padding = this.width % 4;
this.scanImage(padding, this.width, (x, line) => {
const loc = line * this.width * 4 + x * 4;
const blue = this.buffer.readUInt8(this.pos++);
const green = this.buffer.readUInt8(this.pos++);
const red = this.buffer.readUInt8(this.pos++);
this.data[loc + this.locRed] = red;
this.data[loc + this.locGreen] = green;
this.data[loc + this.locBlue] = blue;
this.data[loc + this.locAlpha] = 0;
});
}

The documentation is also a bit unclear, endianess is pretty confusing when it comes to handling pixels, and since the library by default always decodes the image data to XBGR (because one might expect it's the same as input BMP), I think that the toRGBA option documentation should be clarified, "reverses the pixel byte order from ABGR to RGBA making it compatible with other libraries like pngjs"

Additionally said option is completely ignored if you pass in a palettized image.

bmp-ts/src/decoder.ts

Lines 326 to 329 in 2521cce

this.data[location] = 0;
this.data[location + 1] = rgb.blue;
this.data[location + 2] = rgb.green;
this.data[location + 3] = rgb.red;

Error trying to use.

When try to use, receive the fallowing error:

node_modules\bmp-ts\dist\index.js:1
import BmpDecoder from './decoder';
^^^^^^

SyntaxError: Cannot use import statement outside a module

Any idea of how solve this.

unreachable code

In the scanImage function in the decoder there's this bit:

const result = processPixel.call(this, x, line); if (result === false) { return; }

...but processPixel is defined as returning void, hence the if statement will never evaluate to true. Any idea what was intended with that code? I can submit a pr and fix it, but just glancing at it I'm not sure what the intent was. Thanks!

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.