home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / sdk / graphics / gdi / winnt / plgblt / bitmap.c next >
C/C++ Source or Header  |  1997-10-05  |  6KB  |  175 lines

  1.  
  2. /******************************************************************************\
  3. *       This is a part of the Microsoft Source Code Samples. 
  4. *       Copyright (C) 1992-1997 Microsoft Corporation.
  5. *       All rights reserved. 
  6. *       This source code is only intended as a supplement to 
  7. *       Microsoft Development Tools and/or WinHelp documentation.
  8. *       See these sources for detailed information regarding the 
  9. *       Microsoft samples programs.
  10. \******************************************************************************/
  11.  
  12. /**************************************************************************\
  13. *  bitmap.c -- support for reading in and drawing bitmaps.
  14. \**************************************************************************/
  15.  
  16. #include <windows.h>
  17. #include <commdlg.h>
  18. #include "plgblt.h"
  19.  
  20. /**************************************************************************\
  21. *
  22. *  function:  DrawBitmap()
  23. *
  24. *  input parameters:  HDC, HBITMAP
  25. *
  26. *  Draw the bitmap into the hdc.  Source rectangle computed to include the
  27. *   whole bitmap.  Destination location is 0,0.
  28. *
  29. *  global variables: none.
  30. *
  31. \**************************************************************************/
  32. VOID DrawBitmap (HDC hdc, HBITMAP hbm)
  33. {
  34.     BOOL f;
  35.     HDC hdcBits;
  36.     BITMAP bm;
  37.  
  38.     hdcBits = CreateCompatibleDC(hdc);
  39.     GetObject (hbm, sizeof(BITMAP), &bm);
  40.     SelectObject(hdcBits,hbm);
  41.     f = BitBlt(hdc,0,0,bm.bmWidth, bm.bmHeight,hdcBits,0,0,SRCCOPY);
  42.     DeleteDC(hdcBits);
  43. }
  44.  
  45.  
  46.  
  47. /**************************************************************************\
  48. *
  49. *  function:  GetBitmap()
  50. *
  51. *  input parameters:
  52. *   hdc - hdc to make the bitmap compatible with.
  53. *   hInst - instance handle
  54. *
  55. *  Put up a common dialog box to open a new *.BMP file.
  56. *   Once this is complete, open the file, read in the information,
  57. *   and create a compatible bitmap.
  58. *
  59. *  returns:  handle to the bitmap iff successful.  NULL otherwise.
  60. *
  61. \**************************************************************************/
  62. HBITMAP GetBitmap (HDC hdc, HANDLE hInst, BOOL monochrome)
  63. {
  64.     HBITMAP hbm;
  65.     PBITMAPFILEHEADER pbmfh;
  66.     PBITMAPINFOHEADER pbmih;
  67.     PBYTE             pBits;
  68.     int fh;
  69.     int bfOffBits;
  70.     int nbytes;
  71.     OPENFILENAME  of;
  72.     char buffer [MAX_PATH];
  73.  
  74.     buffer[0] = 0;
  75.  
  76.     /* set up the OPENFILE structure,
  77.      *  then use the appropriate common dialog
  78.      */
  79.     of.lStructSize       = sizeof (OPENFILENAME);
  80.     of.hwndOwner         = NULL;
  81.     of.hInstance         = hInst;
  82.     of.lpstrCustomFilter = NULL;
  83.     of.nMaxCustFilter    = 0;
  84.     of.nFilterIndex      = 0;
  85.     of.lpstrFile         = buffer;
  86.     of.nMaxFile          = MAX_PATH;
  87.     of.lpstrFileTitle    = NULL;
  88.     of.nMaxFileTitle     = 0;
  89.     of.lpstrInitialDir   = "c:\\nt\\windows";
  90.     of.lpstrTitle        = NULL;
  91.     of.Flags             = OFN_HIDEREADONLY;
  92.     of.nFileOffset       = 0;
  93.     of.nFileExtension    = 0;
  94.     of.lpstrDefExt       = NULL;
  95.     of.lCustData         = 0;
  96.     of.lpfnHook          = NULL;
  97.     of.lpTemplateName    = NULL;
  98.  
  99.     if (PRIMARYLANGID(GetUserDefaultLangID ()) == LANG_JAPANESE)
  100.       of.lpstrFilter       = "╦▐»─╧»╠▀(*.bmp)\000 *.BMP\000\000";
  101.     else
  102.       of.lpstrFilter       = "Bitmaps\000 *.BMP\000\000";
  103.  
  104.     if (!GetOpenFileName (&of)) return NULL;
  105.  
  106.     /* Try to open the file.  If successful, then allocate space for it,
  107.      *  and read in all of the bytes.
  108.      */
  109.     fh = _lopen (buffer, OF_READ);
  110.     if (fh == -1) return NULL;
  111.     nbytes = GetFileSize ((HANDLE) fh, NULL);
  112.  
  113.     /* The contents of the file are read in here in three parts.  First
  114.      *  the bitmap file header is read, then the bitmap header along with
  115.      *  the color table, then finally the actual bit data.  I.e. from
  116.      *  a total of nbytes...
  117.      *    1.  sizeof (BITMAPFILEHEADER)
  118.      *    2.  bfOffBits- sizeof (BITMAPFILEHEADER)
  119.      *    3.  (nbytes - bfOffBits)
  120.      */
  121.  
  122.     /* read in the bitmap file header.  save the offset to bits. */
  123.     if (!(pbmfh = (PBITMAPFILEHEADER)LocalAlloc (LPTR, sizeof (BITMAPFILEHEADER))))
  124.         return NULL;
  125.     _lread (fh, (LPSTR)pbmfh, sizeof (BITMAPFILEHEADER));
  126.     bfOffBits=pbmfh->bfOffBits;
  127.  
  128.     /* read in the bitmap info header and the color table right after it.
  129.      * both BITMAPINFOHEADER and BITMAPINFO needed for CreateDIBitmap()
  130.      */
  131.     if (!(pbmih = (PBITMAPINFOHEADER)LocalAlloc (LPTR, bfOffBits- sizeof (BITMAPFILEHEADER))))
  132.         return NULL;
  133.     _lread (fh, (LPSTR)pbmih, bfOffBits- sizeof (BITMAPFILEHEADER));
  134.  
  135.     /* finally read in the bit data. */
  136.     if (!(pBits = (PBYTE)LocalAlloc (LPTR, (nbytes - bfOffBits)))) return NULL;
  137.     _lread (fh, (LPSTR)pBits, (nbytes - bfOffBits));
  138.  
  139.  
  140.     /* in the case of desiring a monochrome bitmap (input parameter),
  141.      *  verify that is what we got.  If it is, then use CreateBitmap,
  142.      *  else use a device independent bitmap.
  143.      */
  144.     if (monochrome) {
  145.       if (pbmih->biBitCount != 1) {
  146.         MessageBox (NULL,
  147.           GetStringRes (IDS_MONOCHROME),
  148.           NULL, MB_APPLMODAL | MB_ICONSTOP | MB_OK);
  149.         hbm = NULL;
  150.       } else {
  151.     BITMAP bmpInf;
  152.     memset( &bmpInf, 0, sizeof(bmpInf) );
  153.     bmpInf.bmWidth = pbmih->biWidth;
  154.     bmpInf.bmHeight = pbmih->biHeight;
  155.     bmpInf.bmWidthBytes = ((pbmih->biWidth+31)&(~31))/8;
  156.     bmpInf.bmPlanes = pbmih->biPlanes;
  157.     bmpInf.bmBitsPixel = pbmih->biBitCount;
  158.     bmpInf.bmBits = pBits;
  159.  
  160.         hbm = CreateBitmapIndirect ( &bmpInf );
  161.       }
  162.     } else  /* bitmap is NOT monochrome, use DIB. */
  163.       hbm = CreateDIBitmap (hdc, pbmih, CBM_INIT,
  164.                           pBits, (PBITMAPINFO) pbmih, DIB_RGB_COLORS);
  165.  
  166.  
  167.     /* hbm is set... free up memory, and return */
  168.     LocalFree (LocalHandle ((LPSTR)pBits));
  169.     LocalFree (LocalHandle ((LPSTR)pbmih));
  170.     LocalFree (LocalHandle ((LPSTR)pbmfh));
  171.     _lclose (fh);
  172.  
  173.     return hbm;
  174. }
  175.