home *** CD-ROM | disk | FTP | other *** search
- //
- // Copyright (c) 1997,1998 Colosseum Builders, Inc.
- // All rights reserved.
- //
- // Colosseum Builders, Inc. makes no warranty, expressed or implied
- // with regards to this software. It is provided as is.
- //
- // See the README.TXT file that came with this software for restrictions
- // on the use and redistribution of this file or send E-mail to
- // info@colosseumbuilders.com
- //
-
- //
- // Title: Simple Windows BMP Viewer
- //
- // Author: John M. Miano miano@colosseumbuilders.com
- //
- // Description:
- //
- // This application is a simple BMP file viewer for Windows.
-
- #include <windows.h>
- #include <fstream>
- using namespace std ;
-
- #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 (CommandLine, ios::binary) ;
- if (! strm)
- {
- string msg = "Can't open input file '" ;
- msg += CommandLine ;
- msg += "'" ;
- MessageBox (0, msg.c_str (), "Image Viewer", 0) ;
- return 1 ;
- }
- 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 ;
- }
-
-