home *** CD-ROM | disk | FTP | other *** search
/ The Elite Hackers Toolkit / TheEliteHackersToolkitVolume1_1998.rar / HACKERS.BIN / appcraks / P5WGLIDE.ZIP / DIBSECTI.CPP < prev    next >
C/C++ Source or Header  |  1998-01-03  |  6KB  |  234 lines

  1. #include <windows.h>
  2.  
  3. // Justin's nifty DIBSection class for Win32. Feel free to use this in your programs.
  4. // Just email justin@nullsoft.com if you like 'em.
  5. // (And I don't want any bug reports! If you have bugfixes, though, send em my way) :)
  6. // Copr. 1997-1998, Justin Frankel/Nullsoft, Inc.
  7.  
  8. #include "DIBSection.h"
  9.  
  10.  
  11. /* 
  12. ** BASE CLASS
  13. */
  14.  
  15. void CDIBSection::m_DeInitialize()
  16. {
  17.     if (m_hdc) {
  18.         ::SelectObject(m_hdc, m_oldhbm);
  19.         ::DeleteObject(m_hbm);
  20.         ::DeleteDC(m_hdc);
  21.     }
  22.     m_hdc = 0;
  23.     m_hbm = 0;
  24. }
  25.  
  26. void CDIBSection::SetRect(RECT *r)
  27. {
  28.     m_DeInitialize();
  29.     m_Initialize(m_hwndParent,r);
  30. }
  31.  
  32. void CDIBSection::Update()
  33. {
  34.     HDC wndDC = ::GetDC(m_hwndParent);
  35.     m_PreRender(wndDC);
  36.     {
  37.         RECT r;
  38.         ::GetWindowRect(m_hwndParent,&r);
  39.         ::BitBlt(wndDC,0,0,GetWidth(),GetHeight(),m_hdc,0,0,SRCCOPY);
  40.     }
  41.     ::ReleaseDC(m_hwndParent,wndDC);
  42. }
  43.  
  44. void CDIBSection::Paint(HDC hdc)
  45. {
  46.     m_PreRender(hdc);
  47.     ::BitBlt(hdc,m_rect.left,m_rect.top,GetWidth(),GetHeight(),m_hdc,0,0,SRCCOPY);
  48. }
  49.  
  50.  
  51.  
  52. /*
  53. ** 8BPP CLASS
  54. */
  55.  
  56. CDIBSection8::CDIBSection8(HWND hwnd,RECT *r,unsigned char *palette)
  57. {
  58.     RECT r2;
  59.     if (palette) ::memcpy(m_pal,palette,sizeof(m_pal));
  60.     if (!r) ::GetClientRect(hwnd,&r2);
  61.     else r2 = *r;
  62.     m_Initialize(hwnd,&r2);
  63. }
  64.  
  65. CDIBSection8::~CDIBSection8()
  66. {
  67.     m_DeInitialize();
  68. }
  69.  
  70. void CDIBSection8::m_DeInitialize()
  71. {
  72.     if (m_hpal) ::DeleteObject(m_hpal);
  73.     m_hpal = 0;
  74.     CDIBSection::m_DeInitialize();
  75. }
  76.  
  77. void CDIBSection8::m_Initialize(HWND hwndParent, RECT *r)
  78. {    
  79.     m_rect = *r;
  80.     int width = m_rect.right-m_rect.left;
  81.     int height = m_rect.bottom-m_rect.top;
  82.     if (width & 3) width += 3;
  83.     width &= ~3;
  84.     m_hwndParent = hwndParent;
  85.     m_SetupPalette();
  86.     m_bitmap.bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
  87.     m_bitmap.bmi.bmiHeader.biPlanes = 1;
  88.     m_bitmap.bmi.bmiHeader.biBitCount = 8;
  89.     m_bitmap.bmi.bmiHeader.biCompression = BI_RGB;
  90.     m_bitmap.bmi.bmiHeader.biSizeImage = 0;
  91.     m_bitmap.bmi.bmiHeader.biClrUsed = 0;
  92.     m_bitmap.bmi.bmiHeader.biClrImportant = 0;
  93.     m_bitmap.bmi.bmiHeader.biWidth = width;
  94.     m_bitmap.bmi.bmiHeader.biHeight = height;
  95.     m_bitmap.bmi.bmiHeader.biSizeImage = width*height;
  96.     m_hdc = ::CreateCompatibleDC(NULL);
  97.     m_hbm = ::CreateDIBSection(m_hdc,&m_bitmap.bmi,DIB_RGB_COLORS, &m_bitmap.data, NULL, 0);
  98.     m_oldhbm = (HBITMAP) ::SelectObject(m_hdc, m_hbm);
  99. }
  100.  
  101. void CDIBSection8::m_SetupPalette()
  102. {
  103.     int c;
  104.     unsigned char *pal = m_pal;
  105.     struct 
  106.     {
  107.         LOGPALETTE logpal;
  108.         PALETTEENTRY palentries[255];
  109.     } palette;
  110.     palette.logpal.palVersion = 0x300;
  111.     palette.logpal.palNumEntries = 256;
  112.  
  113.     for (c = 0; c < 256; c ++) 
  114.     {
  115.         m_bitmap.bmi.bmiColors[c].rgbRed =
  116.             palette.logpal.palPalEntry[c].peRed = *pal++;
  117.         m_bitmap.bmi.bmiColors[c].rgbGreen =
  118.             palette.logpal.palPalEntry[c].peGreen = *pal++;
  119.         m_bitmap.bmi.bmiColors[c].rgbBlue =
  120.             palette.logpal.palPalEntry[c].peBlue = *pal++;
  121.         m_bitmap.bmi.bmiColors[c].rgbReserved = 0;
  122.         palette.logpal.palPalEntry[c].peFlags = 0;
  123.     }
  124.     if (m_hpal) ::DeleteObject(m_hpal);
  125.     m_hpal = ::CreatePalette((LOGPALETTE *)&palette.logpal);
  126.     HDC dc = ::GetDC(NULL);
  127.     ::SelectPalette(dc,m_hpal,FALSE);
  128.     ::RealizePalette(dc);
  129.     ::UpdateColors(dc);
  130.     ::ReleaseDC(NULL,dc);
  131. }
  132.  
  133. void CDIBSection8::SetPalette(unsigned char *pal)
  134. {
  135.     HWND hwnd = m_hwndParent;
  136.     RECT r = m_rect;
  137.     ::memcpy(m_pal,pal,sizeof(m_pal));
  138.     m_SetupPalette();
  139.     ::DeleteObject(SelectObject(m_hdc, m_oldhbm));
  140.     m_hbm = ::CreateDIBSection(m_hdc,&m_bitmap.bmi,DIB_RGB_COLORS, &m_bitmap.data, NULL, 0);
  141.     ::SelectObject(m_hdc, m_hbm);
  142. }
  143.  
  144. void CDIBSection8::m_PreRender(HDC hdc)
  145. {
  146.     ::SelectPalette(hdc,m_hpal,FALSE);
  147.     ::RealizePalette(hdc);
  148. }
  149.  
  150. /*
  151. ** 16BPP VERSION
  152. */
  153.  
  154. CDIBSection16::CDIBSection16(HWND hwnd,RECT *r)
  155. {
  156.     RECT r2;
  157.     if (!r) ::GetClientRect(hwnd,&r2);
  158.     else r2 = *r;
  159.     m_Initialize(hwnd,&r2);
  160. }
  161.  
  162. CDIBSection16::~CDIBSection16()
  163. {
  164.     m_DeInitialize();
  165. }
  166.  
  167. void CDIBSection16::m_Initialize(HWND hwndParent, RECT *r)
  168. {    
  169.     m_rect = *r;
  170.     int width = m_rect.right-m_rect.left;
  171.     int height = m_rect.bottom-m_rect.top;
  172.     if (width & 3) width += 3;
  173.     width &= ~3;
  174.     m_hwndParent = hwndParent;
  175.     m_bitmap.bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
  176.     m_bitmap.bmi.bmiHeader.biPlanes = 1;
  177.     m_bitmap.bmi.bmiHeader.biBitCount = 16;
  178.     m_bitmap.bmi.bmiHeader.biCompression = BI_BITFIELDS;
  179.     m_bitmap.bmi.bmiHeader.biSizeImage = 0;
  180.     m_bitmap.bmi.bmiHeader.biClrUsed = 0;
  181.     m_bitmap.bmi.bmiHeader.biClrImportant = 0;
  182.     m_bitmap.bmi.bmiHeader.biWidth = width;
  183.     m_bitmap.bmi.bmiHeader.biHeight = -height;
  184.     m_bitmap.bmi.bmiHeader.biSizeImage = 0;
  185.     unsigned int *a = (unsigned int *) m_bitmap.bmi.bmiColors;
  186.     a[0] = 0xF800;
  187.     a[1] = 0x7E0;
  188.     a[2] = 0x1f;
  189.     m_hdc = ::CreateCompatibleDC(NULL);
  190.     m_hbm = ::CreateDIBSection(m_hdc,&m_bitmap.bmi,DIB_RGB_COLORS, &m_bitmap.data, NULL, 0);
  191.     m_oldhbm = (HBITMAP) ::SelectObject(m_hdc, m_hbm);
  192.     m_SetupPalette();
  193. }
  194.  
  195. void CDIBSection16::m_SetupPalette()
  196. {
  197.     int c;
  198.     struct 
  199.     {
  200.         LOGPALETTE logpal;
  201.         PALETTEENTRY palentries[255];
  202.     } palette;
  203.     palette.logpal.palVersion = 0x300;
  204.     palette.logpal.palNumEntries = 256;
  205.  
  206.     for (c = 0; c < 256; c ++) 
  207.     {
  208.         palette.logpal.palPalEntry[c].peRed = c;
  209.         palette.logpal.palPalEntry[c].peGreen = c;
  210.         palette.logpal.palPalEntry[c].peBlue = c;
  211.         palette.logpal.palPalEntry[c].peFlags = 0;
  212.     }
  213.     if (m_hpal) ::DeleteObject(m_hpal);
  214.     m_hpal = ::CreatePalette((LOGPALETTE *)&palette.logpal);
  215.     HDC dc = ::GetDC(NULL);
  216.     ::SelectPalette(dc,m_hpal,FALSE);
  217.     ::RealizePalette(dc);
  218.     ::UpdateColors(dc);
  219.     ::ReleaseDC(NULL,dc);
  220. }
  221.  
  222. void CDIBSection16::m_DeInitialize()
  223. {
  224.     if (m_hpal) ::DeleteObject(m_hpal);
  225.     m_hpal = 0;
  226.     CDIBSection::m_DeInitialize();
  227. }
  228.  
  229. void CDIBSection16::m_PreRender(HDC hdc)
  230. {
  231.     ::SelectPalette(hdc,m_hpal,FALSE);
  232.     ::RealizePalette(hdc);
  233. }
  234.