This source file includes following definitions.
- wrprInitGigEVisionAPI
- wrprExitGigEVisionAPI
- wrprGetGigEVisionAPI
- wrprUnregisterCallback
- wrprDeviceIsConnect
- wrprDeviceIsValid
- wrprDeviceDisconnect
- getCaptureDomain
- init
- close
- open
- grabImage
- start
- stop
- grabFrame
- retrieveFrame
- getProperty
- setProperty
- cvCreateCameraCapture_Giganetix
#include "precomp.hpp"
#include <GigEVisionSDK.h>
#include <GigEVisionSDK.cpp>
#ifdef WIN32
#include <io.h>
#else
#include <stdio.h>
#endif
#ifdef NDEBUG
#define CV_WARN(message)
#else
#define CV_WARN(message) fprintf(stderr, "warning: %s (%s:%d)\n", message, __FILE__, __LINE__)
#endif
#define QTGIG_HEARTBEAT_TIME (12000.0)
#define QTGIG_MAX_WAIT_TIME (2.0)
#define QTGIG_IMG_WAIT_TIME (3.0)
bool
wrprInitGigEVisionAPI()
{
CV_FUNCNAME("wrprInitGigEVisionAPI");
__BEGIN__;
try {
gige::InitGigEVisionAPI ();
} catch(...) {
CV_ERROR(CV_StsError, "GigEVisionAPI: initialization (InitGigEVisionAPI()) failed.\n");
}
__END__;
return true;
}
bool
wrprExitGigEVisionAPI()
{
CV_FUNCNAME("wrprExitGigEVisionAPI");
__BEGIN__;
try {
gige::ExitGigEVisionAPI ();
} catch(...) {
CV_ERROR(CV_StsError, "GigEVisionAPI: finalization (ExitGigEVisionAPI()) failed.\n");
return false;
}
__END__;
return true;
}
gige::IGigEVisionAPI
wrprGetGigEVisionAPI()
{
gige::IGigEVisionAPI b_ret = 0;
CV_FUNCNAME("wrprGetGigEVisionAPI");
__BEGIN__;
try {
b_ret = gige::GetGigEVisionAPI ();
} catch(...) {
CV_ERROR(CV_StsError, "GigEVisionAPI: API instance (from GetGigEVisionAPI()) failed.\n");
}
__END__;
return b_ret;
}
bool
wrprUnregisterCallback( const gige::IGigEVisionAPI* api, gige::ICallbackEvent* eventHandler)
{
bool b_ret = api != NULL;
if(b_ret) b_ret = api->IsValid ();
CV_FUNCNAME("wrprUnregisterCallback");
__BEGIN__;
if(b_ret)
{
if(eventHandler != NULL)
{
try {
b_ret = ((gige::IGigEVisionAPIInterface*)api)->UnregisterCallback (eventHandler);
} catch(...) {
CV_ERROR(CV_StsError, "GigEVisionAPI: API unregister callback function (from UnregisterCallback()) failed.\n");
b_ret = false;
}
}
}
__END__;
return (b_ret);
}
bool
wrprDeviceIsConnect( gige::IDevice& device )
{
bool b_ret = device != NULL;
CV_FUNCNAME("wrprDeviceIsConnect");
__BEGIN__;
if(b_ret)
{
try {
b_ret = device->IsConnected ();
} catch (...) {
CV_ERROR(CV_StsError, "GigEVisionAPI: API device connection state (from IsConnected()) failed.\n");
b_ret = false;
}
}
__END__;
return (b_ret);
}
bool
wrprDeviceIsValid( gige::IDevice& device )
{
bool b_ret = device != NULL;
CV_FUNCNAME("wrprDeviceIsConnect");
__BEGIN__;
if(b_ret)
{
try {
b_ret = device.IsValid ();
} catch (...) {
CV_ERROR(CV_StsError, "GigEVisionAPI: API device validation state (from IsValid()) failed.\n");
b_ret = false;
}
}
__END__;
return (b_ret);
}
bool
wrprDeviceDisconnect ( gige::IDevice& device )
{
bool b_ret = device != NULL;
CV_FUNCNAME("wrprDeviceDisconnect");
__BEGIN__;
if(b_ret)
{
try {
device->Disconnect ();
} catch (...) {
CV_ERROR(CV_StsError, "GigEVisionAPI: API device disconnect (from Disconnect()) failed.\n");
b_ret = false;
}
}
__END__;
return (b_ret);
}
class CvCaptureCAM_Giganetix : public CvCapture
{
public:
CvCaptureCAM_Giganetix();
virtual ~CvCaptureCAM_Giganetix();
virtual bool open( int index );
virtual void close();
virtual double getProperty(int) const;
virtual bool setProperty(int, double);
virtual bool grabFrame();
virtual IplImage* retrieveFrame(int);
virtual int getCaptureDomain()
{
return CV_CAP_GIGANETIX;
}
bool start ();
bool stop ();
protected:
void init ();
void grabImage ();
gige::IGigEVisionAPI m_api;
bool m_api_on;
gige::IDevice m_device;
bool m_active;
IplImage* m_raw_image;
UINT32 m_rawImagePixelType;
bool m_monocrome;
};
void
CvCaptureCAM_Giganetix::init ()
{
m_monocrome = m_active = m_api_on = false;
m_api = 0;
m_device = 0;
m_raw_image = 0;
m_rawImagePixelType = 0;
}
CvCaptureCAM_Giganetix::CvCaptureCAM_Giganetix()
{
init ();
m_api_on = wrprInitGigEVisionAPI ();
if(m_api_on)
{
if((m_api = wrprGetGigEVisionAPI ()) != NULL)
{
m_api->SetHeartbeatTime (QTGIG_HEARTBEAT_TIME);
}
}
}
CvCaptureCAM_Giganetix::~CvCaptureCAM_Giganetix()
{
close();
}
void
CvCaptureCAM_Giganetix::close()
{
stop ();
(void)wrprDeviceDisconnect(m_device);
(void)wrprExitGigEVisionAPI ();
if(m_raw_image) cvReleaseImageHeader(&m_raw_image);
init ();
}
bool
CvCaptureCAM_Giganetix::open( int index )
{
bool b_ret = m_api_on;
CV_FUNCNAME("CvCaptureCAM_Giganetix::open");
__BEGIN__;
if(b_ret)
b_ret = m_api.IsValid ();
if(b_ret )
{
m_api->FindAllDevices (QTGIG_MAX_WAIT_TIME);
gige::DevicesList DevicesList = m_api->GetAllDevices ();
m_device = 0;
b_ret = false;
for (int i = 0; i < (int) DevicesList.size() && !b_ret; i++)
{
b_ret = (i == index);
if(b_ret)
{
m_device = DevicesList[i];
b_ret = m_device->Connect ();
if(b_ret)
{
b_ret =
m_device->SetStringNodeValue("AcquisitionStatusSelector", "AcquisitionActive")
&&
m_device->SetStringNodeValue ("TriggerMode", "Off")
&&
m_device->SetStringNodeValue ("AcquisitionMode", "Continuous")
&&
m_device->SetIntegerNodeValue ("AcquisitionFrameCount", 20)
;
}
}
}
}
if(!b_ret)
{
CV_ERROR(CV_StsError, "Giganetix: Error cannot find camera\n");
close ();
} else {
start ();
}
__END__;
return b_ret;
}
void
CvCaptureCAM_Giganetix::grabImage ()
{
CV_FUNCNAME("CvCaptureCAM_Giganetix::grabImage");
__BEGIN__;
if(wrprDeviceIsValid(m_device) && wrprDeviceIsConnect(m_device))
{
if(!m_device->IsBufferEmpty ())
{
gige::IImageInfo imageInfo;
m_device->GetImageInfo (&imageInfo);
assert(imageInfo.IsValid());
if (m_device->GetPendingImagesCount() == 1)
{
UINT32 newPixelType;
UINT32 newWidth, newHeight;
imageInfo->GetPixelType(newPixelType);
imageInfo->GetSize(newWidth, newHeight);
bool b_validation = m_raw_image != NULL;
if(b_validation)
{
b_validation =
m_raw_image->imageSize == (int)(imageInfo->GetRawDataSize ())
&&
m_rawImagePixelType == newPixelType;
} else {
if(m_raw_image) cvReleaseImageHeader(&m_raw_image);
}
m_rawImagePixelType = newPixelType;
m_monocrome = GvspGetBitsPerPixel((GVSP_PIXEL_TYPES)newPixelType) == IPL_DEPTH_8U;
try {
if (m_monocrome)
{
if (!b_validation)
{
m_raw_image = cvCreateImageHeader (cvSize((int)newWidth, (int)newHeight),IPL_DEPTH_8U,1);
m_raw_image->origin = IPL_ORIGIN_TL;
m_raw_image->dataOrder = IPL_DATA_ORDER_PIXEL;
m_raw_image->widthStep = newWidth;
}
m_raw_image->imageData = (char*)(imageInfo->GetRawData ());
}
if (!m_monocrome && newPixelType == GVSP_PIX_RGB8_PACKED)
{
if (!b_validation)
{
m_raw_image = cvCreateImageHeader (cvSize((int)newWidth, (int)newHeight), IPL_DEPTH_32F, 3);
m_raw_image->origin = IPL_ORIGIN_TL;
m_raw_image->dataOrder = IPL_DATA_ORDER_PIXEL;
m_raw_image->widthStep = newWidth * 3;
}
m_raw_image->imageData = (char*)(imageInfo->GetRawData ());
}
} catch (...) {
CV_ERROR(CV_StsError, "Giganetix: failed to queue a buffer on device\n");
close ();
}
} else {
m_raw_image = 0;
CV_WARN("Giganetix: Undefined image pixel type\n");
}
m_device->PopImage (imageInfo);
m_device->ClearImageBuffer ();
}
}
__END__;
}
bool
CvCaptureCAM_Giganetix::start ()
{
CV_FUNCNAME("CvCaptureCAM_Giganetix::start");
__BEGIN__;
m_active = wrprDeviceIsValid(m_device) && wrprDeviceIsConnect(m_device);
if(m_active)
{
(void)m_device->SetIntegerNodeValue("TLParamsLocked", 1);
(void)m_device->CommandNodeExecute("AcquisitionStart");
m_active = m_device->GetBooleanNodeValue("AcquisitionStatus", m_active);
}
if(!m_active)
{
CV_ERROR(CV_StsError, "Giganetix: Cannot open camera\n");
close ();
}
__END__;
return m_active;
}
bool
CvCaptureCAM_Giganetix::stop ()
{
if (!m_active) return true;
CV_FUNCNAME("CvCaptureCAM_Giganetix::stop");
__BEGIN__;
if(wrprDeviceIsValid(m_device) && wrprDeviceIsConnect(m_device))
{
(void)m_device->GetBooleanNodeValue("AcquisitionStatus", m_active);
if(m_active)
{
(void)m_device->CommandNodeExecute("AcquisitionStop");
(void)m_device->SetIntegerNodeValue("TLParamsLocked", 0);
m_device->ClearImageBuffer ();
(void)m_device->GetBooleanNodeValue("AcquisitionStatus", m_active);
}
}
if(m_active)
{
CV_ERROR(CV_StsError, "Giganetix: Improper closure of the camera\n");
close ();
}
__END__;
return !m_active;
}
bool
CvCaptureCAM_Giganetix::grabFrame()
{
bool b_ret =
wrprDeviceIsValid(m_device)
&&
wrprDeviceIsConnect(m_device);
if(b_ret) grabImage ();
return b_ret;
}
IplImage*
CvCaptureCAM_Giganetix::retrieveFrame(int)
{
return (
wrprDeviceIsValid(m_device) && wrprDeviceIsConnect(m_device) ?
m_raw_image :
NULL
);
}
double
CvCaptureCAM_Giganetix::getProperty( int property_id ) const
{
double d_ret = -1.0;
INT64 i;
if(wrprDeviceIsConnect(m_device))
{
switch ( property_id )
{
case CV_CAP_PROP_FRAME_WIDTH:
m_device->GetIntegerNodeValue ("Width", i);
d_ret = i;
break;
case CV_CAP_PROP_FRAME_HEIGHT:
m_device->GetIntegerNodeValue ("Height", i);
d_ret = i;
break;
case CV_CAP_PROP_GIGA_FRAME_OFFSET_X:
m_device->GetIntegerNodeValue ("OffsetX", i);
d_ret = i;
break;
case CV_CAP_PROP_GIGA_FRAME_OFFSET_Y:
m_device->GetIntegerNodeValue ("OffsetY", i);
d_ret = i;
break;
case CV_CAP_PROP_GIGA_FRAME_WIDTH_MAX:
m_device->GetIntegerNodeValue ("WidthMax", i);
d_ret = i;
break;
case CV_CAP_PROP_GIGA_FRAME_HEIGH_MAX:
m_device->GetIntegerNodeValue ("HeightMax", i);
d_ret = i;
break;
case CV_CAP_PROP_GIGA_FRAME_SENS_WIDTH:
m_device->GetIntegerNodeValue ("SensorWidth", i);
d_ret = i;
break;
case CV_CAP_PROP_GIGA_FRAME_SENS_HEIGH:
m_device->GetIntegerNodeValue ("SensorHeight", i);
d_ret = i;
break;
case CV_CAP_PROP_FRAME_COUNT:
m_device->GetIntegerNodeValue ("AcquisitionFrameCount", i);
d_ret = i;
break;
case CV_CAP_PROP_EXPOSURE:
m_device->GetFloatNodeValue ("ExposureTime",d_ret);
break;
case CV_CAP_PROP_GAIN :
m_device->GetFloatNodeValue ("Gain",d_ret);
break;
case CV_CAP_PROP_TRIGGER :
bool b;
m_device->GetBooleanNodeValue ("TriggerMode",b);
d_ret = (double)b;
break;
case CV_CAP_PROP_TRIGGER_DELAY :
m_device->GetFloatNodeValue ("TriggerDelay",d_ret);
break;
default : ;
}
}
return d_ret;
}
bool
CvCaptureCAM_Giganetix::setProperty( int property_id, double value )
{
bool b_ret = wrprDeviceIsConnect(m_device);
if(b_ret)
{
bool b_val = m_active;
switch ( property_id )
{
case CV_CAP_PROP_FRAME_WIDTH:
stop ();
b_ret = m_device->SetIntegerNodeValue ("Width", (INT64)value);
if(b_val) start ();
break;
case CV_CAP_PROP_GIGA_FRAME_WIDTH_MAX:
stop ();
b_ret = m_device->SetIntegerNodeValue ("WidthMax", (INT64)value);
if(b_val) start ();
break;
case CV_CAP_PROP_GIGA_FRAME_SENS_WIDTH:
stop ();
b_ret = m_device->SetIntegerNodeValue ("SensorWidth", (INT64)value);
if(b_val) start ();
break;
case CV_CAP_PROP_FRAME_HEIGHT:
stop ();
b_ret = m_device->SetIntegerNodeValue ("Height", (INT64)value);
if(b_val) start ();
break;
case CV_CAP_PROP_GIGA_FRAME_HEIGH_MAX:
stop ();
b_ret = m_device->SetIntegerNodeValue ("HeightMax", (INT64)value);
if(b_val) start ();
break;
case CV_CAP_PROP_GIGA_FRAME_SENS_HEIGH:
stop ();
b_ret = m_device->SetIntegerNodeValue ("SensorHeight", (INT64)value);
if(b_val) start ();
break;
case CV_CAP_PROP_GIGA_FRAME_OFFSET_X: {
INT64 w, wmax, val = (INT64)value;
if((b_ret = m_device->GetIntegerNodeValue ("Width", w)))
if((b_ret = m_device->GetIntegerNodeValue ("WidthMax", wmax)))
b_ret = m_device->SetIntegerNodeValue ("OffsetX", (val + w) > wmax ? (wmax - w) : val);
} break;
case CV_CAP_PROP_GIGA_FRAME_OFFSET_Y: {
INT64 h, hmax, val = (INT64)value;
if((b_ret = m_device->GetIntegerNodeValue ("Height", h)))
if((b_ret = m_device->GetIntegerNodeValue ("HeightMax", hmax)))
b_ret = m_device->SetIntegerNodeValue ("OffsetY", (val + h) > hmax ? (hmax - h) : val);
b_ret = m_device->SetIntegerNodeValue ("OffsetY", (INT64)value);
}
break;
case CV_CAP_PROP_EXPOSURE:
b_ret = m_device->SetFloatNodeValue ("ExposureTime",value);
break;
case CV_CAP_PROP_GAIN :
b_ret = m_device->SetFloatNodeValue ("Gain",value);
break;
case CV_CAP_PROP_TRIGGER :
b_ret = m_device->SetBooleanNodeValue ("TriggerMode",(bool)value);
break;
case CV_CAP_PROP_TRIGGER_DELAY :
stop ();
b_ret = m_device->SetFloatNodeValue ("TriggerDelay",value);
if(b_val) start ();
break;
default:
b_ret = false;
}
}
return b_ret;
}
CvCapture*
cvCreateCameraCapture_Giganetix( int index )
{
CvCaptureCAM_Giganetix* capture = new CvCaptureCAM_Giganetix;
if (!(capture->open( index )))
{
delete capture;
capture = NULL;
}
return ((CvCapture*)capture);
}