--- /modules/highgui/src/cap_pvapi.cpp	(original) Fri May 25 09:19:40 2012
+++ /modules/highgui/src/cap_pvapi.cpp	(modified) Fri May 25 09:18:41 2012
@@ -57,7 +57,11 @@
 #endif
 
 #include <PvApi.h>
+#ifdef WIN32
+#include <io.h>
+#elif
 #include <unistd.h>
+#endif
 #include <string>
 //#include <arpa/inet.h>
 
@@ -69,7 +73,7 @@
 {
 public:
     CvCaptureCAM_PvAPI();
-    virtual ~CvCaptureCAM_PvAPI() 
+    virtual ~CvCaptureCAM_PvAPI()
     {
         close();
     }
@@ -86,46 +90,47 @@
     }
 
 protected:
-	virtual void Sleep(unsigned int time);
+    #ifndef WIN32
+    virtual void Sleep(unsigned int time);
+    #endif
 
     typedef struct
     {
-        unsigned long   UID;
-        tPvHandle       Handle;
-        tPvFrame        Frame;
-
-
+        unsigned long UID;
+        tPvHandle     Handle;
+        tPvFrame      Frame;
     } tCamera;
+
     IplImage *frame;
     IplImage *grayframe;
-    tCamera Camera;
-    tPvErr          Errcode;
-    bool monocrome;
-
-
+    tCamera  Camera;
+    tPvErr   Errcode;
+    bool     monocrome;
 };
 
 
 CvCaptureCAM_PvAPI::CvCaptureCAM_PvAPI()
 {
-	monocrome=false;
+    monocrome = false;
     memset(&this->Camera, 0, sizeof(this->Camera));
 }
+#ifndef WIN32
 void CvCaptureCAM_PvAPI::Sleep(unsigned int time)
 {
     struct timespec t,r;
     
     t.tv_sec    = time / 1000;
-    t.tv_nsec   = (time % 1000) * 1000000;    
+    t.tv_nsec   = (time % 1000) * 1000000;
     
     while(nanosleep(&t,&r)==-1)
         t = r;
 }
+#endif
 void CvCaptureCAM_PvAPI::close()
 {
-	// Stop the acquisition & free the camera
-	PvCommandRun(Camera.Handle, "AcquisitionStop");
-	PvCaptureEnd(Camera.Handle);
+    // Stop the acquisition & free the camera
+    PvCommandRun(Camera.Handle, "AcquisitionStop");
+    PvCaptureEnd(Camera.Handle);
     PvCameraClose(Camera.Handle);
     PvUnInitialize();
 }
@@ -134,46 +139,51 @@
 bool CvCaptureCAM_PvAPI::open( int index )
 {
     tPvCameraInfo cameraList[MAX_CAMERAS];
-    
-    tPvCameraInfo  camInfo;
+
+    tPvCameraInfo camInfo;
     tPvIpSettings ipSettings;
 
- 
-    if (PvInitialize()) {
+
+    if (PvInitialize())
+    {
+    }
+
+    // One second sleep is too long and not flexible enought
+    int timercounter=0;
+
+    while(!(PvCameraCount() > 1||timercounter > 25))
+    {
+       Sleep(25);
+       timercounter = timercounter + 1;
     }
-        //return false;
-        
-    Sleep(1000);
 
-    //close();
-    
     int numCameras=PvCameraList(cameraList, MAX_CAMERAS, NULL);
 
     if (numCameras <= 0 || index >= numCameras)
         return false;
 
-    Camera.UID = cameraList[index].UniqueId;    
-
-    if (!PvCameraInfo(Camera.UID,&camInfo) && !PvCameraIpSettingsGet(Camera.UID,&ipSettings)) {
-		/*
-		struct in_addr addr;
-		addr.s_addr = ipSettings.CurrentIpAddress;
-		printf("Current address:\t%s\n",inet_ntoa(addr));
-		addr.s_addr = ipSettings.CurrentIpSubnet;
-		printf("Current subnet:\t\t%s\n",inet_ntoa(addr));
-		addr.s_addr = ipSettings.CurrentIpGateway;
-		printf("Current gateway:\t%s\n",inet_ntoa(addr));
-		*/
-	}	
-	else {
-		fprintf(stderr,"ERROR: could not retrieve camera IP settings.\n");
-		return false;
-	}	
+    Camera.UID = cameraList[index].UniqueId;
 
+    if (!PvCameraInfo(Camera.UID,&camInfo) && !PvCameraIpSettingsGet(Camera.UID,&ipSettings))
+    {
+        /*
+        struct in_addr addr;
+        addr.s_addr = ipSettings.CurrentIpAddress;
+        printf("Current address:\t%s\n",inet_ntoa(addr));
+        addr.s_addr = ipSettings.CurrentIpSubnet;
+        printf("Current subnet:\t\t%s\n",inet_ntoa(addr));
+        addr.s_addr = ipSettings.CurrentIpGateway;
+        printf("Current gateway:\t%s\n",inet_ntoa(addr));
+        */
+    }
+    else
+    {
+        fprintf(stderr,"ERROR: could not retrieve camera IP settings.\n");
+        return false;
+    }
 
     if (PvCameraOpen(Camera.UID, ePvAccessMaster, &(Camera.Handle))==ePvErrSuccess)
     {
-    
         //Set Pixel Format to BRG24 to follow conventions 
         /*Errcode = PvAttrEnumSet(Camera.Handle, "PixelFormat", "Bgr24");
         if (Errcode != ePvErrSuccess)
@@ -184,7 +194,7 @@
         */
         tPvUint32 frameWidth, frameHeight, frameSize;
         unsigned long maxSize;
-		char pixelFormat[256];
+        char pixelFormat[256];
         PvAttrUint32Get(Camera.Handle, "TotalBytesPerFrame", &frameSize);
         PvAttrUint32Get(Camera.Handle, "Width", &frameWidth);
         PvAttrUint32Get(Camera.Handle, "Height", &frameHeight);
@@ -192,31 +202,34 @@
         maxSize = 8228;
         //PvAttrUint32Get(Camera.Handle,"PacketSize",&maxSize);
         if (PvCaptureAdjustPacketSize(Camera.Handle,maxSize)!=ePvErrSuccess)
-			return false;
-        if (strcmp(pixelFormat, "Mono8")==0) {
-				grayframe = cvCreateImage(cvSize((int)frameWidth, (int)frameHeight), IPL_DEPTH_8U, 1);
-				grayframe->widthStep = (int)frameWidth;
-				frame = cvCreateImage(cvSize((int)frameWidth, (int)frameHeight), IPL_DEPTH_8U, 3);
-				frame->widthStep = (int)frameWidth*3;		 
-				Camera.Frame.ImageBufferSize = frameSize;
-				Camera.Frame.ImageBuffer = grayframe->imageData;   
-		}	    
-		else if (strcmp(pixelFormat, "Mono16")==0) {
-				grayframe = cvCreateImage(cvSize((int)frameWidth, (int)frameHeight), IPL_DEPTH_16U, 1);
-				grayframe->widthStep = (int)frameWidth;	
-				frame = cvCreateImage(cvSize((int)frameWidth, (int)frameHeight), IPL_DEPTH_16U, 3);
-				frame->widthStep = (int)frameWidth*3;
-				Camera.Frame.ImageBufferSize = frameSize;
-				Camera.Frame.ImageBuffer = grayframe->imageData;
-		}	  
-		else if	(strcmp(pixelFormat, "Bgr24")==0) {
-				frame = cvCreateImage(cvSize((int)frameWidth, (int)frameHeight), IPL_DEPTH_8U, 3);
-				frame->widthStep = (int)frameWidth*3;
-				Camera.Frame.ImageBufferSize = frameSize;
-				Camera.Frame.ImageBuffer = frame->imageData;
-		}		
-		else
-				return false;
+            return false;
+        if (strcmp(pixelFormat, "Mono8")==0)
+        {
+            grayframe = cvCreateImage(cvSize((int)frameWidth, (int)frameHeight), IPL_DEPTH_8U, 1);
+            grayframe->widthStep = (int)frameWidth;
+            frame = cvCreateImage(cvSize((int)frameWidth, (int)frameHeight), IPL_DEPTH_8U, 3);
+            frame->widthStep = (int)frameWidth*3;
+            Camera.Frame.ImageBufferSize = frameSize;
+            Camera.Frame.ImageBuffer = grayframe->imageData;
+        }
+        else if (strcmp(pixelFormat, "Mono16")==0)
+        {
+            grayframe = cvCreateImage(cvSize((int)frameWidth, (int)frameHeight), IPL_DEPTH_16U, 1);
+            grayframe->widthStep = (int)frameWidth;
+            frame = cvCreateImage(cvSize((int)frameWidth, (int)frameHeight), IPL_DEPTH_16U, 3);
+            frame->widthStep = (int)frameWidth*3;
+            Camera.Frame.ImageBufferSize = frameSize;
+            Camera.Frame.ImageBuffer = grayframe->imageData;
+        }
+        else if (strcmp(pixelFormat, "Bgr24")==0)
+        {
+            frame = cvCreateImage(cvSize((int)frameWidth, (int)frameHeight), IPL_DEPTH_8U, 3);
+            frame->widthStep = (int)frameWidth*3;
+            Camera.Frame.ImageBufferSize = frameSize;
+            Camera.Frame.ImageBuffer = frame->imageData;
+        }
+        else
+           return false;
         // Start the camera
         PvCaptureStart(Camera.Handle);
 
@@ -226,44 +239,40 @@
             fprintf(stderr,"Could not set Prosilica Acquisition Mode\n");
             return false;
         }
-        
         if(PvCommandRun(Camera.Handle, "AcquisitionStart")!= ePvErrSuccess)
         {
             fprintf(stderr,"Could not start Prosilica acquisition\n");
             return false;
         }
-        
         if(PvAttrEnumSet(Camera.Handle, "FrameStartTriggerMode", "Freerun")!= ePvErrSuccess)
         {
             fprintf(stderr,"Error setting Prosilica trigger to \"Freerun\"");
             return false;
         }
-        
         return true;
     }
     fprintf(stderr,"Error cannot open camera\n");
     return false;
-
 }
 
 bool CvCaptureCAM_PvAPI::grabFrame()
 {
-	//if(Camera.Frame.Status != ePvErrUnplugged && Camera.Frame.Status != ePvErrCancelled)
-	return PvCaptureQueueFrame(Camera.Handle, &(Camera.Frame), NULL) == ePvErrSuccess;
+    //if(Camera.Frame.Status != ePvErrUnplugged && Camera.Frame.Status != ePvErrCancelled)
+    return PvCaptureQueueFrame(Camera.Handle, &(Camera.Frame), NULL) == ePvErrSuccess;
 }
 
-
 IplImage* CvCaptureCAM_PvAPI::retrieveFrame(int)
 {
-
-    if (PvCaptureWaitForFrameDone(Camera.Handle, &(Camera.Frame), 1000) == ePvErrSuccess) {
-		if (!monocrome) {
-			cvMerge(grayframe,grayframe,grayframe,NULL,frame); 
-			return frame;	
-		}	
-		return grayframe;
-    }		
-    else return NULL;		
+    if (PvCaptureWaitForFrameDone(Camera.Handle, &(Camera.Frame), 1000) == ePvErrSuccess)
+    {
+        if (!monocrome)
+        {
+            cvMerge(grayframe,grayframe,grayframe,NULL,frame);
+            return frame;
+        }
+        return grayframe;
+    }
+    else return NULL;
 }
 
 double CvCaptureCAM_PvAPI::getProperty( int property_id )
@@ -279,26 +288,42 @@
         PvAttrUint32Get(Camera.Handle, "Height", &nTemp);
         return (double)nTemp;
     case CV_CAP_PROP_EXPOSURE:
-	PvAttrUint32Get(Camera.Handle,"ExposureValue",&nTemp);
-	return (double)nTemp;
+        PvAttrUint32Get(Camera.Handle,"ExposureValue",&nTemp);
+        return (double)nTemp;
+    case CV_CAP_PROP_GAIN:
+        PvAttrUint32Get(Camera.Handle, "GainValue", &nTemp);
+        return (double)nTemp;
+    case CV_CAP_PROP_GAINMODE:
+        char gainMode[256];
+        PvAttrEnumGet(Camera.Handle, "GainMode", gainMode, sizeof(gainMode), NULL);
+        if (strcmp(gainMode, "Manual") == 0)
+            return 1;
+        if (strcmp(gainMode, "Auto") == 0)
+            return 2;
+        if (strcmp(gainMode, "AutoOnce") == 0)
+            return 3;
+        else
+            return -1;
     case CV_CAP_PROP_FPS:
-	tPvFloat32 nfTemp;
+        tPvFloat32 nfTemp;
         PvAttrFloat32Get(Camera.Handle, "StatFrameRate", &nfTemp);
         return (double)nfTemp;
     case CV_CAP_PROP_PVAPI_MULTICASTIP:
-	char mEnable[2];
-	char mIp[11];
-	PvAttrEnumGet(Camera.Handle,"MulticastEnable",mEnable,sizeof(mEnable),NULL);
-	if (strcmp(mEnable, "Off") == 0) {
-	    return -1;
-	}
-	else {
-	    long int ip;
-	    int a,b,c,d;
-	    PvAttrStringGet(Camera.Handle, "MulticastIPAddress",mIp,sizeof(mIp),NULL);
-	    sscanf(mIp, "%d.%d.%d.%d", &a, &b, &c, &d); ip = ((a*256 + b)*256 + c)*256 + d;
-	    return (double)ip;
-	}
+        char mEnable[2];
+        char mIp[11];
+        PvAttrEnumGet(Camera.Handle,"MulticastEnable",mEnable,sizeof(mEnable),NULL);
+        if (strcmp(mEnable, "Off") == 0)
+        {
+            return -1;
+        }
+        else
+        {
+            long int ip;
+            int a,b,c,d;
+            PvAttrStringGet(Camera.Handle, "MulticastIPAddress",mIp,sizeof(mIp),NULL);
+            sscanf(mIp, "%d.%d.%d.%d", &a, &b, &c, &d); ip = ((a*256 + b)*256 + c)*256 + d;
+            return (double)ip;
+        }
     }
     return -1.0;
 }
@@ -316,39 +341,62 @@
         break;
     */
     case CV_CAP_PROP_MONOCROME:
-        if (value==1) {
-			char pixelFormat[256];
-			PvAttrEnumGet(Camera.Handle, "PixelFormat", pixelFormat,256,NULL);
-			if ((strcmp(pixelFormat, "Mono8")==0) || strcmp(pixelFormat, "Mono16")==0) {
-				monocrome=true;
-			}	
-			else
-				return false;
-		}
-		else
-			monocrome=false;
-		break;	
+        if (value==1)
+        {
+            char pixelFormat[256];
+            PvAttrEnumGet(Camera.Handle, "PixelFormat", pixelFormat,256,NULL);
+            if ((strcmp(pixelFormat, "Mono8")==0) || strcmp(pixelFormat, "Mono16")==0)
+            {
+                monocrome=true;
+            }
+            else
+                return false;
+        }
+        else
+            monocrome=false;
+        break;
     case CV_CAP_PROP_EXPOSURE:
-	    if ((PvAttrUint32Set(Camera.Handle,"ExposureValue",(tPvUint32)value)==ePvErrSuccess)) 
-		break;
-	    else
-		return false;
-    case CV_CAP_PROP_PVAPI_MULTICASTIP:
-	    
-    	    if (value==-1) {
-		if ((PvAttrEnumSet(Camera.Handle,"MulticastEnable", "Off")==ePvErrSuccess)) 
-		    break;
-		else 
-		    return false;
-	    }
-	    else {
-		std::string ip=cv::format("%d.%d.%d.%d", ((int)value>>24)&255, ((int)value>>16)&255, ((int)value>>8)&255, (int)value&255);
-		if ((PvAttrEnumSet(Camera.Handle,"MulticastEnable", "On")==ePvErrSuccess) &&
-		(PvAttrStringSet(Camera.Handle, "MulticastIPAddress", ip.c_str())==ePvErrSuccess)) 
-		    break;
-		else
-		    return false;
-	    }
+        if ((PvAttrUint32Set(Camera.Handle,"ExposureValue",(tPvUint32)value)==ePvErrSuccess)) 
+            break;
+        else
+            return false;
+    case CV_CAP_PROP_GAIN:
+        if (!PvAttrUint32Set(Camera.Handle,"GainValue",(tPvUint32)value))
+            break;
+        else
+            return false;
+    case CV_CAP_PROP_GAINMODE:
+        if (value==1)
+        {
+            if (!PvAttrEnumSet(Camera.Handle,"GainMode", "Manual"))
+                break;
+            else
+                return false;
+        }
+        else
+        {
+            if ((PvAttrEnumSet(Camera.Handle,"GainMode", "Auto")==ePvErrSuccess))
+                break;
+            else
+                return false;
+        }
+  case CV_CAP_PROP_PVAPI_MULTICASTIP:
+        if (value==-1)
+        {
+            if ((PvAttrEnumSet(Camera.Handle,"MulticastEnable", "Off")==ePvErrSuccess))
+                break;
+            else
+               return false;
+        }
+        else
+        {
+            std::string ip=cv::format("%d.%d.%d.%d", ((int)value>>24)&255, ((int)value>>16)&255, ((int)value>>8)&255, (int)value&255);
+            if ((PvAttrEnumSet(Camera.Handle,"MulticastEnable", "On")==ePvErrSuccess) &&
+               (PvAttrStringSet(Camera.Handle, "MulticastIPAddress", ip.c_str())==ePvErrSuccess))
+                break;
+            else
+                return false;
+        }
     default:
         return false;
     }
