jimp-dev / bmp-ts

A pure typescript bmp encoder and decoder
MIT License
17 stars 6 forks source link

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

Open alula opened 3 months ago

alula commented 3 months ago

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

The output data buffer seems to store the pixel data as RGBA or ABGR: https://github.com/jimp-dev/bmp-ts/blob/2521cce93dbc770380940483af1a546bc42d0ed6/src/decoder.ts#L60-L63

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.

https://github.com/jimp-dev/bmp-ts/blob/2521cce93dbc770380940483af1a546bc42d0ed6/src/decoder.ts#L438-L452

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.

https://github.com/jimp-dev/bmp-ts/blob/2521cce93dbc770380940483af1a546bc42d0ed6/src/decoder.ts#L326-L329

hipstersmoothie commented 1 month ago

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