home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / WIN_NT / MASK2.ZIP / BITMAP.C next >
Encoding:
C/C++ Source or Header  |  1993-01-12  |  5.1 KB  |  157 lines

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