Code:
bool LoadBitmapFile(string filename, unsigned char **pixelmap, int &width, int &height)
{
if(filename.empty()) return false;
int j=0; //Index variable
FILE *l_file; //File pointer
// windows.h gives us these types to work with the Bitmap files
BITMAPFILEHEADER fileheader;
BITMAPINFOHEADER infoheader;
RGBTRIPLE rgbt; //store one pixel at a time in here
l_file = fopen(filename.c_str(), "rb");
if(!l_file) return false; // Open the file for reading
fread(&fileheader, sizeof(fileheader), 1, l_file); // Read the fileheader
fseek(l_file, sizeof(fileheader), SEEK_SET);
fread(&infoheader, sizeof(infoheader), 1, l_file);
if(fileheader.bfType != 0x4D42){
printf("***BMP load error: bad file format***\n");
return false;
}
if(infoheader.biBitCount != 24){
printf("***BMP load error: invalid color depth***\n");
return false;
}
if(infoheader.biCompression != BI_RGB){
printf("BMP image is compressed\n");
return false;
}
width = infoheader.biWidth; //assign width value
height = infoheader.biHeight; //assign height value
(*pixelmap) = (unsigned char*)malloc(width*height*3);
//ZeroMemory(*pixelmap, width*height*3);
unsigned char padding;
for (int i = 0; i < infoheader.biWidth*infoheader.biHeight; i++)
{
fread(&rgbt, sizeof(RGBTRIPLE), 1, l_file);
(*pixelmap)[j+0] = rgbt.rgbtRed; // Red component
(*pixelmap)[j+1] = rgbt.rgbtGreen; // Green component
(*pixelmap)[j+2] = rgbt.rgbtBlue; // Blue component
j += 3; // Go to the next position
if(i % infoheader.biWidth == 0){
for(int n = 0; n < infoheader.biWidth % 4; n++)
fread(&padding, 1, 1, l_file);
}
}
fclose(l_file); //closes the file stream
return true; //returns true if it was successful
}
//This is the message posted when the user says file open... obviously...
case CTRL_FILE_OPEN:
{
if(hCanvas != NULL) DestroyWindow(hCanvas); //destroy any canvas window
OPENFILENAME ofn;
char szFileName[100] = "";
ZeroMemory(&ofn, sizeof(ofn));
ofn.lStructSize = sizeof(ofn);
ofn.hwndOwner = hwnd;
ofn.lpstrFilter = "24-bit Bitmap (*.bmp)\0*.bmp\0All Files (*.*)\0*.*\0";
ofn.lpstrFile = szFileName;
ofn.nMaxFile = MAX_PATH;
ofn.Flags = OFN_EXPLORER | OFN_FILEMUSTEXIST | OFN_HIDEREADONLY | OFN_ENABLESIZING;
ofn.lpstrDefExt = "bmp";
//ofn.lpstrInitialDir = "C:\\"; //might use this later on
if(GetOpenFileName(&ofn))
{
SendMessage(hStatus,SB_SETTEXT,0,(LPARAM)"Loading bitmap");
//filename is stored in lpstrFile
unsigned char* rgb;
int l_width, l_height;
if(hCanvas != NULL) DestroyWindow(hCanvas); //destroy any previous window
if(!LoadBitmapFile(ofn.lpstrFile, &rgb, l_width, l_height))
printf("Error: cannot open image file");
else
printf("Image pixel map loaded\n");
hCanvas = CreateWindowEx(0,"STATIC",NULL,WS_CHILD|WS_VISIBLE,0,0,l_width,l_height,
hwnd,(HMENU)HCANVAS,GetModuleHandle(NULL),NULL);
char temp[25];
sprintf_s(temp, 25, "(%i, %i)", l_width, l_height); //set up width and height to display to status bar
SendMessage(hStatus,SB_SETTEXT,1,(LPARAM)temp);
SendMessage(hStatus,SB_SETTEXT,0,(LPARAM)"Loading successful");
g_Canvas.SetCanvas(rgb, l_width, l_height);
if(g_Canvas.empty()) printf("Error: canvas is still empty\n");
else printf("Canvas is set\n");
SetWindowPos(g_MDIChildWindow, HWND_TOP, 0, 0, g_Canvas.Width(), g_Canvas.Height(), SWP_SHOWWINDOW);
UpdateCanvasImage = true;
InvalidateRect(hwnd, 0, true);
//PostMessage(hwnd, WM_PAINT, (UINT)g_Canvas.Width(), (UINT)g_Canvas.Height() );
delete [] rgb;
}
}
break;
//This part didn't indent entirely right, but you can get the idea
case WM_PAINT:
//hdc = BeginPaint(hwnd, &ps);
//EndPaint(hwnd, &ps);
//InvalidateRect(hCanvas, 0, false);
if(UpdateCanvasImage){
printf("UpdateCanvasImage = true\n");
unsigned int error;
//InvalidateRect(hCanvas, 0, true);
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hwnd, &ps);
//hdc = GetDC(hCanvas);
for(int y = 0; y < g_Canvas.Height(); y++){
for(int x = 0; x < g_Canvas.Width(); x++){
unsigned char RGB[3];
error = g_Canvas.GetPixel(x, y, PIXEL_R, RGB);
if(error != ERR_SUCCESS) printf("Error obtaining pixel: %i\n", error);
SetPixel(hdc, x, g_Canvas.Height() - y, RGB(RGB[0], RGB[1], RGB[2]));
}
}
EndPaint(hwnd, &ps);
//ReleaseDC(hwnd, hdc);
PostMessage(hwnd, RECEIVE_MSG, MSG_SETREADY, 0);
UpdateCanvasImage = false;
}
break;