dib.cpp
Upload User: xhy777
Upload Date: 2007-02-14
Package Size: 24088k
Code Size: 7k
Category:

Windows Kernel

Development Platform:

Visual C++

  1. /////////////////////////////////////////////////////////////////////////////
  2. // DIB.CPP
  3. //
  4. // Implementation of CDIB
  5. //
  6. // History:
  7. //
  8. // Author   Date        Description
  9. // ------   ----        -----------
  10. // jaym     02/03/97    Created
  11. /////////////////////////////////////////////////////////////////////////////
  12. #include "precomp.h"
  13. #include "dib.h"
  14. /////////////////////////////////////////////////////////////////////////////
  15. // CDIB
  16. /////////////////////////////////////////////////////////////////////////////
  17. CDIB::CDIB
  18. (
  19. )
  20. {
  21.     m_pDIB = NULL;
  22.     m_hPalette = NULL;
  23. }
  24. CDIB::~CDIB
  25. (
  26. )
  27. {
  28.     Cleanup();
  29. }
  30. /////////////////////////////////////////////////////////////////////////////
  31. // CDIB::Cleanup
  32. /////////////////////////////////////////////////////////////////////////////
  33. void CDIB::Cleanup
  34. (
  35. )
  36. {
  37.     if (m_pDIB != NULL)
  38.     {
  39.         delete [] m_pDIB;
  40.         m_pDIB = NULL;
  41.     }
  42.     if (m_hPalette != NULL)
  43.     {
  44.         EVAL(DeleteObject(m_hPalette));
  45.         m_hPalette = NULL;
  46.     }
  47. }
  48. /////////////////////////////////////////////////////////////////////////////
  49. // CDIB::LoadFromResource
  50. /////////////////////////////////////////////////////////////////////////////
  51. BOOL CDIB::LoadFromResource
  52. (
  53.     HINSTANCE   hInstance,
  54.     WORD        wResID
  55. )
  56. {
  57.     BOOL bResult = FALSE;
  58.     for (;;)
  59.     {
  60.         ASSERT(m_pDIB == NULL);
  61.         if ((m_pDIB = (BITMAP *)GetResource(hInstance,
  62.                                             MAKEINTRESOURCE(wResID))) == NULL)
  63.         {
  64.             break;
  65.         }
  66.         ASSERT(m_hPalette == NULL);
  67.         if ((m_hPalette = GetPalette()) == NULL)
  68.             break;
  69.         bResult = TRUE;
  70.         break;
  71.     }
  72.     if (!bResult)
  73.         Cleanup();
  74.     return bResult;
  75. }
  76. /////////////////////////////////////////////////////////////////////////////
  77. // CDIB::Draw
  78. /////////////////////////////////////////////////////////////////////////////
  79. void CDIB::Draw
  80. (
  81.     HDC     hDC,
  82.     RECT *  prectSrc,
  83.     RECT *  prectDest
  84. )
  85. {
  86.     HPALETTE hPalOld = NULL;
  87.     // Bail early if there is nothing to draw.
  88.     if ((m_pDIB == NULL) || (prectDest == NULL))
  89.         return;
  90.     if (m_hPalette != NULL)
  91.     {
  92.         hPalOld = SelectPalette(hDC, m_hPalette, FALSE);
  93.         RealizePalette(hDC);
  94.     }
  95.     SetStretchBltMode(hDC, COLORONCOLOR);
  96.     SetDIBitsToDevice(  hDC, 
  97.                         prectDest->left,
  98.                         prectDest->top, 
  99.                         ((prectSrc == NULL)
  100.                             ? Width()
  101.                             : (prectSrc->right - prectSrc->left)),
  102.                         ((prectSrc == NULL)
  103.                             ? Height()
  104.                             : (prectSrc->bottom - prectSrc->top)),
  105.                         ((prectSrc == NULL)
  106.                             ? 0
  107.                             : prectSrc->left),
  108.                         ((prectSrc == NULL)
  109.                             ? 0
  110.                             : prectSrc->top),
  111.                         0,
  112.                         Height(),
  113.                         GetBitsAddr(),
  114.                         (BITMAPINFO *)m_pDIB,
  115.                         DIB_RGB_COLORS);
  116.     if (hPalOld != NULL)
  117.         SelectPalette(hDC, hPalOld, TRUE);
  118. }
  119. /////////////////////////////////////////////////////////////////////////////
  120. // CDIB::GetResource
  121. /////////////////////////////////////////////////////////////////////////////
  122. void * CDIB::GetResource
  123. (
  124.     HINSTANCE       hInstance,
  125.     const char *    pszBitmapName
  126. )
  127. {
  128.     HRSRC   hResDIB = NULL;
  129.     HGLOBAL hDIB = NULL;
  130.     void *  pResource = NULL;
  131.     void *  pDest = NULL;
  132.     // We will attempt to load the DIB as a resource and
  133.     // copy it to memory. This is done so we can free up
  134.     // the resource and still have access to the information.
  135.     if ((hResDIB = FindResource(hInstance,
  136.                                 pszBitmapName,
  137.                                 RT_BITMAP)) != NULL)
  138.     {
  139.         // We found the resource so load it up and save it for later.
  140.         DWORD dwSize = SizeofResource(hInstance, hResDIB);
  141.         if ((hDIB = LoadResource(hInstance, hResDIB)) != NULL)
  142.         {
  143.             // We have the bitmap. Make a copy and return the resource.
  144.             pResource = (void *) LockResource(hDIB);
  145.             if (pResource != NULL)
  146.             {
  147.                 pDest = new BYTE [dwSize];
  148.                 if (pDest != NULL)
  149.                     CopyMemory(pDest, pResource, dwSize);
  150.                 UnlockResource(hDIB);
  151.             }
  152.             FreeResource(hDIB);
  153.         }
  154.     }
  155.     return pDest;
  156. }
  157. /////////////////////////////////////////////////////////////////////////////
  158. // CDIB::GetPalette
  159. /////////////////////////////////////////////////////////////////////////////
  160. HPALETTE CDIB::GetPalette
  161. (
  162. )
  163. {
  164.     BITMAPINFOHEADER *  pBmpInfoHdr;
  165.     HANDLE              hPalMem;
  166.     LOGPALETTE *        pPal;
  167.     HPALETTE            hPal;
  168.     RGBQUAD *           pRGB;
  169.     int                 iColors;
  170.     pBmpInfoHdr = (BITMAPINFOHEADER *) m_pDIB;
  171.     if (pBmpInfoHdr->biSize != sizeof(BITMAPINFOHEADER))
  172.         return NULL;
  173.     pRGB = (RGBQUAD *)((LPSTR)pBmpInfoHdr + (WORD)pBmpInfoHdr->biSize);
  174.     if ((iColors = GetColorTableCount()) == 0)
  175.         return NULL;
  176.     hPalMem = LocalAlloc(LMEM_MOVEABLE,
  177.                          sizeof(LOGPALETTE) + iColors * sizeof(PALETTEENTRY));
  178.     if (!hPalMem)
  179.         return NULL;
  180.     pPal = (LOGPALETTE *) LocalLock(hPalMem);
  181.     pPal->palVersion    = 0x300;
  182.     pPal->palNumEntries = (WORD)iColors;
  183.     for (int i=0; i < iColors; i++)
  184.     {
  185.         pPal->palPalEntry[i].peRed      = pRGB[i].rgbRed;
  186.         pPal->palPalEntry[i].peGreen    = pRGB[i].rgbGreen;
  187.         pPal->palPalEntry[i].peBlue     = pRGB[i].rgbBlue;
  188.         pPal->palPalEntry[i].peFlags    = 0;
  189.     }
  190.     hPal = CreatePalette(pPal);
  191.     LocalUnlock(hPalMem);
  192.     LocalFree(hPalMem);
  193.     return hPal;
  194. }
  195. /////////////////////////////////////////////////////////////////////////////
  196. // CDIB::GetColorTableCount
  197. /////////////////////////////////////////////////////////////////////////////
  198. DWORD CDIB::GetColorTableCount
  199. (
  200. )
  201. {
  202.     DWORD dwNumColors;
  203.     if (m_pDIB == NULL)
  204.         return 0;
  205.     if (GetInfoHeaderSize() >= 36)
  206.         dwNumColors = ((BITMAPINFOHEADER *) m_pDIB)->biClrUsed;
  207.     else
  208.         dwNumColors = 0;
  209.     if (dwNumColors == 0)
  210.     {
  211.         WORD wBitCount = ((BITMAPINFOHEADER *) m_pDIB)->biBitCount;
  212.         if (wBitCount < 16)
  213.             dwNumColors = 1L << wBitCount;
  214.         else
  215.             dwNumColors = 0; // For 16, 24 and 32 bits we return 0
  216.     }
  217.     return dwNumColors;
  218. }
  219. /////////////////////////////////////////////////////////////////////////////
  220. // CDIB::GetBitsAddr
  221. /////////////////////////////////////////////////////////////////////////////
  222. void * CDIB::GetBitsAddr
  223. (
  224. )
  225. {
  226.     DWORD   dwColorTableSize;
  227.     BYTE *  pVoid;
  228.     dwColorTableSize = GetColorTableCount() * sizeof(RGBQUAD);
  229.     
  230.     // Compute the new addresse as a void so that we get true byte offsets
  231.     pVoid = (BYTE *) m_pDIB;
  232.     return (void *) (pVoid + GetInfoHeaderSize() + dwColorTableSize);
  233. }