Szczepan
Szczepan

Reputation: 365

Opening bitmap in c++

At the begin sorry for my English.

My purpose is to load rgb array from the bitmap. But there is a difference between size of the image and the product of height * width * 3. When i read about bmp format i notice when the widht % 4 is != 0 then i must add to width some digit to equilibrium. (width%4==1 i must add 3, width%4==2 i must add 2 etc.) Size of the image is 1 442 454 Bytes, height is 601 and width is 801. 804 * 600 * 3 == 1 441 800 and it is more then size of the image. 801 * 600 * 3 == 1441800 it must less then size of the image (even when I add 54 bits of headers). What i must do to read it correctly? (headers are loaded correctly)

Upvotes: 1

Views: 202

Answers (1)

Paul R
Paul R

Reputation: 212969

Note that each row is padded to a multiple of 4 bytes (not pixels).

So if you have 801 pixels per row and each pixel is 3 bytes (RGB) them you have 801*3=2403 bytes per row and this will be padded with one additional byte to 2404 bytes. The bitmap size will therefore be 601*2404=1444804 bytes.

If however your row width is only 800 pixels then you have 800*3=2440 bytes per row which is already a multiple of 4 bytes so there will be no additional pad bytes and the bitmap size will be 601*2400=1442400 bytes. With a 54 byte header this gives 1442454 bytes.

Conclusion: your image size is actually 801 x 600, not 801 x 601.

Upvotes: 4

Related Questions