Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Alpha pixels are set to zero on all opaque pixel formats #31

Open
alula opened this issue Jul 27, 2024 · 1 comment
Open

Alpha pixels are set to zero on all opaque pixel formats #31

alula opened this issue Jul 27, 2024 · 1 comment

Comments

@alula
Copy link

alula commented Jul 27, 2024

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;

@hipstersmoothie
Copy link
Collaborator

If you'd like to make a pr fixing this I'd love to merge it!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants