supposed to load a windows 24bpp bitmap file into KOS's Image structure.
it did at one point. the colors were whacked out. changed the r g and b variables from int to uint16 and removed some code that checked to make sure it was a .bmp, now all it does is reset the dreamcast
Code: Select all
//Loads a windows .bmp file
//Only loads 24-bit bitmaps
//fn is a string containing the filename
//rI is short for return Image. it's where the bitmap is loaded
void loadbmp(char *fn, Image *rI)
{
file_t fh; //file handle
struct bitmap loadit; //where we load the file
//if necessary I can provide the source for these structures as well
int i; //Loop counter
uint16 r, g, b; //rgb conversion variables
fh = fs_open(fn, O_RDONLY); //Open the file as readonly
//bfh is the bitmap file header, and is 14 bytes long
fs_read(fh, &loadit.bfh, 14); //read in the file header
//bih is the bitmap info header, and is 40 bytes long
//which is why nesterdc skips the first 54 bytes of the file
fs_read(fh, &loadit.bih, 40); //read in the info header
//pixelbits is an uint8 pointer
//Q: should i allocate memory for pixelbits before hand?
fs_read(fh, loadit.pixelbits, loadit.bih.sizeImage);
//Closes the file :: lay off me I'm trying to be as verbose as possible :D
fs_close(fh);
//Store the bitmap height and width into the Image struct
rI->height = loadit.bih.height;
rI->width = loadit.bih.width;
//The pixels in a 24bpp bitmap should be arranged as r-g-b
// with one byte for each (checking the actual file size
// against the calculated file size proves this)
// ((length * width * 3) + 54)
//So what I do here, since the Image struct's pixel_data is
// a uint16 pointer (to mesh with the vid memory), is i
// process 3 pixels (a red, green, and blue) at a time.
for (i = 0; i < loadit.bih.sizeImage; i += 3)
{
//Shamelessly borrowed from NesterDC ;D
//But at least I know why it was done
//I also noticed that NesterDC also does an explicit type-cast.
//Q: Should I follow suit?
r = (loadit.pixelbits[i+0]*32)/256; //convert the red bit
g = (loadit.pixelbits[i+1]*64)/256; //convert the green bit
b = (loadit.pixelbits[i+2]*32)/256; //convert the blue bits
rI->pixel_data[i/3] = ((r >> 3) << 11) | ((g >> 2) << 5) | ((b >> 3) << 0);
}
}
Newbies, take a look. It is a semi-working .bmp loader with potential to load more than a 320*240 24bpp bitmap. Feel free to jack it After the devvers correct it that is ;D
Thanks for the help!