//---------------------------------------------------------------------------- // Laser Guard Ltd. // Copyright © 2000-2002. All Rights Reserved. // Version 1.0 01/01/2000 // Last modification: 08/10/2003 // PROJECT: // SUBSYSTEM: // FILE: VideoWnd.cpp // AUTHOR: Sergey Chizhevskiy // OVERVIEW // ~~~~~~~~ // Source file of VideoWnd.cpp //---------------------------------------------------------------------------- #include "stdafx.h" #include "VideoWnd.h" #include "AuxLib.h" #include "..\..\LgLib\Version.h" //#include "..\Chicago\VideoBase.h" #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif #ifndef WIDTHBYTES #define WIDTHBYTES(bits) (((bits + 31) >> 5) << 2) #endif CVideoForWindowsWnd::CVideoForWindowsWnd() { m_Capture.hBitmap = NULL; m_Capture.pBmi = NULL; m_Capture.pBuffer = NULL; LGLOG( LOG_LEVEL_MEDIUM, LOG_TYPE_INFO, this, _T("CVideoForWindowsWnd::CVideoForWindowsWnd") ); } CVideoForWindowsWnd::~CVideoForWindowsWnd() { CaptureDone(); LGLOG( LOG_LEVEL_MEDIUM, LOG_TYPE_INFO, this, _T("CVideoForWindowsWnd::~CVideoForWindowsWnd") ); } BEGIN_MESSAGE_MAP(CVideoForWindowsWnd, CWnd) //{{AFX_MSG_MAP(CVideoForWindowsWnd) //}}AFX_MSG_MAP END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// // CVideoForWindowsWnd message handlers BOOL CVideoForWindowsWnd::DestroyWindow() { if( GetSafeHwnd() ) { CaptureDone(); HWND hWnd = GetSafeHwnd(); UnsubclassWindow(); capOverlay( hWnd, FALSE ); capDriverDisconnect( hWnd ); ::DestroyWindow( hWnd ); } return CWnd::DestroyWindow(); } BOOL CVideoForWindowsWnd::DlgVideoSource() { HWND hWnd = GetSafeHwnd(); if( hWnd ) { BOOL bResult = capDlgVideoSource( hWnd ); CaptureInit( hWnd ); return bResult; } return FALSE; } BOOL CVideoForWindowsWnd::DlgVideoFormat() { HWND hWnd = GetSafeHwnd(); if( hWnd ) { BOOL bResult = capDlgVideoFormat( hWnd ); CaptureInit( hWnd ); return bResult; } return FALSE; } BOOL CVideoForWindowsWnd::DlgVideoDisplay() { HWND hWnd = GetSafeHwnd(); if( hWnd ) { BOOL bResult = capDlgVideoDisplay( hWnd ); CaptureInit( hWnd ); return bResult; } return FALSE; } BOOL CVideoForWindowsWnd::FileSaveDIB( LPSTR pFileName ) { HWND hWnd = GetSafeHwnd(); if( hWnd ) { return capFileSaveDIB( hWnd, pFileName ); } return FALSE; } BOOL CVideoForWindowsWnd::GrabFrameNoStop() { HWND hWnd = GetSafeHwnd(); if( hWnd ) { return capGrabFrameNoStop( hWnd ); } return FALSE; } LRESULT CVideoForWindowsWnd::ErrorCallbackProc( HWND hWnd, int nErrID, LPSTR lpErrorText ) { return ((CVideoForWindowsWnd*)capGetUserData( hWnd ))->OnErrorCallback( hWnd, nErrID, lpErrorText ); } LRESULT CVideoForWindowsWnd::FrameCallbackProc( HWND hWnd, LPVIDEOHDR lpVHdr ) { return ((CVideoForWindowsWnd*)capGetUserData( hWnd ))->OnFrameCallback( hWnd, lpVHdr ); } BOOL CVideoForWindowsWnd::CaptureInit( HWND hWnd ) { CaptureDone(); DWORD dwVFSize = capGetVideoFormatSize( hWnd ); m_Capture.pBmi = (BITMAPINFO *) new BYTE[ dwVFSize ]; ::ZeroMemory( m_Capture.pBmi, dwVFSize ); capGetVideoFormat( hWnd, m_Capture.pBmi, dwVFSize ); // if( !m_Capture.pBmi->bmiHeader.biCompression ) // m_Capture.pBmi->bmiHeader.biSizeImage = WIDTHBYTES( m_Capture.pBmi->bmiHeader.biBitCount * m_Capture.pBmi->bmiHeader.biWidth ) * m_Capture.pBmi->bmiHeader.biHeight; m_Capture.hBitmap = ::CreateDIBSection( 0, m_Capture.pBmi, DIB_RGB_COLORS, (void**)&m_Capture.pBuffer, NULL, NULL ); return TRUE; } BOOL CVideoForWindowsWnd::CaptureDone() { if( m_Capture.hBitmap ) { ::DeleteObject( m_Capture.hBitmap ); m_Capture.hBitmap = NULL; } if( m_Capture.pBmi ) { delete [] (LPBYTE)m_Capture.pBmi; m_Capture.pBmi = NULL; } m_Capture.pBuffer = NULL; return TRUE; } HBITMAP CVideoForWindowsWnd::Capture( LPBITMAPINFO *ppBmi, LPBYTE *ppBytes, DWORD dwPreviewTime ) { if( GrabFrameNoStop() ) { if( ppBmi ) *ppBmi = m_Capture.pBmi; if( ppBytes ) *ppBytes = m_Capture.pBuffer; return (HBITMAP)::CopyImage( m_Capture.hBitmap, IMAGE_BITMAP, 0, 0, 0 ); } return NULL; } LRESULT CVideoForWindowsWnd::OnFrameCallback( HWND hWnd, LPVIDEOHDR lpVHdr ) { if( m_Capture.pBuffer ) { ::CopyMemory( m_Capture.pBuffer, lpVHdr->lpData, m_Capture.pBmi->bmiHeader.biSizeImage ); } return 0; } LRESULT CVideoForWindowsWnd::OnErrorCallback( HWND hWnd, int nErrID, LPSTR lpErrorText ) { return 0; } BOOL CVideoForWindowsWnd::Create(CWnd *pParentWnd, int x, int y, int cx, int cy, int ccx, int ccy) { HWND hWnd = NULL; for ( INT iIndex = 0; iIndex <10; iIndex++ ) { TCHAR pDriverName[ MAX_PATH ]; TCHAR pDriverVer[ MAX_PATH ]; if( capGetDriverDescription( iIndex, pDriverName, MAX_PATH, pDriverVer, MAX_PATH ) ) { hWnd = capCreateCaptureWindow( NULL, WS_CHILD | WS_VISIBLE, x, y, cx, cy, pParentWnd->GetSafeHwnd(), 1 ); if( hWnd ) { if( capDriverConnect( hWnd, iIndex ) ) { /* INT nSize = capGetVideoFormatSize( hWnd ); LPBITMAPINFO pBitmapInfo = (LPBITMAPINFO)malloc(nSize); if( !pBitmapInfo ) return FALSE; if( capGetVideoFormat( hWnd, pBitmapInfo, nSize ) ) { pBitmapInfo->bmiHeader.biBitCount = 24; pBitmapInfo->bmiHeader.biCompression = 0; pBitmapInfo->bmiHeader.biSizeImage = WIDTHBYTES( pBitmapInfo->bmiHeader.biWidth * pBitmapInfo->bmiHeader.biBitCount ) * pBitmapInfo->bmiHeader.biHeight; capSetVideoFormat( hWnd, pBitmapInfo, nSize ); } else */ { BITMAPINFO bmi; memset( &bmi, 0, sizeof(BITMAPINFOHEADER) ); bmi.bmiHeader.biSize = sizeof( BITMAPINFOHEADER ); bmi.bmiHeader.biWidth = ccx; bmi.bmiHeader.biHeight = ccy; bmi.bmiHeader.biPlanes = 1; bmi.bmiHeader.biBitCount = 24; bmi.bmiHeader.biCompression = 0; bmi.bmiHeader.biSizeImage = WIDTHBYTES( bmi.bmiHeader.biWidth * bmi.bmiHeader.biBitCount ) * bmi.bmiHeader.biHeight; capSetVideoFormat( hWnd, &bmi, sizeof(BITMAPINFOHEADER) ); } // free( pBitmapInfo ); if( capOverlay( hWnd, TRUE ) ) { CaptureInit( hWnd ); if( pParentWnd->IsWindowVisible() ) ::ShowWindow( hWnd, SW_SHOW ); else ::ShowWindow( hWnd, SW_HIDE ); ::UpdateWindow( hWnd ); ::InvalidateRect( hWnd, NULL, FALSE ); capSetUserData( hWnd, (LONG)this ); capSetCallbackOnFrame( hWnd, FrameCallbackProc ); capSetCallbackOnError( hWnd, ErrorCallbackProc ); // return SubclassWindow( hWnd ); LGLOG( LOG_LEVEL_MEDIUM, LOG_TYPE_INFO, this, _T("CVideoForWindowsWnd::Create"), _T("Succeeded") ); return TRUE; } } } } } LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CVideoForWindowsWnd::Create"), _T("Failed") ); return FALSE; } /////////////////////////////////////////////////////////////////////////////////// CVideoFrameDIB::CVideoFrameDIB() { // reset members m_pBitmapInfo = NULL; m_pBytes = NULL; m_nBitmapInfoSize = 0; LGLOG( LOG_LEVEL_HIGH, LOG_TYPE_INFO, this, _T("CVideoFrameDIB::CVideoFrameDIB") ); } CVideoFrameDIB::~CVideoFrameDIB() { // destroy members Destroy(); LGLOG( LOG_LEVEL_HIGH, LOG_TYPE_INFO, this, _T("CVideoFrameDIB::~CVideoFrameDIB") ); } BOOL CVideoFrameDIB::Create( LPBITMAPINFO pBitmapInfo, PBYTE pBytes ) { // protect data from simultaniosly access CLgLocker Locker( &m_CriticalSection ); // check entered values if( !pBitmapInfo || !pBytes ) { LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CVideoFrameDIB::Create"), _T("Parameters are incorrect (%p,%p)"), pBitmapInfo, pBytes ); return FALSE; } // calculate bitmap info size INT nBitmapInfoSize = sizeof(BITMAPINFOHEADER) + ( pBitmapInfo->bmiHeader.biBitCount > 8 ? 0 : (1 << pBitmapInfo->bmiHeader.biBitCount) ); if( nBitmapInfoSize != m_nBitmapInfoSize ) { // calculate pallette size m_pBitmapInfo = (LPBITMAPINFO)realloc( m_pBitmapInfo, nBitmapInfoSize ); if( !m_pBitmapInfo ) return FALSE; // remember size m_nBitmapInfoSize = nBitmapInfoSize; // create array for holding pixel data m_pBytes = (PBYTE)realloc( m_pBytes, sizeof(BYTE) * pBitmapInfo->bmiHeader.biSizeImage ); if( !m_pBytes ) return FALSE; } else // allocate memory for pixels if nessessary if( m_pBitmapInfo->bmiHeader.biSizeImage < pBitmapInfo->bmiHeader.biSizeImage ) { m_pBytes = (PBYTE)realloc( m_pBytes, sizeof(BYTE) * pBitmapInfo->bmiHeader.biSizeImage ); if( !m_pBytes ) return FALSE; } // copy dib bits memcpy( m_pBytes, pBytes, pBitmapInfo->bmiHeader.biSizeImage ); // remember new attributes memcpy( m_pBitmapInfo, pBitmapInfo, m_nBitmapInfoSize ); return TRUE; } void CVideoFrameDIB::Destroy() { // protect data from simultaniosly access CLgLocker Locker( &m_CriticalSection ); // clear memory if( m_pBytes ) { free( m_pBytes ); m_pBytes = NULL; } // clear attributes if( m_pBitmapInfo ) { free( m_pBitmapInfo ); m_pBitmapInfo = NULL; } } BOOL CVideoFrameDIB::Draw( HDC hDC, PRECT pRect ) { LGLOG( LOG_LEVEL_REALTIME, LOG_TYPE_INFO, this, _T("CVideoFrameDIB::Draw"), _T("Started...") ); // protect data from simultaniosly access CLgLocker Locker( &m_CriticalSection ); if( !m_pBytes ) { LGLOG( LOG_LEVEL_REALTIME, LOG_TYPE_ERROR, this, _T("CVideoFrameDIB::Draw"), _T("m_pBytes is NULL") ); return FALSE; } INT nMode = ::SetStretchBltMode( hDC, COLORONCOLOR ); INT nWidth = pRect->right - pRect->left; INT nHeight = pRect->bottom - pRect->top; ::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 ); ::SetStretchBltMode( hDC, nMode ); LGLOG( LOG_LEVEL_REALTIME, LOG_TYPE_INFO, this, _T("CVideoFrameDIB::Draw"), _T("Done succeessfully") ); return TRUE; } HBITMAP CVideoFrameDIB::CreateBitmap() { // protect data from simultaniosly access CLgLocker Locker( &m_CriticalSection ); if( !m_pBytes ) return FALSE; // get dc of entire screen HDC hDC = ::GetDC( NULL ); if( !hDC ) return NULL; // create dib bitmap HBITMAP hBitmap = ::CreateDIBitmap( hDC, &m_pBitmapInfo->bmiHeader, CBM_INIT, m_pBytes, m_pBitmapInfo, DIB_RGB_COLORS ); // relase dc ::ReleaseDC( NULL, hDC ); return hBitmap; } HBITMAP CVideoFrameDIB::CreateBitmap( LPBITMAPINFO *ppBitmapInfo ) { // protect data from simultaniosly access CLgLocker Locker( &m_CriticalSection ); if( !m_pBytes ) return FALSE; // get dc of entire screen HDC hDC = ::GetDC( NULL ); if( !hDC ) return NULL; // create dib bitmap HBITMAP hBitmap = ::CreateDIBitmap( hDC, &m_pBitmapInfo->bmiHeader, CBM_INIT, m_pBytes, m_pBitmapInfo, DIB_RGB_COLORS ); // relase dc ::ReleaseDC( NULL, hDC ); // return format if( ppBitmapInfo ) { *ppBitmapInfo = m_pBitmapInfo; } return hBitmap; }