Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

236 lines
7.0 KiB

  1. #include "precomp.h"
  2. #pragma hdrstop
  3. CGdiPlusThumb::CGdiPlusThumb()
  4. {
  5. m_pImage = NULL;
  6. m_pImageFactory = NULL;
  7. m_szPath[0] = 0;
  8. }
  9. CGdiPlusThumb::~CGdiPlusThumb()
  10. {
  11. if (m_pImage)
  12. m_pImage->Release();
  13. if (m_pImageFactory)
  14. m_pImageFactory->Release();
  15. }
  16. STDMETHODIMP CGdiPlusThumb::GetLocation(LPWSTR pszPathBuffer, DWORD cch,
  17. DWORD * pdwPriority, const SIZE *prgSize,
  18. DWORD dwRecClrDepth, DWORD *pdwFlags)
  19. {
  20. HRESULT hr = S_OK;
  21. if (*pdwFlags & IEIFLAG_ASYNC)
  22. {
  23. hr = E_PENDING;
  24. // higher than normal priority, this task is relatively fast
  25. *pdwPriority = PRIORITY_EXTRACT_FAST;
  26. }
  27. m_rgSize = *prgSize;
  28. m_dwRecClrDepth = dwRecClrDepth;
  29. // We only work in non-paletted modes. Some of our extractors simply always use 24bbp, should we?
  30. if (m_dwRecClrDepth < 16)
  31. m_dwRecClrDepth = 16;
  32. m_fOrigSize = BOOLIFY(*pdwFlags & IEIFLAG_ORIGSIZE);
  33. m_fHighQuality = BOOLIFY(*pdwFlags & IEIFLAG_QUALITY);
  34. *pdwFlags = IEIFLAG_CACHE;
  35. StrCpyNW(pszPathBuffer, m_szPath, cch);
  36. return hr;
  37. }
  38. STDMETHODIMP CGdiPlusThumb::Extract(HBITMAP *phBmpThumbnail)
  39. {
  40. HRESULT hr = E_FAIL;
  41. // Do the GDI plus stuff here
  42. if (m_pImageFactory && m_pImage)
  43. {
  44. if (m_fHighQuality)
  45. {
  46. hr = m_pImage->Decode(SHIMGDEC_DEFAULT, 0, 0);
  47. }
  48. if (FAILED(hr))
  49. {
  50. hr = m_pImage->Decode(SHIMGDEC_THUMBNAIL, m_rgSize.cx, m_rgSize.cy);
  51. }
  52. if (SUCCEEDED(hr))
  53. {
  54. // Get the image's actual size, which might be less than the requested size since we asked for a thumbnail
  55. SIZE sizeImg;
  56. m_pImage->GetSize(&sizeImg);
  57. // we need to fill the background with the default color if we can't resize the bitmap
  58. // or if the image is transparent:
  59. m_fFillBackground = !m_fOrigSize || (m_pImage->IsTransparent() == S_OK);
  60. if (m_fOrigSize)
  61. {
  62. // if its too damn big, lets squish it, but try to
  63. // maintain the same aspect ratio. This is the same
  64. // sort of thing we do for the !m_fOrigSize case, except
  65. // here we want to do it here because we want to return
  66. // a correctly sized bitmap.
  67. if (sizeImg.cx != 0 && sizeImg.cy != 0 &&
  68. (sizeImg.cx > m_rgSize.cx || sizeImg.cy > m_rgSize.cy))
  69. {
  70. if (m_rgSize.cx * sizeImg.cy > m_rgSize.cy * sizeImg.cx)
  71. {
  72. m_rgSize.cx = MulDiv(m_rgSize.cy,sizeImg.cx,sizeImg.cy);
  73. }
  74. else
  75. {
  76. m_rgSize.cy = MulDiv(m_rgSize.cx,sizeImg.cy,sizeImg.cx);
  77. }
  78. }
  79. else
  80. {
  81. // Use the size if it was small enough and they wanted original size
  82. m_rgSize = sizeImg;
  83. }
  84. }
  85. hr = CreateDibFromBitmapImage( phBmpThumbnail );
  86. }
  87. }
  88. return SUCCEEDED(hr) ? S_OK : hr;
  89. }
  90. STDMETHODIMP CGdiPlusThumb::GetClassID(CLSID *pClassID)
  91. {
  92. *pClassID = CLSID_GdiThumbnailExtractor;
  93. return S_OK;
  94. }
  95. STDMETHODIMP CGdiPlusThumb::IsDirty()
  96. {
  97. return E_NOTIMPL;
  98. }
  99. STDMETHODIMP CGdiPlusThumb::Load(LPCOLESTR pszFileName, DWORD dwMode)
  100. {
  101. StrCpyNW(m_szPath, pszFileName, ARRAYSIZE(m_szPath));
  102. // Call ImageFactory->CreateFromFile here. If Create from file failes then
  103. // return E_FAIL, otherwise return S_OK.
  104. ASSERT(NULL==m_pImageFactory);
  105. HRESULT hr = CoCreateInstance(CLSID_ShellImageDataFactory, NULL, CLSCTX_INPROC_SERVER, IID_PPV_ARG(IShellImageDataFactory, &m_pImageFactory));
  106. if (SUCCEEDED(hr))
  107. {
  108. hr = m_pImageFactory->CreateImageFromFile(m_szPath, &m_pImage);
  109. ASSERT((SUCCEEDED(hr) && m_pImage) || (FAILED(hr) && !m_pImage));
  110. }
  111. return hr;
  112. }
  113. STDMETHODIMP CGdiPlusThumb::Save(LPCOLESTR pszFileName, BOOL fRemember)
  114. {
  115. return E_NOTIMPL;
  116. }
  117. STDMETHODIMP CGdiPlusThumb::SaveCompleted(LPCOLESTR pszFileName)
  118. {
  119. return E_NOTIMPL;
  120. }
  121. STDMETHODIMP CGdiPlusThumb::GetCurFile(LPOLESTR *ppszFileName)
  122. {
  123. return E_NOTIMPL;
  124. }
  125. HRESULT CGdiPlusThumb::CreateDibFromBitmapImage(HBITMAP * pbm)
  126. {
  127. HRESULT hr = E_FAIL;
  128. BITMAPINFO bmi = {0};
  129. void * pvBits;
  130. bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
  131. bmi.bmiHeader.biWidth = m_rgSize.cx;
  132. bmi.bmiHeader.biHeight = m_rgSize.cy;
  133. bmi.bmiHeader.biPlanes = 1;
  134. bmi.bmiHeader.biBitCount = (USHORT)m_dwRecClrDepth;
  135. bmi.bmiHeader.biCompression = BI_RGB;
  136. DWORD dwBPL = (((bmi.bmiHeader.biWidth * m_dwRecClrDepth) + 31) >> 3) & ~3;
  137. bmi.bmiHeader.biSizeImage = dwBPL * bmi.bmiHeader.biHeight;
  138. HDC hdc = GetDC(NULL);
  139. HBITMAP hbmp = CreateDIBSection(hdc, &bmi, DIB_RGB_COLORS, &pvBits, NULL, 0);
  140. if (hbmp)
  141. {
  142. HDC hMemDC = CreateCompatibleDC(hdc);
  143. if (hMemDC)
  144. {
  145. HBITMAP hbmOld = (HBITMAP)SelectObject(hMemDC, hbmp);
  146. RECT rc = {0, 0, m_rgSize.cx, m_rgSize.cy};
  147. if (m_fFillBackground)
  148. {
  149. FillRect(hMemDC, &rc, GetSysColorBrush(COLOR_WINDOW));
  150. }
  151. // if the m_fOrigSize flag isn't set then we need to return a bitmap that is the
  152. // requested size. In order to maintain aspect ratio that means we need to
  153. // center the thumbnail.
  154. if (!m_fOrigSize)
  155. {
  156. SIZE sizeImg;
  157. m_pImage->GetSize(&sizeImg);
  158. // if its too damn big, lets squish it, but try to
  159. // maintain the same aspect ratio. This is the same
  160. // sort of thing we did for the m_fOrigSize case, except
  161. // here we want to do it before centering.
  162. if (sizeImg.cx != 0 && sizeImg.cy != 0 &&
  163. (sizeImg.cx > m_rgSize.cx || sizeImg.cy > m_rgSize.cy))
  164. {
  165. if (m_rgSize.cx * sizeImg.cy > m_rgSize.cy * sizeImg.cx)
  166. {
  167. sizeImg.cx = MulDiv(m_rgSize.cy,sizeImg.cx,sizeImg.cy);
  168. sizeImg.cy = m_rgSize.cy;
  169. }
  170. else
  171. {
  172. sizeImg.cy = MulDiv(m_rgSize.cx,sizeImg.cy,sizeImg.cx);
  173. sizeImg.cx = m_rgSize.cx;
  174. }
  175. }
  176. rc.left = (m_rgSize.cx-sizeImg.cx)/2;
  177. rc.top = (m_rgSize.cy-sizeImg.cy)/2;
  178. rc.right = rc.left + sizeImg.cx;
  179. rc.bottom = rc.top + sizeImg.cy;
  180. }
  181. hr = m_pImage->Draw(hMemDC, &rc, NULL);
  182. SelectObject(hMemDC, hbmOld);
  183. DeleteDC(hMemDC);
  184. }
  185. }
  186. ReleaseDC(NULL, hdc);
  187. if (SUCCEEDED(hr))
  188. {
  189. *pbm = hbmp;
  190. }
  191. else if (hbmp)
  192. {
  193. DeleteObject(hbmp);
  194. }
  195. return hr;
  196. }