home *** CD-ROM | disk | FTP | other *** search
- #include <windows.h>
- #include <fstream.h>
-
- #include "bmpdecod.h"
-
- static BitmapImage image ;
-
- LRESULT CALLBACK WindowProc(
- HWND window,
- UINT message,
- WPARAM wparam,
- LPARAM lparam)
- {
- switch (message)
- {
- case WM_PAINT:
- {
- PAINTSTRUCT paintstruct ;
- HDC devicecontext = BeginPaint (window, &paintstruct) ;
- if (image.Width () > 0 && image.Height () > 0)
- {
- // The largest possible Bitmap Info Header
- char buffer [sizeof (BITMAPINFOHEADER) + 256 * sizeof (RGBQUAD)] ;
- BITMAPINFO *bitmapinfo = (BITMAPINFO *) buffer ;
- bitmapinfo->bmiHeader.biSize = sizeof (BITMAPINFOHEADER) ;
- bitmapinfo->bmiHeader.biWidth = image.Width () ;
- bitmapinfo->bmiHeader.biHeight = image.Height () ;
- bitmapinfo->bmiHeader.biPlanes = 1 ;
- bitmapinfo->bmiHeader.biBitCount = (WORD) image.BitCount () ;
- bitmapinfo->bmiHeader.biCompression = BI_RGB ;
- bitmapinfo->bmiHeader.biSizeImage = 0 ;
- bitmapinfo->bmiHeader.biXPelsPerMeter = 0 ;
- bitmapinfo->bmiHeader.biYPelsPerMeter = 0 ;
- bitmapinfo->bmiHeader.biClrUsed = 0 ;
- bitmapinfo->bmiHeader.biClrImportant = 0 ;
- for (unsigned int ii = 0 ; ii < image.ColorCount () ; ++ ii)
- {
- bitmapinfo->bmiColors [ii].rgbRed = image.ColorMap (ii).red ;
- bitmapinfo->bmiColors [ii].rgbGreen = image.ColorMap (ii).green ;
- bitmapinfo->bmiColors [ii].rgbBlue = image.ColorMap (ii).blue ;
- bitmapinfo->bmiColors [ii].rgbReserved = 0 ;
- }
-
- SetDIBitsToDevice (devicecontext,
- 10,
- 10,
- image.Width (),
- image.Height (),
- 0,
- 0,
- 0,
- bitmapinfo->bmiHeader.biHeight,
- image.ImageData (),
- bitmapinfo,
- DIB_RGB_COLORS) ;
- }
- EndPaint (window, &paintstruct) ;
- }
- return 0 ;
-
- case WM_DESTROY:
- PostQuitMessage (0) ;
- return 0 ;
- }
- return DefWindowProc (window, message, wparam, lparam) ;
- }
-
- int WINAPI WinMain(
- HINSTANCE Instance,
- HINSTANCE PrevInstance,
- LPSTR CommandLine,
- int ShowState)
- {
- static const char windowclassname [] = "ImageViewer" ;
-
-
- // Read the bitmap file and set up the image for display.
- ifstream strm ;
- strm.open (CommandLine, ios::binary) ;
- if (strm.bad ())
- return 0 ;
- BmpDecoder bmp ;
- try
- {
- bmp.ReadImage (strm, image) ;
- }
- catch (EGraphicsException &ee)
- {
- MessageBox (0, ee.what (), "Image Viewer", MB_OK|MB_ICONERROR) ;
- throw ;
- }
-
- if (PrevInstance == 0)
- {
- WNDCLASS windowclass ;
- windowclass.style = CS_HREDRAW | CS_VREDRAW ;
- windowclass.lpfnWndProc = WindowProc ;
- windowclass.cbClsExtra = 0 ;
- windowclass.cbWndExtra = 0 ;
- windowclass.hInstance = Instance ;
- windowclass.hIcon = LoadIcon (NULL, IDI_APPLICATION) ;
- windowclass.hCursor = LoadCursor (NULL, IDC_CROSS) ;
- windowclass.hbrBackground = GetStockObject (WHITE_BRUSH) ;
- windowclass.lpszMenuName = NULL ;
- windowclass.lpszClassName = windowclassname ;
- RegisterClass (&windowclass) ;
- }
-
- HWND window ;
- window = CreateWindow (
- windowclassname,
- "Image Viewer",
- WS_OVERLAPPEDWINDOW,
- CW_USEDEFAULT,
- CW_USEDEFAULT,
- CW_USEDEFAULT,
- CW_USEDEFAULT,
- NULL,
- NULL,
- Instance,
- NULL) ;
-
- ShowWindow (window, ShowState) ;
- MSG msg ;
- while (GetMessage (&msg, NULL, 0, 0))
- {
- TranslateMessage (&msg) ;
- DispatchMessage (&msg) ;
- }
-
- return msg.wParam ;
- }
-
-