As I said in my comments, you are indeed missing the padding, but not only that.
A bitmap file is composed of multi parts: Headers, a color map, and a Pixel map (mainly).
From what I understand of your question, you need your function to return the offset address in the file fp (considered to be a bitmap file) of the pixel that would be at position xp ; yp
. To do that you need at least three things:
- The offset of the pixel map's begginning : you will find it by reading the last 4 bytes (a
dword
) of the Bitmap file header, you can get it by reading at offset 10 in your file. - The pixel-per-row (or image width) number : you will find it in the BITMAPINFOHEADER
- The bit-per-pixel number : you will find it in the BITMAPINFOHEADER
When you have this, the address of your pixel in the file is :
rowSizeInBytes = (((bitPerPixel * imageWidth + 31) * 4) / 32);
pixAddress = pixelMapStartAddress + rowSizeInBytes * yp + ((xp * bitPerPixel) / 8);