Bitmap File Structure

Download Report

Transcript Bitmap File Structure

Bitmap Files
Ray Wisman
[email protected]
Objectives
• Introduce
- Reading bitmaps
- Displaying bitmaps
OpenGL Game Programming
2
Bitmap
• BMP files can be loaded into program memory
• No compression, similar to PPM but not ASCII text
• Simple but useful for direct drawing or textures
• File structure defined in Windows.h
- BITMAPFILEHEADER
• For texture, dimension must be power of 2
OpenGL Game Programming
3
Bitmap File Structure
• File header
typedef struct _tagBITMAPFILEHEADER {
WORD bfType;
DWORD bfSize;
WORD bfReserved1;
WORD bfReserved2;
DWORD bfOffBits;
} BITMAPFILEHEADER;
OpenGL Game Programming
4
Bitmap File Structure
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;
OpenGL Game Programming
5
Bitmap Example
BITMAPINFOHEADER
unsigned char*
bmpinfoheader;
bmpImage;
bmpImage= LoadBitmapFile(“RayHead.bmp”, &bmpinfoheader);
glPixelStorei(GL_UNPACK_ALIGNMENT, 4); mem alignment
glRasterPos2i(100,100);
raster position
glDrawPixels(bmpinfoheader.biWidth, bmpinfoheader.biHeight,
GL_RGB, GL_UNSIGNED_BYTE,
bmpImage);
draw
6
Bitmap File Load Example
Returns a pointer to the bitmap image of the bitmap specified by
filename and bitmap header information. No support for 8-bit
bitmaps.
unsigned char *LoadBitmapFile(char *fn,
BITMAPINFOHEADER *bitmapInfoHeader) {
FILE *filePtr;
BITMAPFILEHEADER
unsigned char
int
unsigned char
the file pointer
bitmapFileHeader; file header
*bitmapImage;
image data
imageIdx = 0;
image counter
tempRGB;
swap variable
OpenGL Game Programming
7
Bitmap File Load Example
open in "read binary" mode
filePtr = fopen(fn, "rb");
if file not found, return
if (filePtr == NULL) return NULL;
read the bitmap file header
fread(&bitmapFileHeader,
sizeof(BITMAPFILEHEADER), 1, filePtr);
verify that the file is a bitmap
if (bitmapFileHeader.bfType != BITMAP_ID) {
fclose(filePtr);
return NULL;
}
OpenGL Game Programming
8
move file pointer to beginning of bitmap data
fseek(filePtr, bitmapFileHeader.bfOffBits, SEEK_SET);
allocate enough memory for the bitmap image data
bitmapImage = (unsigned char*)
malloc(bitmapInfoHeader->biSizeImage);
if (!bitmapImage) {
verify memory allocation
free(bitmapImage);
fclose(filePtr);
return NULL;
}
read in the bitmap image data
fread(bitmapImage, 1, bitmapInfoHeader->biSizeImage, filePtr);
verify bitmap image data read
if (bitmapImage == NULL) {
fclose(filePtr);
return NULL;
}
9
OpenGL Game Programming
Bitmap Example
swap the R and B values to get RGB since the bitmap
color format is in BGR
for ( imageIdx = 0;
imageIdx < bitmapInfoHeader->biSizeImage;
imageIdx+=3)
{
tempRGB = bitmapImage[imageIdx];
bitmapImage[imageIdx] = bitmapImage[imageIdx + 2];
bitmapImage[imageIdx + 2] = tempRGB;
}
close the file and return the bitmap image data
fclose(filePtr);
return bitmapImage;
}
OpenGL Game Programming
10
Bitmap Example
Download source and bitmap
RayHead.bmp
Size: 256x256
Other image loading options:
1. Use FreeImage to load other
formats, a good short tutorial is
here.
2. Use glbmp, a lightweight
function loading BMP files.
Download a Visual Studio
example using glbmp to texture
a square.
OpenGL Game Programming
11