home *** CD-ROM | disk | FTP | other *** search
/ Windows Graphics Programming / Feng_Yuan_Win32_GDI_DirectX.iso / Samples / include / PalLoadBitmap.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2000-05-12  |  3.8 KB  |  132 lines

  1. //-----------------------------------------------------------------------------------//
  2. //              Windows Graphics Programming: Win32 GDI and DirectDraw               //
  3. //                             ISBN  0-13-086985-6                                   //
  4. //                                                                                   //
  5. //  Written            by  Yuan, Feng                             www.fengyuan.com   //
  6. //  Copyright (c) 2000 by  Hewlett-Packard Company                www.hp.com         //
  7. //  Published          by  Prentice Hall PTR, Prentice-Hall, Inc. www.phptr.com      //
  8. //                                                                                   //
  9. //  FileName   : palloadbitmap.cpp                                                     //
  10. //  Description: Load a bitmap under a logical palette                               //
  11. //  Version    : 1.00.000, May 31, 2000                                              //
  12. //-----------------------------------------------------------------------------------//
  13.  
  14. #define STRICT
  15. #define WIN32_LEAN_AND_MEAN
  16.  
  17. #include <windows.h>
  18. #include <assert.h>
  19. #include <tchar.h>
  20.  
  21. #include "palloadbitmap.h"
  22.  
  23. static int GetDIBColorCount(const BITMAPINFOHEADER & bmih)
  24. {
  25.     if ( bmih.biBitCount <= 8 )
  26.         if ( bmih.biClrUsed )
  27.             return bmih.biClrUsed;
  28.         else
  29.             return 1 << bmih.biBitCount;
  30.     else if ( bmih.biCompression==BI_BITFIELDS )
  31.         return 3 + bmih.biClrUsed;
  32.     else
  33.         return bmih.biClrUsed;
  34. }
  35.  
  36. static BYTE * GetDIBPixelArray(BITMAPINFO * pDIB)
  37. {
  38.     return (BYTE *) & pDIB->bmiColors[GetDIBColorCount(pDIB->bmiHeader)];
  39. }
  40.  
  41.  
  42. BITMAPINFO * LoadDIB(HINSTANCE hInst, LPCTSTR pBitmapName, bool & bNeedFree)
  43. {
  44.     HRSRC         hRes = FindResource(hInst, pBitmapName, RT_BITMAP);
  45.     BITMAPINFO * pDIB;
  46.     
  47.     if ( hRes )
  48.     {
  49.         HGLOBAL hGlobal = LoadResource(hInst, hRes);
  50.         pDIB = (BITMAPINFO *) LockResource(hGlobal);
  51.  
  52.         bNeedFree = false;
  53.     }
  54.     else
  55.     {
  56.         HANDLE handle = CreateFile(pBitmapName, GENERIC_READ, FILE_SHARE_READ, 
  57.             NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
  58.     
  59.         if ( handle == INVALID_HANDLE_VALUE )
  60.             return NULL;
  61.  
  62.         BITMAPFILEHEADER bmFH;
  63.  
  64.         DWORD dwRead = 0;
  65.         ReadFile(handle, & bmFH, sizeof(bmFH), & dwRead, NULL);
  66.  
  67.         if ( (bmFH.bfType == 0x4D42) && (bmFH.bfSize<=GetFileSize(handle, NULL)) )
  68.         {
  69.             pDIB = (BITMAPINFO *) new BYTE[bmFH.bfSize];
  70.         
  71.             if ( pDIB )
  72.             {
  73.                 bNeedFree = true;
  74.                 ReadFile(handle, pDIB, bmFH.bfSize, & dwRead, NULL);
  75.             }
  76.         }
  77.         CloseHandle(handle);
  78.     }
  79.  
  80.     return pDIB;
  81. }
  82.  
  83.  
  84. HBITMAP PaletteLoadBitmap(HINSTANCE hInst, LPCTSTR pBitmapName, HPALETTE hPalette)
  85. {
  86.     bool bDIBNeedFree;
  87.     BITMAPINFO * pDIB = LoadDIB(hInst, pBitmapName, bDIBNeedFree);
  88.  
  89.     int width     = pDIB->bmiHeader.biWidth;
  90.     int height    = pDIB->bmiHeader.biHeight;
  91.  
  92.     HDC hMemDC    = CreateCompatibleDC(NULL);
  93.     HBITMAP hBmp  = CreateBitmap(width, height, GetDeviceCaps(hMemDC, PLANES), GetDeviceCaps(hMemDC, BITSPIXEL), NULL);
  94.     
  95.     HGDIOBJ hOldBmp = SelectObject(hMemDC, hBmp);
  96.  
  97.     HPALETTE hOld = SelectPalette(hMemDC, hPalette, FALSE);
  98.     RealizePalette(hMemDC);
  99.  
  100.     SetStretchBltMode(hMemDC, HALFTONE);
  101.     StretchDIBits(hMemDC, 0, 0, width, height, 0, 0, width, height, GetDIBPixelArray(pDIB), pDIB, DIB_RGB_COLORS, SRCCOPY);
  102.  
  103.     SelectPalette(hMemDC, hOld, FALSE);
  104.     SelectObject(hMemDC, hOldBmp);
  105.     DeleteObject(hMemDC);
  106.     
  107.     if ( bDIBNeedFree )
  108.         delete [] (BYTE *) pDIB;
  109.  
  110.     return hBmp;
  111. }
  112.  
  113.  
  114. HPALETTE CreateSystemPalette(void)
  115. {
  116.     LOGPALETTE * pLogPal = (LOGPALETTE *) new char[sizeof(LOGPALETTE) + sizeof(PALETTEENTRY) * 255];
  117.  
  118.     pLogPal->palVersion    = 0x300;
  119.     pLogPal->palNumEntries = 256;
  120.  
  121.     HDC hDC = GetDC(NULL);
  122.  
  123.     GetSystemPaletteEntries(hDC, 0, 256, pLogPal->palPalEntry);
  124.     
  125.     ReleaseDC(NULL, hDC);
  126.  
  127.     HPALETTE hPal = CreatePalette(pLogPal);
  128.     delete [] (char *) pLogPal;
  129.  
  130.     return hPal;
  131. }
  132.