Q: Bitmap Loading Suggestions

Hi all.
I’ve been all over the net looking for an nice, easy way to load a windows bitmap (.bmp) file.

I am developing under linux, but I want to learn to code in a portable fashion, and keep windows/mac/linux port coding as minimal as possible.

I’ve definitely seen that glaux is NOT the way to go, and apparently it isn’t an option for me on linux anyway.

I haven’t found a tutorial or reference on how to actually manually load a .bmp file though, so I was hoping perhaps someone could point me to a nice C code referece/tutorial/howto on this subject.

Any help would be appreciated.

Cheers!

j

  1. Convert to a compressed Targa. It takes up less space than an uncompressed BMP. Compressed BMPs are difficult to decode IMO. The GIMP can convert the files for you.
  2. Load the targa file. This will get you started: http://nehe.gamedev.net/data/lessons/lesson.asp?lesson=33

Well…
First there is the bitmap file header:

typedef struct tagBITMAPFILEHEADER { 
  WORD    bfType; // 2 chars "BM"
  DWORD   bfSize; // size of file
  WORD    bfReserved1; // reserved always 0
  WORD    bfReserved2; // same
  DWORD   bfOffBits; // sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER)[+sizeof(colorpalette) if there is one]
} BITMAPFILEHEADER, *PBITMAPFILEHEADER; 

then the bitmap info header:

typedef struct tagBITMAPINFOHEADER{
  DWORD  biSize; // size of this struct
  LONG   biWidth; // image width (in pixels)
  LONG   biHeight; // image height (in pixels)
  WORD   biPlanes; // always 1
  WORD   biBitCount; // bits per pixel
  DWORD  biCompression; //compression
  DWORD  biSizeImage; // size of image in bytes
  LONG   biXPelsPerMeter; // crap
  LONG   biYPelsPerMeter; // more crap
  DWORD  biClrUsed; // 
  DWORD  biClrImportant; //
} BITMAPINFOHEADER, *PBITMAPINFOHEADER; 

for more info see MSDN :frowning:
then the palette (only if bpp is 1, 4 or 8)
basicaly an array of

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

then the image.
note that the number of BYTES in a scanline must be a multiple of 4 (DWORD aligned)
i.e. for an uncompressed 24 bpp image
BGR BGR BGR BGR <– 12 bytes it is DWORD aligned
but
BGR BGR <– 6 bytes NOT DWORD aligned so it is padded like that:
BGR BGR 00 <– 8 bytes - DWORD aligned.
So, for uncompressed or RLE compredded bitmaps, it is not very difficult to write a function that loads them.

T101 that info will help me in the future I would imagine so thanks.

uruk that’s exactly what I was looking for, my brain farted and I couldn’t think of the right place to look for the specs.
Thanks so very much!

Cheers!

http://www.gamedev.net/reference/articles/article1966.asp

…Worked great for me, and I’m running it on Linux. If you want to use his source, make sure you download the latest one in the zip package (the one in the tutorial has a couple bugs).