BotHack

BotHack makes getting things done easy and fun. Delving deep into the technoweb, BotHack brings back simple and totally life-altering tips and tricks for managing your information and time. At this wild moment in the development of human-oriented technology, BotHack is your own personal early adopter, here to guide you through the onslaught of the new. The world is full of fascinating problems waiting to be solved: BotHack can help.

Friday, November 11, 2005

BoTutorials : Short and simple C++ function reading a BMP file into an array

I was working on a program in C++ and I needed a simple function that could read a BMP file into a simple array of the pixels and BGR color information. So I Googled around and all I could find was a bunch of complicated code using complicated things like objects and a bunch of library files and stuff. So, just to expand my experience in C++, I decided to make my own function. This function uses only the stdio.h header file.

First, I had to find out how a Bitmap file is put together. I could have found a tutorial but instead I decided to open up a small BMP in a hex editor to take a look. And what I found was that BMP files are surprisingly simple to read.

I noticed that the width and height were integers starting at offsets 18 and 22 respectively.

I'm not sure what the other stuff in the header is for but the important thing to know is that the actual pixel color information starts at offset 54. From then on, each byte alternates Blue, Green, Red (the opposite of usual). The only other thing to know is that after each row you have to skip a number of bytes which is equal to the remainder after dividing the width by four. I don't know why this is, doesn't it just waste disk space?

Now, about using my function. Here is how you might set up your program to load the Bitmap:



#define MAXW 500
#define MAXH 500

byte Bitmap[MAXW][MAXH][3];
int BMPw;
int BMPh;

loadBMP("../kompo_l.bmp", &Bitmap[0][0][0], &BMPw, &BMPh, MAXW);




The arguments that are passed to the function are: loadBMP(filename of Bitmap, pointer to the start of an array of bytes, pointer to integer where the width will be stored, pointer to the integer for the height, length of row in array)

And, here's the function:



void loadBMP(char *filename, byte *BitmapP, int *owidth, int *oheight, int max){
byte curnt[3];
int width;
int height;

//Open File
FILE *fptr;
fptr = fopen(filename, "rb");

//First, get the width and height
fseek(fptr, 18, SEEK_SET);
fread(&width, sizeof(int), (size_t)1, fptr);

fseek(fptr, 22, SEEK_SET);
fread(&height, sizeof(int), (size_t)1, fptr);

*owidth = width;
*oheight = height;

//Now start to read all the pixels and colors and everything.
fseek(fptr, 54, SEEK_SET);
for(int y=1; y < height + 1; y++){
for(int x=1; x < width + 1; x++){
fread((BitmapP + x*(max*3) + y*3), sizeof(byte), (size_t)3, fptr);
}
fseek(fptr, width % 4, SEEK_CUR);
}


fclose(fptr);
}



The array now contains: Bitmap[x][y][Green(0) or Blue(1) or Red(2)]


Die Dulci Fruere