home *** CD-ROM | disk | FTP | other *** search
/ Windows Graphics Programming / Feng_Yuan_Win32_GDI_DirectX.iso / Samples / include / basicdib.cpp < prev    next >
C/C++ Source or Header  |  2000-05-12  |  3KB  |  107 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   : basicdib.cpp                                                         //
  10. //  Description: Basic DIB handling                                                  //
  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 "basicdib.h"
  19.  
  20.  
  21. BITMAPINFO * LoadBMP(HMODULE hModule, const TCHAR * pResName)
  22. {
  23.     HRSRC   hRes = FindResource(hModule, pResName, RT_BITMAP);
  24.  
  25.     if ( hRes==NULL )
  26.         return NULL;
  27.  
  28.     HGLOBAL hGlb = LoadResource(hModule, hRes);
  29.  
  30.     if ( hGlb==NULL )
  31.         return NULL;
  32.  
  33.     return (BITMAPINFO *) LockResource(hGlb);
  34. }
  35.  
  36.  
  37. int GetDIBColorCount(const BITMAPINFO * pDIB)
  38. {
  39.     if ( pDIB->bmiHeader.biBitCount <= 8 )
  40.         if ( pDIB->bmiHeader.biClrUsed )
  41.             return pDIB->bmiHeader.biClrUsed;
  42.         else
  43.             return 1 << pDIB->bmiHeader.biBitCount;
  44.     else if ( pDIB->bmiHeader.biCompression==BI_BITFIELDS )
  45.         return 3 + pDIB->bmiHeader.biClrUsed;
  46.     else
  47.         return pDIB->bmiHeader.biClrUsed;
  48. }
  49.  
  50.  
  51. BITMAPINFO * LoadBMPFile(const TCHAR * pFileName)
  52. {
  53.     if ( pFileName==NULL )
  54.         return NULL;
  55.  
  56.     HANDLE handle = CreateFile(pFileName, 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.     BITMAPINFO * pDIB = NULL;
  68.  
  69.     if ( (bmFH.bfType == 0x4D42) && (bmFH.bfSize<=GetFileSize(handle, NULL)) )
  70.     {
  71.         pDIB = (BITMAPINFO *) new BYTE[bmFH.bfSize];
  72.         
  73.         if ( pDIB )
  74.             ReadFile(handle, pDIB, bmFH.bfSize, & dwRead, NULL);
  75.     }
  76.     CloseHandle(handle);
  77.  
  78.     return pDIB;
  79. }
  80.  
  81. // 0.299 * red + 0.587 * green + 0.114 * blue 
  82. inline void MaptoGray(BYTE & red, BYTE & green, BYTE & blue)
  83. {    
  84.     red   = ( red * 77 + green * 150 + blue * 29 + 128 ) / 256;    
  85.     green = red;    
  86.     blue  = red;
  87. }
  88.  
  89. // map a 1-,2-,4-,8-bpp DIB to grayscale
  90. BOOL MaptoGray(BITMAPINFO * pDIB)
  91. {    
  92.     if ( pDIB->bmiHeader.biBitCount>8 ) // reject high-color, true-color
  93.         return FALSE;
  94.  
  95.     int nColor = pDIB->bmiHeader.biClrUsed;    // color used
  96.     
  97.     if ( nColor==0 )                           // zero for all    
  98.         nColor = 1 << pDIB->bmiHeader.biBitCount;    
  99.     
  100.     for (int i=0; i<nColor; i++)               // map each color to gray
  101.         MaptoGray(pDIB->bmiColors[i].rgbRed,
  102.                   pDIB->bmiColors[i].rgbGreen,
  103.                   pDIB->bmiColors[i].rgbBlue);
  104.  
  105.     return TRUE;
  106. }
  107.