-
BMP Loading Structs
I'm trying to load a BMP in Linux using the Windows structures. It's loading, but a lot of the values are just junk. First, heres the code:
Code:
#include <iostream>
#include <cstdio>
#define BYTE unsigned char
#define DWORD unsigned int
#define LONG int
#define UINT unsigned int
#define WORD unsigned short int
#define LPSTR char*
typedef struct tagBITMAPFILEHEADER {
WORD bfType;
DWORD bfSize;
WORD bfReserved1;
WORD bfReserved2;
DWORD bfOffBits;
} BITMAPFILEHEADER, *PBITMAPFILEHEADER;
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, *PBITMAPINFOHEADER;
typedef struct tagRGBQUAD {
BYTE rgbBlue;
BYTE rgbGreen;
BYTE rgbRed;
BYTE rgbReserved;
} RGBQUAD;
void loadBMP(LPSTR filename, BITMAPFILEHEADER *tbmfh, BITMAPINFOHEADER *tbmih)
{
FILE *in;
in=fopen(filename,"rb");
fread(tbmfh,sizeof(BITMAPFILEHEADER),1,in);
fread(tbmih,sizeof(BITMAPINFOHEADER),1,in);
return;
}
int main()
{
BITMAPFILEHEADER bmfh;
BITMAPINFOHEADER bmih;
loadBMP("/home/david/DavidsStuff/Pictures/somebmp.bmp", &bmfh, &bmih);
if(bmih.biBitCount != 24)
std::cout << bmih.biSize << std::endl;
std::cin.get();
}
bmfh.bfType will give me 19778, like it's supposed to. bfOffBits returns a good value too, I think. As for most of the other values, then seem bad. Here's some examples:
biBitCount: 0 (Should be 1, 4, 8, or 14)
biWidth: 39976960
biSize: 44892160 (Should be 40)
biCompression: 5588251136
This article explains what the values are: http://www.gamedev.net/reference/art...rticle1966.asp
The rest are more of the same. I also get similar results with different bmp's. Any clue why?
Thanks,
David
-
Search: Key Word(s): padding, alignment
There is no (good) way to guarantee the amount of internal padding within a structure.
In addition, if your Linux is running on something other than an x86 processor, then you may have an endian problem as well.
The only good way of getting consistent answers on all machines is to read the file one byte at a time and using bitwise ops (<<, >>, &, |) to move the byte to the appropriate part of the appropriate member of your struct.
-
Hey Salem,
I've been working on this for a while and am really having some trouble with it. I just can't comprehend how alignment works. I read that an image that was something like 256x256 for example, would not have any padding, because it ends on a DWORD, or something of that nature.
Basically I'm just trying to load the bitmap headers, but I guess the alignment is bad. Could you give me an example of how you would fix the alignment?
Thanks :confused:
David
-
If you're using g++ (gcc), you can do this:
Code:
#pragma pack(1)
typedef struct tagBITMAPFILEHEADER {
WORD bfType;
DWORD bfSize;
WORD bfReserved1;
WORD bfReserved2;
DWORD bfOffBits;
} BITMAPFILEHEADER, *PBITMAPFILEHEADER;
#pragma pack()
-
Well that's rather nifty. It worked fine...