home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c480 / 19.ddi / SAMPLES / DIBVIEW / PAINT.C_ / PAINT.C
Encoding:
C/C++ Source or Header  |  1993-02-08  |  10.1 KB  |  299 lines

  1. /*************************************************************************
  2.  
  3.       File:  PAINT.C
  4.  
  5.    Purpose:  Contains the routines to do rendering of device dependent
  6.              bitmaps (DDBs), device independent bitmaps (DIBs), and
  7.              DDBs->DIBs via SetDIBits then DIB->device.
  8.  
  9.              These routines are called by CHILD.C to do the actual
  10.              bitmap rendering.
  11.  
  12.  Functions:  DIBPaint
  13.              DDBPaint
  14.              SetDIBitsPaint
  15.  
  16.   Comments:  Note that a commercial application would probably only
  17.              want to use DDBs, since they are much faster when rendering.
  18.              The DIB and SetDIBits() routines are here to demonstrate
  19.              how to manipulate rendering of DIBs, and DDB->DIB->Screen,
  20.              which has some useful applications.
  21.  
  22.    History:   Date      Reason
  23.              6/ 1/91    Created
  24.  
  25. *************************************************************************/
  26.  
  27.  
  28. #include "master.h"
  29.  
  30.  
  31.  
  32.  
  33.  
  34. //---------------------------------------------------------------------
  35. //
  36. // Function:   DIBPaint
  37. //
  38. // Purpose:    Painting routine for a DIB.  Calls StretchDIBits() or
  39. //             SetDIBitsToDevice() to paint the DIB.  The DIB is
  40. //             output to the specified DC, at the coordinates given
  41. //             in lpDCRect.  The area of the DIB to be output is
  42. //             given by lpDIBRect.  The specified palette is used.
  43. //
  44. // Parms:      hDC       == DC to do output to.
  45. //             lpDCRect  == Rectangle on DC to do output to.
  46. //             hDIB      == Handle to global memory with a DIB spec
  47. //                          in it (either a BITMAPINFO or BITMAPCOREINFO
  48. //                          followed by the DIB bits).
  49. //             lpDIBRect == Rect of DIB to output into lpDCRect.
  50. //             hPal      == Palette to be used.
  51. //
  52. // History:   Date      Reason
  53. //             6/01/91  Created
  54. //
  55. //---------------------------------------------------------------------
  56.  
  57. void DIBPaint (HDC hDC,
  58.             LPRECT lpDCRect,
  59.             HANDLE hDIB,
  60.             LPRECT lpDIBRect,
  61.           HPALETTE hPal)
  62. {
  63.    LPSTR    lpDIBHdr, lpDIBBits;
  64.  
  65.    if (!hDIB)
  66.       return;
  67.  
  68.  
  69.       // Lock down the DIB, and get a pointer to the beginning of the bit
  70.       //  buffer.
  71.  
  72.    lpDIBHdr  = GlobalLock (hDIB);
  73.    lpDIBBits = FindDIBBits (lpDIBHdr);
  74.  
  75.  
  76.       // Make sure to use the stretching mode best for color pictures.
  77.  
  78.    SetStretchBltMode (hDC, COLORONCOLOR);
  79.  
  80.  
  81.       // Determine whether to call StretchDIBits() or SetDIBitsToDevice().
  82.  
  83.    if ((RECTWIDTH (lpDCRect)  == RECTWIDTH (lpDIBRect)) &&
  84.        (RECTHEIGHT (lpDCRect) == RECTHEIGHT (lpDIBRect)))
  85.       {
  86.       SetDIBitsToDevice (hDC,                          // hDC
  87.                          lpDCRect->left,               // DestX
  88.                          lpDCRect->top,                // DestY
  89.                          RECTWIDTH (lpDCRect),         // nDestWidth
  90.                          RECTHEIGHT (lpDCRect),        // nDestHeight
  91.                          lpDIBRect->left,              // SrcX
  92.                          (int) DIBHeight (lpDIBHdr) -
  93.                            lpDIBRect->top -
  94.                            RECTHEIGHT (lpDIBRect),     // SrcY
  95.                          0,                            // nStartScan
  96.                          (WORD) DIBHeight (lpDIBHdr),  // nNumScans
  97.                          lpDIBBits,                    // lpBits
  98.                          (LPBITMAPINFO) lpDIBHdr,      // lpBitsInfo
  99.                          DIB_RGB_COLORS);              // wUsage
  100.       }
  101.    else
  102.       StretchDIBits (hDC,                            // hDC
  103.                      lpDCRect->left,                 // DestX
  104.                      lpDCRect->top,                  // DestY
  105.                      RECTWIDTH (lpDCRect),           // nDestWidth
  106.                      RECTHEIGHT (lpDCRect),          // nDestHeight
  107.                      lpDIBRect->left,                // SrcX
  108.                      lpDIBRect->top,                 // SrcY
  109.                      RECTWIDTH (lpDIBRect),          // wSrcWidth
  110.                      RECTHEIGHT (lpDIBRect),         // wSrcHeight
  111.                      lpDIBBits,                      // lpBits
  112.                      (LPBITMAPINFO) lpDIBHdr,        // lpBitsInfo
  113.                      DIB_RGB_COLORS,                 // wUsage
  114.                      SRCCOPY);                       // dwROP
  115.  
  116.    GlobalUnlock (hDIB);
  117. }
  118.  
  119.  
  120.  
  121.  
  122.  
  123. //---------------------------------------------------------------------
  124. //
  125. // Function:   DDBPaint
  126. //
  127. // Purpose:    Painting routine for a DDB.  Calls BitBlt() or
  128. //             StretchBlt() to paint the DDB.  The DDB is
  129. //             output to the specified DC, at the coordinates given
  130. //             in lpDCRect.  The area of the DDB to be output is
  131. //             given by lpDDBRect.  The specified palette is used.
  132. //
  133. //             IMPORTANT assumption:  The palette has been realized
  134. //             elsewhere...  We won't bother figuring out whether it
  135. //             should be realized as a foreground or background palette
  136. //             here.
  137. //
  138. // Parms:      hDC       == DC to do output to.
  139. //             lpDCRect  == Rectangle on DC to do output to.
  140. //             hDDB      == Handle to the device dependent bitmap (DDB).
  141. //             lpDDBRect == Rect of DDB to output into lpDCRect.
  142. //             hPal      == Palette to be used.
  143. //
  144. // History:   Date      Reason
  145. //             6/01/91  Created
  146. //
  147. //---------------------------------------------------------------------
  148.  
  149. void DDBPaint (HDC hDC,
  150.             LPRECT lpDCRect,
  151.            HBITMAP hDDB,
  152.             LPRECT lpDDBRect,
  153.           HPALETTE hPal)
  154. {
  155.    HDC      hMemDC;
  156.    HBITMAP  hOldBitmap;
  157.    HPALETTE hOldPal1 = NULL;
  158.    HPALETTE hOldPal2 = NULL;
  159.  
  160.  
  161.    hMemDC = CreateCompatibleDC (hDC);
  162.  
  163.    if (!hMemDC)
  164.       return;
  165.  
  166.    if (hPal)
  167.       {
  168.       hOldPal1   = SelectPalette (hMemDC, hPal, FALSE);
  169.       hOldPal2   = SelectPalette (hDC, hPal, FALSE);
  170.       // Assume the palette's already been realized (no need to
  171.       //  call RealizePalette().  It should have been realized in
  172.       //  our WM_QUERYNEWPALETTE or WM_PALETTECHANGED messages...
  173.       }
  174.  
  175.    hOldBitmap = SelectObject (hMemDC, hDDB);
  176.  
  177.    SetStretchBltMode (hDC, COLORONCOLOR);
  178.  
  179.    if ((RECTWIDTH (lpDCRect)  == RECTWIDTH (lpDDBRect)) &&
  180.        (RECTHEIGHT (lpDCRect) == RECTHEIGHT (lpDDBRect)))
  181.       {
  182.       BitBlt (hDC,
  183.               lpDCRect->left,
  184.               lpDCRect->top,
  185.               lpDCRect->right - lpDCRect->left,
  186.               lpDCRect->bottom - lpDCRect->top,
  187.               hMemDC,
  188.               lpDDBRect->left,
  189.               lpDDBRect->top,
  190.               SRCCOPY);
  191.       }
  192.    else
  193.       StretchBlt (hDC,
  194.                   lpDCRect->left,
  195.                   lpDCRect->top,
  196.                   lpDCRect->right - lpDCRect->left,
  197.                   lpDCRect->bottom - lpDCRect->top,
  198.                   hMemDC,
  199.                   lpDDBRect->left,
  200.                   lpDDBRect->top,
  201.                   lpDDBRect->right - lpDDBRect->left,
  202.                   lpDDBRect->bottom - lpDDBRect->top,
  203.                   SRCCOPY);
  204.  
  205.    SelectObject (hMemDC, hOldBitmap);
  206.  
  207.    if (hOldPal1)
  208.       SelectPalette (hMemDC, hOldPal1, FALSE);
  209.  
  210.    if (hOldPal2)
  211.       SelectPalette (hDC, hOldPal2, FALSE);
  212.  
  213.    DeleteDC (hMemDC);
  214. }
  215.  
  216.  
  217.  
  218. //---------------------------------------------------------------------
  219. //
  220. // Function:   SetDIBitsPaint
  221. //
  222. // Purpose:    Paint routine used when the SetDIBits option is being
  223. //             used.  Routine first call SetDIBits() to convert a DIB
  224. //             to a DDB, then it calls DDBPaint.
  225. //
  226. //             NOTE:  This routine was included for two reasons -- first,
  227. //             to test drivers SetDIBits() functions.  Second, to demo
  228. //             such a technique.  Most applications wouldn't bother to
  229. //             do anything like this (why not just SetDIBitsToDevice()!
  230. //
  231. // Parms:      hDC       == DC to do output to.
  232. //             lpDCRect  == Rectangle on DC to do output to.
  233. //             hDIB      == Handle to global memory with a DIB spec
  234. //                          in it (either a BITMAPINFO or BITMAPCOREINFO
  235. //                          followed by the DIB bits).
  236. //             lpDIBRect == Rect of DIB to output into lpDCRect.
  237. //             hPal      == Palette to be used.
  238. //
  239. // History:   Date      Reason
  240. //             6/01/91  Created
  241. //
  242. //---------------------------------------------------------------------
  243.  
  244. void SetDIBitsPaint (HDC hDC,
  245.                   LPRECT lpDCRect,
  246.                   HANDLE hDIB,
  247.                   LPRECT lpDIBRect,
  248.                 HPALETTE hPal)
  249. {
  250.    LPSTR   lpDIBHdr, lpDIBBits;
  251.    HBITMAP hBitmap;
  252.  
  253.  
  254.       // Return if we don't have a DIB.
  255.  
  256.    if (!hDIB)
  257.       return;
  258.  
  259.  
  260.  
  261.       // Lock down the DIB, and get a pointer to the beginning of the bit
  262.       //  buffer.
  263.  
  264.    lpDIBHdr  = GlobalLock (hDIB);
  265.    lpDIBBits = FindDIBBits (lpDIBHdr);
  266.  
  267.  
  268.       // Create the DDB.  Note that the palette has already been
  269.       //  selected into the DC before calling this routine.
  270.  
  271.    hBitmap = CreateCompatibleBitmap (hDC,
  272.                                      (int) DIBWidth (lpDIBHdr),
  273.                                      (int) DIBHeight (lpDIBHdr));
  274.    if (!hBitmap)
  275.       {
  276.       DIBError (ERR_CREATEDDB);
  277.       GlobalUnlock (hDIB);
  278.       return;
  279.       }
  280.  
  281.    if (SetDIBits (hDC,                        // hDC compat. with DDB
  282.                   hBitmap,                    // handle to DDB
  283.                   0,                          // Start scan in lpBits
  284.                   (int) DIBHeight (lpDIBHdr), // Num Scans in lpBits
  285.                   lpDIBBits,                  // Pointer to bits
  286.                   (LPBITMAPINFO) lpDIBHdr,    // Pointer to DIB header
  287.                   DIB_RGB_COLORS)             // DIB contains RGBs in color table
  288.                      == 0)
  289.       DIBError (ERR_SETDIBITS);
  290.  
  291.  
  292.       // Call DDBPaint to paint the bitmap.  Then clean up.
  293.  
  294.    GlobalUnlock (hDIB);
  295.    DDBPaint (hDC, lpDCRect, hBitmap, lpDIBRect, hPal);
  296.    DeleteObject (hBitmap);
  297. }
  298.  
  299.