skeeto/bmp

2x2 bmps aren't handled properly

Closed this issue · 0 comments

Hey, just thought I'd leave a finding here. So indexing into the BMP doesn't appear to be handled properly for at least 2x2 bmp files. The equation I am talking about is here:

 long pad = (width * 3) % 4;
 p = hdr + 14 + 40 + y * (width + pad) * 3 + x * 3;

For a 2x2 bmp, (width + pad) * 3 = (2 + 2) * 3 = 12
When the desired result is 8 I believe.

A fixed version of this formula is as follows:

 long pad = (width * 3 * 3) % 4;
 p = hdr + 14 + 40 + y * (width * 3 + pad) + x * 3;

Which, for a 2x2 bmp gives: (width * 3 + pad) = (2 * 3 + 2) = 8

It also works for images larger than 2x2 from what I have tested, but I have not tested exhaustively. I hope you consider this change.

Image as your library does it currently:
test

Image after applying the fixed formula:
fix

I uploaded pngs because unfortunately github does not allow bmps.