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.

437 lines
11 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: VideoWnd.cpp
  9. // AUTHOR: Sergey Chizhevskiy
  10. // OVERVIEW
  11. // ~~~~~~~~
  12. // Source file of VideoWnd.cpp
  13. //----------------------------------------------------------------------------
  14. #include "stdafx.h"
  15. #include "VideoWnd.h"
  16. #include "AuxLib.h"
  17. #include "..\..\LgLib\Version.h"
  18. //#include "..\Chicago\VideoBase.h"
  19. #ifdef _DEBUG
  20. #define new DEBUG_NEW
  21. #undef THIS_FILE
  22. static char THIS_FILE[] = __FILE__;
  23. #endif
  24. #ifndef WIDTHBYTES
  25. #define WIDTHBYTES(bits) (((bits + 31) >> 5) << 2)
  26. #endif
  27. CVideoForWindowsWnd::CVideoForWindowsWnd()
  28. {
  29. m_Capture.hBitmap = NULL;
  30. m_Capture.pBmi = NULL;
  31. m_Capture.pBuffer = NULL;
  32. LGLOG( LOG_LEVEL_MEDIUM, LOG_TYPE_INFO, this, _T("CVideoForWindowsWnd::CVideoForWindowsWnd") );
  33. }
  34. CVideoForWindowsWnd::~CVideoForWindowsWnd()
  35. {
  36. CaptureDone();
  37. LGLOG( LOG_LEVEL_MEDIUM, LOG_TYPE_INFO, this, _T("CVideoForWindowsWnd::~CVideoForWindowsWnd") );
  38. }
  39. BEGIN_MESSAGE_MAP(CVideoForWindowsWnd, CWnd)
  40. //{{AFX_MSG_MAP(CVideoForWindowsWnd)
  41. //}}AFX_MSG_MAP
  42. END_MESSAGE_MAP()
  43. /////////////////////////////////////////////////////////////////////////////
  44. // CVideoForWindowsWnd message handlers
  45. BOOL CVideoForWindowsWnd::DestroyWindow()
  46. {
  47. if( GetSafeHwnd() )
  48. {
  49. CaptureDone();
  50. HWND hWnd = GetSafeHwnd();
  51. UnsubclassWindow();
  52. capOverlay( hWnd, FALSE );
  53. capDriverDisconnect( hWnd );
  54. ::DestroyWindow( hWnd );
  55. }
  56. return CWnd::DestroyWindow();
  57. }
  58. BOOL CVideoForWindowsWnd::DlgVideoSource()
  59. {
  60. HWND hWnd = GetSafeHwnd();
  61. if( hWnd )
  62. {
  63. BOOL bResult = capDlgVideoSource( hWnd );
  64. CaptureInit( hWnd );
  65. return bResult;
  66. }
  67. return FALSE;
  68. }
  69. BOOL CVideoForWindowsWnd::DlgVideoFormat()
  70. {
  71. HWND hWnd = GetSafeHwnd();
  72. if( hWnd )
  73. {
  74. BOOL bResult = capDlgVideoFormat( hWnd );
  75. CaptureInit( hWnd );
  76. return bResult;
  77. }
  78. return FALSE;
  79. }
  80. BOOL CVideoForWindowsWnd::DlgVideoDisplay()
  81. {
  82. HWND hWnd = GetSafeHwnd();
  83. if( hWnd )
  84. {
  85. BOOL bResult = capDlgVideoDisplay( hWnd );
  86. CaptureInit( hWnd );
  87. return bResult;
  88. }
  89. return FALSE;
  90. }
  91. BOOL CVideoForWindowsWnd::FileSaveDIB( LPSTR pFileName )
  92. {
  93. HWND hWnd = GetSafeHwnd();
  94. if( hWnd )
  95. {
  96. return capFileSaveDIB( hWnd, pFileName );
  97. }
  98. return FALSE;
  99. }
  100. BOOL CVideoForWindowsWnd::GrabFrameNoStop()
  101. {
  102. HWND hWnd = GetSafeHwnd();
  103. if( hWnd )
  104. {
  105. return capGrabFrameNoStop( hWnd );
  106. }
  107. return FALSE;
  108. }
  109. LRESULT CVideoForWindowsWnd::ErrorCallbackProc( HWND hWnd, int nErrID, LPSTR lpErrorText )
  110. {
  111. return ((CVideoForWindowsWnd*)capGetUserData( hWnd ))->OnErrorCallback( hWnd, nErrID, lpErrorText );
  112. }
  113. LRESULT CVideoForWindowsWnd::FrameCallbackProc( HWND hWnd, LPVIDEOHDR lpVHdr )
  114. {
  115. return ((CVideoForWindowsWnd*)capGetUserData( hWnd ))->OnFrameCallback( hWnd, lpVHdr );
  116. }
  117. BOOL CVideoForWindowsWnd::CaptureInit( HWND hWnd )
  118. {
  119. CaptureDone();
  120. DWORD dwVFSize = capGetVideoFormatSize( hWnd );
  121. m_Capture.pBmi = (BITMAPINFO *) new BYTE[ dwVFSize ];
  122. ::ZeroMemory( m_Capture.pBmi, dwVFSize );
  123. capGetVideoFormat( hWnd, m_Capture.pBmi, dwVFSize );
  124. // if( !m_Capture.pBmi->bmiHeader.biCompression )
  125. // m_Capture.pBmi->bmiHeader.biSizeImage = WIDTHBYTES( m_Capture.pBmi->bmiHeader.biBitCount * m_Capture.pBmi->bmiHeader.biWidth ) * m_Capture.pBmi->bmiHeader.biHeight;
  126. m_Capture.hBitmap = ::CreateDIBSection( 0, m_Capture.pBmi, DIB_RGB_COLORS, (void**)&m_Capture.pBuffer, NULL, NULL );
  127. return TRUE;
  128. }
  129. BOOL CVideoForWindowsWnd::CaptureDone()
  130. {
  131. if( m_Capture.hBitmap )
  132. {
  133. ::DeleteObject( m_Capture.hBitmap );
  134. m_Capture.hBitmap = NULL;
  135. }
  136. if( m_Capture.pBmi )
  137. {
  138. delete [] (LPBYTE)m_Capture.pBmi;
  139. m_Capture.pBmi = NULL;
  140. }
  141. m_Capture.pBuffer = NULL;
  142. return TRUE;
  143. }
  144. HBITMAP CVideoForWindowsWnd::Capture( LPBITMAPINFO *ppBmi, LPBYTE *ppBytes, DWORD dwPreviewTime )
  145. {
  146. if( GrabFrameNoStop() )
  147. {
  148. if( ppBmi )
  149. *ppBmi = m_Capture.pBmi;
  150. if( ppBytes )
  151. *ppBytes = m_Capture.pBuffer;
  152. return (HBITMAP)::CopyImage( m_Capture.hBitmap, IMAGE_BITMAP, 0, 0, 0 );
  153. }
  154. return NULL;
  155. }
  156. LRESULT CVideoForWindowsWnd::OnFrameCallback( HWND hWnd, LPVIDEOHDR lpVHdr )
  157. {
  158. if( m_Capture.pBuffer )
  159. {
  160. ::CopyMemory( m_Capture.pBuffer, lpVHdr->lpData, m_Capture.pBmi->bmiHeader.biSizeImage );
  161. }
  162. return 0;
  163. }
  164. LRESULT CVideoForWindowsWnd::OnErrorCallback( HWND hWnd, int nErrID, LPSTR lpErrorText )
  165. {
  166. return 0;
  167. }
  168. BOOL CVideoForWindowsWnd::Create(CWnd *pParentWnd, int x, int y, int cx, int cy, int ccx, int ccy)
  169. {
  170. HWND hWnd = NULL;
  171. for ( INT iIndex = 0; iIndex <10; iIndex++ )
  172. {
  173. TCHAR pDriverName[ MAX_PATH ];
  174. TCHAR pDriverVer[ MAX_PATH ];
  175. if( capGetDriverDescription( iIndex, pDriverName, MAX_PATH, pDriverVer, MAX_PATH ) )
  176. {
  177. hWnd = capCreateCaptureWindow( NULL, WS_CHILD | WS_VISIBLE, x, y, cx, cy, pParentWnd->GetSafeHwnd(), 1 );
  178. if( hWnd )
  179. {
  180. if( capDriverConnect( hWnd, iIndex ) )
  181. {
  182. /*
  183. INT nSize = capGetVideoFormatSize( hWnd );
  184. LPBITMAPINFO pBitmapInfo = (LPBITMAPINFO)malloc(nSize);
  185. if( !pBitmapInfo )
  186. return FALSE;
  187. if( capGetVideoFormat( hWnd, pBitmapInfo, nSize ) )
  188. {
  189. pBitmapInfo->bmiHeader.biBitCount = 24;
  190. pBitmapInfo->bmiHeader.biCompression = 0;
  191. pBitmapInfo->bmiHeader.biSizeImage = WIDTHBYTES( pBitmapInfo->bmiHeader.biWidth * pBitmapInfo->bmiHeader.biBitCount ) * pBitmapInfo->bmiHeader.biHeight;
  192. capSetVideoFormat( hWnd, pBitmapInfo, nSize );
  193. }
  194. else
  195. */ {
  196. BITMAPINFO bmi;
  197. memset( &bmi, 0, sizeof(BITMAPINFOHEADER) );
  198. bmi.bmiHeader.biSize = sizeof( BITMAPINFOHEADER );
  199. bmi.bmiHeader.biWidth = ccx;
  200. bmi.bmiHeader.biHeight = ccy;
  201. bmi.bmiHeader.biPlanes = 1;
  202. bmi.bmiHeader.biBitCount = 24;
  203. bmi.bmiHeader.biCompression = 0;
  204. bmi.bmiHeader.biSizeImage = WIDTHBYTES( bmi.bmiHeader.biWidth * bmi.bmiHeader.biBitCount ) * bmi.bmiHeader.biHeight;
  205. capSetVideoFormat( hWnd, &bmi, sizeof(BITMAPINFOHEADER) );
  206. }
  207. // free( pBitmapInfo );
  208. if( capOverlay( hWnd, TRUE ) )
  209. {
  210. CaptureInit( hWnd );
  211. if( pParentWnd->IsWindowVisible() )
  212. ::ShowWindow( hWnd, SW_SHOW );
  213. else
  214. ::ShowWindow( hWnd, SW_HIDE );
  215. ::UpdateWindow( hWnd );
  216. ::InvalidateRect( hWnd, NULL, FALSE );
  217. capSetUserData( hWnd, (LONG)this );
  218. capSetCallbackOnFrame( hWnd, FrameCallbackProc );
  219. capSetCallbackOnError( hWnd, ErrorCallbackProc );
  220. // return SubclassWindow( hWnd );
  221. LGLOG( LOG_LEVEL_MEDIUM, LOG_TYPE_INFO, this, _T("CVideoForWindowsWnd::Create"), _T("Succeeded") );
  222. return TRUE;
  223. }
  224. }
  225. }
  226. }
  227. }
  228. LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CVideoForWindowsWnd::Create"), _T("Failed") );
  229. return FALSE;
  230. }
  231. ///////////////////////////////////////////////////////////////////////////////////
  232. CVideoFrameDIB::CVideoFrameDIB()
  233. {
  234. // reset members
  235. m_pBitmapInfo = NULL;
  236. m_pBytes = NULL;
  237. m_nBitmapInfoSize = 0;
  238. LGLOG( LOG_LEVEL_HIGH, LOG_TYPE_INFO, this, _T("CVideoFrameDIB::CVideoFrameDIB") );
  239. }
  240. CVideoFrameDIB::~CVideoFrameDIB()
  241. {
  242. // destroy members
  243. Destroy();
  244. LGLOG( LOG_LEVEL_HIGH, LOG_TYPE_INFO, this, _T("CVideoFrameDIB::~CVideoFrameDIB") );
  245. }
  246. BOOL CVideoFrameDIB::Create( LPBITMAPINFO pBitmapInfo, PBYTE pBytes )
  247. {
  248. // protect data from simultaniosly access
  249. CLgLocker Locker( &m_CriticalSection );
  250. // check entered values
  251. if( !pBitmapInfo || !pBytes )
  252. {
  253. LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CVideoFrameDIB::Create"), _T("Parameters are incorrect (%p,%p)"), pBitmapInfo, pBytes );
  254. return FALSE;
  255. }
  256. // calculate bitmap info size
  257. INT nBitmapInfoSize = sizeof(BITMAPINFOHEADER) +
  258. ( pBitmapInfo->bmiHeader.biBitCount > 8 ? 0 : (1 << pBitmapInfo->bmiHeader.biBitCount) );
  259. if( nBitmapInfoSize != m_nBitmapInfoSize )
  260. {
  261. // calculate pallette size
  262. m_pBitmapInfo = (LPBITMAPINFO)realloc( m_pBitmapInfo, nBitmapInfoSize );
  263. if( !m_pBitmapInfo )
  264. return FALSE;
  265. // remember size
  266. m_nBitmapInfoSize = nBitmapInfoSize;
  267. // create array for holding pixel data
  268. m_pBytes = (PBYTE)realloc( m_pBytes, sizeof(BYTE) * pBitmapInfo->bmiHeader.biSizeImage );
  269. if( !m_pBytes )
  270. return FALSE;
  271. }
  272. else
  273. // allocate memory for pixels if nessessary
  274. if( m_pBitmapInfo->bmiHeader.biSizeImage < pBitmapInfo->bmiHeader.biSizeImage )
  275. {
  276. m_pBytes = (PBYTE)realloc( m_pBytes, sizeof(BYTE) * pBitmapInfo->bmiHeader.biSizeImage );
  277. if( !m_pBytes )
  278. return FALSE;
  279. }
  280. // copy dib bits
  281. memcpy( m_pBytes, pBytes, pBitmapInfo->bmiHeader.biSizeImage );
  282. // remember new attributes
  283. memcpy( m_pBitmapInfo, pBitmapInfo, m_nBitmapInfoSize );
  284. return TRUE;
  285. }
  286. void CVideoFrameDIB::Destroy()
  287. {
  288. // protect data from simultaniosly access
  289. CLgLocker Locker( &m_CriticalSection );
  290. // clear memory
  291. if( m_pBytes )
  292. {
  293. free( m_pBytes );
  294. m_pBytes = NULL;
  295. }
  296. // clear attributes
  297. if( m_pBitmapInfo )
  298. {
  299. free( m_pBitmapInfo );
  300. m_pBitmapInfo = NULL;
  301. }
  302. }
  303. BOOL CVideoFrameDIB::Draw( HDC hDC, PRECT pRect )
  304. {
  305. LGLOG( LOG_LEVEL_REALTIME, LOG_TYPE_INFO, this, _T("CVideoFrameDIB::Draw"), _T("Started...") );
  306. // protect data from simultaniosly access
  307. CLgLocker Locker( &m_CriticalSection );
  308. if( !m_pBytes )
  309. {
  310. LGLOG( LOG_LEVEL_REALTIME, LOG_TYPE_ERROR, this, _T("CVideoFrameDIB::Draw"), _T("m_pBytes is NULL") );
  311. return FALSE;
  312. }
  313. INT nMode = ::SetStretchBltMode( hDC, COLORONCOLOR );
  314. INT nWidth = pRect->right - pRect->left;
  315. INT nHeight = pRect->bottom - pRect->top;
  316. ::StretchDIBits( hDC, pRect->left, pRect->top, nWidth, nHeight, 0, 0, m_pBitmapInfo->bmiHeader.biWidth, m_pBitmapInfo->bmiHeader.biHeight, m_pBytes, m_pBitmapInfo, DIB_RGB_COLORS, SRCCOPY );
  317. ::SetStretchBltMode( hDC, nMode );
  318. LGLOG( LOG_LEVEL_REALTIME, LOG_TYPE_INFO, this, _T("CVideoFrameDIB::Draw"), _T("Done succeessfully") );
  319. return TRUE;
  320. }
  321. HBITMAP CVideoFrameDIB::CreateBitmap()
  322. {
  323. // protect data from simultaniosly access
  324. CLgLocker Locker( &m_CriticalSection );
  325. if( !m_pBytes )
  326. return FALSE;
  327. // get dc of entire screen
  328. HDC hDC = ::GetDC( NULL );
  329. if( !hDC )
  330. return NULL;
  331. // create dib bitmap
  332. HBITMAP hBitmap = ::CreateDIBitmap( hDC, &m_pBitmapInfo->bmiHeader, CBM_INIT, m_pBytes, m_pBitmapInfo, DIB_RGB_COLORS );
  333. // relase dc
  334. ::ReleaseDC( NULL, hDC );
  335. return hBitmap;
  336. }
  337. HBITMAP CVideoFrameDIB::CreateBitmap( LPBITMAPINFO *ppBitmapInfo )
  338. {
  339. // protect data from simultaniosly access
  340. CLgLocker Locker( &m_CriticalSection );
  341. if( !m_pBytes )
  342. return FALSE;
  343. // get dc of entire screen
  344. HDC hDC = ::GetDC( NULL );
  345. if( !hDC )
  346. return NULL;
  347. // create dib bitmap
  348. HBITMAP hBitmap = ::CreateDIBitmap( hDC, &m_pBitmapInfo->bmiHeader, CBM_INIT, m_pBytes, m_pBitmapInfo, DIB_RGB_COLORS );
  349. // relase dc
  350. ::ReleaseDC( NULL, hDC );
  351. // return format
  352. if( ppBitmapInfo )
  353. {
  354. *ppBitmapInfo = m_pBitmapInfo;
  355. }
  356. return hBitmap;
  357. }