12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220 |
- // ImgBrd.cpp : 实现文件
- //
- #include "stdafx.h"
- //#include "ImgAcq.h"
- #include "ImgBrd.h"
- #include <process.h>
- #include "AppSettings.h"
- #include "EComRFMessage_i.h"
- #include "CShareMemManager.h"
- //============================================
- PV_INIT_SIGNAL_HANDLER();
- //============================================
- // CImgBrd
- extern CString strAppPath;
- extern CString strAppDYNHandleName;
- extern CString strAppMechCompHandleName;
- extern CString strAppRFDTHandleName;
- extern CString strAppDRDTHandleName;
- extern CString strAppHardwareHandleName;
- extern CString strAppRFOCHandleName;
- extern CString g_strLogFilePath;
- extern CString g_strHWConfFilePath;
- extern CString g_strConfFilePath;
- PvStreamGEV *m_pvStream;
- PvPipeline *m_pvPipeline;
- PipelineEventSink lPipelineEventSink;
- #define BUFFER_NUM 10
- #define IMAGEWIDTH 2880
- CImgBrd* CImgBrd::m_pCab = NULL;
- IMPLEMENT_DYNAMIC(CImgBrd, CWnd)
- CImgBrd::CImgBrd()
- {
- m_nMode = 1;
- m_IsValid = false;
- m_IsGrab = false;
- m_nBuf = 0;
- m_pImgBuffer = NULL;
- m_pImgBufferSize = 0;
- m_strDCFFile = "";
- m_pLog = NULL;
- m_nGrabMode = 2;
- m_pDataBuffer = new CShareMem(_T("ImageBuffer"));
- m_pDataBuffer->CreateShareMem(IMAGEWIDTH*(IMAGEWIDTH+1)*BUFFER_NUM*sizeof(WORD));
- m_nCurrentFrame = -1;
- m_nTotalFrameGrab = 0;
- m_nPixelSpacing = 148;
- m_nCropLeft = 0;
- m_nCropTop = 0;
- m_nCropRight = 0;
- m_nCropBottom = 0;
- m_pwData = NULL;
- SizeX = 960;
- SizeY = 960;
- m_fCurAcqFrameRate = 0.0f;
- m_hEventGrab = NULL;
- m_pGrapThdPro = NULL;
- m_bExitGrapThdPro = false;
- m_IsInGrabPeriod = CreateEvent( NULL, TRUE, FALSE, NULL );
- m_IsLeaveSlave = CreateEvent( NULL, TRUE, FALSE, NULL );
- m_pCab = this;
- m_Mutext_Buff = CreateMutex(0,0,0);
- m_pvStream = NULL;
- m_pvPipeline = NULL;
- }
- CImgBrd::~CImgBrd()
- {
- ExitGrabThread();
- CloseEbusDevice();
- if(m_pLog != NULL)
- {
- delete m_pLog;
- m_pLog = NULL;
- }
- if(m_pDataBuffer)
- {
- delete m_pDataBuffer;
- m_pDataBuffer = NULL;
- }
- if(m_pwData)
- {
- delete[] m_pwData;
- m_pwData = NULL;
- }
- WaitForSingleObject(m_Mutext_Buff,INFINITE);
- if(m_pImgBuffer)
- {
- delete[] m_pImgBuffer;
- m_pImgBuffer = NULL;
- m_pImgBufferSize = 0;
- }
- ReleaseMutex(m_Mutext_Buff);
- CloseHandle(m_IsInGrabPeriod);
- CloseHandle(m_IsLeaveSlave);
- CloseHandle(m_Mutext_Buff);
- }
- // 测试使用,保存RAW图到特定目录
- CFile *g_TestSavingImage = NULL;
- bool g_bTestTrue = false;
- BEGIN_MESSAGE_MAP(CImgBrd, CWnd)
- END_MESSAGE_MAP()
- void CImgBrd::trimipstr(string &trimtarget)
- {
- vector<char> trimstr;
- DWORD len = trimtarget.size();
- for(DWORD i = 0;i < len;i++)
- {
- if((trimtarget[i] >= '0' && trimtarget[i] <= '9') || trimtarget[i] == '.')
- {
- trimstr.push_back(trimtarget[i]);
- }
- }
- trimtarget = "";
- trimtarget.assign(trimstr.begin(),trimstr.end());
- }
- int CImgBrd::CloseEbusDevice()
- {
- m_pLog->WriteLog("Stop pipeline",LOG_INFORMATION,LOG_DEBUG);
- if(m_pvPipeline)
- {
- m_pvPipeline->Stop();
- // Now close the stream. Also optional but nice to have.
- m_pLog->WriteLog("Closing stream" ,LOG_INFORMATION,LOG_DEBUG);
- if(m_pvStream)
- {
- m_pvStream->Close();
- }
- // Unregistered pipeline event sink. Optional but nice to have.
- m_pvPipeline->UnregisterEventSink( &lPipelineEventSink );
- delete m_pvPipeline;
- delete m_pvStream;
- PV_SAMPLE_TERMINATE();
- return 1;
- }
- return 0;
- }
- int CImgBrd::OpenEbusDevice()
- {
- PvResult lResult;
- const PvDeviceInfo* lLastDeviceInfo = NULL;
- char szSep[] = ":";
- char szIpAddress[MAX_PATH] = {0};
- // Find all devices on the network.
- PvSystem lSystem;
- lResult = lSystem.Find();
- if ( !lResult.IsOK() )
- {
- m_pLog->WriteLog("Error opening stream",LOG_ERROR,LOG_DEBUG);
- return 0;
- }
- //get interfaces of ip&port
- CString strPixiumPath;// = strAppPath + ("\\config\\config_2121S\\PixDyn.ini");
- strPixiumPath.Format("%s\\%s\\config\\PixDyn.ini", g_strHWConfFilePath, m_SubConfigPath);
- m_pLog->WriteLog(strPixiumPath,LOG_ERROR,LOG_DEBUG);
- GetPrivateProfileStringA("hardware","gigevision.emission.remote.address","",szIpAddress,MAX_PATH,strPixiumPath.GetBuffer());
- LogInfo(szIpAddress);
- string localipaddr;
- string localipport;
- if(strlen(szIpAddress) == 0)
- {
- m_pLog->WriteLog("gigevision.emission.remote.address in PixDyn.ini not set.using default as 192.168.0.1:50000",LOG_WARNING,LOG_DEBUG);
- localipaddr = "192.168.0.1";
- localipport = "50000";
- }
- else
- {
- char *pContext = strtok(szIpAddress,szSep);
- if(pContext)
- {
- localipaddr = pContext;
- trimipstr(localipaddr);
- pContext = strtok(NULL,szSep);
- }
- if(pContext)
- {
- localipport = pContext;
- }
- if(localipaddr.size() ==0 || localipport.size() == 0)
- {
- m_pLog->WriteLog("Error gigevision.emission.remote.address in PixDyn.ini ",LOG_ERROR,LOG_DEBUG);
- return 0;
- }
- }
- //LogInfo(localipaddr.c_str());
- //LogInfo(localipport.c_str());
- //target ip address
- memset(szIpAddress,0,MAX_PATH);
- GetPrivateProfileStringA("hardware","gigevision.emission.host.address","",szIpAddress,MAX_PATH,strPixiumPath.GetBuffer());
- //LogInfo(szIpAddress);
- if(strlen(szIpAddress) == 0)
- {
- m_pLog->WriteLog("Error gigevision.emission.host.address in PixDyn.ini not set.using default as 192.168.0.2",LOG_ERROR,LOG_DEBUG);
- strcpy(szIpAddress,"192.168.0.2");
- }
- string targetipaddr = szIpAddress;
- trimipstr(targetipaddr);
- PV_SAMPLE_INIT();
- // Go through all interfaces
- uint32_t lInterfaceCount = lSystem.GetInterfaceCount();
- for ( uint32_t x = 0; x < lInterfaceCount; x++ )
- {
- //cout << "Interface " << x << endl;
- // Get pointer to the interface.
- const PvInterface* lInterface = lSystem.GetInterface( x );
- // Is it a PvNetworkAdapter?
- const PvNetworkAdapter* lNIC = dynamic_cast<const PvNetworkAdapter*>( lInterface );
- if ( lNIC != NULL )
- {
- string ip1 = lNIC->GetIPAddress().GetAscii();
- trimipstr(ip1);
- if(ip1 != localipaddr)
- {
- continue;
- }
- //cout << " MAC Address: " << lNIC->GetMACAddress().GetAscii() << endl;
- //cout << " IP Address: " << lNIC->GetIPAddress().GetAscii() << endl;
- //cout << " Subnet Mask: " << lNIC->GetSubnetMask().GetAscii() << endl << endl;
- }
- // Go through all the devices attached to the interface
- uint32_t lDeviceCount = lInterface->GetDeviceCount();
- for ( uint32_t y = 0; y < lDeviceCount ; y++ )
- {
- const PvDeviceInfo *lDeviceInfo = lInterface->GetDeviceInfo( y );
- //cout << " Device " << y << endl;
- //cout << " Display ID: " << lDeviceInfo->GetDisplayID().GetAscii() << endl;
- const PvDeviceInfoGEV* lDeviceInfoGEV = dynamic_cast<const PvDeviceInfoGEV*>( lDeviceInfo );
- //const PvDeviceInfoU3V *lDeviceInfoU3V = dynamic_cast<const PvDeviceInfoU3V *>( lDeviceInfo );
- //const PvDeviceInfoUSB *lDeviceInfoUSB = dynamic_cast<const PvDeviceInfoUSB *>( lDeviceInfo );
- //const PvDeviceInfoPleoraProtocol* lDeviceInfoPleora = dynamic_cast<const PvDeviceInfoPleoraProtocol*>( lDeviceInfo );
- if ( lDeviceInfoGEV != NULL ) // Is it a GigE Vision device?
- {
- string targetip1 = lDeviceInfoGEV->GetIPAddress().GetAscii();
- trimipstr(targetip1);
- if(targetipaddr != targetip1)
- {
- continue;
- }
- //cout << "GigE Vision device MAC Address: " << lDeviceInfoGEV->GetMACAddress().GetAscii() << endl;
- //cout << "GigE Vision device IP Address: " << lDeviceInfoGEV->GetIPAddress().GetAscii() << endl;
- //cout << "GigE Vision device Serial number: " << lDeviceInfoGEV->GetSerialNumber().GetAscii() << endl << endl;
- lLastDeviceInfo = lDeviceInfo;
- }
- //else if ( lDeviceInfoU3V != NULL ) // Is it a USB3 Vision device?
- //{
- // cout << " GUID: " << lDeviceInfoU3V->GetDeviceGUID().GetAscii() << endl;
- // cout << " S/N: " << lDeviceInfoU3V->GetSerialNumber().GetAscii() << endl;
- // cout << " Speed: " << lUSB->GetSpeed() << endl << endl;
- // lLastDeviceInfo = lDeviceInfo;
- //}
- //else if ( lDeviceInfoUSB != NULL ) // Is it an unidentified USB device?
- //{
- // cout << endl;
- //}
- //else if ( lDeviceInfoPleora != NULL ) // Is it a Pleora Protocol device?
- //{
- // cout << "Pleora Protocol device MAC Address: " << lDeviceInfoPleora->GetMACAddress().GetAscii() << endl;
- // cout << "Pleora Protocol device IP Address: " << lDeviceInfoPleora->GetIPAddress().GetAscii() << endl;
- // cout << "Pleora Protocol device Serial number: " << lDeviceInfoPleora->GetSerialNumber().GetAscii() << endl << endl;
- //}
- //got a device
- }
- }
- // Connect to the last device found
- if ( lLastDeviceInfo != NULL )
- {
- //cout << "Connecting to " << lLastDeviceInfo->GetDisplayID().GetAscii() << endl;
- m_pvStream = new PvStreamGEV();
- m_pvStream->SetUserModeDataReceiverThreadPriority(6);
- m_pvPipeline = new PvPipeline(m_pvStream);
- m_pvPipeline->RegisterEventSink( &lPipelineEventSink );
- m_pvPipeline->SetBufferCount( 64 );
- m_pvPipeline->SetBufferSize(1920*1024*4);//for test
- m_pvPipeline->SetHandleBufferTooSmall(true);
- m_pvPipeline->SetBufferHandlingThreadPriority(6);//THREAD_PRIORITY_TIME_CRITICAL
- // Open stream
- m_pLog->WriteLog("Opening stream",LOG_INFORMATION,LOG_DEBUG);
- if ( !m_pvStream->Open( lLastDeviceInfo->GetConnectionID(),atoi(localipport.c_str()) ).IsOK() )
- {
- m_pLog->WriteLog("Error opening stream",LOG_ERROR,LOG_DEBUG);
- return 0;
- }
- //
- m_pLog->WriteLog("Starting pipeline",LOG_INFORMATION,LOG_DEBUG);
- PvResult res1 = m_pvPipeline->Start();
- if(res1.IsOK() == false)
- {
- m_pLog->WriteLog("Error start pipe",LOG_ERROR,LOG_DEBUG);
- return 0;
- }
- //succeed
- m_pLog->WriteLog("Starting pipeline Succeed",LOG_INFORMATION,LOG_DEBUG);
- return 1;
- }
- m_pLog->WriteLog("Error eBus Device not found ",LOG_ERROR,LOG_DEBUG);
- return 0;
- }
- bool CImgBrd::InitConnection()
- {
- //// Let the user select the device to receive from.
- //PvString lDeviceIP;
- //if ( !SelectDevice( lDeviceIP ) )
- //{
- // return false;
- //}
- //// Create the PvStreamGEV object.
- //PvStreamGEV m_lStreamGEV;
- //// Create the PvPipeline object.
- //PvPipeline m_lPipeline( &m_lStreamGEV );
- //// Create a PvPipeline event sink (used to trap buffer too small events).
- //PipelineEventSink lPipelineEventSink;
- //m_lPipeline.RegisterEventSink( &lPipelineEventSink );
- //m_lPipeline.SetBufferCount( 16 );
- //m_lPipeline.SetBufferSize(1920*1024*4);//for test
- //m_lPipeline.SetHandleBufferTooSmall(true);
- //// Open stream
- //m_pLog->WriteLog("Opening stream",LOG_INFORMATION,LOG_DEBUG);
- ///*if ( !m_lStreamGEV.Open( lDeviceIP, "239.192.1.1", 1042 ).IsOK() )*/
- //if ( !m_lStreamGEV.Open( lDeviceIP,50000 ).IsOK() )
- //{
- // m_pLog->WriteLog("Error opening stream",LOG_ERROR,LOG_DEBUG);
- // return false;
- //}
- //LogInfo(lDeviceIP);
- //// IMPORTANT: the pipeline needs to be "armed", or started before
- //// we instruct the device to send us images.
- //m_pLog->WriteLog("Starting pipeline",LOG_INFORMATION,LOG_DEBUG);
- return false;
- }
- bool CImgBrd::InitBrd(HWND hWnd)
- {
- m_hWnd = hWnd;
- if(g_bTestTrue) AfxMessageBox("Saving RAWIMAGE IN D:\\TESTRAW_IMAGE.raw");
- CString strlog;
-
- CAppSettings logsettings(g_strConfFilePath + "\\DROCLogMode.xml");
- if(m_pLog)
- {
- delete m_pLog;
- m_pLog = NULL;
- }
- m_pLog = new CLogFile(g_strLogFilePath,_T("Acq"),atoi(logsettings[("LogDays")]));
- m_pLog->SetLogMode(atoi(logsettings[_T("Acq")]));
- m_pLog->WriteLog("\n\n\n===========================================Begin ACQ board Log====================================\n",LOG_INFORMATION,LOG_DEBUG);
- if(OpenEbusDevice() == 0)
- {
- return false;
- }
- strlog.Format("Image size: Size X = %d, Size Y = %d",SizeX,SizeY);
- m_pLog->WriteLog(strlog,LOG_INFORMATION,LOG_DEBUG);
- m_ImgInfo.nWidth = SizeX;
- m_ImgInfo.nHeight = SizeY;
-
- ReadyImgBuff(SizeX*SizeY);
- m_pwData = new WORD[SizeX*SizeY];
- IniGrabThread();
- return true;
- }
- bool CImgBrd::CloseBrd(void)
- {
- if(m_IsGrab)
- {
- m_pLog->WriteLog("ERROR Calling: CloseBrd during ACQ: StopGrab here.",LOG_ERROR);
-
- StopGrab();
- }
- //if(m_pImgBuffer)
- //{
- // delete[] m_pImgBuffer;
- // m_pImgBuffer = NULL;
- // m_pImgBufferSize = 0;
- //}
- if(m_pwData)
- {
- delete[] m_pwData;
- m_pwData = NULL;
- }
- return true;
- }
- bool CImgBrd::SetAcqMode(int nMode)
- {
- return true;
- }
- WORD* CImgBrd::ReadyImgBuff(DWORD ImgSize)
- {
- WaitForSingleObject(m_Mutext_Buff,INFINITE);
- if (m_pImgBuffer==NULL)
- {
- // m_pLog->WriteLog("***m_pImgBuffer is NULL",LOG_INFORMATION,LOG_DEBUG);
- m_pImgBufferSize = ImgSize;
- m_pImgBuffer = new WORD[m_pImgBufferSize];
- }
- else
- {
- // m_strLog.Format("***lWidth*lHeight=%d, m_pImgBufferSize=%d",lWidth*lHeight, m_pImgBufferSize);
- // m_pLog->WriteLog(m_strLog,LOG_INFORMATION,LOG_DEBUG);
- if(ImgSize > m_pImgBufferSize)
- {
- delete []m_pImgBuffer;
- m_pImgBufferSize = ImgSize;
- m_pImgBuffer = new WORD[m_pImgBufferSize];
- }
- }
- ReleaseMutex(m_Mutext_Buff);
- return m_pImgBuffer;
- }
- bool CImgBrd::StartGrab(void)
- {
-
- CString strlog;
-
- m_nCurrentFrame = -1;
- m_nTotalFrameGrab = 0;
- strlog.Format("start grab ProcessFunction!");
- m_pLog->WriteLog(strlog,LOG_INFORMATION,LOG_DEBUG);
- m_IsValid = true;
- if(g_bTestTrue)
- g_TestSavingImage = new CFile("D:\\TESTRAW_IMAGE.raw",CFile::modeCreate|CFile::modeWrite);
-
- //StartSlave();
- //LogInfo("Set grab event1");
- //if ((m_nGrabMode==ZSKK_HARDWARE_SEQUENCE_SEQUENCERAD)||(m_nGrabMode==ZSKK_HARDWARE_SEQUENCE_PULSEFLU))
- //{
- // LogInfo("Sleep 50ms");
- //}
- //Sleep(50);
- m_IsGrab = true;
- SetEvent(m_IsInGrabPeriod);
- return true;
- }
- bool CImgBrd::StopGrab(int nXRayOnNum)
- {
- CString strlog;
- m_nXRayOnNum = nXRayOnNum;
- m_IsGrab = false;
- if(m_nGrabMode != ZSKK_HARDWARE_SEQUENCE_SINGLERAD)//single spot just caputre one image and stop capture;
- {
- ResetEvent(m_IsInGrabPeriod);
- }
- else
- {
- m_pLog->WriteLog("Single shot mode, stop grab!",LOG_INFORMATION,LOG_DEBUG);
- }
- strlog.Format("Total image frame number: %d, Frequency: %.1lf",m_nTotalFrameGrab,m_fCurAcqFrameRate );
- m_pLog->WriteLog(strlog,LOG_INFORMATION,LOG_DEBUG);
- return true;
- }
- bool CImgBrd::MSGControl(CWnd *Wnd, int nMSGType, int nMSGwParam, int nMSGpParam, int nMSGOutMode,int nTargetPosition)
- {
- CString strHandleName = strAppHardwareHandleName;
- switch (nTargetPosition)
- {
- case ZSKK_MSG_TARGETHANDLE_NULL:
- break;
- case ZSKK_MSG_TARGETHANDLE_RFOC:
- strHandleName = strAppRFOCHandleName;
- break;
- case ZSKK_MSG_TARGETHANDLE_RFHARDWARE:
- strHandleName = strAppHardwareHandleName;
- break;
- case ZSKK_MSG_TARGETHANDLE_RFMECHNICAL:
- strHandleName = strAppMechCompHandleName;
- break;
- case ZSKK_MSG_TARGETHANDLE_RFDETECTOR:
- strHandleName = strAppRFDTHandleName;
- break;
- case ZSKK_MSG_TARGETHANDLE_DRDETECTOR:
- strHandleName = strAppDRDTHandleName;
- break;
- case ZSKK_MSG_TARGETHANDLE_RFDYN:
- strHandleName = strAppDYNHandleName;
- break;
- default:
- break;
- }
- CWnd *targetWnd = CWnd::FindWindow(NULL,strHandleName);
- if(targetWnd != NULL)
- {
- if(nMSGOutMode == 0)
- {
- targetWnd->PostMessage(nMSGType,nMSGwParam,nMSGpParam);
- }
- else
- {
- targetWnd->SendMessage(nMSGType,nMSGwParam,nMSGpParam);
- }
- }
- return true;
- }
- bool CImgBrd::WriteFrame(WORD* pImgData,int nWidth,int nHeight)
- {
- if(pImgData == NULL)
- return false;
- if((nWidth <=0)||(nHeight <= 0))
- return false;
- DWORD nTotalMem = m_pDataBuffer->GetShareMemSize();
- int ntotal = nTotalMem/(nWidth*nHeight*sizeof(WORD));
- if(ntotal == 0)
- {
- m_pLog->WriteLog("GetShareMemSize failed, shared memory size is 0", LOG_ERROR);
- return false;
- }
- m_nCurrentFrame = (m_nCurrentFrame+1)%ntotal;
- m_pDataBuffer->WriteShareMemEx(m_nCurrentFrame*nWidth*nHeight*sizeof(WORD),(LPVOID)pImgData,nWidth*nHeight*sizeof(WORD));
- if(g_bTestTrue)
- {
- if(m_nCurrentFrame < 12)
- g_TestSavingImage->Write(pImgData,nWidth*nHeight*sizeof(WORD));
- }
- return true;
- }
- bool CImgBrd::SetFrameValid()
- {
- m_IsValid = true;
- return true;
- }
- bool CImgBrd::SetGrabMode(int nmode)
- {
- m_nGrabMode = nmode;
- CString strlog;
- strlog.Format("Grab mode: %d",m_nGrabMode);
- this->m_pLog->WriteLog(strlog,LOG_INFORMATION,LOG_DEBUG,true);
- return true;
- }
- bool CImgBrd::GetImageInfo(ZSKK_ACQ_INFO &imgInfo)
- {
- imgInfo.nBits = 16;
- imgInfo.nColumn = SizeX;
- imgInfo.nRow = SizeY;
- return true;
- }
- int CImgBrd::CropImageMargin(LPVOID pDstData, int& nDstWidth, int& nDstHeight,
- LPVOID pScrData, int nSrcWidth, int nSrcHeight, int nBits,
- int nLeftMargin,int nTopMargin,int nRightMargin,int nBottomMargin)
- {
- if(nLeftMargin == 0 && nTopMargin == 0 && nRightMargin == 0 && nBottomMargin == 0) return -1;
- if((pDstData == NULL)||(pScrData == NULL)||(nSrcWidth <= 0)||(nSrcHeight <= 0)||(nBits <= 0))
- return -1;
- if((nLeftMargin >= nSrcWidth)||(nTopMargin >= nSrcHeight))
- return -1;
- int nBitsToBYTE =(int)((nBits+7)*0.125);
- if(nBitsToBYTE < 1)
- return -1;
- int nXL, nXR, nYL, nYR;
- nXL = nLeftMargin;
- nYL = nTopMargin;
- if(nSrcWidth - nRightMargin < 0)
- return -1;
- nXR = nSrcWidth - nRightMargin - 1;
- if(nXR < nXL)
- return -1;
- if(nSrcHeight - nBottomMargin < 0)
- return -1;
- nYR = nSrcHeight - nBottomMargin - 1;
- if(nYR < nYL)
- return -1;
- nDstWidth = nXR - nXL + 1;
- nDstHeight = nYR - nYL + 1;
- int i;
- #pragma omp parallel private(i)
- {
- #pragma omp for
- for(i = nYL; i< nYR; i++)
- ::memcpy((WORD*)pDstData + (i - nYL) * nDstWidth, (WORD*)pScrData + (i * nSrcWidth + nXL), nDstWidth * nBitsToBYTE);
- }
- return 0;
- }
- long CImgBrd::ProcessingFunction(/*long HookType, MIL_ID HookId, void MPTYPE *HookDataPtr*/)
- {
- //for test
- //m_pCab->m_pLog->WriteLog("Process image...",LOG_INFORMATION,LOG_DEBUG);
- m_pCab->m_ImgInfo.nBits = 16;
- int res = m_pCab->CropImageMargin( m_pCab->m_pwData, m_pCab->m_ImgInfo.nWidth, m_pCab->m_ImgInfo.nHeight,
- m_pCab->m_pImgBuffer, m_pCab->SizeX, m_pCab->SizeY, m_pCab->m_ImgInfo.nBits,
- m_pCab->m_nCropLeft, m_pCab->m_nCropTop, m_pCab->m_nCropRight, m_pCab->m_nCropBottom);
- if( res == 0 )
- {
- m_pCab->m_ImgInfo.pwData = m_pCab->m_pwData;
- }
- else
- {
- m_pCab->m_ImgInfo.nWidth = m_pCab->SizeX;
- m_pCab->m_ImgInfo.nHeight = m_pCab->SizeY;
- m_pCab->m_ImgInfo.pwData = m_pCab->m_pImgBuffer;
- }
- CString strlog;
- CWnd *pWndTest = NULL;
- if((m_pCab->m_nGrabMode == ZSKK_HARDWARE_SEQUENCE_SINGLERAD) && (m_pCab->m_nTotalFrameGrab == 0))
- {
- //for test
- strlog.Format("Single shot RAD Image ready: SizeX = %d, SizeY = %d",m_pCab->m_ImgInfo.nWidth,m_pCab->m_ImgInfo.nHeight);
- m_pCab->m_pLog->WriteLog(strlog,LOG_INFORMATION,LOG_DEBUG);
-
- m_pCab->WriteFrame(m_pCab->m_ImgInfo.pwData, m_pCab->m_ImgInfo.nWidth, m_pCab->m_ImgInfo.nHeight);
- m_pCab->MSGControl(pWndTest, ZSKK_MSG_RFHW_WFOUT, ZSKK_MSG_RFHW_WF_DATAINFOSINGLE, m_pCab->m_nCurrentFrame, ZSKK_MSG_TYPE_SYNC_NO);
- m_pCab->m_pLog->WriteLog("Image Send Out to UI!",LOG_INFORMATION,LOG_DEBUG);
- //m_pCab->m_strLog.Format("c:\\2121\\%d.raw",m_pCab->m_nTotalFrameGrab);
- //m_pCab->SaveImage(m_pCab->m_ImgInfo.pwData,m_pCab->m_strLog,m_pCab->m_ImgInfo.nWidth, m_pCab->m_ImgInfo.nHeight);
- m_pCab->StopGrab();
- }
- else if(m_pCab->m_nGrabMode == ZSKK_HARDWARE_SEQUENCE_SEQUENCERAD)
- {
- m_pCab->WriteFrame(m_pCab->m_ImgInfo.pwData, m_pCab->m_ImgInfo.nWidth, m_pCab->m_ImgInfo.nHeight);
- m_pCab->MSGControl(pWndTest, ZSKK_MSG_RFHW_WFOUT, ZSKK_MSG_RFHW_WF_DATAINFOSEQUENCE, m_pCab->m_nCurrentFrame, 0);
- strlog.Format("Sequence RAD Image ready: SizeX = %d, SizeY = %d",m_pCab->m_ImgInfo.nWidth,m_pCab->m_ImgInfo.nHeight);
- m_pCab->m_pLog->WriteLog(strlog,LOG_INFORMATION,LOG_DEBUG);
- }
- else if((m_pCab->m_nGrabMode == ZSKK_HARDWARE_SEQUENCE_PULSEFLU) && (m_pCab->m_nTotalFrameGrab == 0))
- {
- strlog.Format("First Flu Image .Ignore it");
- m_pCab->m_pLog->WriteLog(strlog,LOG_INFORMATION,LOG_DEBUG);
- }
- else if( ((m_pCab->m_nGrabMode == ZSKK_HARDWARE_SEQUENCE_PULSEFLU) || ( m_pCab->m_nGrabMode == ZSKK_HARDWARE_SEQUENCE_CONTINOUSFLU)) &&( m_pCab->m_IsGrab) )
- {
- m_pCab->WriteFrame(m_pCab->m_ImgInfo.pwData, m_pCab->m_ImgInfo.nWidth, m_pCab->m_ImgInfo.nHeight);
- m_pCab->MSGControl(pWndTest, ZSKK_MSG_RFHW_WFOUT, ZSKK_MSG_RFHW_WF_DATAINFOFLU, m_pCab->m_nCurrentFrame, ZSKK_MSG_TYPE_SYNC_NO);
-
- strlog.Format("Flu Image ready: SizeX = %d, SizeY = %d",m_pCab->m_ImgInfo.nWidth,m_pCab->m_ImgInfo.nHeight);
- m_pCab->m_pLog->WriteLog(strlog,LOG_INFORMATION,LOG_DEBUG);
- }
- ++m_pCab->m_nTotalFrameGrab;
- bool bSave = false;
- if (bSave)
- {
- m_pCab->m_strLog.Format("c:\\2121\\%d.raw",m_pCab->m_nTotalFrameGrab);
- m_pCab->SaveImage(m_pCab->m_ImgInfo.pwData,m_pCab->m_strLog,m_pCab->m_ImgInfo.nWidth, m_pCab->m_ImgInfo.nHeight);
- }
- return 0;
- }
- bool CImgBrd::ChangeActiveMode(CString ExamType, int nPUMode,int nSzieX,int nSizeY, int nCropLeft, int nCropTop, int nCropRight, int nCropBottom)
- {
- if(m_IsGrab)
- {
- m_pLog->WriteLog("ERROR Calling: Change DCF during ACQ: StopGrab here.",LOG_ERROR);
- StopGrab();
- }
- CloseBrd();
- SizeX = nSzieX;
- SizeY = nSizeY;
- ReadyImgBuff(SizeX*SizeY);
- m_strLog.Format("ChangeActiveMode: SizeX=%d, SizeY=%d", SizeX, SizeY);
- m_pLog->WriteLog(m_strLog,LOG_INFORMATION);
- //m_pImgBuffer = new WORD[SizeX*SizeY];
- if (m_pwData)
- {
- delete []m_pwData;
- m_pwData = NULL;
- }
- m_pwData = new WORD[SizeX*SizeY];
- m_nCropLeft = nCropLeft;
- m_nCropTop = nCropTop;
- m_nCropRight = nCropRight;
- m_nCropBottom = nCropBottom;
- m_ImgInfo.nWidth = SizeX - m_nCropLeft - m_nCropRight;
- m_ImgInfo.nHeight = SizeY - m_nCropTop - m_nCropBottom;
- m_ACQInfo.nColumn = m_ImgInfo.nWidth;
- m_ACQInfo.nRow = m_ImgInfo.nHeight;
- m_ACQInfo.nBits = 16;
- m_ACQInfo.fACQElementSpacing = (float)(m_nPixelSpacing*0.001);
- CWnd *pWndTest = NULL;
- CString strlog;
- m_strExamType = ExamType;
- if( ExamType != _T("RAD"))
- {
- strlog.Format("Send Flu Image information: SizeX = %d, SizeY = %d, PixelSpacing = %.3f,",m_ACQInfo.nColumn,m_ACQInfo.nRow,m_ACQInfo.fACQElementSpacing);
- m_pLog->WriteLog(strlog,LOG_INFORMATION,LOG_DEBUG);
- COPYDATASTRUCT copyData;
- copyData.lpData = &m_ACQInfo;
- copyData.cbData = sizeof(ZSKK_ACQ_INFO);
- MSGControl(pWndTest,ZSKK_MSG_RFDATA_OUT,ZSKK_DETECTOR_DATA_FLU_IMAGESIZEBIT,(LPARAM)©Data,ZSKK_MSG_TYPE_SYNC_YES);
- }
- else
- {
- strlog.Format("Send RAD image information: SizeX = %d, SizeY = %d, PixelSpacing = %.3f",m_ACQInfo.nColumn,m_ACQInfo.nRow,m_ACQInfo.fACQElementSpacing);
- m_pLog->WriteLog(strlog,LOG_INFORMATION,LOG_DEBUG);
- COPYDATASTRUCT copyData;
- copyData.lpData = &m_ACQInfo;
- copyData.cbData = sizeof(ZSKK_ACQ_INFO);
- MSGControl(pWndTest,ZSKK_MSG_RFDATA_OUT,ZSKK_DETECTOR_DATA_RAD_IMAGESIZEBIT,(LPARAM)©Data,ZSKK_MSG_TYPE_SYNC_YES);
- }
- this->m_pLog->WriteLog("Change acquire board mode success!",LOG_INFORMATION,LOG_DEBUG,true);
- return true;
- }
- void CImgBrd::Pausetime(DWORD dwSpan)
- {
- DWORD dtstart = ::GetTickCount();
- while((::GetTickCount() - dtstart)<dwSpan)
- {
- MSG msg;
- ::GetMessage(&msg, NULL, 0, 0);
- ::TranslateMessage(&msg);
- ::DispatchMessage(&msg);
- }
- }
- bool CImgBrd::SetPixelSpacing(int nPixelSpacing)
- {
- m_nPixelSpacing = nPixelSpacing;
- CString str;
- str.Format("pixel spacing(%d)",m_nPixelSpacing);
- m_pLog->WriteLog(str,LOG_INFORMATION,LOG_DEBUG);
- return true;
- }
- bool CImgBrd:: StartSlave()
- {
- // Get stream parameters/stats.
- PvGenParameterArray *lStreamParams = m_pvStream->GetParameters();
- PvGenInteger *lBlockCount = dynamic_cast<PvGenInteger *>( lStreamParams->Get( "BlockCount" ) );
- PvGenFloat *lFrameRate = dynamic_cast<PvGenFloat *>( lStreamParams->Get( "AcquisitionRate" ) );
- PvGenFloat *lBandwidth = dynamic_cast<PvGenFloat *>( lStreamParams->Get( "Bandwidth" ) );
- PvGenBoolean *lRequestMissingPackets = dynamic_cast<PvGenBoolean *>( lStreamParams->Get( "RequestMissingPackets" ) );
- // Disabling request missing packets.
- lRequestMissingPackets->SetValue( false );
- int64_t lBlockCountVal = 0;
- double lFrameRateVal = 0.0;
- double lBandwidthVal = 0.0;
- // Acquire images until the user instructs us to stop.
- //for test
- //m_pLog->WriteLog("Ready to get image.going to loop...",LOG_INFORMATION,LOG_DEBUG);
- //PvFlushKb();
- while ( 1 )//for test
- {
- if(m_pvPipeline->IsStarted() == false)
- {
- PvResult res1 = m_pvPipeline->Start();
- if(res1.IsOK() == false)
- {
- m_pLog->WriteLog("Error start pipe",LOG_ERROR,LOG_DEBUG);
- return false;
- }
- }
- // Retrieve next buffer
- PvBuffer *lBuffer = NULL;
- PvResult lOperationResult;
- //LogInfo("before PvResult lResult = m_pvPipeline->RetrieveNextBuffer( &lBuffer, 500, &lOperationResult )");
- PvResult lResult = m_pvPipeline->RetrieveNextBuffer( &lBuffer, 1000, &lOperationResult );
- //LogInfo("after PvResult lResult = m_pvPipeline->RetrieveNextBuffer( &lBuffer, 500, &lOperationResult )");
- if ( lResult.IsOK() )
- {
- if (lOperationResult.IsOK())
- {
- //if(WaitForSingleObject(m_IsInGrabPeriod,0) == WAIT_OBJECT_0)
- if(m_IsGrab == true)
- {
- //
- // We now have a valid buffer. This is where you would typically process the buffer.
- // -----------------------------------------------------------------------------------------
- // ...
- lBlockCount->GetValue( lBlockCountVal );
- lFrameRate->GetValue( lFrameRateVal );
- lBandwidth->GetValue( lBandwidthVal );
- // If the buffer contains an image, display width and height.
- uint32_t lWidth = 0, lHeight = 0;
- int temp = lBuffer->GetBlockID();
- //for test
- /*CString lLog;
- lLog.Format("BlockID(%d),ChunkCount(%d),PayLoadType(%d)",temp,lBuffer->GetChunkCount(),(int)lBuffer->GetPayloadType());
- m_pLog->WriteLog(lLog,LOG_INFORMATION,LOG_DEBUG);
- for(DWORD li = 0; li < lBuffer->GetChunkCount();li++)
- {
- uint32_t cID = 0;
- PvResult lres = lBuffer->GetChunkIDByIndex(li,cID);
- if(lres.IsOK())
- {
- CString dataLog;
- uint32_t cSize = lBuffer->GetChunkSizeByID(cID);
- const uint8_t *pChunkData = lBuffer->GetChunkRawDataByID(cID);
- lLog.Format("ChunkIdx(%d),ChunkId(%d),ChunkSize(%d),ChunkData:"
- ,li,cID,cSize);
- for(DWORD lj = 0;lj < cSize;lj++)
- {
- dataLog.Format("-%02X",pChunkData[lj]);
- lLog += dataLog;
- }
- m_pLog->WriteLog(lLog,LOG_INFORMATION,LOG_DEBUG);
- }
- }*/
- if ( lBuffer->GetPayloadType() == PvPayloadTypeImage )
- {
- // Get image specific buffer interface.
- PvImage *lImage = lBuffer->GetImage();
- // Read width, height
- lWidth = lBuffer->GetImage()->GetWidth();
- lHeight = lBuffer->GetImage()->GetHeight();
- // m_strLog.Format("***lWidth = %d, lHeight = %d", lWidth, lHeight);
- // m_pLog->WriteLog(m_strLog,LOG_INFORMATION,LOG_DEBUG);
- if ((lWidth>0)&&(lHeight>0))
- {
- WORD *p = ReadyImgBuff(((DWORD)lWidth)*((DWORD)lHeight));
- memcpy(m_pImgBuffer,lImage->GetDataPointer(),((DWORD)lWidth)*((DWORD)lHeight)*sizeof(WORD));
- ProcessingFunction();
- }
- lImage = NULL;
- }
- m_fCurAcqFrameRate = lFrameRateVal;
- m_strLog.Format(" BlockID(%d),Width(%d),Hight(%d),FrameRate(%.1lf),Bandwidth(%.1lf)Mb/s"
- ,temp,lWidth,lHeight,m_fCurAcqFrameRate,lBandwidthVal/1000000.0f);
- m_pLog->WriteLog(m_strLog,LOG_INFORMATION,LOG_DEBUG);
- }
- }
- // We have an image - do some processing (...) and VERY IMPORTANT,
- // release the buffer back to the pipeline.
- m_pvPipeline->ReleaseBuffer( lBuffer );
- //m_pvPipeline->Reset();
- }
- else
- {
- break;
- }
- }
- // We stop the pipeline - letting the object lapse out of
- // scope would have had the destructor do the same, but we do it anyway.
- //for test
- //LogInfo("leave StartSlave");
- return true;
- }
- bool CImgBrd:: SelectDevice( PvString &aIP )
- {
- /*
- const PvDeviceInfo *lDeviceInfo = NULL;
- PvResult lResult;
- #ifdef PV_GUI_NOT_AVAILABLE
- PvSystem lSystem;
- lDeviceInfo = PvSelectDevice( lSystem );
- if( lDeviceInfo == NULL )
- {
- cout << "No device selected." << endl;
- return false;
- }
- #else
- PvDeviceFinderWnd lDeviceFinderWnd;
- lResult = lDeviceFinderWnd.ShowModal();
- if( !lResult.IsOK() )
- {
- cout << "No device selected." << endl;
- return false;
- }
- lDeviceInfo = lDeviceFinderWnd.GetSelected();
- #endif // PV_GUI_NOT_AVAILABLE
- if( lDeviceInfo == NULL )
- {
- cout << "No device selected." << endl;
- return false;
- }
- // This sample only supports GigE Vision devices
- if ( lDeviceInfo->GetType() != PvDeviceInfoTypeGEV )
- {
- cout << "This sample only supports GigE Vision devices." << endl;
- return NULL;
- }
- aIP = lDeviceInfo->GetConnectionID();
- */
- return true;
- }
- bool CImgBrd::SaveImage(const WORD* pRawImg, const char* pImgName,int nWidth, int nHeight)
- {
- /*LogInfo("Begin to Save Raw Image" );
- LogInfo(pImgName);*/
- CString str;
- /*str.Format("with(%d) hight(%d)",nWidth,nHeight);
- LogInfo(str);*/
- if ( pRawImg == NULL || pImgName == NULL )
- {
- LogInfo("image buffer is null");
- return false;
- }
- CString strTemp;
- strTemp = /*m_strRawImgPath + "\\" + */ pImgName;
- CFile file;
- if(!file.Open(strTemp, CFile::modeCreate | CFile::modeWrite ))
- {
- LogInfo("Open file failed");
- return false;
- }
- else
- {
- file.Write(pRawImg, nWidth*nHeight*sizeof(WORD));
- }
- file.Close();
- //LogInfo("End to Save Raw Image");
- return true;
- }
- UINT CImgBrd::IniGrabThread()
- {
- if ( m_hEventGrab != NULL )
- CloseHandle( m_hEventGrab );
- m_hEventGrab = CreateEvent( NULL, FALSE, FALSE, NULL );
- if ( m_hEventExitGrapThd != NULL )
- CloseHandle( m_hEventExitGrapThd );
- m_hEventExitGrapThd = CreateEvent( NULL, FALSE, FALSE, NULL );
-
- UINT uRtn = 1;
- if ( m_pGrapThdPro == NULL )
- {
- m_pGrapThdPro = ::AfxBeginThread( GrabThread, this );
- if (m_pGrapThdPro == NULL)
- {
- LogInfo( "create GrabThread failed" );
- uRtn = 0;
- }
- }
- LogInfo( "create GrabThread success" );
- return uRtn;
- }
- UINT CImgBrd::ExitGrabThread()
- {
- UINT uRtn = 1;
- BOOL bKillThrd = false;
- m_bExitGrapThdPro = true;
- SetEvent(m_hEventGrab);
- DWORD wRtn = WaitForSingleObject(m_hEventExitGrapThd,5000);
- switch(wRtn)
- {
- case WAIT_TIMEOUT:
- LogInfo("Exit Grab thread time out");
- bKillThrd = TerminateThread(m_pGrapThdPro->m_hThread,0);
- break;
- case WAIT_OBJECT_0:
- LogInfo("Exit grab thread success");
- break;
- default:
- LogInfo("Exit Grab thread unknown default");
- bKillThrd = TerminateThread(m_pGrapThdPro->m_hThread,0);
- break;
- }
- if ( m_hEventExitGrapThd != NULL )
- {
- CloseHandle( m_hEventExitGrapThd );
- m_hEventExitGrapThd = NULL;
- }
- m_pGrapThdPro = NULL;
- return uRtn;
- }
- UINT CImgBrd:: GrabThread( LPVOID pParam )
- {
- DWORD dTimeOut=55;
- bool bExit = false;
- CString str;
- int icount =0;
- bool bRtn =false;
- CImgBrd* pImgBd = reinterpret_cast<CImgBrd*>(pParam);
- DWORD dwRet;
- SetThreadPriority(GetCurrentThread(),THREAD_PRIORITY_TIME_CRITICAL);
- while ((dwRet = WaitForSingleObject(pImgBd->m_hEventGrab, 0)) != WAIT_OBJECT_0)
- {
- pImgBd->StartSlave();
- }
- if (pImgBd->m_hEventExitGrapThd)
- {
- pImgBd->LogInfo("Thread is closed,set event exit grab thread");
- SetEvent(pImgBd->m_hEventExitGrapThd);
- }
- return true;
- }
- bool CImgBrd::LogInfo( LPCSTR strLog )
- {
- if (m_pLog == NULL)
- {
- return false;
- }
- else
- {
- m_pLog->WriteLog(strLog,LOG_INFORMATION,LOG_RELEASE,true);
- return true;
- }
- }
- bool CImgBrd::LogError( LPCSTR strLog )
- {
- if (m_pLog == NULL)
- {
- return false;
- }
- else
- {
- m_pLog->WriteLog(strLog,LOG_ERROR,LOG_RELEASE,true);
- return true;
- }
- }
- bool CImgBrd::LogWarn( LPCSTR strLog )
- {
- if (m_pLog == NULL)
- {
- return false;
- }
- else
- {
- m_pLog->WriteLog(strLog,LOG_WARNING,LOG_RELEASE,true);
- return true;
- }
- }
|