BitmapHelper.cpp 9.9 KB

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