BitmapHelper.cpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. // BitmapHelper.cpp: implementation of the CBitmapHelper class.
  2. //
  3. //////////////////////////////////////////////////////////////////////
  4. #include "stdafx.h"
  5. #include "cp_main.h"
  6. #include "BitmapHelper.h"
  7. #ifdef _DEBUG
  8. #undef THIS_FILE
  9. static char THIS_FILE[]=__FILE__;
  10. #define new DEBUG_NEW
  11. #endif
  12. //////////////////////////////////////////////////////////////////////
  13. // Construction/Destruction
  14. //////////////////////////////////////////////////////////////////////
  15. CBitmapHelper::CBitmapHelper()
  16. {
  17. }
  18. CBitmapHelper::~CBitmapHelper()
  19. {
  20. }
  21. int CBitmapHelper::GetCBitmapWidth(const CBitmap & cbm)
  22. {
  23. BITMAP bm;
  24. cbm.GetObject(sizeof(BITMAP),&bm);
  25. return bm.bmWidth;
  26. }
  27. int CBitmapHelper::GetCBitmapHeight(const CBitmap & cbm)
  28. {
  29. BITMAP bm;
  30. cbm.GetObject(sizeof(BITMAP),&bm);
  31. return bm.bmHeight;
  32. }
  33. BOOL CBitmapHelper::GetCBitmap(void *pClip2, CDC *pDC, CBitmap *pBitMap, int nMaxHeight)
  34. {
  35. LPBITMAPINFO lpBI ;
  36. void* pDIBBits;
  37. BOOL bRet = FALSE;
  38. CClipFormat *pClip = (CClipFormat *)pClip2;
  39. switch(pClip->m_cfType)
  40. {
  41. case CF_DIB:
  42. {
  43. lpBI = (LPBITMAPINFO)GlobalLock(pClip->m_hgData);
  44. if(lpBI)
  45. {
  46. int nColors = lpBI->bmiHeader.biClrUsed ? lpBI->bmiHeader.biClrUsed : 1 << lpBI->bmiHeader.biBitCount;
  47. if( lpBI->bmiHeader.biBitCount > 8 )
  48. {
  49. pDIBBits = (LPVOID)((LPDWORD)(lpBI->bmiColors + lpBI->bmiHeader.biClrUsed) +
  50. ((lpBI->bmiHeader.biCompression == BI_BITFIELDS) ? 3 : 0));
  51. }
  52. else
  53. {
  54. pDIBBits = (LPVOID)(lpBI->bmiColors + nColors);
  55. }
  56. int nHeight = min(nMaxHeight, lpBI->bmiHeader.biHeight);
  57. int nWidth = (nHeight * lpBI->bmiHeader.biWidth) / lpBI->bmiHeader.biHeight;
  58. if(pBitMap)
  59. {
  60. pBitMap->CreateCompatibleBitmap(pDC, nWidth, nHeight);
  61. ASSERT(pBitMap->m_hObject != NULL);
  62. CDC MemDc;
  63. MemDc.CreateCompatibleDC(pDC);
  64. CBitmap* oldBitmap = MemDc.SelectObject(pBitMap);
  65. ::StretchDIBits(MemDc.m_hDC,
  66. 0, 0,
  67. nWidth, nHeight,
  68. 0, 0, lpBI->bmiHeader.biWidth,
  69. lpBI->bmiHeader.biHeight,
  70. pDIBBits, lpBI, DIB_PAL_COLORS, SRCCOPY);
  71. MemDc.SelectObject(oldBitmap);
  72. bRet = TRUE;
  73. }
  74. }
  75. }
  76. }
  77. return bRet;
  78. }
  79. WORD CBitmapHelper::PaletteSize(LPSTR lpDIB)
  80. {
  81. // calculate the size required by the palette
  82. if (IS_WIN30_DIB (lpDIB))
  83. return (DIBNumColors(lpDIB) * sizeof(RGBQUAD));
  84. else
  85. return (DIBNumColors(lpDIB) * sizeof(RGBTRIPLE));
  86. }
  87. WORD CBitmapHelper::DIBNumColors(LPSTR lpDIB)
  88. {
  89. WORD wBitCount; // DIB bit count
  90. // If this is a Windows-style DIB, the number of colors in the
  91. // color table can be less than the number of bits per pixel
  92. // allows for (i.e. lpbi->biClrUsed can be set to some value).
  93. // If this is the case, return the appropriate value.
  94. if (IS_WIN30_DIB(lpDIB))
  95. {
  96. DWORD dwClrUsed;
  97. dwClrUsed = ((LPBITMAPINFOHEADER)lpDIB)->biClrUsed;
  98. if (dwClrUsed)
  99. return (WORD)dwClrUsed;
  100. }
  101. // Calculate the number of colors in the color table based on
  102. // the number of bits per pixel for the DIB.
  103. if (IS_WIN30_DIB(lpDIB))
  104. wBitCount = ((LPBITMAPINFOHEADER)lpDIB)->biBitCount;
  105. else
  106. wBitCount = ((LPBITMAPCOREHEADER)lpDIB)->bcBitCount;
  107. // return number of colors based on bits per pixel
  108. switch (wBitCount)
  109. {
  110. case 1:
  111. return 2;
  112. case 4:
  113. return 16;
  114. case 8:
  115. return 256;
  116. default:
  117. return 0;
  118. }
  119. }
  120. HANDLE CBitmapHelper::hBitmapToDIB(HBITMAP hBitmap, DWORD dwCompression, HPALETTE hPal)
  121. {
  122. BITMAP bm;
  123. BITMAPINFOHEADER bi;
  124. LPBITMAPINFOHEADER lpbi;
  125. DWORD dwLen;
  126. HANDLE hDIB;
  127. HANDLE handle;
  128. HDC hDC;
  129. // The function has no arg for bitfields
  130. if( dwCompression == BI_BITFIELDS )
  131. return NULL;
  132. // If a palette has not been supplied use defaul palette
  133. if (hPal==NULL)
  134. hPal = (HPALETTE) GetStockObject(DEFAULT_PALETTE);
  135. // Get bitmap information
  136. (void)GetObject( hBitmap, sizeof(bm), (LPSTR)&bm );
  137. // Initialize the bitmapinfoheader
  138. bi.biSize = sizeof(BITMAPINFOHEADER);
  139. bi.biWidth = bm.bmWidth;
  140. bi.biHeight = bm.bmHeight;
  141. bi.biPlanes = 1;
  142. bi.biBitCount = static_cast<USHORT>( bm.bmPlanes * bm.bmBitsPixel );
  143. bi.biCompression = dwCompression;
  144. bi.biSizeImage = 0;
  145. bi.biXPelsPerMeter = 0;
  146. bi.biYPelsPerMeter = 0;
  147. bi.biClrUsed = 0;
  148. bi.biClrImportant = 0;
  149. dwLen = bi.biSize + PaletteSize((LPSTR)&bi);
  150. // We need a device context to get the DIB from
  151. hDC = GetDC(NULL);
  152. hPal = SelectPalette(hDC,hPal,FALSE);
  153. (void)RealizePalette(hDC);
  154. // Allocate enough memory to hold bitmapinfoheader and color table
  155. hDIB = GlobalAlloc(GMEM_FIXED,dwLen);
  156. if (!hDIB)
  157. {
  158. (void)SelectPalette(hDC,hPal,FALSE);
  159. ReleaseDC(NULL,hDC);
  160. return NULL;
  161. }
  162. lpbi = (LPBITMAPINFOHEADER)hDIB;
  163. *lpbi = bi;
  164. // Call GetDIBits with a NULL lpBits param, so the device driver
  165. // will calculate the biSizeImage field
  166. (void)GetDIBits(hDC, hBitmap, 0L, (DWORD)bi.biHeight,
  167. (LPBYTE)NULL, (LPBITMAPINFO)lpbi, (DWORD)DIB_RGB_COLORS);
  168. bi = *lpbi;
  169. // If the driver did not fill in the biSizeImage field, then compute it
  170. // Each scan line of the image is aligned on a DWORD (32bit) boundary
  171. if (bi.biSizeImage == 0)
  172. {
  173. bi.biSizeImage = ((((bi.biWidth * bi.biBitCount) + 31) & ~31) / 8)
  174. * bi.biHeight;
  175. // If a compression scheme is used the result may infact be larger
  176. // Increase the size to account for this.
  177. if (dwCompression != BI_RGB)
  178. bi.biSizeImage = (bi.biSizeImage * 3) / 2;
  179. }
  180. // Realloc the buffer so that it can hold all the bits
  181. dwLen += bi.biSizeImage;
  182. handle = GlobalReAlloc(hDIB, dwLen, GMEM_MOVEABLE);
  183. if(handle)
  184. {
  185. hDIB = handle;
  186. }
  187. else
  188. {
  189. GlobalFree(hDIB);
  190. // Reselect the original palette
  191. (void)SelectPalette(hDC,hPal,FALSE);
  192. ReleaseDC(NULL,hDC);
  193. return NULL;
  194. }
  195. // Get the bitmap bits
  196. lpbi = (LPBITMAPINFOHEADER)hDIB;
  197. // FINALLY get the DIB
  198. BOOL bGotBits = GetDIBits( hDC, hBitmap,
  199. 0L, // Start scan line
  200. (DWORD)bi.biHeight, // # of scan lines
  201. (LPBYTE)lpbi // address for bitmap bits
  202. + (bi.biSize + PaletteSize((LPSTR)&bi)),
  203. (LPBITMAPINFO)lpbi, // address of bitmapinfo
  204. (DWORD)DIB_RGB_COLORS); // Use RGB for color table
  205. if( !bGotBits )
  206. {
  207. GlobalFree(hDIB);
  208. (void)SelectPalette(hDC,hPal,FALSE);
  209. ReleaseDC(NULL,hDC);
  210. return NULL;
  211. }
  212. (void)SelectPalette(hDC,hPal,FALSE);
  213. ReleaseDC(NULL,hDC);
  214. return hDIB;
  215. }
  216. bool CBitmapHelper::DrawDIB(CDC *pDC, HANDLE hData, int nLeft, int nRight, int &nWidth)
  217. {
  218. LPBITMAPINFO lpBI ;
  219. void* pDIBBits;
  220. bool bRet = false;
  221. lpBI = (LPBITMAPINFO)GlobalLock(hData);
  222. if(lpBI)
  223. {
  224. int nColors = lpBI->bmiHeader.biClrUsed ? lpBI->bmiHeader.biClrUsed : 1 << lpBI->bmiHeader.biBitCount;
  225. if( lpBI->bmiHeader.biBitCount > 8 )
  226. {
  227. pDIBBits = (LPVOID)((LPDWORD)(lpBI->bmiColors + lpBI->bmiHeader.biClrUsed) +
  228. ((lpBI->bmiHeader.biCompression == BI_BITFIELDS) ? 3 : 0));
  229. }
  230. else
  231. {
  232. pDIBBits = (LPVOID)(lpBI->bmiColors + nColors);
  233. }
  234. ::StretchDIBits(pDC->m_hDC,
  235. nLeft, nRight,
  236. lpBI->bmiHeader.biWidth, lpBI->bmiHeader.biHeight,
  237. 0, 0, lpBI->bmiHeader.biWidth,
  238. lpBI->bmiHeader.biHeight,
  239. pDIBBits, lpBI, DIB_PAL_COLORS, SRCCOPY);
  240. nWidth = lpBI->bmiHeader.biWidth;
  241. GlobalUnlock(hData) ;
  242. bRet = true;
  243. }
  244. return bRet;
  245. }