something wrong with BMPs

Search this archive.

From: Jim Dutcher (dutcher@volny.cz)
Date: Thu 09 Dec 1999 - 08:10:55 IST


Hi
there is something really strange with BMP files.
I'm rewriting my WIN32 application for Linux.
I work with truecolor pictures but in hicolor
mode. This is my code for loading the picture
(I created my own definitions for Win32 API
structures such as BITMAPFILEHEADER,
BITMAPINFOHEADER or BITMAPINFO,
same as in windows.h):

BOOL SPicture::LoadBitmap(LPSTR lpstrfn)
{
 FILE* hfile;
 DWORD br;
 int x, y, dy;
 BYTE* p_data;

 hfile = fopen(lpstrfn, "rb");
 if(hfile == NULL) return FALSE;

 fread(&bfh, sizeof(bfh), 1, hfile);

 p_bi = (LPBITMAPINFO) malloc(sizeof(BITMAPINFO));
 if(p_bi == NULL) return FALSE;

 fread(&p_bi -> bmiHeader, sizeof(BITMAPINFOHEADER), 1, hfile);

 if(p_bi -> bmiHeader.biBitCount != 24) return FALSE;

 p_data = (BYTE*) malloc(sizeof(BYTE) * 3 * p_bi -> bmiHeader.biWidth *
p_bi -> bmiHeader.biHeight);
 if(p_data == NULL) return FALSE;
 p_dt = (WORD*) malloc(sizeof(WORD) * p_bi -> bmiHeader.biWidth *  p_bi ->
bmiHeader.biHeight);
 if(p_dt == NULL) return FALSE;

 fseek(hfile, bfh.bfOffBits, SEEK_SET);
 fread(p_data, sizeof(BYTE), 3 * p_bi -> bmiHeader.biWidth * p_bi ->
bmiHeader.biHeight, hfile);

 fclose(hfile);

 dy = p_bi -> bmiHeader.biHeight - 1;
 for(y = 0; y < p_bi -> bmiHeader.biHeight; y++) {
  for(x = 0; x < p_bi -> bmiHeader.biWidth; x++) {
   *(p_dt + ValueOf(x, y)) = gl_rgbcolor(*(p_data + (ValueOf(x, dy) * 3) +
2),
           *(p_data + (ValueOf(x, dy) * 3) + 1),
             *(p_data + (ValueOf(x, dy) * 3) + 0));
    }
    dy--;
   }

   free(p_data);

 return TRUE;
}

p_data is a temporary array, as I said I'm using
hi-color mode so I have to find the nearest color,
16-bit (WORD* p_dt).

Everything works well for pictures like 300 x 200
or 800 x 600 pixels. When I use this code for a
90 x 35 bitmap I receive really strange values:
Of course this is not exactly the case, but for example, it
should appear like this:

        XXXXXXXXXXXXXXXXXXXXXXXXX
        X                                                               X
        X                                                               X
        X                                                               X
        XXXXXXXXXXXXXXXXXXXXXXXXX

but it appears like this:

        XXXXXXXXXXXXXXXXXXXXXXXXX
                X                                                      X
              X                                                     X
            X                                                     X
         XXXXXXXXXXXXXXXXXXXXXXXXX

What's wrong ?
It works OK, but the picture must not be to small !
Can you help me, please, somebody, anybody ?

Thank You


This archive was generated by hypermail 2.1.4 : Wed 21 Jan 2004 - 22:10:22 IST