📄 cpecontrol.cpp
字号:
// CPEControl.cpp: implementation of the CCPEControl class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "CPEControl.h"
#include "cpew32.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//MultiThread Function
UINT VGAImageShowThreadProc(LPVOID pParam)
{
POINT winOrgPoint;
ThreadRecord* pRecord;
pRecord=(ThreadRecord*)pParam;
winOrgPoint=pRecord->winOrgPoint;
if (int i= W32_CPE_RealGrab_VGA((WORD)winOrgPoint.x,(WORD) winOrgPoint.y) < 0 )
{
ShowCursor(TRUE);
pRecord->bRealGrab = FALSE;
// AfxMessageBox("实时数据采集失败!");//Real Time Grab VGA error!");
return -1;
}
else{
pRecord->bRealGrab = TRUE;
}
return 0;
}
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CCPEControl::CCPEControl()
{
m_pThreadRecord=NULL;
m_GrabWndHeight=300;
m_GrabWndWidth=400;
GrabParamInit();
lpDib=NULL;
lpMark=NULL;
hwnd=NULL;
}
CCPEControl::~CCPEControl()
{
if(m_pThreadRecord!=NULL){
delete m_pThreadRecord;
m_pThreadRecord=NULL;
}
if (lpDib != NULL){
GlobalUnlock( hglb );
GlobalFree( hglb );
lpDib=NULL;
}
if (lpMark != NULL){
free(lpMark);
lpMark=NULL;
}
}
void CCPEControl::GrabParamInit()
{
wCurrent_pixel_Format = RGB24_PACKED;
wScreenWidth = 640; // Width of screen
wScreenHeight = 480; // Height of screen
wScaleWidth = 640;
wScaleHeight = 480;
wCurrent_Frame= 1; // Current frame
bNonGrab = TRUE;
wImgWidth = 512; // Width of grab image
wImgHeight = 512; // Height of grab image
wVGAWidth = 640;
wVGAHeight = 480;
wCurrent_source_Type= SOURCE_NTSC;
wGrabWinX1 = 0; // Coordinates of grab window
wGrabWinY1 = 0;
wGrabWinX2 = 639;
wGrabWinY2 = 479;
wScale = IDM_100_SCALE;
firstTime = TRUE;
bZoomMark = FALSE;
bRealGrab = FALSE;
}
void CCPEControl::LoadProfile(int* pParam)
{
/* char szIniFile[0x200];
GetWindowsDirectory((LPSTR)szIniFile, sizeof(szIniFile));
strcat( szIniFile, "\\ca-c1000.ini");*/
CString strIniFile;
//char szbuf[10];
strIniFile=AfxGetApp()->m_pszHelpFilePath;
strIniFile=strIniFile.Left(strIniFile.ReverseFind('\\')+1);
strIniFile+="ca-c100.ini";
pParam[0] =(int)(GetPrivateProfileInt( "Parameters", "Contrast", 110, strIniFile ));
if (pParam[0] == 0) pParam[0] = 120;
pParam[1] = GetPrivateProfileInt( "Parameters", "Brightness", 128, strIniFile );
if (pParam[1] == 0) pParam[1] = 110;
pParam[2] = GetPrivateProfileInt( "Parameters", "Saturate", 60, strIniFile );
if (pParam[2] == 0) pParam[2] = 60;
pParam[3] = GetPrivateProfileInt( "Parameters", "Hue", 0, strIniFile );
pParam[4] = GetPrivateProfileInt( "StartPoint", "X_Start", 0, strIniFile );
TRACE("pParam[4]=%d\n",pParam[4] );
pParam[5] = GetPrivateProfileInt( "StartPoint", "Y_Start", 0, strIniFile );
TRACE("pParam[5]=%d\n",pParam[5] );
wCurrent_pixel_Format = GetPrivateProfileInt( "ImageFormat", "format", YUV9_PLANAR, strIniFile );
for(int j=0;j<6;j++)
m_MCpeParam[j]=pParam[j];
return ;
}
int CCPEControl::InitCPE(HWND pWnd)
{
/* CA-CPE-1000 Initialization include:
Load Parametre from Prifile ini file;
Initialize CA-CPE-1000;
Get IRQ Num;
Set Grab Window-configeration;
Set Image-Input Source;
Set Grab StartPoint
Initialize parametres from ini File;
return Value:
0----succeed;
<0----failed!
*/
hwnd=pWnd;
CRect Rect;
GetClientRect(pWnd,Rect);
m_GrabWndHeight=Rect.Height();//nHeight;
m_GrabWndWidth=Rect.Width();//nWidth;
wVGAWidth=Rect.Width();//nWidth;
wVGAHeight=Rect.Height();//nHeight;
if(wVGAWidth>500){
m_GrabWndWidth=500;//nWidth;
wVGAWidth=500;//nWidth;
}
if(wVGAHeight>400){
m_GrabWndHeight=400;//nHeight;
wVGAHeight=400;//nHeight;
}
//Initialize parametres from ini File;
LoadProfile(cpeParam);
// Initialize CA-CPE-1000
wMemSize =W32_Init_CPE1000();
if (wMemSize < -16) {
wMemSize=0x10000l+wMemSize;
if (wMemSize>0xf000l)
wMemSize=(wMemSize-0xf000l)<<8; //wMemSize: long int
}
if (wMemSize < 0) {
AfxMessageBox("不能初始化图象采集卡参数设置!");//CA-CPE1000 Initializtion Error!");
return -1;
}
wIRQ = (WORD)W32_Get_CPE1000_IRQnum();
wTotal_Frames = W32_Set_Grab_Window(0, 0, 639,473 );
W32_Set_Input_Source(0);
wScreenWidth = GetSystemMetrics(SM_CXSCREEN);
wScreenHeight = GetSystemMetrics(SM_CYSCREEN);
videoY = GetSystemMetrics(SM_CYMENU)+GetSystemMetrics(SM_CYCAPTION)+2;
videoX = 0;
wCurrent_Frame = 1;
bNonGrab = TRUE;
W32_Set_StartPoint( 0x12 , 0x28 );
//SizeSet!
// HWND wnd=AfxGetMainWnd()->m_hWnd;
HWND wnd=pWnd;
int i=SizeSet(wnd,wVGAWidth,wVGAHeight);
if(i!=0)
{
AfxMessageBox("采集卡参数设置失败");//CPE SizeSet Failed!");
return -1 ;
}
else
// AfxMessageBox("CPE SizeSet Succeed!");
return 0 ;
}
void CCPEControl::RTDisplay()
{
wVGAWidth=m_GrabWndWidth;
wVGAHeight=m_GrabWndHeight;
TRACE("bRealGraab=%d\n",bRealGrab);
if(bRealGrab==TRUE) return;
LoadProfile(cpeParam);
int i=SizeSet(hwnd,wVGAWidth,wVGAHeight);
if(i!=0){
AfxMessageBox("采集卡参数设置失败");//CPE SizeSet Failed!");
return;
}
SetGrabPrepare();
m_pThreadRecord=new ThreadRecord;
m_pThreadRecord->winOrgPoint.x=winOrgPoint.x;
m_pThreadRecord->winOrgPoint.y=winOrgPoint.y;
AfxBeginThread(VGAImageShowThreadProc,(LPVOID)m_pThreadRecord,
THREAD_PRIORITY_NORMAL);
}
int CCPEControl::SizeSet(HWND hWnd,int cxClient,int cyClient)
{
// hwnd=AfxGetMainWnd()->m_hWnd;
hdc = GetDC( hWnd );
GetTextMetrics( hdc, &tm );
cyChar = (WORD)tm.tmHeight;
cxChar = (WORD)tm.tmMaxCharWidth;
ReleaseDC( hWnd, hdc );
wImgWidth = (WORD)(cxClient & 0xfffc);
wImgHeight = (WORD)(cyClient & 0xfffc);
if ( wImgWidth > 764 ) wImgWidth = 764;
if ( wCurrent_source_Type == SOURCE_NTSC )
wImgWidth = min( wImgWidth, 532 ), wImgHeight = min( wImgHeight, 480 );
wGrabWinX1 = cpeParam[4];
wGrabWinY1 = cpeParam[5];
wGrabWinX2 = (WORD)(wImgWidth - 1 + cpeParam[4]);
wGrabWinY2 = (WORD)(wImgHeight - 1 + cpeParam[5]);
if ( ( (wImgWidth + 0x12) < 900 ) & ( wImgHeight < 576 ) )
wTotal_Frames = W32_Set_Grab_Window(wGrabWinX1, wGrabWinY1, wGrabWinX2, wGrabWinY2);
wImgWidth = (WORD)(wGrabWinX2 - wGrabWinX1 + 1);
wImgHeight = (WORD)(wGrabWinY2 - wGrabWinY1 + 1);
if ( bZoomMark == TRUE ){
wTemp = (wScale - IDM_10_SCALE)*10 + 10;
wTotal_Frames =(WORD)W32_Set_Zoom_Value( (SHORT)wTemp, (SHORT)wTemp, &wScaleWidth, &wScaleHeight );
}
if (wCurrent_Frame > wTotal_Frames) wCurrent_Frame = 1;
if (lpMark != NULL) free (lpMark) ;
lpMark = (BYTE *) malloc (wTotal_Frames + 2) ;
if (lpMark == NULL) return 0;
for (int i=1;i<=wTotal_Frames;i++)
lpMark[i]=0;
lpMark[wCurrent_Frame]=1;
if (lpDib != NULL)
{
GlobalUnlock( hglb );
GlobalFree( hglb );
}
if ( bZoomMark == TRUE )
{
wTempWidth = cxDib = wScaleWidth;
wTempHeight = cyDib = wScaleHeight;
}
else {
wTempWidth = cxDib = wImgWidth;
wTempHeight = cyDib = wImgHeight;
}
if ( wCurrent_pixel_Format == RGB24_PACKED )
dwDibSize = (DWORD)wTempHeight * 3L;
else dwDibSize = (DWORD)wTempHeight;
dwDibSize = dwDibSize * (DWORD)wTempWidth;
hglb = GlobalAlloc( GMEM_MOVEABLE, dwDibSize );
if ( hglb == NULL )
{
AfxMessageBox( "内存分配失败!");//Alloc lpDib error!!!");
return 0;
}
lpDib = (BYTE *)GlobalLock( hglb );
bNonGrab = FALSE;
int j;
if ( wCurrent_pixel_Format == RGB24_PACKED )
j = 24;
else j = 8;
if ( firstTime )
{
// InitLogicPal( hdc, (short)cxDib, (short)cyDib, (WORD)j );
// InitMenu(hwnd);
W32_Modify_Contrast((BYTE)cpeParam[0]);
W32_Modify_Brightness((BYTE)cpeParam[1]);
W32_Modify_Saturate((BYTE)cpeParam[2]);
W32_Modify_Hue((BYTE)cpeParam[3]);
// PrepareDataFile(hwnd);
firstTime = FALSE;
}
if ( bZoomMark == TRUE )
{
i=0;
}
else {
i = W32_CPE_Grab_Xms(wCurrent_Frame, 1);
}
if ( i!=0 )
{
// AfxMessageBox( "实时采集数据失败!");//There are some errors when GRAB!!!");
return 0;
}
lpMark[wCurrent_Frame]=1;
if (lpDib != NULL) {
if ( wCurrent_pixel_Format == RGB24_PACKED )
{
if ( W32_ReadXMS2Buf( wCurrent_Frame, lpDib) < 0 )
{
AfxMessageBox("采集图象数据读取失败!");//Sorry, ReadXMS2Buf error!!!");
return 0;
}
// sprintf(szFileName,"TMP.XMS"); // Suppose .XMS while displaying image in XMS.
}
else {
if ( W32_ReadXMS2Buf_MONO( wCurrent_Frame, lpDib) < 0 )
{
AfxMessageBox("采集图象数据读取失败!");//Sorry, ReadXMS2Buf error!!!");
return 0;
}
}
//InvalidateRect (hwnd, NULL, TRUE) ;
}
return 0 ;
}
void CCPEControl::SetGrabPrepare()
{
if ( wCurrent_pixel_Format != RGB24_PACKED ) return ;
//hwnd=AfxGetMainWnd()->m_hWnd;
if(hwnd==NULL) return;
hdc = GetDC( hwnd );
// retrieves the x- and y-coordinates of the origin of the window
bReturn = GetWindowOrgEx( hdc, &winOrgPoint );
winOrgPoint.y=winOrgPoint.y;
if ( bReturn == 0x00 )
{
AfxMessageBox("不能获取窗口起点!");//Get Windiws origin error!");
return ;
}
ClientToScreen( hwnd, &winOrgPoint );
ReleaseDC( hwnd, hdc );
if ( winOrgPoint.x < 0 ){
wVGAWidth = (WORD)(( wImgWidth + winOrgPoint.x )& 0xfffc) ;
winOrgPoint.x = 0;
}
else wVGAWidth = wImgWidth;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -