BitmapHelper.cpp 9.7 KB

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