Вопрос

I wrote a function in order to get the position of the requested pixel position (x250 y230 - central of the entire picture - x500 y460). The problem is that the function returns the position with 17 pixels difference more on up and 12 pixels difference more on right. What am i missing.. the padd? How can i use this function properly?

size_t find (FILE* fp, dword xp, dword yp)
{
    int i;
    int pointer = (sizeof(DIB)+sizeof(BMP)+2)+(250*3);

    for(i=0; i<460; i++)
    {
        fseek(fp, pointer+(i*pointer), SEEK_SET);
    }
     return ftell(fp);
}
Это было полезно?

Решение

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);
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top