home *** CD-ROM | disk | FTP | other *** search
/ Compressed Image File Formats / CompressedImageFileFormatsJohnMiano.iso / pc / Library / viewer / viewer.bak < prev    next >
Encoding:
Text File  |  1998-12-17  |  4.0 KB  |  134 lines

  1. #include <windows.h>
  2. #include <fstream.h>
  3.  
  4. #include "bmpdecod.h"
  5.  
  6. static BitmapImage image ;
  7.  
  8. LRESULT CALLBACK WindowProc(
  9.                   HWND  window,
  10.                   UINT  message,
  11.                   WPARAM  wparam,
  12.                   LPARAM  lparam)
  13. {
  14.   switch (message)
  15.   {
  16.   case WM_PAINT:
  17.     {
  18.       PAINTSTRUCT paintstruct ;
  19.       HDC devicecontext = BeginPaint (window, &paintstruct) ;
  20.       if (image.Width () > 0 && image.Height () > 0)
  21.       {
  22.         // The largest possible Bitmap Info Header
  23.         char buffer [sizeof (BITMAPINFOHEADER) + 256 * sizeof (RGBQUAD)] ;
  24.         BITMAPINFO *bitmapinfo = (BITMAPINFO *) buffer ;
  25.         bitmapinfo->bmiHeader.biSize = sizeof (BITMAPINFOHEADER) ;
  26.         bitmapinfo->bmiHeader.biWidth = image.Width () ;
  27.         bitmapinfo->bmiHeader.biHeight = image.Height () ;
  28.         bitmapinfo->bmiHeader.biPlanes = 1 ;
  29.         bitmapinfo->bmiHeader.biBitCount = (WORD) image.BitCount () ;
  30.         bitmapinfo->bmiHeader.biCompression = BI_RGB ;
  31.         bitmapinfo->bmiHeader.biSizeImage = 0 ;
  32.         bitmapinfo->bmiHeader.biXPelsPerMeter = 0 ;
  33.         bitmapinfo->bmiHeader.biYPelsPerMeter = 0 ;
  34.         bitmapinfo->bmiHeader.biClrUsed = 0 ;
  35.         bitmapinfo->bmiHeader.biClrImportant = 0 ;
  36.         for (unsigned int ii = 0 ; ii < image.ColorCount () ; ++ ii)
  37.         {
  38.           bitmapinfo->bmiColors [ii].rgbRed = image.ColorMap (ii).red ;
  39.           bitmapinfo->bmiColors [ii].rgbGreen = image.ColorMap (ii).green ;
  40.           bitmapinfo->bmiColors [ii].rgbBlue = image.ColorMap (ii).blue ;
  41.           bitmapinfo->bmiColors [ii].rgbReserved = 0 ;
  42.         }
  43.  
  44.         SetDIBitsToDevice (devicecontext,
  45.                            10,
  46.                            10,
  47.                            image.Width (),
  48.                            image.Height (),
  49.                            0,
  50.                            0,
  51.                            0,
  52.                            bitmapinfo->bmiHeader.biHeight,
  53.                            image.ImageData (),
  54.                            bitmapinfo,
  55.                            DIB_RGB_COLORS) ;
  56.       }
  57.       EndPaint (window, &paintstruct) ;
  58.     }
  59.     return 0 ;
  60.  
  61.   case WM_DESTROY:
  62.     PostQuitMessage (0) ;
  63.     return 0 ;
  64.   }
  65.   return DefWindowProc (window, message, wparam, lparam) ;
  66. }
  67.  
  68. int WINAPI WinMain(
  69.               HINSTANCE  Instance,
  70.               HINSTANCE  PrevInstance,
  71.               LPSTR  CommandLine,
  72.               int  ShowState)
  73. {
  74.   static const char windowclassname [] = "ImageViewer" ;
  75.  
  76.  
  77.   // Read the bitmap file and set up the image for display.
  78.   ifstream strm ;
  79.   strm.open (CommandLine, ios::binary) ;
  80.   if (strm.bad ())
  81.     return 0 ;
  82.   BmpDecoder  bmp ;
  83.   try
  84.   {
  85.     bmp.ReadImage (strm, image) ;
  86.   }
  87.   catch (EGraphicsException &ee)
  88.   {
  89.     MessageBox (0, ee.what (), "Image Viewer", MB_OK|MB_ICONERROR) ;
  90.     throw ;
  91.   }
  92.  
  93.   if (PrevInstance == 0)
  94.   {
  95.     WNDCLASS windowclass ;
  96.     windowclass.style = CS_HREDRAW | CS_VREDRAW ;
  97.     windowclass.lpfnWndProc = WindowProc ;
  98.     windowclass.cbClsExtra = 0 ;
  99.     windowclass.cbWndExtra = 0 ;
  100.     windowclass.hInstance = Instance ;
  101.     windowclass.hIcon = LoadIcon (NULL, IDI_APPLICATION) ;
  102.     windowclass.hCursor = LoadCursor (NULL, IDC_CROSS) ;
  103.     windowclass.hbrBackground = GetStockObject (WHITE_BRUSH) ;
  104.     windowclass.lpszMenuName = NULL ;
  105.     windowclass.lpszClassName = windowclassname ;
  106.     RegisterClass (&windowclass) ;
  107.   }
  108.  
  109.   HWND window ;
  110.   window = CreateWindow (
  111.               windowclassname,
  112.               "Image Viewer",
  113.               WS_OVERLAPPEDWINDOW,
  114.               CW_USEDEFAULT,
  115.               CW_USEDEFAULT,
  116.               CW_USEDEFAULT,
  117.               CW_USEDEFAULT,
  118.               NULL,
  119.               NULL,
  120.               Instance,
  121.               NULL) ;
  122.  
  123.   ShowWindow (window, ShowState) ;
  124.   MSG msg ;
  125.   while (GetMessage (&msg, NULL, 0, 0))
  126.   {
  127.     TranslateMessage (&msg) ;
  128.     DispatchMessage (&msg) ;
  129.   }
  130.  
  131.   return msg.wParam ;
  132. }
  133.  
  134.