BitmapHelper.cpp 9.3 KB

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