Old fashion design for PC-104
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.

358 lines
8.8 KiB

4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. //----------------------------------------------------------------------------
  2. // Laser Guard Ltd.
  3. // Copyright � 2000-2002. All Rights Reserved.
  4. // Version 1.0 01/01/2000
  5. // Last modification: 08/10/2003
  6. // PROJECT:
  7. // SUBSYSTEM:
  8. // FILE: LgMemoryDC.cpp
  9. // AUTHOR: Sergey Chizhevskiy
  10. // OVERVIEW
  11. // ~~~~~~~~
  12. // Source file of LgMemoryDC.cpp
  13. //----------------------------------------------------------------------------
  14. #include "stdafx.h"
  15. #include "LgMemoryDC.h"
  16. #include "..\LgLib\LgImageJPEG.h"
  17. #include "..\LgLib\Version.h"
  18. CLgMemoryDC::CLgMemoryDC()
  19. {
  20. m_Size.cx = 0;
  21. m_Size.cy = 0;
  22. m_hDC = NULL;
  23. m_hOldBitmap= NULL;
  24. // log information
  25. LGLOG( LOG_LEVEL_MEDIUM, LOG_TYPE_INFO, this, "CLgMemoryDC::CLgMemoryDC" );
  26. }
  27. CLgMemoryDC::~CLgMemoryDC()
  28. {
  29. if( GetSafeHdc() )
  30. DeleteDC();
  31. // log information
  32. LGLOG( LOG_LEVEL_MEDIUM, LOG_TYPE_INFO, this, "CLgMemoryDC::~CLgMemoryDC" );
  33. }
  34. BOOL CLgMemoryDC::Create( HBITMAP hBitmap, BOOL bCopy )
  35. {
  36. DIBSECTION DibSection;
  37. if( !::GetObject( hBitmap, sizeof( DibSection ), &DibSection ) )
  38. {
  39. LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CLgMemoryDC::Create"), _T("Can't get bitmap dimensions") );
  40. return FALSE;
  41. }
  42. DeleteDC();
  43. m_Size.cx = DibSection.dsBm.bmWidth;
  44. m_Size.cy = DibSection.dsBm.bmHeight;
  45. HWND hWnd = ::GetDesktopWindow();
  46. HDC hDC = ::GetDC( hWnd );
  47. if( hDC )
  48. {
  49. m_hDC = ::CreateCompatibleDC( hDC );
  50. m_hBitmap = bCopy ? ::CreateCompatibleBitmap( hDC, m_Size.cx, m_Size.cy ) : hBitmap;
  51. m_hOldBitmap = (HBITMAP)::SelectObject( m_hDC, m_hBitmap );
  52. LGLOG( LOG_LEVEL_HIGH, LOG_TYPE_INFO, this, _T("CLgMemoryDC::Create"), _T("DC %p, BITMAP %p, OLDBITMAP %p"), m_hDC, m_hBitmap, m_hOldBitmap );
  53. ::ReleaseDC( hWnd, hDC );
  54. }
  55. if( !bCopy )
  56. return TRUE;
  57. if( !m_hDC || !m_hBitmap )
  58. {
  59. LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CLgMemoryDC::Create"), _T("Can't copy: DC %p, BITMAP %p, OLDBITMAP %p"), m_hDC, m_hBitmap, m_hOldBitmap );
  60. return FALSE;
  61. }
  62. hDC = ::GetDC( hWnd );
  63. if( hDC )
  64. {
  65. HDC hTempDC = ::CreateCompatibleDC( hDC );
  66. if( hTempDC )
  67. {
  68. HBITMAP hOldTempBitmap = (HBITMAP )::SelectObject( hTempDC, hBitmap );
  69. ::BitBlt( m_hDC, 0, 0, m_Size.cx, m_Size.cy, hTempDC, 0, 0, SRCCOPY );
  70. ::SelectObject( hTempDC, hOldTempBitmap );
  71. ::DeleteDC( hTempDC );
  72. }
  73. ::ReleaseDC( hWnd, hDC );
  74. }
  75. return TRUE;
  76. }
  77. BOOL CLgMemoryDC::Create( int cx, int cy, COLORREF color )
  78. {
  79. if( m_Size.cx != cx || m_Size.cy != cy )
  80. {
  81. DeleteDC();
  82. m_Size.cx = cx;
  83. m_Size.cy = cy;
  84. HWND hWnd = ::GetDesktopWindow();
  85. HDC hDC = ::GetDC( hWnd );
  86. if( hDC )
  87. {
  88. m_hDC = ::CreateCompatibleDC( hDC );
  89. m_hBitmap = ::CreateCompatibleBitmap( hDC, m_Size.cx, m_Size.cy );
  90. m_hOldBitmap = (HBITMAP)::SelectObject( m_hDC, m_hBitmap );
  91. ::ReleaseDC( hWnd, hDC );
  92. }
  93. }
  94. if( m_hDC && m_hBitmap )
  95. {
  96. HBRUSH hBrush = ::CreateSolidBrush( color );
  97. if( hBrush )
  98. {
  99. RECT rect = { 0, 0, m_Size.cx, m_Size.cy };
  100. ::FillRect( m_hDC, &rect, hBrush );
  101. ::DeleteObject( hBrush );
  102. }
  103. }
  104. return TRUE;
  105. }
  106. BOOL CLgMemoryDC::DeleteDC()
  107. {
  108. if( m_hOldBitmap )
  109. {
  110. ::SelectObject( m_hDC, m_hOldBitmap );
  111. m_hOldBitmap = NULL;
  112. }
  113. if( m_hBitmap )
  114. {
  115. ::DeleteObject( m_hBitmap );
  116. m_hBitmap = NULL;
  117. }
  118. if( m_hDC )
  119. {
  120. ::DeleteDC( m_hDC );
  121. m_hDC = NULL;
  122. }
  123. return TRUE;
  124. }
  125. //#define WIDTHBYTES(bits) (((bits) + 31) / 32 * 4)
  126. BOOL CLgMemoryDC::SaveDIB( LPCTSTR pFileName, INT nBitCount )
  127. {
  128. BOOL bResult = FALSE;
  129. INT nBiSize = sizeof(BITMAPINFOHEADER) + sizeof(RGBQUAD) * 256;
  130. // prepare BitmapInfo structure
  131. LPBITMAPINFO pBI = (LPBITMAPINFO)malloc( sizeof(BYTE) * nBiSize );
  132. if( !pBI )
  133. return FALSE;
  134. memset( pBI, 0, nBiSize );
  135. pBI->bmiHeader.biSize = sizeof( BITMAPINFOHEADER );
  136. // unselect bitmap in dc (actually it is m_hBitmap)
  137. HBITMAP hBitmap = (HBITMAP)::SelectObject( m_hDC, m_hOldBitmap );
  138. if( ::GetDIBits( GetSafeHdc(), hBitmap, 0, 0, NULL, pBI, DIB_RGB_COLORS ) )
  139. {
  140. switch( nBitCount )
  141. {
  142. case 1:
  143. case 2:
  144. case 4:
  145. case 8:
  146. case 16:
  147. case 24:
  148. pBI->bmiHeader.biBitCount = nBitCount;
  149. pBI->bmiHeader.biSizeImage = WIDTHBYTES( pBI->bmiHeader.biBitCount * pBI->bmiHeader.biWidth ) * pBI->bmiHeader.biHeight;
  150. break;
  151. case ORIGINAL_BIT_COUNT:
  152. break;
  153. default:
  154. // delete BitmapInfo structure
  155. delete [] (PBYTE)pBI;
  156. // select bitmap into dc
  157. SelectObject( m_hDC, hBitmap );
  158. return FALSE;
  159. }
  160. // nessessary uncompressed bitmap
  161. pBI->bmiHeader.biCompression = 0;
  162. PBYTE pByte = (PBYTE)malloc( sizeof(BYTE) * pBI->bmiHeader.biSizeImage );
  163. if( pByte )
  164. {
  165. if( ::GetDIBits( GetSafeHdc(), hBitmap, 0, pBI->bmiHeader.biHeight, pByte, pBI, DIB_RGB_COLORS ) )
  166. {
  167. bResult = SaveDIB( pBI, pByte, pFileName );
  168. }
  169. free( pByte );
  170. }
  171. free( pBI );
  172. }
  173. // select bitmap into dc
  174. ::SelectObject( m_hDC, hBitmap );
  175. return bResult;
  176. }
  177. BOOL CLgMemoryDC::SaveDIB( LPBITMAPINFO pBitmapInfo, LPBYTE pByte, LPCTSTR pFileName )
  178. {
  179. if( !pBitmapInfo || !pByte || !pFileName )
  180. return FALSE;
  181. // open file
  182. HANDLE hFile = ::CreateFile( pFileName, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, NULL );
  183. if( hFile == INVALID_HANDLE_VALUE )
  184. return FALSE;
  185. // generate palette size
  186. int nPaletteSize = sizeof(RGBQUAD) * ( ( pBitmapInfo->bmiHeader.biBitCount > 8 ) ? 0 : ( 1 << pBitmapInfo->bmiHeader.biBitCount ) );
  187. // write file header
  188. BITMAPFILEHEADER bfh;
  189. bfh.bfType = MAKEWORD( 'B', 'M' );
  190. bfh.bfSize = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + nPaletteSize + pBitmapInfo->bmiHeader.biSizeImage;
  191. bfh.bfReserved1 = 0;
  192. bfh.bfReserved2 = 0;
  193. bfh.bfOffBits = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + nPaletteSize;
  194. DWORD dwWrite;
  195. ::WriteFile( hFile, &bfh, sizeof(BITMAPFILEHEADER), &dwWrite, NULL );
  196. // write bitmap info
  197. ::WriteFile( hFile, pBitmapInfo, sizeof(BITMAPINFOHEADER) + nPaletteSize, &dwWrite, NULL );
  198. // write bits
  199. ::WriteFile( hFile, pByte, pBitmapInfo->bmiHeader.biSizeImage, &dwWrite, NULL );
  200. // close file
  201. ::CloseHandle( hFile );
  202. return TRUE;
  203. }
  204. BOOL CLgMemoryDC::SaveJPEG( LPCTSTR pFileName, INT nQuality )
  205. {
  206. BOOL bResult = FALSE;
  207. INT nBiSize = sizeof(BITMAPINFOHEADER) + sizeof(RGBQUAD) * 256;
  208. // prepare BitmapInfo structure
  209. LPBITMAPINFO pBI = (LPBITMAPINFO)malloc( sizeof(BYTE) * nBiSize );
  210. if( !pBI )
  211. return FALSE;
  212. memset( pBI, 0, nBiSize );
  213. pBI->bmiHeader.biSize = sizeof( BITMAPINFOHEADER );
  214. // unselect bitmap in dc (actually it is m_hBitmap)
  215. HBITMAP hBitmap = (HBITMAP)::SelectObject( m_hDC, m_hOldBitmap );
  216. if( ::GetDIBits( GetSafeHdc(), hBitmap, 0, 0, NULL, pBI, DIB_RGB_COLORS ) )
  217. {
  218. // nessessary uncompressed bitmap
  219. pBI->bmiHeader.biCompression = 0;
  220. pBI->bmiHeader.biBitCount = 24;
  221. pBI->bmiHeader.biSizeImage = pBI->bmiHeader.biHeight * WIDTHBYTES( pBI->bmiHeader.biWidth * 24 );
  222. PBYTE pByte = (PBYTE)malloc( sizeof(BYTE) * pBI->bmiHeader.biSizeImage );
  223. if( pByte )
  224. {
  225. if( ::GetDIBits( GetSafeHdc(), hBitmap, 0, pBI->bmiHeader.biHeight, pByte, pBI, DIB_RGB_COLORS ) )
  226. {
  227. CLgImageJPEG jpeg;
  228. bResult = jpeg.Create( pBI, pByte, nQuality ) && jpeg.Save( pFileName );
  229. }
  230. free( pByte );
  231. }
  232. free( pBI );
  233. }
  234. // select bitmap into dc
  235. ::SelectObject( m_hDC, hBitmap );
  236. return bResult;
  237. }
  238. BOOL CLgMemoryDC::GetDIB( CLgImageDIB *pDIB )
  239. {
  240. if( !m_hDC || !m_hOldBitmap )
  241. return FALSE;
  242. BOOL bResult = FALSE;
  243. HBITMAP hBitmap = (HBITMAP)::SelectObject( m_hDC, m_hOldBitmap );
  244. if( hBitmap )
  245. {
  246. bResult = pDIB->Create( hBitmap );
  247. ::SelectObject( m_hDC, hBitmap );
  248. }
  249. return bResult;
  250. }
  251. HBITMAP CLgMemoryDC::CopyImage()
  252. {
  253. HBITMAP hBitmap = (HBITMAP)::SelectObject( m_hDC, m_hOldBitmap );
  254. HBITMAP hResult = (HBITMAP)::CopyImage( m_hBitmap, IMAGE_BITMAP, 0, 0, LR_CREATEDIBSECTION );
  255. ::SelectObject( m_hDC, hBitmap );
  256. return hResult;
  257. }
  258. BOOL CLgMemoryDC::Ellipse( int nLeftRect, int nTopRect, int nRightRect, int nBottomRect )
  259. {
  260. return ::Ellipse( m_hDC, nLeftRect, nTopRect, nRightRect, nBottomRect );
  261. }
  262. BOOL CLgMemoryDC::LineTo( int x, int y )
  263. {
  264. return ::LineTo( m_hDC, x, y );
  265. }
  266. BOOL CLgMemoryDC::MoveTo( int x, int y )
  267. {
  268. return ::MoveToEx( m_hDC, x, y, NULL );
  269. }
  270. int CLgMemoryDC::DrawText( LPCTSTR lpString, int nCount, LPRECT lpRect, UINT uFormat )
  271. {
  272. return ::DrawText( m_hDC, lpString, nCount, lpRect, uFormat );
  273. }
  274. BOOL CLgMemoryDC::FillSolidRect( LPCRECT lpRect, COLORREF clr )
  275. {
  276. HBRUSH hBrush = ::CreateSolidBrush( clr );
  277. if( !hBrush )
  278. return FALSE;
  279. BOOL bResult = ::FillRect( m_hDC, lpRect, hBrush );
  280. ::DeleteObject( hBrush );
  281. return bResult;
  282. }
  283. BOOL CLgMemoryDC::DrawIcon( int X, int Y, HICON hIcon )
  284. {
  285. return ::DrawIcon( m_hDC, X, Y, hIcon );
  286. }