BitmapHelper.cpp 8.4 KB

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