BMP files

Search this archive.

From: Great One (suda_j@iol.cz)
Date: Sat 27 Nov 1999 - 02:35:53 IST


Hi

Sorry if this is out of topic or too long, but:
I'm trying to use BMP files under Linux.
So far I've been using them under WIN32 API
and I want to rewrite my code for Linux.

Under win32 I had:

BOOL SPicture::LoadBitmap(LPSTR lpstrfn)
{
 HANDLE hfile;
 DWORD br;

 hfile = CreateFile(lpstrfn, GENERIC_READ, 0, NULL, OPEN_EXISTING,
FILE_ATTRIBUTE_ARCHIVE, NULL);
 if(hfile == INVALID_HANDLE_VALUE) return FALSE;

 ReadFile(hfile, &bfh, sizeof(bfh), &br, NULL);

 h_bi = GlobalAlloc(GHND, sizeof(BITMAPINFO));
 if(h_bi == NULL) return FALSE;
 p_bi = (LPBITMAPINFO) GlobalLock(h_bi);

 ReadFile(hfile, &p_bi -> bmiHeader, sizeof(BITMAPINFOHEADER), &br, NULL);

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

 h_data = GlobalAlloc(GHND, sizeof(BYTE) * 3 * p_bi -> bmiHeader.biWidth *
p_bi -> bmiHeader.biHeight +
   sizeof(BYTE) * 3 * p_bi -> bmiHeader.biWidth);
 if(h_data == NULL) return FALSE;
 p_data = (BYTE*) GlobalLock(h_data);

 SetFilePointer(hfile, bfh.bfOffBits , NULL, FILE_BEGIN);
 ReadFile(hfile, p_data, sizeof(BYTE) * 3 * p_bi -> bmiHeader.biWidth *
p_bi -> bmiHeader.biHeight +
   sizeof(BYTE) * 3 * p_bi -> bmiHeader.biWidth, &br, NULL);

 CloseHandle(hfile);

 return TRUE;
}

This is a very specific function for loading truecolor
bitmaps.

This function uses:

BITMAPFILEHEADER bfh;        // bitmap file header
HGLOBAL h_bi; LPBITMAPIFNO p_bi;        // bitmap info
HGLOBAL h_data; BYTE* p_data;        // bitmap's data

WIN32 API definitions of these structures are:

typedef struct tagBITMAPFILEHEADER { // bmfh
        WORD    bfType;
        DWORD   bfSize;
        WORD    bfReserved1;
        WORD    bfReserved2;
        DWORD   bfOffBits;
} BITMAPFILEHEADER;

typedef struct tagBITMAPINFOHEADER{ // bmih
   DWORD  biSize;
   LONG   biWidth;
   LONG   biHeight;
   WORD   biPlanes;
   WORD   biBitCount
   DWORD  biCompression;
   DWORD  biSizeImage;
   LONG   biXPelsPerMeter;
   LONG   biYPelsPerMeter;
   DWORD  biClrUsed;
   DWORD  biClrImportant;
} BITMAPINFOHEADER;

typedef struct tagRGBQUAD {                 // rgbq
    BYTE    rgbBlue;
    BYTE    rgbGreen;
    BYTE    rgbRed;
    BYTE    rgbReserved;
} RGBQUAD;

typedef struct tagBITMAPINFO { // bmi
   BITMAPINFOHEADER bmiHeader;
   RGBQUAD          bmiColors[1];
} BITMAPINFO;

I made my own under Linux:
----------------------------------------------------------------------------
-----

#define BYTE  unsigned char
#define DWORD  unsigned int
#define LONG  int
#define UINT  unsigned int
#define WORD  unsigned short int

#define LPSTR  char*

#define BOOL  int
#define FALSE  0
#define TRUE  1

// MS-Windows bitmaps' definition:

typedef struct tagBITMAPFILEHEADER
{
 WORD bfType;
 DWORD bfSize;
 WORD bfReserved1;
 WORD bfReserved2;
 DWORD bfOffBits;
} BITMAPFILEHEADER;

typedef struct tagBITMAPINFOHEADER
{
 DWORD biSize;
 LONG biWidth;
 LONG biHeight;
 WORD biPlanes;
 WORD biBitCount;
 DWORD biCompression;
 DWORD biSizeImage;
 LONG biXPelsPerMeter;
 LONG biYPelsPerMeter;
 DWORD biClrUsed;
 DWORD biClrImportant;
} BITMAPINFOHEADER;

typedef struct tagRGBQUAD
{
 BYTE rgbBlue;
 BYTE rgbGreen;
 BYTE rgbRed;
 BYTE rgbReserved;
} RGBQUAD;

typedef struct tagBITMAPINFO
{
 BITMAPINFOHEADER bmiHeader;
 RGBQUAD bmiColors[1];
} BITMAPINFO;

#define LPBITMAPFILEHEADER  BITMAPFILEHEADER*
#define LPBITMAPINFOHEADER  BITMAPINFOHEADER*
#define LPBITMAPINFO   BITMAPINFO*

And my code under Linux:

BOOL SPicture::LoadBitmap(LPSTR lpstrfn)
{
 FILE* hfile;

 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;

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

 fclose(hfile);

 return TRUE;
}

I'm using g++. The program runs but the values
in bfh and p_bi are absolutely crazy. Nosense.

Did I do anything wrong ?

Answer anybody, please.

Thank you.


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