#include "Bitmap.h"
Bitmap::Bitmap(){
reset();
}
Bitmap::Bitmap(char *file){
reset();
loadBMP(file);
}
Bitmap::~Bitmap(){
if(colours!=0) {
delete[] colours;
}
if(data!=0) {
delete[] data;
}
}
bool Bitmap::loadBMP(char *file) {
FILE *in;
char *tempData;
int numColours;
loaded=false;
if(colours!=0) {
delete[] colours;
}
if(data!=0) {
delete[] data;
}
in=fopen(file,"rb");
if(in==NULL) {
error="File not found";
fclose(in);
return false;
}
fread(&bmfh,sizeof(BitmapFileHeader),1,in);
cout << "sizeof(BitmapFileHeader)=" << sizeof(BitmapFileHeader) << endl;
if(bmfh.bfType!=BITMAP_MAGIC_NUMBER) {
error="File is not in DIB format";
fclose(in);
return false;
}
fread(&bmih,sizeof(BitmapInfoHeader),1,in);
cout << "sizeof(BitmapInfoHeader)=" << sizeof(BitmapInfoHeader) << endl;
width=bmih.biWidth;
height=bmih.biHeight;
bpp=bmih.biBitCount;
cout << "biBitCount =" << bmih.biBitCount << endl;
cout << "biClrImportant =" << bmih.biClrImportant << endl;
cout << "biClrUsed =" << bmih.biClrUsed << endl;
cout << "biCompression =" << bmih.biCompression << endl;
cout << "biHeight =" << bmih.biHeight << endl;
cout << "biPlanes =" << bmih.biPlanes << endl;
cout << "biSize =" << bmih.biSize << endl;
cout << "biSizeImage =" << bmih.biSizeImage << endl;
cout << "biWidth =" << bmih.biWidth << endl;
cout << "biXPelsPerMeter =" << bmih.biXPelsPerMeter << endl;
cout << "biYPelsPerMeter =" << bmih.biYPelsPerMeter << endl;
dataSize=(width*height*(unsigned int)(bmih.biBitCount/8.0));
numColours=1<<bmih.biBitCount;
if(bpp<8) {
error="File is not 8 or 24 bits per pixel";
fclose(in);
return false;
}
if(bpp==8) {
colours=new RGBQuad[numColours];
fread(colours,sizeof(RGBQuad),numColours,in);
}
tempData=new char[dataSize];
if(tempData==NULL) {
error="Not enough memory to allocate a temporary buffer";
fclose(in);
return false;
}
fread(tempData,sizeof(char),dataSize,in);
fclose(in);
byteWidth=padWidth=(int)((float)width*(float)bpp/8.0);
while(padWidth%4!=0) {
padWidth++;
}
if(bpp==8) {
loaded=convert8(tempData);
}
else if(bpp==24) {
loaded=convert24(tempData);
}
delete[] tempData;
error="Bitmap loaded";
return loaded;
}
void Bitmap::reset(void) {
loaded=false;
colours=0;
data=0;
error="";
}
bool Bitmap::convert24(char* tempData) {
int offset,diff;
diff=width*height*RGB_BYTE_SIZE;
data=new char[diff];
if(data==NULL) {
error="Not enough memory to allocate an image buffer";
delete[] data;
return false;
}
if(height>0) {
offset=padWidth-byteWidth;
for(int i=0;i<dataSize;i+=3) {
if((i+1)%padWidth==0) {
i+=offset;
}
*(data+i+2)=*(tempData+i);
*(data+i+1)=*(tempData+i+1);
*(data+i)=*(tempData+i+2);
}
}
else {
offset=padWidth-byteWidth;
int j=dataSize-3;
for(int i=0;i<dataSize;i+=3) {
if((i+1)%padWidth==0) {
i+=offset;
}
*(data+j+2)=*(tempData+i);
*(data+j+1)=*(tempData+i+1);
*(data+j)=*(tempData+i+2);
j-=3;
}
}
return true;
}
bool Bitmap::convert8(char* tempData) {
int offset,diff;
diff=width*height*RGB_BYTE_SIZE;
data=new char[diff];
if(data==NULL) {
error="Not enough memory to allocate an image buffer";
delete[] data;
return false;
}
if(height>0) {
offset=padWidth-byteWidth;
int j=0;
for(int i=0;i<dataSize*RGB_BYTE_SIZE;i+=3) {
if((i+1)%padWidth==0) {
i+=offset;
}
*(data+i)=colours[*(tempData+j)].rgbRed;
*(data+i+1)=colours[*(tempData+j)].rgbGreen;
*(data+i+2)=colours[*(tempData+j)].rgbBlue;
j++;
}
}
else {
offset=padWidth-byteWidth;
int j=dataSize-1;
for(int i=0;i<dataSize*RGB_BYTE_SIZE;i+=3) {
if((i+1)%padWidth==0) {
i+=offset;
}
*(data+i)=colours[*(tempData+j)].rgbRed;
*(data+i+1)=colours[*(tempData+j)].rgbGreen;
*(data+i+2)=colours[*(tempData+j)].rgbBlue;
j--;
}
}
return true;
}
errors:
[Linker error] undefined reference to `WinMain@16'
.....................
my header file:
#ifndef _BITMAP_H
#define _BITMAP_H
#include <iostream>
#include <cstdio>
#include <string>
using namespace std;
const short BITMAP_MAGIC_NUMBER=19778;
const int RGB_BYTE_SIZE=3;
#pragma pack(push,bitmap_data,1)
typedef struct tagRGBQuad {
char rgbBlue;
char rgbGreen;
char rgbRed;
char rgbReserved;
} RGBQuad;
typedef struct tagBitmapFileHeader {
unsigned short bfType;
unsigned int bfSize;
unsigned short bfReserved1;
unsigned short bfReserved2;
unsigned int bfOffBits;
} BitmapFileHeader;
typedef struct tagBitmapInfoHeader {
unsigned int biSize;
int biWidth;
int biHeight;
unsigned short biPlanes;
unsigned short biBitCount;
unsigned int biCompression;
unsigned int biSizeImage;
int biXPelsPerMeter;
int biYPelsPerMeter;
unsigned int biClrUsed;
unsigned int biClrImportant;
} BitmapInfoHeader;
#pragma pack(pop,bitmap_data)
class Bitmap {
public:
RGBQuad *colours;
char *data;
bool loaded;
int width,height;
unsigned short bpp;
string error;
Bitmap(void);
Bitmap(char *);
~Bitmap();
bool loadBMP(char *);
private:
BitmapFileHeader bmfh;
BitmapInfoHeader bmih;
int byteWidth;
int padWidth;
unsigned int dataSize;
void reset(void);
bool convert24(char *);
bool convert8(char *);
};
#endif _BITMAP_H
[edited by - TheNerd Tk421 on August 10, 2003 11:27:03 PM]