home *** CD-ROM | disk | FTP | other *** search
/ Windows Graphics Programming / Feng_Yuan_Win32_GDI_DirectX.iso / Samples / include / BitmapInfo.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2000-05-11  |  4.3 KB  |  135 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   : bitmapinfo.cpp                                                         //
  10. //  Description: Wrapper for BITMAPINFO structure                                    //
  11. //  Version    : 1.00.000, May 31, 2000                                              //
  12. //-----------------------------------------------------------------------------------//
  13.  
  14. #define STRICT
  15. #define WIN32_LEAN_AND_MEAN
  16. #include <windows.h>
  17. #include <assert.h>
  18. #include <tchar.h>
  19.  
  20. #include "BitmapInfo.h"
  21.  
  22. const RGBQUAD StandardColorTable16[] = {
  23.     { 0x00, 0x00, 0x00, 0x00 }, 
  24.     { 0x00, 0x00, 0x80, 0x00 },
  25.     { 0x00, 0x80, 0x00, 0x00 },
  26.     { 0x00, 0x80, 0x80, 0x00 },
  27.     { 0x80, 0x00, 0x00, 0x00 },
  28.     { 0x80, 0x00, 0x80, 0x00 },
  29.     { 0x80, 0x80, 0x00, 0x00 },
  30.     { 0xC0, 0xC0, 0xC0, 0x00 },
  31.     { 0x80, 0x80, 0x80, 0x00 },
  32.     { 0x00, 0x00, 0xFF, 0x00 },
  33.     { 0x00, 0xFF, 0x00, 0x00 },
  34.     { 0x00, 0xFF, 0xFF, 0x00 },
  35.     { 0xFF, 0x00, 0x00, 0x00 },
  36.     { 0xFF, 0x00, 0xFF, 0x00 },
  37.     { 0xFF, 0xFF, 0x00, 0x00 },
  38.     { 0xFF, 0xFF, 0xFF, 0x00 }
  39. };
  40.  
  41. // R G B
  42. const DWORD StandardMask555[] =  { 0x007C00, 0x0003E0, 0x00001F };
  43. const DWORD StandardMask565[] =  { 0x00F800, 0x0007E0, 0x00001F };
  44. const DWORD StandardMask888[] =  { 0xFF0000, 0x00FF00, 0x0000FF };
  45.  
  46.  
  47. void KBitmapInfo::SetFormat(int width, int height, int bitcount, int compression)
  48. {
  49.     // fill out BITMAPINFOHEADER based on size and required format
  50.     memset(&m_dibinfo, 0, sizeof(m_dibinfo));
  51.  
  52.     m_dibinfo.bmiHeader.biSize        = sizeof(BITMAPINFOHEADER);
  53.     m_dibinfo.bmiHeader.biWidth       = width;
  54.     m_dibinfo.bmiHeader.biHeight      = height;
  55.     m_dibinfo.bmiHeader.biPlanes      = 1;
  56.     m_dibinfo.bmiHeader.biBitCount    = bitcount;
  57.     m_dibinfo.bmiHeader.biCompression = compression;
  58.  
  59.     // set up standard palette
  60.     switch ( bitcount )
  61.     {
  62.         case 2:
  63.             m_dibinfo.bmiColors[0].rgbRed   = 0;    // black    
  64.             m_dibinfo.bmiColors[0].rgbGreen = 0;
  65.             m_dibinfo.bmiColors[0].rgbBlue  = 0;
  66.             m_dibinfo.bmiColors[1].rgbRed   = 0xFF; // white
  67.             m_dibinfo.bmiColors[1].rgbGreen = 0xFF;
  68.             m_dibinfo.bmiColors[1].rgbBlue  = 0xFF;
  69.  
  70.         case 4:
  71.             memcpy(m_dibinfo.bmiColors, StandardColorTable16, 16 * sizeof(RGBQUAD));
  72.             break;
  73.  
  74.         case 8: // halftone palette with 6*6*6=216 colors
  75.         {
  76.             for (int blue =0; blue <6; blue ++)
  77.             for (int green=0; green<6; green++)
  78.             for (int red  =0; red  <6; red  ++)
  79.             {
  80.                 int i = (blue * 6 + green) * 6 + red;
  81.  
  82.                 m_dibinfo.bmiColors[i].rgbRed   = red   * 55;
  83.                 m_dibinfo.bmiColors[i].rgbGreen = green * 55;
  84.                 m_dibinfo.bmiColors[i].rgbBlue  = blue  * 55;
  85.             }
  86.             break;
  87.         }
  88.  
  89.         case 16:
  90.             if ( compression==BI_BITFIELDS )
  91.                 memcpy(m_dibinfo.bmiColors, StandardMask555, sizeof(StandardMask555));
  92.             break;
  93.  
  94.         case 32:
  95.             if ( compression==BI_BITFIELDS )
  96.                 memcpy(m_dibinfo.bmiColors, StandardMask888, sizeof(StandardMask888));
  97.             break;
  98.     }
  99. }
  100.  
  101.  
  102. void KBitmapInfo::SetGrayScale(DWORD mask)
  103. {
  104.     int nColor = 1 << m_dibinfo.bmiHeader.biBitCount;
  105.  
  106.     for (int i=0; i<nColor; i++)
  107.     {
  108.         m_dibinfo.bmiColors[i].rgbRed        = i & GetRValue(mask);
  109.         m_dibinfo.bmiColors[i].rgbGreen        = i & GetGValue(mask);
  110.         m_dibinfo.bmiColors[i].rgbBlue        = i & GetBValue(mask);
  111.         m_dibinfo.bmiColors[i].rgbReserved  = 0;
  112.     }        
  113. }
  114.  
  115.  
  116. BITMAPINFO * KBitmapInfo::CopyBMI(void) const
  117. {
  118.     int nColor;
  119.  
  120.     if ( m_dibinfo.bmiHeader.biCompression==BI_BITFIELDS )
  121.         nColor = 3;
  122.     else if ( m_dibinfo.bmiHeader.biBitCount <= 8 )
  123.         nColor = 1 << m_dibinfo.bmiHeader.biBitCount;
  124.     else
  125.         nColor = 0;
  126.  
  127.     int nSize = sizeof(BITMAPINFOHEADER) + sizeof(RGBQUAD) * nColor;
  128.  
  129.     BITMAPINFO * pBMI = (BITMAPINFO *) new BYTE[nSize];
  130.  
  131.     if ( pBMI )
  132.         memcpy(pBMI, & m_dibinfo, nSize);
  133.     
  134.     return pBMI;
  135. }