JPEG.cpp 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. #include "stdafx.h"
  2. #include "LYFZIPManage.h"
  3. #include "JPEG.h"
  4. CPicture::CPicture()
  5. {
  6. }
  7. CPicture::~CPicture()
  8. {
  9. }
  10. //-----------------------------------------------------------------------------
  11. // Does: Free The Allocated Memory That Holdes The IPicture Interface Data
  12. // ~~~~ And Clear Picture Information
  13. //
  14. // Note: This Might Also Be Useful If U Only Need To Show The Picture Once
  15. // ~~~~~ Or If U Copy The Picture To The Device Context, So It Can Still
  16. // Remain On Screen - But IPicture Data Is Not Needed No More
  17. //
  18. //-----------------------------------------------------------------------------
  19. void CPicture::UnloadPicture()
  20. //=============================================================================
  21. {
  22. if(m_pPict != NULL){
  23. m_pPict->Release();
  24. m_pPict = NULL;
  25. }
  26. }
  27. //-----------------------------------------------------------------------------
  28. // Does: Open a Resource And Load It Into IPicture (Interface)
  29. // ~~~~ (.BMP .DIB .EMF .GIF .ICO .JPG .WMF)
  30. //
  31. // Note: When Adding a Bitmap Resource It Would Automatically Show On "Bitmap"
  32. // ~~~~ This NOT Good Coz We Need To Load It From a Custom Resource "BMP"
  33. // To Add a Custom Rresource: Import Resource -> Open As -> Custom
  34. // (Both .BMP And .DIB Should Be Found Under "BMP")
  35. //
  36. // InPut: ResourceName - As a UINT Defined (Example: IDR_PICTURE_RESOURCE)
  37. // ~~~~~ ResourceType - Type Name (Example: "JPG")
  38. //
  39. // OutPut: TRUE If Succeeded...
  40. // ~~~~~~
  41. //-----------------------------------------------------------------------------
  42. BOOL CPicture::Load(HINSTANCE hInstance,LPCTSTR lpszResourceName, LPCSTR ResourceType)
  43. //=============================================================================
  44. {
  45. HGLOBAL hGlobal = NULL;
  46. HRSRC hSource = NULL;
  47. LPVOID lpVoid = NULL;
  48. int nSize = 0;
  49. BOOL bResult=FALSE;
  50. if(m_pPict != NULL) UnloadPicture(); // Important - Avoid Leaks...
  51. hSource = FindResource(hInstance, lpszResourceName, ResourceType);
  52. if(hSource == NULL)
  53. {
  54. // HWND hWnd = AfxGetApp()->GetMainWnd()->m_hWnd;
  55. // MessageBoxEx(NULL, "´íÎó´úºÅ:1\t", ERROR_TITLE, MB_OK | MB_ICONSTOP, LANG_ENGLISH);
  56. return(FALSE);
  57. }
  58. hGlobal = LoadResource(hInstance, hSource);
  59. if(hGlobal == NULL)
  60. {
  61. // HWND hWnd = AfxGetApp()->GetMainWnd()->m_hWnd;
  62. // MessageBoxEx(NULL, "´íÎó´úºÅ:2\t", ERROR_TITLE, MB_OK | MB_ICONSTOP, LANG_ENGLISH);
  63. return(FALSE);
  64. }
  65. lpVoid = LockResource(hGlobal);
  66. if(lpVoid == NULL)
  67. {
  68. //HWND hWnd = AfxGetApp()->GetMainWnd()->m_hWnd;
  69. // MessageBoxEx(NULL, "´íÎó´úºÅ:3\t", ERROR_TITLE, MB_OK | MB_ICONSTOP, LANG_ENGLISH);
  70. return(FALSE);
  71. }
  72. nSize = (UINT)SizeofResource(hInstance, hSource);
  73. if(LoadPictureData((BYTE*)hGlobal, nSize)) bResult = TRUE;
  74. UnlockResource(hGlobal); // 16Bit Windows Needs This
  75. FreeResource(hGlobal); // 16Bit Windows Needs This (32Bit - Automatic Release)
  76. return(bResult);
  77. }
  78. //-----------------------------------------------------------------------------
  79. // Does: Open a File And Load It Into IPicture (Interface)
  80. // ~~~~ (.BMP .DIB .EMF .GIF .ICO .JPG .WMF)
  81. //
  82. // InPut: sFilePathName - Path And FileName Target To Save
  83. // ~~~~~
  84. //
  85. // OutPut: TRUE If Succeeded...
  86. // ~~~~~~
  87. //-----------------------------------------------------------------------------
  88. BOOL CPicture::Load(CString sFilePathName)
  89. //=============================================================================
  90. {
  91. //if(!PathFileExists(sFilePathName))return FALSE;
  92. BOOL bResult = FALSE;
  93. CFile PictureFile;
  94. CFileException e;
  95. int nSize = 0;
  96. if(m_pPict != NULL) UnloadPicture(); // Important - Avoid Leaks...
  97. if(PictureFile.Open(sFilePathName, CFile::modeRead | CFile::typeBinary, &e))
  98. {
  99. nSize = PictureFile.GetLength();
  100. BYTE* pBuffer = new BYTE[nSize];
  101. if(PictureFile.Read(pBuffer, nSize) > 0)
  102. {
  103. if(LoadPictureData(pBuffer, nSize)) bResult = TRUE;
  104. }
  105. PictureFile.Close();
  106. delete [] pBuffer;
  107. }
  108. else // Open Failed...
  109. {
  110. TCHAR szCause[255];
  111. e.GetErrorMessage(szCause, 255, NULL);
  112. //HWND hWnd = AfxGetApp()->GetMainWnd()->m_hWnd;
  113. // MessageBoxEx(NULL, "´íÎó´úºÅ:4", ERROR_TITLE, MB_OK | MB_ICONSTOP, LANG_ENGLISH);
  114. bResult = FALSE;
  115. }
  116. return(bResult);
  117. }
  118. //-----------------------------------------------------------------------------
  119. // Does: Read The Picture Data From a Source (File / Resource)
  120. // ~~~~ And Load It Into The Current IPicture Object In Use
  121. //
  122. // InPut: Buffer Of Data Source (File / Resource) And Its Size
  123. // ~~~~~
  124. //
  125. // OutPut: Feed The IPicture Object With The Picture Data
  126. // ~~~~~~ (Use Draw Functions To Show It On a Device Context)
  127. // TRUE If Succeeded...
  128. //-----------------------------------------------------------------------------
  129. BOOL CPicture::LoadPictureData(BYTE *pBuffer, int nSize)
  130. //=============================================================================
  131. {
  132. BOOL bResult = FALSE;
  133. HGLOBAL hGlobal = GlobalAlloc(GMEM_MOVEABLE, nSize);
  134. if(hGlobal == NULL)
  135. {
  136. // HWND hWnd = AfxGetApp()->GetMainWnd()->m_hWnd;
  137. // MessageBoxEx(NULL,"´íÎó´úºÅ:5\t", ERROR_TITLE, MB_OK | MB_ICONSTOP, LANG_ENGLISH);
  138. return(FALSE);
  139. }
  140. void* pData = GlobalLock(hGlobal);
  141. memcpy(pData, pBuffer, nSize);
  142. GlobalUnlock(hGlobal);
  143. IStream* pStream = NULL;
  144. if(CreateStreamOnHGlobal(hGlobal, TRUE, &pStream) == S_OK)
  145. {
  146. HRESULT hr;
  147. if((hr = OleLoadPicture(pStream, nSize, FALSE, IID_IPicture, (LPVOID *)&m_pPict)) == E_NOINTERFACE)
  148. {
  149. //HWND hWnd = AfxGetApp()->GetMainWnd()->m_hWnd;
  150. // MessageBoxEx(NULL, "´íÎó´úºÅ:6\t", ERROR_TITLE, MB_OK | MB_ICONSTOP, LANG_ENGLISH);
  151. return(FALSE);
  152. }
  153. else // S_OK
  154. {
  155. pStream->Release();
  156. pStream = NULL;
  157. bResult = TRUE;
  158. }
  159. }
  160. GlobalFree(hGlobal); // 16Bit Windows Needs This (32Bit - Automatic Release)
  161. return(bResult);
  162. }
  163. //-----------------------------------------------------------------------------
  164. // Does: Draw The Loaded Picture Direct To The Client DC
  165. // ~~~~
  166. //
  167. // Note: Bigger OR Smaller Dimentions Than The Original Picture Size
  168. // ~~~~ Will Draw The Picture Streached To Its New Given NEW Dimentions...
  169. //
  170. // InPut: pDC - Given DC To Draw On
  171. // ~~~~~ pSrcRect- Dimentions Of The Picture To Draw From(As a Rectangle)
  172. // DrawRect - Dimentions Of The Picture To Draw To(As a Rectangle)
  173. // OutPut: TRUE If Succeeded...
  174. // ~~~~~~
  175. //-----------------------------------------------------------------------------
  176. //=============================================================================
  177. void CPicture::Render(CDC* pDC, LPRECT pDrawRect, LPRECT pSrcRect/*=NULL*/,LPCRECT prcWBounds/*=NULL*/)
  178. {
  179. if(pDC == NULL || m_pPict == NULL) return ;
  180. CRect recrDest(pDrawRect);
  181. long Width = 0;
  182. long Height = 0;
  183. m_pPict->get_Width(&Width);
  184. m_pPict->get_Height(&Height);
  185. CRect SrcRect(0,0,Width,Height);
  186. if(pSrcRect){
  187. SrcRect=*pSrcRect;
  188. }
  189. CRect DrawRect(pDrawRect);
  190. HRESULT hrP = NULL;
  191. hrP = m_pPict->Render(pDC->m_hDC,
  192. DrawRect.left, // Left
  193. DrawRect.top, // Top
  194. DrawRect.Width(), // Right
  195. DrawRect.Height(), // Bottom
  196. SrcRect.left,
  197. SrcRect.top,
  198. SrcRect.Width(),
  199. SrcRect.Height(),
  200. prcWBounds);
  201. if (SUCCEEDED(hrP)) return;
  202. AfxThrowMemoryException();
  203. return;
  204. }//-----------------------------------------------------------------------------
  205. LONG CPicture::get_Height()
  206. {
  207. LONG nHeight = 0;
  208. if (m_pPict != NULL)
  209. {
  210. m_pPict->get_Height(&nHeight);
  211. }
  212. return nHeight;
  213. }
  214. LONG CPicture::get_Width()
  215. {
  216. LONG nWidth = 0;
  217. if (m_pPict != NULL)
  218. {
  219. m_pPict->get_Width(&nWidth);
  220. }
  221. return nWidth;
  222. }
  223. void CPicture::RenderBmp(CDC *pDC, LPRECT pDrawRect, LPRECT pSrcRect, LPCRECT prcWBounds)
  224. {
  225. if(pDC == NULL || m_pPict == NULL) return ;
  226. CRect recrDest(pDrawRect);
  227. long Width = 0;
  228. long Height = 0;
  229. m_pPict->get_Width(&Width);
  230. m_pPict->get_Height(&Height);
  231. CRect SrcRect(0,0,Width,Height);
  232. if(pSrcRect){
  233. SrcRect=*pSrcRect;
  234. }
  235. CRect DrawRect(pDrawRect);
  236. HRESULT hrP = NULL;
  237. hrP = m_pPict->Render(pDC->m_hDC,
  238. DrawRect.left, // Left
  239. DrawRect.bottom-1, // Top
  240. DrawRect.Width(), // Right
  241. -DrawRect.Height()+1, // Bottom
  242. SrcRect.left,
  243. SrcRect.top,
  244. SrcRect.Width(),
  245. SrcRect.Height()-1,
  246. prcWBounds);
  247. if (SUCCEEDED(hrP)) return;
  248. AfxThrowMemoryException();
  249. return;
  250. }