?? okfunc.c
字號:
}
InvalidateRect(hwnd,NULL,FALSE);
UpdateWindow(hwnd);
//Sleep(100);
}
break;
case USERMEMORY:
//內存
for(no=CaptureStart;no<=CaptureEnd;no++){
blkTg.lpBits=lpMemory+(GetFrameSize(hBoard) * (no-1));
if( (LOWORD(bufform)==FORM_GRAY10) ||(LOWORD(bufform)==FORM_GRAY12) ) { //special
//way 1
okConvertRect(hBoard,(TARGET)&blk,0,(TARGET)&blkTg,(short)no,1);
//way 2
//okReadRectEx(hBoard,BUFFER,(short)no,lpdib, MAKELONG(FORM_GRAY8,1));
//read only red data
}
else {
//way 1
okTransferRect(hBoard,(TARGET)&blk,0,(TARGET)&blkTg,(short)no,1);
//way 2
//okReadRect(hBoard,BUFFER,(short)no,lpdib);
}
InvalidateRect(hwnd,NULL,FALSE);
UpdateWindow(hwnd);
Sleep(100);
}
break;
case USERFILE:
//文件
break;
}
}
//to open one board as current board to operate
HANDLE OpenCard(HWND hWnd)
{
//RECT rcVidRect;
//打開采集卡,返回采集卡句柄
// short typecode;
char string[100];
MEMORYSTATUS Status;
//check if pre_allocated buffer
okGetAvailBuffer(NULL,&dwBufSize); //檢測已分配內存
//okGetBufferSize(hBoard,NULL,&dwBufSize);//
if(dwBufSize==0)
MessageBox(hwnd, "There are no buffer pre-allocated !","Warning",MB_OK);
//just starting, then allocated memory for dib
if(!lpbi) {
//下面為應用程序分配內存
GlobalMemoryStatus(&Status);
//determaine how many memory will be allocated as user memory
dwMaxMemSize=Status.dwTotalPhys-dwBufSize-32*(1<<20); //I'd keep 32M for window system
if(dwMaxMemSize<=0)
dwMaxMemSize=5*(1<<20); //5M
else if( dwMaxMemSize>Status.dwAvailPhys )
dwMaxMemSize=Status.dwAvailPhys/2;
if( dwMaxMemSize<5*(1<<20) ) //need 5M at least for user memory
dwMaxMemSize=max(5*(1<<20),Status.dwAvailPhys);
lpbi=(LPBITMAPINFOHEADER)GlobalAlloc(GPTR,dwMaxMemSize);
if(lpbi==NULL) {
dwMaxMemSize=4*(1<<20); //4M
lpbi=GlobalAlloc(GPTR,dwMaxMemSize);
}
lpdib=(LPSTR)lpbi+5120; //keep 5k for bi
lpMemory=(LPSTR)lpbi+2048*1024; //keep 2M for dib
dwMaxMemSize-=2048*1024+5120;
if(lpbi) {
sprintf(string,"Allocated memory = %d",dwMaxMemSize);
}else
sprintf(string,"Allocate memory failed !");
// MessageBox(hwnd,string,"Message",MB_OK);
}
//open one specified board
if( !(hBoard=okOpenBoard(&iCurrUsedNo)) ) {//set
//if( !(hBoard=okOpenImageBoard(-1)) ) //select default
long error;
error=okGetLastError();
if(error==ERR_NOTFOUNDBOARD)
MessageBox(hwnd, "Not found Ok series Image Board !","Warning",MB_OK);
else if (error==ERR_NOTFOUNDVXDDRV)
MessageBox(hwnd, "Not found VxD driver for Ok series Image Board !","Warning",MB_OK);
else
MessageBox(hwnd, "Open selected board failed!","Warning",MB_OK);
return FALSE;
}
GetBitmapHeader(hBoard, SCREEN, lpbi);
//set initial size
sampwidth=(short)lpbi->biWidth;
sampheight=(short)lpbi->biHeight;
//initailize screen
okSetToWndRect(hBoard,hWnd);
EnableItem(hWnd, IDM_FREEZE, FALSE);
EnableItem(hWnd, IDM_BDISPLAY, FALSE);
InvalidateRect(hWnd,NULL,FALSE);
return hBoard;
}
BOOL GetBitmapHeader(HANDLE hBoard, TARGET src, LPBITMAPINFOHEADER lpbi)
{
short width,height;
long form;
form=GetTargetSize(hBoard, src, &width, &height);
//here take form as screen forever
form=okSetCaptureParam(hBoard,CAPTURE_SCRRGBFORMAT,GETCURRPARAM); //-1
SetBitmapHeader(lpbi, width, height, HIWORD(form), LOWORD(form) );
return LOWORD(form);
}
//set bitmap header and bitmap info if need
long SetBitmapHeader(LPBITMAPINFOHEADER lpbi,short width,short height, short bits, short form)
{
long wbytes;
if(!lpbi)
return FALSE;
lpbi->biWidth=width;
lpbi->biHeight=height;
lpbi->biSize=sizeof(BITMAPINFOHEADER);
lpbi->biPlanes=1;
lpbi->biBitCount=bits;
wbytes=((((lpbi->biWidth*bits)+31)&~31)>>3);//bmp need 4 align
lpbi->biSizeImage=wbytes*lpbi->biHeight;
lpbi->biClrUsed=0;
//special format for 555,565 & 32
if(form==FORM_RGB555 || form==FORM_RGB565 || form==FORM_RGB8888 )
lpbi->biCompression=BI_BITFIELDS;
else
lpbi->biCompression=0;
if(lpbi->biCompression==BI_BITFIELDS) {//
DWORD *lpmask;
lpmask=(DWORD *)((LPSTR)lpbi+lpbi->biSize);
if(form==FORM_RGB555) {
lpmask[2]=0x001f; //blue
lpmask[1]=0x03e0;
lpmask[0]=0x7c00;
}
else if(form==FORM_RGB565) {
lpmask[2]=0x001f; //blue
lpmask[1]=0x07e0;
lpmask[0]=0xf800;
}
else if(bits==32) {
lpmask[2]=0x0000ff;
lpmask[1]=0x00ff00;
lpmask[0]=0xff0000;
}
}
else if(bits<=12) { // 8,10,12
int i;
RGBQUAD *lpRGB=(RGBQUAD *)((LPSTR)lpbi+lpbi->biSize);
lpbi->biClrUsed=(1<<bits);
for(i=0; i<(short)lpbi->biClrUsed;i++) {
lpRGB[i].rgbBlue=i;
lpRGB[i].rgbGreen=i;
lpRGB[i].rgbRed=i;
lpRGB[i].rgbReserved=0;
}
}
lpbi->biClrImportant=lpbi->biClrUsed;
return lpbi->biClrUsed;
}
//get size
long GetTargetSize(HANDLE hBoard, TARGET tgt, short *width, short *height)
{
RECT rect;
long form;
if( (tgt==SCREEN ) ||(tgt==BUFFER) ) {
rect.right=-1;
okSetTargetRect(hBoard,tgt,&rect); //get current rect
*width=(short)(rect.right-rect.left);
*height=(short)(rect.bottom-rect.top);
if(tgt==SCREEN ) {
form=okSetCaptureParam(hBoard,CAPTURE_SCRRGBFORMAT,GETCURRPARAM); //-1
//limit to video rect
rect.right=-1; //max. captured rect
okSetTargetRect(hBoard,VIDEO,&rect); //get video rect
*width=(short)min(*width,rect.right-rect.left);
*height=(short)min(*height,rect.bottom-rect.top);
}
else if(tgt==BUFFER)
form=okSetCaptureParam(hBoard,CAPTURE_BUFRGBFORMAT,GETCURRPARAM); //-1
} else if(tgt>BUFFER) { //from blkinfo
LPBLOCKINFO lpblk;
lpblk=(LPBLOCKINFO)tgt;
*width=lpblk->iWidth;
*height=abs(lpblk->iHeight);
form=MAKELONG(lpblk->iFormType,lpblk->iBitCount);
}
return form;
}
//switch menu freeze/active
void EnableItem(HWND hWnd, int iMenuID, BOOL bEnable)
{
HMENU hMenu;
hMenu=GetMenu(hWnd);
//ModifyMenu(hMenu,IDM_FREEZE,MF_BYCOMMAND|MF_STRING,IDM_ACTIVE,"&Active");
if(bEnable)
EnableMenuItem(hMenu,iMenuID,MF_BYCOMMAND|MF_ENABLED);
else
EnableMenuItem(hMenu,iMenuID,MF_BYCOMMAND|MF_GRAYED);
DrawMenuBar(hWnd);
}
//display current dib info
void DisplayDIBInfo(HWND hwnd)
{
char string[200];
LPSTR lpstr;
GetWindowText(hwnd,string,120);
lpstr=strchr(string,'-');
if(!lpstr)
lpstr=string+strlen(string);
sprintf(lpstr,"- %d x %d x %d",lpbi->biWidth,lpbi->biHeight,lpbi->biBitCount);
if(lpbi->biWidth)
SetWindowText(hwnd,string);
}
//set data to dib
long SetDataToDIB(TARGET src, short start, LPBITMAPINFOHEADER lpbi, LPBYTE lpdib)
{
BLOCKINFO blk;
//get image to app buffer from tgt
blk.lpBits=lpdib;
blk.iWidth=(short)lpbi->biWidth;
blk.iHeight=-(short)lpbi->biHeight; //to invert y
blk.iBitCount=lpbi->biBitCount;
if(lpbi->biCompression==BI_BITFIELDS) {//
DWORD *lpMask;
lpMask=(DWORD *)((LPSTR)lpbi+lpbi->biSize);
if(lpMask[1]==0x03e0) //555
blk.iFormType=FORM_RGB555;
}
return okConvertRect(hBoard,(TARGET)&blk,0,src,start,1);
}
//---you must set gray logic palette when VGA is 256 color mode
void SetPaletteToWnd(HDC hDC)
{
static HPALETTE hPalette;
long form;
short bits;
//get current screen(VGA) format
form=okSetCaptureParam(hBoard,CAPTURE_SCRRGBFORMAT,GETCURRPARAM); //-1
bits=HIWORD(form); //take HIWORD
form=LOWORD(form);
if(bits==8 || bits==10) { //gray
if(!hPalette)
hPalette=MakeLogPalette(bits,(short)form);
SelectPalette(hDC,hPalette,0);
RealizePalette(hDC); // GDI Bug
}
}
//----demo to make a gray logic palette
HPALETTE MakeLogPalette(short iBits, short rgbForm)
{
HPALETTE hLogPal;
NPLOGPALETTE npPal;
WORD nNumColors;
short i;
nNumColors=(WORD)(1<<iBits);
npPal = (NPLOGPALETTE)LocalAlloc(LMEM_FIXED, sizeof(LOGPALETTE) +
nNumColors * sizeof(PALETTEENTRY));
if (!npPal)
return(FALSE);
npPal->palVersion = 0x300;
npPal->palNumEntries = nNumColors;
//set palette
if(rgbForm==FORM_RGB332) { //rgb 332
BYTE red=0, green=0, blue=0;
for (i = 0; i < nNumColors; i++ ) {
npPal->palPalEntry[i].peBlue = blue;
npPal->palPalEntry[i].peGreen = green;
npPal->palPalEntry[i].peRed = red;
npPal->palPalEntry[i].peFlags = 0;
if (!(red += 32))
if (!(green += 32))
blue += 64;
}
}
else { //gray 256 or 1024
for (i = 0; i < nNumColors; i++ ) {
npPal->palPalEntry[i].peRed = (BYTE)i;
npPal->palPalEntry[i].peGreen = (BYTE)i;
npPal->palPalEntry[i].peBlue = (BYTE)i;
npPal->palPalEntry[i].peFlags = 0;
}
}
hLogPal = CreatePalette((LPLOGPALETTE)npPal);
LocalFree((HANDLE)npPal);
return(hLogPal);
}
//------------callback runtines----------
long elapsed;
long numframe;
char string[256];
long bufform;
BLOCKINFO blk;
BOOL CALLBACK BeginCapture(HANDLE hBoard)
{
short width,height;
long blkform;
if(bDispToScreen) {
bufform=GetTargetSize(hBoard, BUFFER, &width, &height);
if( (LOWORD(bufform)==FORM_GRAY10) || (LOWORD(bufform)==FORM_GRAY12) ) { //special
//blkform=okSetCaptureParam(hBoard,CAPTURE_SCRRGBFORMAT,GETCURRPARAM); //-1
blkform=MAKELONG(FORM_GRAY8,8);
}
else //take same bits as buffer
blkform=bufform;
SetBitmapHeader(lpbi, width, height, HIWORD(blkform), LOWORD(blkform) );
}
blk.lpBits=lpdib;
blk.iBitCount=lpbi->biBitCount;
blk.iWidth=(short)lpbi->biWidth;
if( okSetCaptureParam(hBoard,CAPTURE_SAMPLEFIELD,-1)==0 ) //sample in field by field
if( LOWORD(okSetVideoParam(hBoard,VIDEO_SIGNALTYPE,-1))==1 ) //interlace video
lpbi->biHeight*=2; //double size for ht
blk.iHeight=-(short)lpbi->biHeight; //note: minus is for invert dib in y by ConvertRect
elapsed=GetTickCount();
numframe=0;
return 1;
}
BOOL CALLBACK BackDisplay(HANDLE hBoard,long no)
{
//return 1;
numframe++;
//sprintf(string,"captured frames = %d ",no);
sprintf(string,"Total frames = %d; Current = %d ",numframe, no);
SetWindowText(hwnd,string);
if(bDispToScreen) {
if( (LOWORD(bufform)==FORM_GRAY10) ||(LOWORD(bufform)==FORM_GRAY12) ) { //special
//way 1
okConvertRect(hBoard,(TARGET)&blk,0,BUFFER,(short)no,1);
//way 2
//okReadRectEx(hBoard,BUFFER,(short)no,lpdib, MAKELONG(FORM_GRAY8,1));
//read only red data
}
else {
//way 1
okTransferRect(hBoard,(TARGET)&blk,0,BUFFER,(short)no,1);
//way 2
//okReadRect(hBoard,BUFFER,(short)no,lpdib);
}
InvalidateRect(hwnd,NULL,FALSE);
}
return 1;
}
BOOL CALLBACK SetToScreen(HANDLE hBoard,long no)
{
//numframe++;
//sprintf(string,"captured frames = %d ",no);
//SetWindowText(hwnd,string);
if( (LOWORD(bufform)==FORM_GRAY10) || (LOWORD(bufform)==FORM_GRAY12) ) { //special
//way 1
okConvertRect(hBoard,(TARGET)&blk,0,BUFFER,(short)no,1);
//way 2
//okReadRectEx(hBoard,BUFFER,(short)no,lpdib);
//ConvertBitmap(lpbi,lpdib);
}
else {
okTransferRect(hBoard,(TARGET)&blk,0,BUFFER,(short)no,1);
//way 2
//okReadRect(hBoard,BUFFER,(short)no,lpdib);
//ConvertBitmap(lpbi,lpdib);
}
InvalidateRect(hwnd,NULL,FALSE);
UpdateWindow(hwnd);
return 1;
}
BOOL CALLBACK EndCapture(HANDLE hBoard)
{
elapsed=GetTickCount()-elapsed;
sprintf(string,"Total frames = %d; elapsed time= %d ms",numframe,elapsed);
SetWindowText(hwnd,string);
//clear callback
okSetSeqCallback(hBoard,NULL,NULL,NULL);
return 1;
}
long GetFrameSize(HANDLE hBoard)
{
long bits,size;
size=okSetCaptureParam(hBoard,CAPTURE_BUFBLOCKSIZE,-1);
bits=okSetCaptureParam(hBoard,CAPTURE_BUFRGBFORMAT,-1);
size=(HIWORD(bits))/8*LOWORD(size)*HIWORD(size);
return size;
}
void SingleCapture(void)
{
okCaptureTo(hBoard,BUFFER,0,1);//single
okGetCaptureStatus(hBoard,1);
Sleep(20); //do waiting here
//buf->scr
GetBitmapHeader(hBoard, BUFFER, lpbi);
SetDataToDIB(BUFFER, 0, lpbi, lpdib);
//okStopCapture(hBoard);
InvalidateRect(hwnd,NULL,TRUE);
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -