I'm trying to load a bitmap and getting weird results when checking what I've loaded into my structures. First I'll show my code:
All the numbers that I check besides "bfType" are completely way off. Then I realized that 19778 was "BM" backwards, so I thought maybe all the numbers are messed up because of the bytes being stored in it are backwards.... Well I went and took the numbers and converted them to hex and tried reversing the bytes and switching back to decimal to get the right numbers and it didn't work. So I'm pretty much clueless to what I'm doing wrong.Code:#include <iostream>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
typedef struct
{
unsigned short bfType;
unsigned int bfSize;
unsigned short bfReserve1;
unsigned short bfReserve2;
unsigned int bfOffBits;
}FILEHEADER;
typedef struct
{
unsigned int biSize;
unsigned int biWidth;
unsigned int biHeight;
unsigned short biPlanes;
unsigned short biBitCount;
unsigned int biCompression;
unsigned int biSizeImage;
unsigned int biXPelsPerMeter;
unsigned int biYPelsPerMeter;
unsigned int biClrUsed;
unsigned int biClrImportant;
}INFOHEADER;
unsigned char* LoadBitmap(const char* filename, INFOHEADER* bInfo)
{
FILEHEADER fHeader;
unsigned char* bitmap;
unsigned char aColors[256];
int infosize = 0;
FILE* fp;
fp = fopen(filename,"rb");
if(fp == NULL)
{
cout << "Couldn't open file!";
return NULL;
}
if(fread(&fHeader,sizeof(FILEHEADER),1,fp) < 1)
{
cout << "Couldn't read in fileheader!";
fclose(fp);
return NULL;
}
if(fHeader.bfType != 19778)
{
cout << "Not a bmp file!";
fclose(fp);
return NULL;
}
if(fread(bInfo,sizeof(INFOHEADER),1,fp) < 1)
{
cout << "Couldn't read in infoheader!";
fclose(fp);
return NULL;
}
fseek(fp,fHeader.bfOffBits,SEEK_SET);
bitmap = (unsigned char*)malloc(bInfo->biSizeImage);
if(!bitmap)
{
cout << "Couldn't allocate bitmap memory";
free(bitmap);
fclose(fp);
return NULL;
}
/*if(fread(bitmap,bInfo->biSizeImage,1,fp)==NULL)
{
cout << "Unable to store bitmap!";
fclose(fp);
return NULL;
}*/
cout << bInfo->biSize << endl;
cout << bInfo->biCompression << endl;
cout << fHeader.bfType << endl;
cout << fHeader.bfSize << endl;
cout << fHeader.bfReserve1 << endl;
cout << fHeader.bfReserve2 << endl;
cout << fHeader.bfOffBits << endl;
return NULL;
}
int main(void)
{
INFOHEADER bmpInfo;
unsigned char* bitmap;
bitmap = LoadBitmap("C:\\Dev-Cpp\\File IO Projects\\AstralAbraxas.bmp",&bmpInfo);
cin.get();
return 0;
}
I'm not really familiar with fread fseek etc either. I never done a whole lot of file i/o before.