why doesn't this work, i played around with it for a while now, but the last fread is failing.....data is all zero's, which it's not.
this line fails....
fread(data,size,1,fp);
the header
#pragma once
struct BMP_HEADER
{
//int Type;
int bfSize;
char bfReserved1;
char bfReserved2;
int bfOffBits;
};
struct BMP_INFO_HEADER {
unsigned long biSize;
long biWidth;
long biHeight;
unsigned short biPlanes;
unsigned short biBitCount;
unsigned short biCompression;
unsigned short biSizeImage;
short biXPelsPerMeter;
short biYPelsPerMeter;
unsigned long biClrUsed;
unsigned long biClrImportant;
};
struct Color
{
int r;
int g;
int b;
};
class BMP
{
public:
BMP();
~BMP();
bool loadBMP(char *file);
int getWidth();
int getHeight();
unsigned char *getData();
private:
BMP_INFO_HEADER bmp_info;
BMP_HEADER bmp_header;
unsigned char *data;
};
the cpp file
#include "bmp.h"
#include <iostream>
using namespace std;
BMP::BMP()
{
data = NULL;
}
BMP::~BMP()
{
if(data)
delete [] data;
}
bool BMP::loadBMP(char *file)
{
if(data)
delete [] data;
FILE *fp = fopen(file,"r+");
if(fp)
{
char bm[2] = {'B','M'};
char tmp[2] = {0};
fread(&tmp,1,2,fp);
if(strcmp(bm,tmp))
{
fread(&bmp_header,1,sizeof(BMP_HEADER),fp);
fread(&bmp_info,1,sizeof(BMP_INFO_HEADER),fp);
//fseek(fp,bmp_header.bfOffBits,SEEK_SET);
int size = bmp_info.biWidth*bmp_info.biHeight*3;
//data = new unsigned char[size+1];
//Color *c = new Color[size+1];
data = new unsigned char[size+1];
//fseek(fp,bmp_header.bfOffBits+1,SEEK_SET);
if(bmp_info.biBitCount == 24)
{
fread(data,size,1,fp);
for(int i = 0; i < size; i+=3)
{
/*data2[i+2] = (unsigned char)fgetc(fp);
data2[i+1] = (unsigned char)fgetc(fp);
data2 = (unsigned char)fgetc(fp);*/
unsigned char temp=data;
data=data[i+2];
data[i+2]=temp;
}
}
fclose(fp);
return true;
}
else
return false;
}
fclose(fp);
return false;
}
int BMP::getWidth()
{
return bmp_info.biWidth;
}
int BMP::getHeight()
{
return bmp_info.biHeight;
}
unsigned char *BMP::getData()
{
return data;
}