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.

297 lines
7.8 KiB

4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. //----------------------------------------------------------------------------
  2. // Laser Guard Ltd.
  3. // Copyright � 2000-2002. All Rights Reserved.
  4. // Version 1.0 24/03/2000
  5. // Last modification: 08/10/2003
  6. // Project Intruder Position Indicator
  7. // SUBSYSTEM: Video camera showing and captiring window
  8. // FILE: VideoDirectShowWnd.cpp
  9. // AUTHOR: Chizhevskiy Sergey
  10. // OVERVIEW
  11. // ~~~~~~~~
  12. // Source file for implementation of class CVideoDirectShowWnd
  13. //----------------------------------------------------------------------------
  14. #include "stdafx.h"
  15. #include "..\LgLib\Version.h"
  16. #include "..\lglib\video\DirectShowStreamVideo.h"
  17. #include "VideoDirectShowWnd.h"
  18. #include "Application.h"
  19. CVideoDirectShowWnd::CVideoDirectShowWnd()
  20. {
  21. m_dwTime = 0;
  22. m_nCount = 0;
  23. m_nLastCount = 0;
  24. m_bStretchVideo = TRUE;
  25. LGLOG( LOG_LEVEL_MEDIUM, LOG_TYPE_INFO, this, "CVideoDirectShowWnd::CVideoDirectShowWnd" );
  26. }
  27. CVideoDirectShowWnd::~CVideoDirectShowWnd()
  28. {
  29. CVideoWnd::DestroyWindow();
  30. CDirectShowVideo::Unlink();
  31. LGLOG( LOG_LEVEL_MEDIUM, LOG_TYPE_INFO, this, "CVideoDirectShowWnd::~CVideoDirectShowWnd" );
  32. }
  33. BOOL CVideoDirectShowWnd::Create( BOOL bStretchVideo, HWND hParentWnd, INT x, INT y, INT cx, INT cy, CProfile *pProfile )
  34. {
  35. if( !CVideoWnd::Create( bStretchVideo, hParentWnd, x, y, cx, cy, pProfile ) )
  36. {
  37. LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CVideoDirectShowWnd::Create"), _T("CVideoWnd::Create failed") );
  38. return FALSE;
  39. }
  40. HINSTANCE hInstance = (HINSTANCE)::GetWindowLong(hParentWnd, GWL_HINSTANCE);
  41. LPCTSTR pClassName = _T("DirectShowVideoWnd");
  42. m_bStretchVideo = bStretchVideo;
  43. if( !CGenericWnd::GetClassInfoEx( hInstance, pClassName, NULL ) )
  44. {
  45. if( !CVideoWnd::RegisterClass( pClassName, hInstance ) )
  46. return FALSE;
  47. }
  48. if( !CGenericWnd::Create( pClassName, NULL, WS_CHILD, x, y, cx, cy, hParentWnd, NULL, hInstance ) )
  49. return FALSE;
  50. ShowWindow( SW_SHOW );
  51. UpdateWindow();
  52. return TRUE;
  53. }
  54. LRESULT CVideoDirectShowWnd::WindowProc( UINT uMsg, WPARAM wParam, LPARAM lParam )
  55. {
  56. switch( uMsg )
  57. {
  58. case WM_VIDEO_SAMPLE:
  59. return OnVideoSample( wParam, lParam );
  60. }
  61. return CVideoWnd::WindowProc( uMsg, wParam, lParam );
  62. }
  63. HBITMAP CVideoDirectShowWnd::StoreImage( HANDLE hFile, LPCTSTR pBuffer, INT nWidth, INT nHeight, INT nBitCount )
  64. {
  65. LPBITMAPINFO pSrcBmi = NULL;
  66. // get current bitmap
  67. HBITMAP hBitmap = GetDirectShowDIB().CreateBitmap( &pSrcBmi, NULL );
  68. if( !hBitmap )
  69. return NULL;
  70. // prepare bitmap info
  71. BITMAPINFO bmi = *pSrcBmi;
  72. LPBITMAPINFO pBmi = &bmi;
  73. pBmi->bmiHeader.biBitCount = 24;
  74. pBmi->bmiHeader.biWidth = nWidth;
  75. pBmi->bmiHeader.biHeight = nHeight;
  76. pBmi->bmiHeader.biSizeImage = WIDTHBYTES( pBmi->bmiHeader.biWidth * pBmi->bmiHeader.biBitCount ) * pBmi->bmiHeader.biHeight;
  77. // streatch bitmap as nessessary
  78. if( !m_StoreDIB.Create( hBitmap, pBmi ) )
  79. return NULL;
  80. PBYTE pBytes = m_StoreDIB;
  81. // draw text into bitmap
  82. if( nBitCount == 8 )
  83. {
  84. int lenDst = WIDTHBYTES( pBmi->bmiHeader.biWidth * 8 );
  85. int lenSrc = WIDTHBYTES( pBmi->bmiHeader.biWidth * pBmi->bmiHeader.biBitCount );
  86. LPBYTE lpDst = pBytes;
  87. LPBYTE lpSrc = pBytes;
  88. for(int y = 0; y < pBmi->bmiHeader.biHeight; y++, lpDst += lenDst, lpSrc += lenSrc )
  89. {
  90. LPBYTE pDst = lpDst;
  91. LPBYTE pSrc = lpSrc;
  92. for(int x = 0; x < lenDst; x++, pSrc += 3)
  93. *pDst++ = (BYTE)((pSrc[0] >>3 ) + (pSrc[1] >> 1) + (pSrc[2] >> 2) + 33);
  94. }
  95. DrawText8( pBmi, pBytes, pBuffer );
  96. }
  97. else
  98. if( nBitCount == 24 )
  99. {
  100. DrawText24( pBmi, pBytes, pBuffer );
  101. }
  102. DWORD dwWritten;
  103. ::WriteFile(hFile, pBytes, WIDTHBYTES( pBmi->bmiHeader.biWidth * nBitCount ) * pBmi->bmiHeader.biHeight, &dwWritten, NULL);
  104. return hBitmap;
  105. }
  106. HBITMAP CVideoDirectShowWnd::StoreImage( LPBYTE pDstBytes, LPCTSTR pBuffer, INT nWidth, INT nHeight, INT nBitCount )
  107. {
  108. LPBITMAPINFO pSrcBmi = NULL;
  109. GetDirectShowDIB().Save( "e:\\test.bmp" );
  110. // get current bitmap
  111. HBITMAP hBitmap = GetDirectShowDIB().CreateBitmap( &pSrcBmi, NULL );
  112. if( !hBitmap )
  113. return NULL;
  114. // prepare bitmap info
  115. BITMAPINFO bmi = *pSrcBmi;
  116. LPBITMAPINFO pBmi = &bmi;
  117. pBmi->bmiHeader.biBitCount = 24;
  118. pBmi->bmiHeader.biWidth = nWidth;
  119. pBmi->bmiHeader.biHeight = nHeight;
  120. pBmi->bmiHeader.biSizeImage = WIDTHBYTES( pBmi->bmiHeader.biWidth * pBmi->bmiHeader.biBitCount ) * pBmi->bmiHeader.biHeight;
  121. // streatch bitmap as nessessary
  122. if( !m_StoreDIB.Create( hBitmap, pBmi ) )
  123. return NULL;
  124. PBYTE pBytes = m_StoreDIB;
  125. // draw text into bitmap
  126. if( nBitCount == 8 )
  127. {
  128. int lenDst = WIDTHBYTES( pBmi->bmiHeader.biWidth * 8 );
  129. int lenSrc = WIDTHBYTES( pBmi->bmiHeader.biWidth * pBmi->bmiHeader.biBitCount );
  130. LPBYTE lpDst = pBytes;
  131. LPBYTE lpSrc = pBytes;
  132. for(int y = 0; y < pBmi->bmiHeader.biHeight; y++, lpDst += lenDst, lpSrc += lenSrc )
  133. {
  134. LPBYTE pDst = lpDst;
  135. LPBYTE pSrc = lpSrc;
  136. for(int x = 0; x < lenDst; x++, pSrc += 3)
  137. *pDst++ = (BYTE)((pSrc[0] >>3 ) + (pSrc[1] >> 1) + (pSrc[2] >> 2) + 33);
  138. }
  139. DrawText8( pBmi, pBytes, pBuffer );
  140. }
  141. else
  142. if( nBitCount == 24 )
  143. {
  144. DrawText24( pBmi, pBytes, pBuffer );
  145. }
  146. memcpy( pDstBytes, pBytes, WIDTHBYTES( pBmi->bmiHeader.biWidth * nBitCount ) * pBmi->bmiHeader.biHeight );
  147. return hBitmap;
  148. }
  149. HBITMAP CVideoDirectShowWnd::GetSnapshot()
  150. {
  151. return GetDirectShowDIB().CreateBitmap( NULL, NULL );
  152. }
  153. LRESULT CVideoDirectShowWnd::OnShowWindow( WPARAM wParam, LPARAM lParam )
  154. {
  155. CDirectShowVideo::ShowVideo( wParam );
  156. return CVideoWnd::OnShowWindow( wParam, lParam );
  157. }
  158. void CVideoDirectShowWnd::OnVideoSample( double SampleTime )
  159. {
  160. if( IsWindow() )
  161. PostMessage( WM_VIDEO_SAMPLE, 0, 0 );
  162. }
  163. LRESULT CVideoDirectShowWnd::OnVideoSample( WPARAM, LPARAM )
  164. {
  165. if( !OnRemoteVideo( &GetDirectShowDIB(), 0 ) )
  166. LGLOG( LOG_LEVEL_MEDIUM, LOG_TYPE_ERROR, this, _T("CVideoDirectShowWnd::OnVideoSample"), _T("OnRemoteVideo failed") );
  167. if( IsWindow() )
  168. {
  169. BOOL bInvalidate = FALSE;
  170. if( ::GetSystemMetrics( SM_REMOTESESSION ) )
  171. {
  172. DWORD dwTime = GetTickCount();
  173. if( m_dwTime <= dwTime )
  174. {
  175. m_dwTime = dwTime + 500UL;
  176. bInvalidate = TRUE;
  177. }
  178. }
  179. else
  180. bInvalidate = TRUE;
  181. if( bInvalidate )
  182. InvalidateRect( NULL, FALSE );
  183. }
  184. return 0;
  185. }
  186. LRESULT CVideoDirectShowWnd::OnDestroy( WPARAM wParam, LPARAM lParam )
  187. {
  188. // unlink from direct show
  189. CDirectShowVideo::Unlink();
  190. // direct draw update
  191. CLgDirectDrawVideo::OnDestroy();
  192. return 0;
  193. }
  194. LRESULT CVideoDirectShowWnd::OnSize( WPARAM wParam, LPARAM lParam )
  195. {
  196. // direct draw update
  197. if( !CLgDirectDrawVideo::OnSize( GetSafeHwnd() ) )
  198. {
  199. RECT rect;
  200. GetClientRect( &rect );
  201. HDC hDC = GetDC();
  202. HBRUSH hBrush = CreateSolidBrush( RGB(0,0,0) );
  203. ::FillRect( hDC, &rect, hBrush );
  204. ::DeleteObject( hBrush );
  205. ReleaseDC( hDC );
  206. }
  207. return 0;
  208. }
  209. void CVideoDirectShowWnd::OnPaint( HDC hDC )
  210. {
  211. RECT rect;
  212. GetClientRect( &rect );
  213. if( GetSystemMetrics( SM_REMOTESESSION ) )
  214. {
  215. TCHAR pText[] = _T("Remote Desktop detected");
  216. INT nBkMode = ::SetBkMode( hDC, TRANSPARENT );
  217. COLORREF cTextColor = ::SetTextColor( hDC, RGB(255,0,0) );
  218. ::DrawText( hDC, pText, _tcslen(pText), &rect, DT_CENTER );
  219. ::SetTextColor( hDC, cTextColor );
  220. ::SetBkMode( hDC, nBkMode );
  221. INT nSmallWidth = 64;
  222. INT nSmallHeight = 64;
  223. INT nWidth = rect.right - rect.left;
  224. INT nHeight = rect.bottom - rect.top;
  225. SetRect( &rect, (nWidth - nSmallWidth) / 2, (nHeight - nSmallHeight) / 2,
  226. (nWidth + nSmallWidth) / 2, (nHeight + nSmallHeight) / 2 );
  227. GetDirectShowDIB().Draw( hDC, &rect, m_bStretchVideo );
  228. }
  229. else
  230. // direct draw update
  231. if( !CLgDirectDrawVideo::OnPaint( m_bStretchVideo ) )
  232. {
  233. GetDirectShowDIB().Draw( hDC, &rect, m_bStretchVideo );
  234. }
  235. }