BitmapHelper.cpp 9.9 KB

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