PDA

View Full Version : Need someone to look over my Class



Brandon
03-17-2012, 02:29 PM
This is gunna be extremely hard.. but I need someone to look over my bitmap class or help me get it to load.. the drawing works fine.. the loading from file does not.. It loads from the file but I cannot get a Handle on the bitmap or get the data from it into a buffer for writing later.. With the code below, it draws black :S but yet it prints out the correct info of the bitmap :S


EDIT: Solved it.. Stupid File Pointer -_______- Took me 2 days to figure it out. One line of code! wtf..




#ifndef BITMAPS_H_INCLUDED
#define BITMAPS_H_INCLUDED

typedef struct
{
BYTE R, G, B;
} RGB, *PRGB;

typedef struct
{
BYTE R, G, B, A;
} RGBA, *PRGBA;

class Bitmap
{
private:
int width;
int height;
BYTE* Bits;

BITMAPFILEHEADER bFileHeader;
BITMAPINFOHEADER bInfoHeader;

protected:
HDC DC;
HBITMAP Image;

public:
~Bitmap()
{
if (Bits)
delete[] Bits;
DeleteDC(DC);
DeleteObject(Image);
}

Bitmap(int Width, int Height) : width(Width), height(Height)
{
BITMAPINFO Info;
memset(&Info, 0, sizeof(BITMAPINFO));
Info.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
Info.bmiHeader.biPlanes = 1;
Info.bmiHeader.biBitCount = 32;
Info.bmiHeader.biCompression = BI_RGB;
Info.bmiHeader.biWidth = width;
Info.bmiHeader.biHeight = -height;
DC = CreateCompatibleDC(0);
Image = CreateDIBSection(DC, &Info, DIB_RGB_COLORS, (void**)&Bits, 0, 0);
SelectObject(DC, Image);
}

Bitmap(int Width, int Height, string Data) : width(Width), height(Height)
{
}

Bitmap(const char* FilePath) //Credits to Mark Bernard For Starting Me on the right track.
{
HANDLE hFile;
hFile = CreateFile(FilePath, GENERIC_READ, FILE_SHARE_READ, 0, OPEN_EXISTING, 0, 0);
if (hFile == 0)
{
cout<<"Error Bitmap Does Not Exist";
return;
}

DWORD Read;
ReadFile(hFile, &bFileHeader, sizeof(bFileHeader), &Read, 0);
ReadFile(hFile, &bInfoHeader, sizeof(bInfoHeader), &Read, 0);

if (bInfoHeader.biBitCount < 32)
{
CloseHandle(hFile);
MessageBox(NULL, "The Image Loaded cannot be less than 32bits.", "Error", 0);
return;
}

if (bFileHeader.bfType != 0x4D42)
{
CloseHandle(hFile);
MessageBox(NULL, "The File Is Not A Bitmap.", "Error", 0);
return;
}

if (bInfoHeader.biCompression != BI_RGB)
{
CloseHandle(hFile);
return;
}

cout << "biBitCount =" << bInfoHeader.biBitCount << endl;
cout << "biClrImportant =" << bInfoHeader.biClrImportant << endl;
cout << "biClrUsed =" << bInfoHeader.biClrUsed << endl;
cout << "biCompression =" << bInfoHeader.biCompression << endl;
cout << "biHeight =" << bInfoHeader.biHeight << endl;
cout << "biPlanes =" << bInfoHeader.biPlanes << endl;
cout << "biSize =" << bInfoHeader.biSize << endl;
cout << "biSizeImage =" << bInfoHeader.biSizeImage << endl;
cout << "biWidth =" << bInfoHeader.biWidth << endl;
cout << "biXPelsPerMeter =" << bInfoHeader.biXPelsPerMeter << endl;
cout << "biYPelsPerMeter =" << bInfoHeader.biYPelsPerMeter << endl;

int WidthBytes = (bInfoHeader.biWidth * bInfoHeader.biBitCount + 31) / 32 * 4;
width = bInfoHeader.biWidth;
height = bInfoHeader.biHeight;


BITMAPINFO Info;
memset(&Info, 0, sizeof(BITMAPINFO));
Info.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
Info.bmiHeader.biPlanes = 1;
Info.bmiHeader.biBitCount = 32;
Info.bmiHeader.biCompression = BI_RGB;
Info.bmiHeader.biWidth = width;
Info.bmiHeader.biHeight = -height;
Info.bmiHeader.biSizeImage = bInfoHeader.biSizeImage;

Bits = new BYTE[Info.bmiHeader.biSizeImage];
SetFilePointer(hFile, bFileHeader.bfOffBits, 0, FILE_BEGIN);
ReadFile(hFile, Bits, Info.bmiHeader.biSizeImage, &Read, 0);

DC = GetDC(0);
Image = CreateDIBitmap(DC, &Info.bmiHeader, CBM_INIT, Bits, &Info, DIB_RGB_COLORS);
CloseHandle(hFile);
}

bool DrawBitmap(HDC DContext)
{
if ((DContext == NULL))
return false;

HDC dcmem = CreateCompatibleDC(NULL);

if (SelectObject(dcmem, Image) == NULL)
{
DeleteObject(Image);
DeleteDC(dcmem);
return false;
}

BITMAP bm;
GetObject(Image, sizeof(bm), &bm);
if (BitBlt(DContext, 0, 0, bm.bmWidth, bm.bmHeight, dcmem, 0, 0, SRCCOPY) == 0)
{
DeleteObject(Image);
DeleteDC(dcmem);
return false;
}

DeleteDC(dcmem);
return true;
}

HDC getDC(){return DC;}

int GetWidth(){return width;}

int GetHeight(){return height;}

void GetSize(int& Width, int& Height){Width = width; Height = height;}
};

#endif // BITMAPS_H_INCLUDED