Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

128 lines
3.0 KiB

  1. // Copyright (C) Microsoft Corporation 1993-1997
  2. #include "header.h"
  3. #include "cpaldc.h"
  4. CPalDC::CPalDC(HBITMAP hbmpSel, HPALETTE hpalSel)
  5. {
  6. ClearMemory(this, sizeof(CPalDC));
  7. m_hdc = CreateCompatibleDC(NULL);
  8. ASSERT(m_hdc);
  9. if (hpalSel)
  10. SelectPal(hpalSel);
  11. m_hpal = hpalSel;
  12. m_hbmp = hbmpSel;
  13. if (m_hbmp) {
  14. // Can fail if hbmp is selected into another DC
  15. VERIFY((m_hbmpOld = (HBITMAP) SelectObject(m_hdc, m_hbmp)));
  16. }
  17. }
  18. CPalDC::CPalDC(int type)
  19. {
  20. ClearMemory(this, sizeof(CPalDC));
  21. switch (type) {
  22. case SCREEN_DC:
  23. m_hdc = CreateDC("DISPLAY", NULL, NULL, NULL);
  24. break;
  25. case SCREEN_IC:
  26. m_hdc = CreateIC("DISPLAY", NULL, NULL, NULL);
  27. break;
  28. default:
  29. ASSERT(type == SCREEN_DC);
  30. break;
  31. }
  32. ASSERT(m_hdc);
  33. }
  34. CPalDC::~CPalDC(void)
  35. {
  36. if (!m_hdc)
  37. return;
  38. if (m_hpalOld)
  39. SelectPalette(m_hdc, m_hpal, FALSE);
  40. if (m_hbmpOld)
  41. SelectObject(m_hdc, m_hbmpOld);
  42. DeleteDC(m_hdc);
  43. }
  44. void CPalDC::SelectPal(HPALETTE hpalSel)
  45. {
  46. if (hpalSel) {
  47. if (m_hpalOld) // m_hpalOld is set once, and only once
  48. SelectPalette(m_hdc, m_hpal, FALSE);
  49. else
  50. m_hpalOld = SelectPalette(m_hdc, m_hpal, FALSE);
  51. RealizePalette(m_hdc);
  52. }
  53. else if (m_hpalOld) {
  54. SelectPalette(m_hdc, m_hpalOld, FALSE);
  55. m_hpalOld = NULL;
  56. }
  57. }
  58. HPALETTE CPalDC::CreateBIPalette(HBITMAP hbmp)
  59. {
  60. PBITMAPINFO pbmi = (PBITMAPINFO) lcCalloc(sizeof(BITMAPINFOHEADER) +
  61. sizeof(RGBQUAD) * 256);
  62. PBITMAPINFOHEADER pbih = (PBITMAPINFOHEADER) pbmi;
  63. BITMAP bmp;
  64. GetObject(hbmp, sizeof(BITMAP), &bmp);
  65. pbih->biPlanes = bmp.bmPlanes;
  66. pbih->biBitCount = bmp.bmBitsPixel;
  67. pbih->biWidth = bmp.bmWidth;
  68. pbih->biHeight = bmp.bmHeight;
  69. pbih->biSize = sizeof(BITMAPINFOHEADER);
  70. pbih->biCompression = BI_RGB;
  71. if (!GetDIBits(m_hdc, hbmp, 0, 1, NULL, pbmi, DIB_RGB_COLORS))
  72. return NULL;
  73. HPALETTE hpal = ::CreateBIPalette(pbih);
  74. lcFree(pbmi);
  75. return hpal;
  76. }
  77. // REVIEW: only works with 256-color bitmaps
  78. #define PALVERSION 0x300
  79. HPALETTE CreateBIPalette(PBITMAPINFOHEADER pbihd)
  80. {
  81. // Allocate for the logical palette structure
  82. LOGPALETTE* pPal = (LOGPALETTE*) lcMalloc(sizeof(LOGPALETTE) +
  83. 256 * sizeof(PALETTEENTRY));
  84. pPal->palNumEntries = 256;
  85. pPal->palVersion = PALVERSION;
  86. /*
  87. * Fill in the palette entries from the DIB color table and
  88. * create a logical color palette.
  89. */
  90. RGBQUAD* pRgb = (RGBQUAD*)(((PBYTE) pbihd) + pbihd->biSize);
  91. for (int i = 0; i < (int) 256; i++){
  92. pPal->palPalEntry[i].peRed = pRgb[i].rgbRed;
  93. pPal->palPalEntry[i].peGreen = pRgb[i].rgbGreen;
  94. pPal->palPalEntry[i].peBlue = pRgb[i].rgbBlue;
  95. pPal->palPalEntry[i].peFlags = (BYTE) 0;
  96. }
  97. HPALETTE hpal = CreatePalette(pPal);
  98. lcFree(pPal);
  99. return hpal;
  100. }