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.

468 lines
12 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: VideoFrameGrabberWnd.cpp
  9. // AUTHOR: Sergey Chizhevskiy
  10. // OVERVIEW
  11. // ~~~~~~~~
  12. // Source file of VideoFrameGrabberWnd.cpp
  13. //----------------------------------------------------------------------------
  14. #include "stdafx.h"
  15. #include "..\LgLib\Version.h"
  16. /*
  17. #include "VideoFrameGrabberWnd.h"
  18. #define VIDEO_NTSC
  19. CVideoFrameGrabberWnd::CVideoFrameGrabberWnd()
  20. {
  21. m_hFrameGrabberWnd = NULL;
  22. m_hFrameGrabberMemDC = NULL;
  23. m_hFrameGrabberMemBitmap = NULL;
  24. m_pFrameGrabberBuffer = NULL;
  25. m_pFrameGrabberBmi = NULL;
  26. ::InitializeCriticalSection( &m_CriticalSection );
  27. LGLOG( LOG_LEVEL_MEDIUM, LOG_TYPE_INFO, this, "CVideoFrameGrabberWnd::CVideoFrameGrabberWnd" );
  28. }
  29. CVideoFrameGrabberWnd::~CVideoFrameGrabberWnd()
  30. {
  31. if( m_hFrameGrabberMemDC )
  32. ::DeleteDC( m_hFrameGrabberMemDC );
  33. if( m_hFrameGrabberMemBitmap )
  34. ::DeleteObject( m_hFrameGrabberMemBitmap );
  35. // if( m_pFrameGrabberBuffer )
  36. // delete m_pFrameGrabberBuffer;
  37. if( m_pFrameGrabberBmi )
  38. delete m_pFrameGrabberBmi;
  39. ::DeleteCriticalSection( &m_CriticalSection );
  40. LGLOG( LOG_LEVEL_MEDIUM, LOG_TYPE_INFO, this, "CVideoFrameGrabberWnd::~CVideoFrameGrabberWnd" );
  41. }
  42. BOOL CVideoFrameGrabberWnd::Create( HWND hParent, LPPOINT pPoint, LPSIZE pSize )
  43. {
  44. POINT p = {0,0};
  45. SIZE s = {1,1};
  46. if( !pPoint ) pPoint = &p;
  47. if( !pSize ) pSize = &s;
  48. WNDCLASS wc;
  49. wc.cbClsExtra = NULL;
  50. wc.cbWndExtra = NULL;
  51. wc.hbrBackground = (HBRUSH)COLOR_WINDOW;
  52. wc.hCursor = NULL;
  53. wc.hIcon = NULL;
  54. wc.hInstance = (HINSTANCE)::GetWindowLong( hParent, GWL_HINSTANCE );
  55. wc.lpfnWndProc = WindowProc_;
  56. wc.lpszClassName = "Video Frame Grabber Wnd";
  57. wc.lpszMenuName = NULL;
  58. wc.style = CS_HREDRAW | CS_HREDRAW;
  59. if( !::RegisterClass( &wc ) )
  60. return FALSE;
  61. m_hFrameGrabberWnd = ::CreateWindow( wc.lpszClassName, "", WS_CHILD, 0, 0, 1, 1, hParent, NULL, wc.hInstance, this );
  62. if( !m_hFrameGrabberWnd )
  63. return FALSE;
  64. HDC hDC = ::GetDC( m_hFrameGrabberWnd );
  65. if( !hDC )
  66. return FALSE;
  67. m_hFrameGrabberMemDC = ::CreateCompatibleDC( hDC );
  68. if( !m_hFrameGrabberMemDC )
  69. return FALSE;
  70. // m_hFrameGrabberMemBitmap = ::CreateCompatibleBitmap( hDC, IMAGE_WIDTH, IMAGE_HEIGHT );
  71. // if( !m_hFrameGrabberMemBitmap )
  72. // return FALSE;
  73. // m_pFrameGrabberBuffer = new BYTE[ dwImageSize ];
  74. // if( !m_pFrameGrabberBuffer )
  75. // return FALSE;
  76. #ifndef VIDEO_NTSC
  77. DWORD dwImageSize = IMAGE_WIDTH * IMAGE_HEIGHT * 2;
  78. m_pFrameGrabberBmi = (LPBITMAPINFO)new BYTE[ sizeof( BITMAPINFO ) ];
  79. if( !m_pFrameGrabberBmi )
  80. return FALSE;
  81. m_pFrameGrabberBmi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
  82. m_pFrameGrabberBmi->bmiHeader.biWidth = IMAGE_WIDTH;
  83. m_pFrameGrabberBmi->bmiHeader.biHeight = -IMAGE_HEIGHT;
  84. m_pFrameGrabberBmi->bmiHeader.biPlanes = 1;
  85. m_pFrameGrabberBmi->bmiHeader.biBitCount = 16;
  86. m_pFrameGrabberBmi->bmiHeader.biCompression = BI_RGB;
  87. m_pFrameGrabberBmi->bmiHeader.biSizeImage = dwImageSize;
  88. m_pFrameGrabberBmi->bmiHeader.biXPelsPerMeter = 0;
  89. m_pFrameGrabberBmi->bmiHeader.biYPelsPerMeter = 0;
  90. m_pFrameGrabberBmi->bmiHeader.biClrUsed = 0;
  91. m_pFrameGrabberBmi->bmiHeader.biClrImportant = 0;
  92. #else
  93. DWORD dwImageSize = IMAGE_WIDTH * IMAGE_HEIGHT;
  94. m_pFrameGrabberBmi = (LPBITMAPINFO)new BYTE[ sizeof( BITMAPINFO ) + sizeof(RGBQUAD) * (1<<8) ];
  95. if( !m_pFrameGrabberBmi )
  96. return FALSE;
  97. m_pFrameGrabberBmi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
  98. m_pFrameGrabberBmi->bmiHeader.biWidth = IMAGE_WIDTH;
  99. m_pFrameGrabberBmi->bmiHeader.biHeight = -IMAGE_HEIGHT;
  100. m_pFrameGrabberBmi->bmiHeader.biPlanes = 1;
  101. m_pFrameGrabberBmi->bmiHeader.biBitCount = 8;
  102. m_pFrameGrabberBmi->bmiHeader.biCompression = BI_RGB;
  103. m_pFrameGrabberBmi->bmiHeader.biSizeImage = dwImageSize;
  104. m_pFrameGrabberBmi->bmiHeader.biXPelsPerMeter = 0;
  105. m_pFrameGrabberBmi->bmiHeader.biYPelsPerMeter = 0;
  106. m_pFrameGrabberBmi->bmiHeader.biClrUsed = 0;
  107. m_pFrameGrabberBmi->bmiHeader.biClrImportant = 0;
  108. for( int i = 0; i < (1<<8); i++ )
  109. {
  110. m_pFrameGrabberBmi->bmiColors[i].rgbRed = i;
  111. m_pFrameGrabberBmi->bmiColors[i].rgbBlue = i;
  112. m_pFrameGrabberBmi->bmiColors[i].rgbGreen = i;
  113. m_pFrameGrabberBmi->bmiColors[i].rgbReserved = 0;
  114. }
  115. #endif
  116. m_hFrameGrabberMemBitmap = ::CreateDIBSection( 0, m_pFrameGrabberBmi, DIB_RGB_COLORS, (void**)&m_pFrameGrabberBuffer, NULL, 0 );
  117. if( !m_hFrameGrabberMemBitmap )
  118. return FALSE;
  119. ::ShowWindow( m_hFrameGrabberWnd, SW_SHOW );
  120. ::UpdateWindow( m_hFrameGrabberWnd );
  121. InitSnapshot();
  122. // ::SetTimer( m_hFrameGrabberWnd, 4, 500, NULL );
  123. return TRUE;
  124. }
  125. LRESULT CALLBACK CVideoFrameGrabberWnd::WindowProc_( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
  126. {
  127. LPCREATESTRUCT pCreateStruct;
  128. switch( uMsg )
  129. {
  130. case WM_CREATE:
  131. pCreateStruct = (LPCREATESTRUCT)lParam;
  132. ::SetWindowLong( hWnd, GWL_USERDATA, (LONG)pCreateStruct->lpCreateParams );
  133. break;
  134. }
  135. CVideoFrameGrabberWnd *pThis = (CVideoFrameGrabberWnd*)::GetWindowLong( hWnd, GWL_USERDATA );
  136. if( pThis )
  137. return pThis->WindowProc( hWnd, uMsg, wParam, lParam );
  138. return ::DefWindowProc( hWnd, uMsg, wParam, lParam );
  139. }
  140. LRESULT CVideoFrameGrabberWnd::WindowProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
  141. {
  142. switch( uMsg )
  143. {
  144. case WM_TIMER:
  145. return OnTimer( hWnd, wParam, lParam );
  146. case WM_PAINT:
  147. return OnPaint( hWnd, wParam, lParam );
  148. }
  149. return ::DefWindowProc( hWnd, uMsg, wParam, lParam );
  150. }
  151. void CVideoFrameGrabberWnd::UpdatePositions()
  152. {
  153. ::UpdateWindow( m_hFrameGrabberWnd );
  154. }
  155. HWND CVideoFrameGrabberWnd::GetSafeHwnd()
  156. {
  157. return m_hFrameGrabberWnd;
  158. }
  159. BOOL CVideoFrameGrabberWnd::ShowWindow( int nShow )
  160. {
  161. ::ShowWindow( m_hFrameGrabberWnd, nShow );
  162. return TRUE;
  163. }
  164. HBITMAP CVideoFrameGrabberWnd::StoreImage( HANDLE hFile, LPCTSTR pText, INT nBitCount )
  165. {
  166. volatile static BOOL fInside = FALSE;
  167. HBITMAP fSuccess = NULL;
  168. if(!fInside)
  169. {
  170. fInside = TRUE;
  171. if(hFile)
  172. {
  173. #ifndef VIDEO_NTSC
  174. LPBYTE lpDIBByte = GetSafeBytes();
  175. LPBYTE lpDst = GetSafeBytes();
  176. struct RGB16
  177. {
  178. unsigned short rgbRed : 5;
  179. unsigned short rgbGreen : 5;
  180. unsigned short rgbBlue : 5;
  181. };
  182. RGB16 *lpSrc = (RGB16*)m_pFrameGrabberBuffer;
  183. lpSrc += IMAGE_HEIGHT * IMAGE_WIDTH - 1;
  184. int lenDst = WIDTHBYTES(GetSafeBmi().bmiHeader.biWidth * 8);
  185. int lenSrc = IMAGE_WIDTH;
  186. for(int y = 0; y < GetSafeBmi().bmiHeader.biHeight; y++, lpDst += lenDst, lpSrc -= lenSrc)
  187. {
  188. LPBYTE pDst = lpDst;
  189. RGB16 *pSrc = lpSrc;
  190. for(int x = 0; x < lenDst && x < IMAGE_WIDTH; x++, pSrc -- )
  191. {
  192. *pDst++ = (BYTE)((pSrc->rgbBlue ) + (pSrc->rgbGreen << 2) + (pSrc->rgbRed << 1) + 33 );
  193. }
  194. }
  195. #else
  196. ZeroMemory( GetSafeBytes(), GetSafeBmi().bmiHeader.biSizeImage );
  197. LPBYTE lpDIBByte = GetSafeBytes();
  198. LPBYTE lpDst = GetSafeBytes();
  199. LPBYTE lpSrc = (LPBYTE)m_pFrameGrabberBuffer;
  200. lpSrc += IMAGE_HEIGHT * IMAGE_WIDTH - 1;
  201. int lenDst = WIDTHBYTES(GetSafeBmi().bmiHeader.biWidth * 8);
  202. int lenSrc = IMAGE_WIDTH;
  203. int xShift = (lenDst - lenSrc) >> 1;
  204. int yShift = (GetSafeBmi().bmiHeader.biHeight - IMAGE_HEIGHT) >> 1;
  205. lpDst += xShift + yShift * lenDst;
  206. int xMin = lenDst < IMAGE_WIDTH ? lenDst : IMAGE_WIDTH;
  207. int yMin = GetSafeBmi().bmiHeader.biHeight < IMAGE_HEIGHT ? GetSafeBmi().bmiHeader.biHeight : IMAGE_HEIGHT;
  208. for(int y = 0; y < yMin; y++, lpDst += lenDst, lpSrc -= lenSrc)
  209. {
  210. LPBYTE pDst = lpDst;
  211. LPBYTE pSrc = lpSrc;
  212. for(int x = 0; x < xMin; x++ )
  213. {
  214. *pDst++ = *pSrc++;
  215. }
  216. }
  217. #endif
  218. DrawText8( &GetSafeBmi(), GetSafeBytes(), pText );
  219. DWORD dwWritten;
  220. ::WriteFile(hFile, GetSafeBytes(), WIDTHBYTES(GetSafeBmi().bmiHeader.biWidth * 8) *
  221. GetSafeBmi().bmiHeader.biHeight, &dwWritten, NULL);
  222. LPBYTE pBytes = NULL;
  223. fSuccess = ::CreateDIBSection( NULL, &GetSafeBmi(), DIB_RGB_COLORS, (void**)&pBytes, NULL, 0 );
  224. if( pBytes )
  225. CopyMemory( pBytes, GetSafeBytes(), GetSafeBmi().bmiHeader.biSizeImage );
  226. } // end of if(m_IsInitiated && hFile)
  227. fInside = FALSE;
  228. } // end of if(!fInside)
  229. return fSuccess;
  230. }
  231. HBITMAP CVideoFrameGrabberWnd::GetSnapshot()
  232. {
  233. Snapshot();
  234. return (HBITMAP)::CopyImage( m_hFrameGrabberMemBitmap, IMAGE_BITMAP, 0, 0, LR_COPYRETURNORG | LR_CREATEDIBSECTION );
  235. }
  236. LRESULT CVideoFrameGrabberWnd::OnTimer( HWND hWnd, WPARAM wParam, LPARAM lParam )
  237. {
  238. if( ::IsWindowVisible( m_hFrameGrabberWnd ) )
  239. {
  240. Snapshot();
  241. HDC hDC = ::GetDC( hWnd );
  242. Draw( hDC );
  243. ::ReleaseDC( hWnd, hDC );
  244. }
  245. return TRUE;
  246. }
  247. void CVideoFrameGrabberWnd::UpdateFrame()
  248. {
  249. OnTimer( m_hFrameGrabberWnd, 0, 0 );
  250. }
  251. LRESULT CVideoFrameGrabberWnd::OnPaint( HWND hWnd, WPARAM wParam, LPARAM lParam )
  252. {
  253. PAINTSTRUCT ps;
  254. HDC hDC = ::BeginPaint( hWnd, &ps );
  255. Draw( hDC );
  256. ::EndPaint( hWnd, &ps );
  257. return TRUE;
  258. }
  259. void CVideoFrameGrabberWnd::Draw( HDC hDC )
  260. {
  261. HBITMAP hBitmap = (HBITMAP)::SelectObject( m_hFrameGrabberMemDC, m_hFrameGrabberMemBitmap );
  262. int nMode = ::SetStretchBltMode( hDC, COLORONCOLOR );
  263. RECT rect;
  264. ::GetClientRect( m_hFrameGrabberWnd, &rect );
  265. // ::StretchBlt( hDC, 0, 0, rect.right - rect.left, rect.bottom - rect.top,
  266. // m_hFrameGrabberMemDC, 0, IMAGE_HEIGHT-1, IMAGE_WIDTH, -IMAGE_HEIGHT, SRCCOPY );
  267. ::StretchBlt( hDC, 0, 0, rect.right - rect.left, rect.bottom - rect.top,
  268. m_hFrameGrabberMemDC, 0, 0, IMAGE_WIDTH, IMAGE_HEIGHT, SRCCOPY );
  269. // ::BitBlt( hDC, ((rect.right - rect.left) - IMAGE_WIDTH) >> 1,
  270. // ((rect.bottom - rect.top) - IMAGE_HEIGHT) >> 1, IMAGE_WIDTH, IMAGE_HEIGHT, m_hFrameGrabberMemDC, 0, 0, SRCCOPY );
  271. ::SetStretchBltMode( hDC, nMode );
  272. ::SelectObject( m_hFrameGrabberMemDC, hBitmap );
  273. }
  274. void CVideoFrameGrabberWnd::Snapshot()
  275. {
  276. ::EnterCriticalSection( &m_CriticalSection );
  277. const short nWidth = IMAGE_WIDTH;
  278. const short nHeight = IMAGE_HEIGHT;
  279. #ifndef VIDEO_NTSC
  280. const unsigned int nSize = IMAGE_WIDTH * IMAGE_HEIGHT;
  281. #else
  282. const unsigned int nSize = IMAGE_WIDTH * IMAGE_HEIGHT / 2;
  283. #endif
  284. unsigned int pBufferPtr = (unsigned int )m_pFrameGrabberBuffer;
  285. _asm
  286. {
  287. mov ax,9209h
  288. mov bx,54
  289. int 60h ;acquire
  290. cld
  291. mov edi,pBufferPtr
  292. mov ecx,nSize
  293. rep insw ;fill buffer with image data
  294. }
  295. ::LeaveCriticalSection( &m_CriticalSection );
  296. }
  297. void CVideoFrameGrabberWnd::InitSnapshot()
  298. {
  299. ::EnterCriticalSection( &m_CriticalSection );
  300. const short nWidth = IMAGE_WIDTH;
  301. const short nHeight = IMAGE_HEIGHT;
  302. #ifndef VIDEO_NTSC
  303. const unsigned int nSize = IMAGE_WIDTH * IMAGE_HEIGHT;
  304. #else
  305. const unsigned int nSize = IMAGE_WIDTH * IMAGE_HEIGHT / 2;
  306. #endif
  307. unsigned int pBufferPtr = (unsigned int )m_pFrameGrabberBuffer;
  308. _asm
  309. {
  310. mov ax,9209h
  311. mov bx,0
  312. int 60h ;init driver
  313. mov ax,9209h
  314. mov bx,41 ;default baseaddr abfragen
  315. int 60h
  316. mov ax,9209h
  317. mov bx,9
  318. // mov cx,300h ;oder basisadresse direkt setzen
  319. int 60h
  320. #ifdef VIDEO_NTSC
  321. mov ax,9209h
  322. mov bx,30 ;get XRAM
  323. mov cx,15 ;127/8
  324. int 60h
  325. mov ax,9209h
  326. mov bx,7 ;set XRAM
  327. mov cx,15 ;127/8
  328. and dl,3Fh ; clear 126,127 bits
  329. // or dl,80h ; set 10 in 126,127 bits
  330. int 60h
  331. #endif
  332. mov ax,9209h
  333. mov bx,8 ;init fg32
  334. int 60h
  335. #ifdef VIDEO_NTSC
  336. mov ax, 9209h
  337. mov bx,47
  338. int 60h
  339. cld
  340. mov eax, 310h
  341. mov edi,pBufferPtr
  342. mov ecx,2
  343. rep insw ;fill buffer with image data
  344. #endif
  345. mov ax,9209h
  346. mov bx,50
  347. mov cx,nWidth
  348. mov dx,nHeight
  349. int 60h ;384x288
  350. mov ax,9209h
  351. mov bx,51
  352. mov cx,0
  353. mov dx,0
  354. int 60h ;@0,0
  355. mov ax,9209h
  356. mov bx,52
  357. #ifndef VIDEO_NTSC
  358. mov cx,72bbh
  359. #else
  360. mov cx,72b2h
  361. #endif
  362. mov dx,2
  363. int 60h ;5+5+5 bit / pixel scan for: ungerades bild aus 2 halbbildern
  364. mov ax,9209h
  365. mov bx,53
  366. int 60h ;acquire
  367. cld
  368. mov edi,pBufferPtr
  369. mov ecx,nSize
  370. rep insw ;fill buffer with image data
  371. }
  372. ::LeaveCriticalSection( &m_CriticalSection );
  373. }
  374. */