home *** CD-ROM | disk | FTP | other *** search
/ Compressed Image File Formats / CompressedImageFileFormatsJohnMiano.iso / pc / Examples / c02 / src / viewer.cpp < prev   
Encoding:
C/C++ Source or Header  |  1998-12-18  |  4.7 KB  |  161 lines

  1. //
  2. // Copyright (c) 1997,1998 Colosseum Builders, Inc.
  3. // All rights reserved.
  4. //
  5. // Colosseum Builders, Inc. makes no warranty, expressed or implied
  6. // with regards to this software. It is provided as is.
  7. //
  8. // See the README.TXT file that came with this software for restrictions
  9. // on the use and redistribution of this file or send E-mail to
  10. // info@colosseumbuilders.com
  11. //
  12.  
  13. //
  14. // Title: Simple Windows BMP Viewer
  15. //
  16. // Author:  John M. Miano miano@colosseumbuilders.com
  17. //
  18. // Description:
  19. //
  20. //  This application is a simple BMP file viewer for Windows.
  21.  
  22. #include <windows.h>
  23. #include <fstream>
  24. using namespace std ;
  25.  
  26. #include "bmpdecod.h"
  27.  
  28. static BitmapImage image ;
  29.  
  30. LRESULT CALLBACK WindowProc(
  31.                   HWND  window,
  32.                   UINT  message,
  33.                   WPARAM  wparam,
  34.                   LPARAM  lparam)
  35. {
  36.   switch (message)
  37.   {
  38.   case WM_PAINT:
  39.     {
  40.       PAINTSTRUCT paintstruct ;
  41.       HDC devicecontext = BeginPaint (window, &paintstruct) ;
  42.       if (image.Width () > 0 && image.Height () > 0)
  43.       {
  44.         // The largest possible Bitmap Info Header
  45.         char buffer [sizeof (BITMAPINFOHEADER) + 256 * sizeof (RGBQUAD)] ;
  46.         BITMAPINFO *bitmapinfo = (BITMAPINFO *) buffer ;
  47.         bitmapinfo->bmiHeader.biSize = sizeof (BITMAPINFOHEADER) ;
  48.         bitmapinfo->bmiHeader.biWidth = image.Width () ;
  49.         bitmapinfo->bmiHeader.biHeight = image.Height () ;
  50.         bitmapinfo->bmiHeader.biPlanes = 1 ;
  51.         bitmapinfo->bmiHeader.biBitCount = (WORD) image.BitCount () ;
  52.         bitmapinfo->bmiHeader.biCompression = BI_RGB ;
  53.         bitmapinfo->bmiHeader.biSizeImage = 0 ;
  54.         bitmapinfo->bmiHeader.biXPelsPerMeter = 0 ;
  55.         bitmapinfo->bmiHeader.biYPelsPerMeter = 0 ;
  56.         bitmapinfo->bmiHeader.biClrUsed = 0 ;
  57.         bitmapinfo->bmiHeader.biClrImportant = 0 ;
  58.         for (unsigned int ii = 0 ; ii < image.ColorCount () ; ++ ii)
  59.         {
  60.           bitmapinfo->bmiColors [ii].rgbRed = image.ColorMap (ii).red ;
  61.           bitmapinfo->bmiColors [ii].rgbGreen = image.ColorMap (ii).green ;
  62.           bitmapinfo->bmiColors [ii].rgbBlue = image.ColorMap (ii).blue ;
  63.           bitmapinfo->bmiColors [ii].rgbReserved = 0 ;
  64.         }
  65.  
  66.         SetDIBitsToDevice (devicecontext,
  67.                            10,
  68.                            10,
  69.                            image.Width (),
  70.                            image.Height (),
  71.                            0,
  72.                            0,
  73.                            0,
  74.                            bitmapinfo->bmiHeader.biHeight,
  75.                            image.ImageData (),
  76.                            bitmapinfo,
  77.                            DIB_RGB_COLORS) ;
  78.       }
  79.       EndPaint (window, &paintstruct) ;
  80.     }
  81.     return 0 ;
  82.  
  83.   case WM_DESTROY:
  84.     PostQuitMessage (0) ;
  85.     return 0 ;
  86.   }
  87.   return DefWindowProc (window, message, wparam, lparam) ;
  88. }
  89.  
  90. int WINAPI WinMain(
  91.               HINSTANCE  Instance,
  92.               HINSTANCE  PrevInstance,
  93.               LPSTR  CommandLine,
  94.               int  ShowState)
  95. {
  96.   static const char windowclassname [] = "ImageViewer" ;
  97.  
  98.  
  99.   // Read the bitmap file and set up the image for display.
  100.   ifstream strm (CommandLine, ios::binary) ;
  101.   if (! strm)
  102.   {
  103.     string msg = "Can't open input file '" ;
  104.     msg += CommandLine ;
  105.     msg += "'" ;
  106.     MessageBox (0, msg.c_str (), "Image Viewer", 0) ;
  107.     return 1 ;
  108.   }
  109.   BmpDecoder  bmp ;
  110.   try
  111.   {
  112.     bmp.ReadImage (strm, image) ;
  113.   }
  114.   catch (EGraphicsException &ee)
  115.   {
  116.     MessageBox (0, ee.what (), "Image Viewer", MB_OK|MB_ICONERROR) ;
  117.     throw ;
  118.   }
  119.  
  120.   if (PrevInstance == 0)
  121.   {
  122.     WNDCLASS windowclass ;
  123.     windowclass.style = CS_HREDRAW | CS_VREDRAW ;
  124.     windowclass.lpfnWndProc = WindowProc ;
  125.     windowclass.cbClsExtra = 0 ;
  126.     windowclass.cbWndExtra = 0 ;
  127.     windowclass.hInstance = Instance ;
  128.     windowclass.hIcon = LoadIcon (NULL, IDI_APPLICATION) ;
  129.     windowclass.hCursor = LoadCursor (NULL, IDC_CROSS) ;
  130.     windowclass.hbrBackground = GetStockObject (WHITE_BRUSH) ;
  131.     windowclass.lpszMenuName = NULL ;
  132.     windowclass.lpszClassName = windowclassname ;
  133.     RegisterClass (&windowclass) ;
  134.   }
  135.  
  136.   HWND window ;
  137.   window = CreateWindow (
  138.               windowclassname,
  139.               "Image Viewer",
  140.               WS_OVERLAPPEDWINDOW,
  141.               CW_USEDEFAULT,
  142.               CW_USEDEFAULT,
  143.               CW_USEDEFAULT,
  144.               CW_USEDEFAULT,
  145.               NULL,
  146.               NULL,
  147.               Instance,
  148.               NULL) ;
  149.  
  150.   ShowWindow (window, ShowState) ;
  151.   MSG msg ;
  152.   while (GetMessage (&msg, NULL, 0, 0))
  153.   {
  154.     TranslateMessage (&msg) ;
  155.     DispatchMessage (&msg) ;
  156.   }
  157.  
  158.   return msg.wParam ;
  159. }
  160.  
  161.