BitmapHelper.cpp 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  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. if (nMaxHeight < INT_MAX)
  61. {
  62. //first create a bitmap of the real size, this is used to pass into Gdiplus::Bitmap to resize more accuratly
  63. CBitmap tmp;
  64. tmp.CreateCompatibleBitmap(pDC, lpBI->bmiHeader.biWidth, lpBI->bmiHeader.biHeight);
  65. ASSERT(tmp.m_hObject != NULL);
  66. CDC MemDc;
  67. MemDc.CreateCompatibleDC(pDC);
  68. CBitmap* oldBitmap = MemDc.SelectObject(&tmp);
  69. ::StretchDIBits(MemDc.m_hDC,
  70. 0, 0,
  71. lpBI->bmiHeader.biWidth, lpBI->bmiHeader.biHeight,
  72. 0, 0, lpBI->bmiHeader.biWidth,
  73. lpBI->bmiHeader.biHeight,
  74. pDIBBits, lpBI, DIB_PAL_COLORS, SRCCOPY);
  75. MemDc.SelectObject(oldBitmap);
  76. //do the resize
  77. pBitMap->CreateCompatibleBitmap(pDC, nWidth, nHeight);
  78. ASSERT(pBitMap->m_hObject != NULL);
  79. CDC MemDc2;
  80. MemDc2.CreateCompatibleDC(pDC);
  81. CBitmap* oldBitmap2 = MemDc2.SelectObject(pBitMap);
  82. HBITMAP h = (HBITMAP)tmp;
  83. Gdiplus::Bitmap gdipBitmap(h, NULL);
  84. Gdiplus::ImageAttributes attrs;
  85. Gdiplus::Rect dest(0, 0, nWidth, nHeight);
  86. Gdiplus::Graphics graphics(MemDc2);
  87. graphics.SetInterpolationMode(Gdiplus::InterpolationModeHighQualityBicubic);
  88. graphics.SetPixelOffsetMode(Gdiplus::PixelOffsetModeHalf);
  89. graphics.DrawImage(&gdipBitmap, dest, 0, 0, lpBI->bmiHeader.biWidth, lpBI->bmiHeader.biHeight, Gdiplus::UnitPixel, &attrs);
  90. MemDc2.SelectObject(oldBitmap2);
  91. }
  92. else
  93. {
  94. pBitMap->CreateCompatibleBitmap(pDC, nWidth, nHeight);
  95. ASSERT(pBitMap->m_hObject != NULL);
  96. CDC MemDc;
  97. MemDc.CreateCompatibleDC(pDC);
  98. CBitmap* oldBitmap = MemDc.SelectObject(pBitMap);
  99. ::StretchDIBits(MemDc.m_hDC,
  100. 0, 0,
  101. lpBI->bmiHeader.biWidth, lpBI->bmiHeader.biHeight,
  102. 0, 0, lpBI->bmiHeader.biWidth,
  103. lpBI->bmiHeader.biHeight,
  104. pDIBBits, lpBI, DIB_PAL_COLORS, SRCCOPY);
  105. MemDc.SelectObject(oldBitmap);
  106. }
  107. bRet = TRUE;
  108. }
  109. }
  110. }
  111. }
  112. return bRet;
  113. }
  114. WORD CBitmapHelper::PaletteSize(LPSTR lpDIB)
  115. {
  116. // calculate the size required by the palette
  117. if (IS_WIN30_DIB (lpDIB))
  118. return (DIBNumColors(lpDIB) * sizeof(RGBQUAD));
  119. else
  120. return (DIBNumColors(lpDIB) * sizeof(RGBTRIPLE));
  121. }
  122. WORD CBitmapHelper::DIBNumColors(LPSTR lpDIB)
  123. {
  124. WORD wBitCount; // DIB bit count
  125. // If this is a Windows-style DIB, the number of colors in the
  126. // color table can be less than the number of bits per pixel
  127. // allows for (i.e. lpbi->biClrUsed can be set to some value).
  128. // If this is the case, return the appropriate value.
  129. if (IS_WIN30_DIB(lpDIB))
  130. {
  131. DWORD dwClrUsed;
  132. dwClrUsed = ((LPBITMAPINFOHEADER)lpDIB)->biClrUsed;
  133. if (dwClrUsed)
  134. return (WORD)dwClrUsed;
  135. }
  136. // Calculate the number of colors in the color table based on
  137. // the number of bits per pixel for the DIB.
  138. if (IS_WIN30_DIB(lpDIB))
  139. wBitCount = ((LPBITMAPINFOHEADER)lpDIB)->biBitCount;
  140. else
  141. wBitCount = ((LPBITMAPCOREHEADER)lpDIB)->bcBitCount;
  142. // return number of colors based on bits per pixel
  143. switch (wBitCount)
  144. {
  145. case 1:
  146. return 2;
  147. case 4:
  148. return 16;
  149. case 8:
  150. return 256;
  151. default:
  152. return 0;
  153. }
  154. }
  155. HANDLE CBitmapHelper::hBitmapToDIB(HBITMAP hBitmap, DWORD dwCompression, HPALETTE hPal)
  156. {
  157. BITMAP bm;
  158. BITMAPINFOHEADER bi;
  159. LPBITMAPINFOHEADER lpbi;
  160. DWORD dwLen;
  161. HANDLE hDIB;
  162. HANDLE handle;
  163. HDC hDC;
  164. // The function has no arg for bitfields
  165. if( dwCompression == BI_BITFIELDS )
  166. return NULL;
  167. // If a palette has not been supplied use defaul palette
  168. if (hPal==NULL)
  169. hPal = (HPALETTE) GetStockObject(DEFAULT_PALETTE);
  170. // Get bitmap information
  171. (void)GetObject( hBitmap, sizeof(bm), (LPSTR)&bm );
  172. // Initialize the bitmapinfoheader
  173. bi.biSize = sizeof(BITMAPINFOHEADER);
  174. bi.biWidth = bm.bmWidth;
  175. bi.biHeight = bm.bmHeight;
  176. bi.biPlanes = 1;
  177. bi.biBitCount = static_cast<USHORT>( bm.bmPlanes * bm.bmBitsPixel );
  178. bi.biCompression = dwCompression;
  179. bi.biSizeImage = 0;
  180. bi.biXPelsPerMeter = 0;
  181. bi.biYPelsPerMeter = 0;
  182. bi.biClrUsed = 0;
  183. bi.biClrImportant = 0;
  184. dwLen = bi.biSize + PaletteSize((LPSTR)&bi);
  185. // We need a device context to get the DIB from
  186. hDC = GetDC(NULL);
  187. hPal = SelectPalette(hDC,hPal,FALSE);
  188. (void)RealizePalette(hDC);
  189. // Allocate enough memory to hold bitmapinfoheader and color table
  190. hDIB = GlobalAlloc(GMEM_FIXED,dwLen);
  191. if (!hDIB)
  192. {
  193. (void)SelectPalette(hDC,hPal,FALSE);
  194. ReleaseDC(NULL,hDC);
  195. return NULL;
  196. }
  197. lpbi = (LPBITMAPINFOHEADER)hDIB;
  198. *lpbi = bi;
  199. // Call GetDIBits with a NULL lpBits param, so the device driver
  200. // will calculate the biSizeImage field
  201. (void)GetDIBits(hDC, hBitmap, 0L, (DWORD)bi.biHeight,
  202. (LPBYTE)NULL, (LPBITMAPINFO)lpbi, (DWORD)DIB_RGB_COLORS);
  203. bi = *lpbi;
  204. // If the driver did not fill in the biSizeImage field, then compute it
  205. // Each scan line of the image is aligned on a DWORD (32bit) boundary
  206. if (bi.biSizeImage == 0)
  207. {
  208. bi.biSizeImage = ((((bi.biWidth * bi.biBitCount) + 31) & ~31) / 8)
  209. * bi.biHeight;
  210. // If a compression scheme is used the result may infact be larger
  211. // Increase the size to account for this.
  212. if (dwCompression != BI_RGB)
  213. bi.biSizeImage = (bi.biSizeImage * 3) / 2;
  214. }
  215. // Realloc the buffer so that it can hold all the bits
  216. dwLen += bi.biSizeImage;
  217. handle = GlobalReAlloc(hDIB, dwLen, GMEM_MOVEABLE);
  218. if(handle)
  219. {
  220. hDIB = handle;
  221. }
  222. else
  223. {
  224. GlobalFree(hDIB);
  225. // Reselect the original palette
  226. (void)SelectPalette(hDC,hPal,FALSE);
  227. ReleaseDC(NULL,hDC);
  228. return NULL;
  229. }
  230. // Get the bitmap bits
  231. lpbi = (LPBITMAPINFOHEADER)hDIB;
  232. // FINALLY get the DIB
  233. BOOL bGotBits = GetDIBits( hDC, hBitmap,
  234. 0L, // Start scan line
  235. (DWORD)bi.biHeight, // # of scan lines
  236. (LPBYTE)lpbi // address for bitmap bits
  237. + (bi.biSize + PaletteSize((LPSTR)&bi)),
  238. (LPBITMAPINFO)lpbi, // address of bitmapinfo
  239. (DWORD)DIB_RGB_COLORS); // Use RGB for color table
  240. if( !bGotBits )
  241. {
  242. GlobalFree(hDIB);
  243. (void)SelectPalette(hDC,hPal,FALSE);
  244. ReleaseDC(NULL,hDC);
  245. return NULL;
  246. }
  247. (void)SelectPalette(hDC,hPal,FALSE);
  248. ReleaseDC(NULL,hDC);
  249. return hDIB;
  250. }
  251. bool CBitmapHelper::DrawDIB(CDC *pDC, HANDLE hData, int nLeft, int nRight, int &nWidth)
  252. {
  253. LPBITMAPINFO lpBI ;
  254. void* pDIBBits;
  255. bool bRet = false;
  256. lpBI = (LPBITMAPINFO)GlobalLock(hData);
  257. if(lpBI)
  258. {
  259. int nColors = lpBI->bmiHeader.biClrUsed ? lpBI->bmiHeader.biClrUsed : 1 << lpBI->bmiHeader.biBitCount;
  260. if( lpBI->bmiHeader.biBitCount > 8 )
  261. {
  262. pDIBBits = (LPVOID)((LPDWORD)(lpBI->bmiColors + lpBI->bmiHeader.biClrUsed) +
  263. ((lpBI->bmiHeader.biCompression == BI_BITFIELDS) ? 3 : 0));
  264. }
  265. else
  266. {
  267. pDIBBits = (LPVOID)(lpBI->bmiColors + nColors);
  268. }
  269. ::StretchDIBits(pDC->m_hDC,
  270. nLeft, nRight,
  271. lpBI->bmiHeader.biWidth, lpBI->bmiHeader.biHeight,
  272. 0, 0, lpBI->bmiHeader.biWidth,
  273. lpBI->bmiHeader.biHeight,
  274. pDIBBits, lpBI, DIB_PAL_COLORS, SRCCOPY);
  275. nWidth = lpBI->bmiHeader.biWidth;
  276. GlobalUnlock(hData) ;
  277. bRet = true;
  278. }
  279. return bRet;
  280. }