home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / listings / v_11_09 / 1109046a < prev    next >
Text File  |  1993-05-03  |  5KB  |  189 lines

  1. Listing 4  Creating and Displaying a DIB
  2.  
  3.  
  4. /*
  5.   This simplied structure could be passed to the
  6.   display routine or a command structure could be
  7.   passed and the required data extracted from it
  8.   to fill in this structure.
  9. */
  10. struct BitMapRecord
  11. {
  12.  int     srcWidth;     /* Total source width (X).   */
  13.  int     srcHeight;    /*   "     "    height (Y).  */
  14.  HANDLE  srcBitsHdl;   /* Handle to raw bitmap.     */
  15.  BYTE    colorPlanes;
  16.  BYTE    BitsPerPixel;
  17.  RECT    displayRect;  /* Display destination rect. */
  18.  int     background;   /* Background brush value.   */
  19.  WORD    options
  20.  
  21.  RECT    mainWin;      /* Client display rectangle. */
  22.  HANDLE  DIBHndl;
  23.  pBITMAPINFO bmi;
  24. } dispRec;
  25.  
  26. /*--------------------------------------------------*/
  27.  
  28. /* Global variable(s): */
  29. HPALETTE newPalette;
  30.  
  31. /* Parameters passed to this routine: */
  32. HWND displayHWND;
  33.  
  34. /* Local variables to this routine: */
  35. HDC     hDC;
  36. HANDLE  hBrush;
  37. LPSTR   srcPtr;
  38. int     result,scanlines;
  39. HBITMAP dstBitMap;
  40. HANDLE  DIBHndl;
  41. LPSTR   DIBPtr;
  42.  
  43.         :
  44.         :
  45.  
  46. result = NO_ERROR;
  47.  
  48. GetClientRect(displayHWND,(LPRECT)&dispRec.mainWin);
  49.  
  50. hDC = GetDC(displayHWND);
  51. if (hDC != NULL)
  52. {
  53.  if (dispRec.options & PAL_CLEAR_BACKGROUND)
  54.  {
  55.   /* Fill background: */
  56.   hBrush = GetStockObject(dispRec.background);
  57.   if (hBrush != NULL)
  58.     FillRect(hDC,
  59.              (LPRECT)&dispRec.mainWin,
  60.              (HBRUSH)hBrush);
  61.  }
  62.  
  63.  srcPtr = (LPSTR)GlobalLock(dispRec.srcBitsHdl);
  64.  
  65.  if (srcPtr != NULL)
  66.  {
  67.   dstBitMap = CreateBitmap(dispRec.srcWidth,
  68.                            dispRec.srcHeight,
  69.                            dispRec.colorPlanes,
  70.                            dispRec.BitsPerPixel,
  71.                            srcPtr);
  72.  
  73.   if (dstBitMap != NULL)
  74.   {
  75.    /* Fill in pBITMAPINFO structure: */
  76.    dispRec.bmi.bmiHeader.biSize =
  77.       (DWORD)sizeof(BITMAPINFOHEADER);
  78.    dispRec.bmi.bmiHeader.biWidth =
  79.       (DWORD)dispRec.srcWidth;
  80.    dispRec.bmi.bmiHeader.biHeight =
  81.       (DWORD)dispRec.srcHeight;
  82.    dispRec.bmi.bmiHeader.biPlanes =
  83.       dispRec.colorPlanes;
  84.    dispRec.bmi.bmiHeader.biBitCount =
  85.       dispRec.BitsPerPixel;
  86.    dispRec.bmi.bmiHeader.biCompression = BI_RGB;
  87.    dispRec.bmi.bmiHeader.biSizeImage =
  88.       (DWORD)dispRec.srcWidth *
  89.       (DWORD)dispRec.srcHeight *
  90.       (DWORD)dispRec.BitsPerPixel;
  91.    dispRec.bmi.bmiHeader.biClrUsed = 0L;
  92.    dispRec.bmi.bmiHeader.biClrImportant = 0L;
  93.  
  94.    /*
  95.      Retrieve DIB size. The third parameter is the
  96.      start scan line in the source bitmap. The next
  97.      parameter is the number of scan lines to be
  98.      "formatted". The next parameter would normally
  99.      point to the buffer which would receive the
  100.      DIB data, but when it is set to NULL, only
  101.      the required size is returned (ie, no DIB is
  102.      generated).
  103.    */
  104.    scanlines = GetDIBits(hDC,
  105.                          dstBitMap,     
  106.                          0,             
  107.                          dispRec.srcHeight,
  108.                          NULL,
  109.                          (LPBITMAPINFO)&dispRec.bmi,
  110.                          DIB_PAL_COLORS);
  111.  
  112.    dispRec.DIBHndl =
  113.       GlobalAlloc(
  114.            (GMEM_DDESHARE|GMEM_MOVEABLE|GMEM_ZEROINIT),
  115.            (dispRec.bmi.bmiHeader.biSizeImage+4096));
  116.  
  117.    /*
  118.      The extra 4K added to the allocation above is
  119.      solely an old programmer's paranoia...
  120.    */
  121.  
  122.    if (dispRec.DIBHndl != NULL)
  123.    {
  124.     DIBPtr = (LPSTR)GlobalLock(dispRec.DIBHndl);
  125.  
  126.     if (DIBPtr != NULL)
  127.     {
  128.      /* Build actual DIB: */
  129.      scanlines = GetDIBits(hDC,
  130.                            dstBitMap,     
  131.                            0,             
  132.                            dispRec.srcHeight,
  133.                            DIBPtr,         
  134.                            (LPBITMAPINFO)&dispRec.bmi,
  135.                            DIB_PAL_COLORS);
  136.  
  137.      if (scanlines != 0)
  138.      {
  139.       /* Handle palette items: */
  140.       if (newPalette == NULL)
  141.         SetGrayScalePalette(displayHWND);
  142.  
  143.       if (SelectPalette(hDC,newPalette,0) != NULL)
  144.       {
  145.        SetSystemPaletteUse(hDC,SYSPAL_STATIC);
  146.        UnrealizeObject(newPalette);
  147.        RealizePalette(hDC);
  148.       }
  149.  
  150.       /* Perform "image" processing: */
  151.       BuildMapping((struct BitMapRec FAR *)&dispRec);
  152.  
  153.       /* Post actual image: */
  154.       if (SetDIBitsToDevice(hDC,
  155.                             dispRec.displayRect.left,
  156.                             dispRec.displayRect.top,
  157.                             dispRec.srcWidth, 
  158.                             dispRec.srcHeight,
  159.                             0,  /* for this example */
  160.                             0,  /*  "    "     "    */
  161.                             0,  /* start scan line  */
  162.                             dispRec.srcHeight,
  163.                             DIBPtr,
  164.                             (LPBITMAPINFO)&dispRec.bmi,
  165.                             DIB_PAL_COLORS) == 0)
  166.                             result = PAL_SDIBITS_ERROR;
  167.      }
  168.      else result = PAL_GETDIB2_ERROR;
  169.  
  170.      GlobalUnlock(dispRec.DIBHndl);
  171.  
  172.      ValidateRect(displayHWND,
  173.                   (LPRECT)&dispRec.mainWin);
  174.     }
  175.     else result = PAL_DIBPTR_ERROR;
  176.  
  177.     if (result != NO_ERROR)
  178.       GlobalFree(dispRec.DIBHndl);
  179.    }
  180.    else result = PAL_DIBALLOC_ERROR;
  181.  
  182.    DeleteObject(dstBitMap);
  183.   }
  184.   else result = PAL_CREATEBM_ERROR;
  185.  
  186.   GlobalUnlock(dispRec.srcBitsHdl);
  187.  }
  188.  else result = PAL_LOCKSRC_ERROR;
  189.