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.

172 lines
4.6 KiB

  1. #include "precomp.h"
  2. #include "imgs.h"
  3. #include "gphelper.h"
  4. #include "ssutil.h"
  5. #include "psutil.h"
  6. CBitmapImage::CBitmapImage(void)
  7. : m_hBitmap(NULL),
  8. m_hPalette(NULL)
  9. {
  10. }
  11. CBitmapImage::~CBitmapImage(void)
  12. {
  13. Destroy();
  14. }
  15. void CBitmapImage::Destroy(void)
  16. {
  17. if (m_hBitmap)
  18. {
  19. DeleteObject(m_hBitmap);
  20. m_hBitmap = NULL;
  21. }
  22. if (m_hPalette)
  23. {
  24. DeleteObject(m_hPalette);
  25. m_hPalette = NULL;
  26. }
  27. }
  28. bool CBitmapImage::IsValid(void) const
  29. {
  30. return(m_hBitmap != NULL);
  31. }
  32. HPALETTE CBitmapImage::Palette(void) const
  33. {
  34. return(m_hPalette);
  35. }
  36. HBITMAP CBitmapImage::GetBitmap(void) const
  37. {
  38. return(m_hBitmap);
  39. }
  40. // Create a palette for the image
  41. HPALETTE CBitmapImage::PreparePalette( CSimpleDC &dc, HBITMAP hBitmap )
  42. {
  43. HPALETTE hPalette = NULL;
  44. if (GetDeviceCaps(dc,RASTERCAPS) & RC_PALETTE)
  45. {
  46. if (hBitmap)
  47. {
  48. DIBSECTION ds = {0};
  49. GetObject(hBitmap, sizeof (DIBSECTION), &ds);
  50. int nColors;
  51. if (ds.dsBmih.biClrUsed != 0)
  52. {
  53. nColors = ds.dsBmih.biClrUsed;
  54. }
  55. else
  56. {
  57. // Handle the special case of an image that claims to be
  58. // a 32bit DIB as a 24bit DIB
  59. if (ds.dsBmih.biBitCount == 32)
  60. {
  61. nColors = 1 << 24;
  62. }
  63. else
  64. {
  65. nColors = 1 << ds.dsBmih.biBitCount;
  66. }
  67. }
  68. // Create a halftone palette if the DIB section contains more
  69. // than 256 colors
  70. if (nColors > 256)
  71. {
  72. hPalette = CreateHalftonePalette(dc);
  73. }
  74. // Create a custom palette from the DIB section's color table
  75. // if the number of colors is 256 or less
  76. else
  77. {
  78. RGBQUAD* pRGB = new RGBQUAD[nColors];
  79. if (pRGB)
  80. {
  81. CSimpleDC MemDC;
  82. MemDC.CreateCompatibleDC(dc);
  83. SelectObject( MemDC, hBitmap );
  84. GetDIBColorTable( MemDC, 0, nColors, pRGB );
  85. UINT nSize = sizeof (LOGPALETTE) + (sizeof (PALETTEENTRY) * (nColors - 1));
  86. LOGPALETTE* pLP = (LOGPALETTE*) new BYTE[nSize];
  87. if (pLP)
  88. {
  89. pLP->palVersion = 0x300;
  90. pLP->palNumEntries = (WORD)nColors;
  91. for (int i=0; i<nColors; i++)
  92. {
  93. pLP->palPalEntry[i].peRed = pRGB[i].rgbRed;
  94. pLP->palPalEntry[i].peGreen = pRGB[i].rgbGreen;
  95. pLP->palPalEntry[i].peBlue = pRGB[i].rgbBlue;
  96. pLP->palPalEntry[i].peFlags = 0;
  97. }
  98. hPalette = CreatePalette(pLP);
  99. delete[] pLP;
  100. }
  101. delete[] pRGB;
  102. }
  103. }
  104. }
  105. }
  106. else
  107. {
  108. hPalette = CreateHalftonePalette(dc);
  109. }
  110. return hPalette;
  111. }
  112. SIZE CBitmapImage::ImageSize(void) const
  113. {
  114. SIZE sizeImage = {0,0};
  115. if (IsValid())
  116. {
  117. BITMAP bm = {0};
  118. if (GetObject( m_hBitmap, sizeof(bm), &bm ))
  119. {
  120. sizeImage.cx = bm.bmWidth;
  121. sizeImage.cy = bm.bmHeight;
  122. }
  123. }
  124. return(sizeImage);
  125. }
  126. bool CBitmapImage::Load( CSimpleDC &dc,
  127. LPCTSTR pszFilename,
  128. const RECT &rcScreen,
  129. int nMaxScreenPercent,
  130. bool bAllowStretching,
  131. bool bDisplayFilename
  132. )
  133. {
  134. // Clean up, if necessary
  135. Destroy();
  136. // Validate the arguments
  137. if (!pszFilename || !lstrlen(pszFilename))
  138. {
  139. return false;
  140. }
  141. // Try to load and scale the image using GDI plus
  142. CGdiPlusHelper GdiPlusHelper;
  143. if (SUCCEEDED(GdiPlusHelper.LoadAndScale( m_hBitmap,
  144. pszFilename,
  145. PrintScanUtil::MulDivNoRound(rcScreen.right - rcScreen.left,nMaxScreenPercent,100),
  146. PrintScanUtil::MulDivNoRound(rcScreen.bottom - rcScreen.top,nMaxScreenPercent,100),
  147. bAllowStretching )) && m_hBitmap)
  148. {
  149. // Prepare the image's palette, if it has one
  150. m_hPalette = PreparePalette( dc, m_hBitmap );
  151. }
  152. return (m_hBitmap != NULL);
  153. }