Original Post
Hello ThreadReader, I load an bitmap with the following function and when it enters the loop it crashes (when i leave the loop out i got the bitmap wiht the r and b values wrong (bgr not rgb)). So does anyone know why its not working???? Thanks for your reaction.
//Load the bmp file
bool Bmp(char* file, int* height, int* width, unsigned char** rgbData, int* error)
{
//Vars
FILE* File = NULL;
BMP_Header Header;
BMP_Info Info;
unsigned char Swap;
//Open the file
File = fopen(file, "rb");
if (File == NULL)
{
*error = 1;
return false;
}
//Read the file header
fread(&Header, 1, sizeof(BMP_Header), File);
//Check if its a bitmap
if (Header.Type[0] != 'B' && Header.Type[1] != 'M')
{
*error = 2;
return false;
}
//Read the info header
fread(&Info, 1, sizeof(BMP_Info), File);
//Bmp type
if (Info.Bits != 24)
{
*error = 2;
return false;
}
//Make room for the image data
*rgbData = new unsigned char[((Info.Width*Info.Height)*3)+1];
//Read the image data
fread(*rgbData, 1, (Info.Width*Info.Height)*3, File);
//Close the file
fclose(File);
// swap the R and B values to get RGB since the bitmap color format is in BGR
for (int i=0;i<(Info.Width*Info.Height)*3;i+=3)
{
Swap = *rgbData;
*rgbData = *rgbData[i+2];
*rgbData[i+2] = Swap;
}
//Set the vars
*height = Info.Height;
*width = Info.Width;
//Succes
return true;
}