Exemplo n.º 1
0
//=============================================================================
// Main Program
//=============================================================================
int 
main(  )
{
   FlyCaptureContext context;
   FlyCaptureImage fwImage;
   FlyCaptureError error;
   // Create the context.
   error = flycaptureCreateContext( &context );
   _HANDLE_ERROR(error, "flycaptureCreateContext()"  );
   configureExternalTriggerCamera(context);
   //
   // Start camera.  This is done after setting the trigger so that
   // excess images isochronously streamed from the camera don't fill up 
   // the internal buffers.
   //
   //printf( "Starting camera.\n" );
   int width=400;
   int height=300;
   // error = flycaptureStartCustomImage(context,0, (1024-width)/2,(1024-height)/2, width,height,100,FlyCapturePixelFormat::FLYCAPTURE_MONO8 );
	
  error = flycaptureStart( 
      context, 
      FLYCAPTURE_VIDEOMODE_ANY,
      FLYCAPTURE_FRAMERATE_ANY );
   _HANDLE_ERROR(error, "flycaptureStart()" );

   //
   // Set the grab timeout so that calls to flycaptureGrabImage2 will return 
   // after TIMEOUT milliseconds if the trigger hasn't fired.
   //
   error = flycaptureSetGrabTimeoutEx( context, TIMEOUT );
   _HANDLE_ERROR(error, "flycaptureSetGrabTimeoutEx()" );

   printf( "This program will quit after %d images are grabbed.\n", IMAGES );

   for( int iImage = 0; iImage < IMAGES; iImage++ )
   {

#ifdef SOFTWARE_TRIGGER_CAMERA

#ifdef USE_SOFT_ASYNC_TRIGGER
      //
      // Check that the camera actually supports the PGR SOFT_ASYNC_TRIGGER
      // method of software triggering
      //
      error = checkSoftwareTriggerPresence( context, SOFT_ASYNC_TRIGGER );
      if( error == FLYCAPTURE_OK )
      {
         checkTriggerReady( context );
         
         //
         // Camera is now ready to be triggered, so generate software trigger
         // by writing a '0' to bit 31
         //
         printf( "Press the Enter key to initiate a software trigger.\n" );
         getchar();
         error = flycaptureSetCameraRegister( 
            context, SOFT_ASYNC_TRIGGER, 0x80000000 );
         _HANDLE_ERROR( "flycaptureSetCameraRegister()", error );
      }
      else
      {
         printf( "SOFT_ASYNC_TRIGGER not implemented! Grab will timeout.\n" );
      }
#else
      //
      // Check that the camera actually supports the DCAM SOFTWARE_TRIGGER
      // method of software triggering    
      //
      error = checkSoftwareTriggerPresence( context, SOFTWARE_TRIGGER );
      if( error == FLYCAPTURE_OK )
      {
         error = checkTriggerReady( context );
         
         //
         // Camera is now ready to be triggered, so generate software trigger
         // by writing a '1' to bit 0
         //
         printf( "Press the Enter key to initiate a software trigger.\n" );
         getchar();
         error = flycaptureSetCameraRegister( 
            context, SOFTWARE_TRIGGER, 0x80000000 );
         _HANDLE_ERROR( "flycaptureSetCameraRegister()", error );
      }
      else
      {
         printf( "SOFTWARE_TRIGGER not implemented! Grab will timeout.\n" );
      }
#endif

#endif
      //
      // Do an image grab.  This call will block until the camera
      // is externally triggered.
      //
      error = flycaptureGrabImage2( context, &fwImage );
      if( error == FLYCAPTURE_TIMEOUT )
      {
     printf( "Grab #%d timed out after %d milliseconds.\n", iImage, TIMEOUT );
      }
      else if( error != FLYCAPTURE_OK )
      {
     _HANDLE_ERROR(error, "flycaptureGrabImage2()" );
      }      
      else
      {     
		  if(iImage%60==0)printf( "Grab %d !\n", iImage );     
      }
   }   

   //
   // Stop the camera and destroy the context.
   //
   flycaptureStop( context );
   flycaptureDestroyContext( context );

   printf( "Done!  (hit enter)" );
   getchar();

   return 0;
}
Exemplo n.º 2
0
void configureExternalTriggerCamera(FlyCaptureContext  context){
	FlyCaptureError    error;
   FlyCaptureImage      image;

   //printf( "Initializing camera.\n" );

      
   //
   // Initialize the first camera on the bus.
   //
   error = flycaptureInitialize( context, 0 );
   _HANDLE_ERROR(error, "flycaptureInitialize()" );

  /* //
   // Reset the camera to default factory settings by asserting bit 0
   //
   error = flycaptureSetCameraRegister( context, INITIALIZE, 0x80000000 );
   _HANDLE_ERROR(error, "flycaptureSetCameraRegister()" );*/

   //
   // Power-up the camera (for cameras that support this feature)
   //
   error = flycaptureSetCameraRegister( context, CAMERA_POWER, 0x80000000 );
   _HANDLE_ERROR(error, "flycaptureSetCameraRegister()" );

   //
   // Determine whether or not the camera supports external trigger mode.
   // If it does, put the camera into external trigger mode and otherwise 
   // exit.
   //
   bool bTriggerPresent;

   error = flycaptureQueryTrigger( 
      context, &bTriggerPresent, NULL, NULL, NULL, NULL, NULL, NULL, NULL );
   _HANDLE_ERROR(error, "flycaptureQueryTrigger()" );

   if( !bTriggerPresent)
   {
      printf("This camera does not support external trigger... exiting\n");
   }

   int   iPolarity;
   int   iSource;
   int   iRawValue;
   int   iMode;

   error = flycaptureGetTrigger( 
      context, NULL, &iPolarity, &iSource, &iRawValue, &iMode, NULL );
   _HANDLE_ERROR(error, "flycaptureGetTrigger()" );

   //printf( "Going into asynchronous Trigger_Mode_0.\n" );
   //
   // Ensure the camera is in Trigger Mode 0 by explicitly setting it, 
   // as the camera could have a different default trigger mode
   //
#ifdef SOFTWARE_TRIGGER_CAMERA
   //
   // We are triggering the camera using the internal software trigger.
   // If we are using the DCAM SOFTWARE_TRIGGER functionality, we must
   // change the Trigger_Source to reflect the Software Trigger ID = 7.
   //
   error = flycaptureSetTrigger( 
      context, true, iPolarity, 7, 0, 0 );
   _HANDLE_ERROR( "flycaptureSetCameraTrigger()", error );
#else
   //
   // We are triggering the camera using an external hardware trigger.
   //
   error = flycaptureSetTrigger( 
      context, true, iPolarity, iSource, 0, 0 );
   _HANDLE_ERROR(error, "flycaptureSetCameraTrigger()" );

#endif

   // 
   // Poll the camera to make sure the camera is actually in trigger mode
   // before we start it (avoids timeouts due to the trigger not being armed)
   //
   checkTriggerReady( context );

   //
   // Start the camera and grab any excess images that are already in the pipe.
   // Although it is extremely rare for spurious images to occur, it is
   // possible for the grab call to return an image that is not a result of a
   // user-generated trigger. To grab excess images, set a zero-length timeout.
   // A value of zero makes the grab call non-blocking.
   //
   //printf( "Checking for any buffered images..." );
   error = flycaptureSetGrabTimeoutEx( context, 0 );
   _HANDLE_ERROR(error, "flycaptureSetGrabTimeoutEx()" );
      
   error = flycaptureStart(
      context, FLYCAPTURE_VIDEOMODE_ANY, FLYCAPTURE_FRAMERATE_ANY );
   _HANDLE_ERROR(error, "flycaptureStart()" );

   //
   // Grab the image immediately whether or not trigger present
   //
   error = flycaptureGrabImage2( context, &image );
   if( error == FLYCAPTURE_OK )
   {
      ;//printf( "buffered image found. Flush successful.\n" );
   }
   else if( error == FLYCAPTURE_TIMEOUT )
   {
      ;//printf( "no flush required! (normal behaviour)\n" );
   }
   else
   {
      _HANDLE_ERROR(error, "flycaptureGrabImage2()" );
   }

   error = flycaptureStop( context );
   _HANDLE_ERROR(error, "flycaptureStop()" );

}
Exemplo n.º 3
0
//=============================================================================
// Main Program
//=============================================================================
int 
main( int /* argc */, char* /* argv[] */ )
{
   FlyCaptureError	error;
   FlyCaptureContext	context;
   FlyCaptureImage      image;

   printf( "Initializing camera.\n" );

   //
   // Create the context.
   //
   error = flycaptureCreateContext( &context );
   HANDLE_ERROR( "flycaptureCreateContext()", error );
      
   //
   // Initialize the first camera on the bus.
   //
   error = flycaptureInitialize( context, 0 );
   HANDLE_ERROR( "flycaptureInitialize()", error );

   //
   // Reset the camera to default factory settings by asserting bit 0
   //
   error = flycaptureSetCameraRegister( context, INITIALIZE, 0x80000000 );
   HANDLE_ERROR( "flycaptureSetCameraRegister()", error );

   //
   // Power-up the camera (for cameras that support this feature)
   //
   error = flycaptureSetCameraRegister( context, CAMERA_POWER, 0x80000000 );
   HANDLE_ERROR( "flycaptureSetCameraRegister()", error );

   //
   // Determine whether or not the camera supports external trigger mode.
   // If it does, put the camera into external trigger mode and otherwise 
   // exit.
   //
   bool bTriggerPresent;

   error = flycaptureQueryTrigger( 
      context, &bTriggerPresent, NULL, NULL, NULL, NULL, NULL, NULL, NULL );
   HANDLE_ERROR( "flycaptureQueryTrigger()", error );

   if( !bTriggerPresent)
   {
      printf("This camera does not support external trigger... exiting\n");
      return 1;
   }

   int   iPolarity;
   int   iSource;
   int   iRawValue;
   int   iMode;

   error = flycaptureGetTrigger( 
      context, NULL, &iPolarity, &iSource, &iRawValue, &iMode, NULL );
   HANDLE_ERROR( "flycaptureGetTrigger()", error );

   printf( "Going into asynchronous Trigger_Mode_0.\n" );
   //
   // Ensure the camera is in Trigger Mode 0 by explicitly setting it, 
   // as the camera could have a different default trigger mode
   //
#ifdef SOFTWARE_TRIGGER_CAMERA
   //
   // We are triggering the camera using the internal software trigger.
   // If we are using the DCAM SOFTWARE_TRIGGER functionality, we must
   // change the Trigger_Source to reflect the Software Trigger ID = 7.
   //
   error = flycaptureSetTrigger( 
      context, true, iPolarity, 7, 0, 0 );
   HANDLE_ERROR( "flycaptureSetCameraTrigger()", error );
#else
   //
   // We are triggering the camera using an external hardware trigger.
   //
   error = flycaptureSetTrigger( 
      context, true, iPolarity, iSource, 0, 0 );
   HANDLE_ERROR( "flycaptureSetCameraTrigger()", error );

#endif

   // 
   // Poll the camera to make sure the camera is actually in trigger mode
   // before we start it (avoids timeouts due to the trigger not being armed)
   //
   checkTriggerReady( context );

   //
   // Start the camera and grab any excess images that are already in the pipe.
   // Although it is extremely rare for spurious images to occur, it is
   // possible for the grab call to return an image that is not a result of a
   // user-generated trigger. To grab excess images, set a zero-length timeout.
   // A value of zero makes the grab call non-blocking.
   //
   printf( "Checking for any buffered images..." );
   error = flycaptureSetGrabTimeoutEx( context, 0 );
   HANDLE_ERROR( "flycaptureSetGrabTimeoutEx()", error );
      
   error = flycaptureStart(
      context, FLYCAPTURE_VIDEOMODE_ANY, FLYCAPTURE_FRAMERATE_ANY );
   HANDLE_ERROR( "flycaptureStart()", error );

   //
   // Grab the image immediately whether or not trigger present
   //
   error = flycaptureGrabImage2( context, &image );
   if( error == FLYCAPTURE_OK )
   {
      printf( "buffered image found. Flush successful.\n" );
   }
   else if( error == FLYCAPTURE_TIMEOUT )
   {
      printf( "no flush required! (normal behaviour)\n" );
   }
   else
   {
      HANDLE_ERROR( "flycaptureGrabImage2()", error );
   }

   error = flycaptureStop( context );
   HANDLE_ERROR( "flycaptureStop()", error );

   //
   // Start camera.  This is done after setting the trigger so that
   // excess images isochronously streamed from the camera don't fill up 
   // the internal buffers.
   //
   printf( "Starting camera.\n" );
   error = flycaptureStart( 
      context, 
      FLYCAPTURE_VIDEOMODE_ANY,
      FLYCAPTURE_FRAMERATE_ANY );
   HANDLE_ERROR( "flycaptureStart()", error );

   //
   // Set the grab timeout so that calls to flycaptureGrabImage2 will return 
   // after TIMEOUT milliseconds if the trigger hasn't fired.
   //
   error = flycaptureSetGrabTimeoutEx( context, TIMEOUT );
   HANDLE_ERROR( "flycaptureSetGrabTimeoutEx()", error );

   printf( "This program will quit after %d images are grabbed.\n", IMAGES );

#ifndef SOFTWARE_TRIGGER_CAMERA
   printf( "Trigger the camera by sending a trigger pulse to GPIO%d.\n", 
      iSource );
#endif

   for( int iImage = 0; iImage < IMAGES; iImage++ )
   {

#ifdef SOFTWARE_TRIGGER_CAMERA

#ifdef USE_SOFT_ASYNC_TRIGGER
      //
      // Check that the camera actually supports the PGR SOFT_ASYNC_TRIGGER
      // method of software triggering
      //
      error = checkSoftwareTriggerPresence( context, SOFT_ASYNC_TRIGGER );
      if( error == FLYCAPTURE_OK )
      {
         checkTriggerReady( context );
         
         //
         // Camera is now ready to be triggered, so generate software trigger
         // by writing a '0' to bit 31
         //
         printf( "Press the Enter key to initiate a software trigger.\n" );
         getchar();
         error = flycaptureSetCameraRegister( 
            context, SOFT_ASYNC_TRIGGER, 0x80000000 );
         HANDLE_ERROR( "flycaptureSetCameraRegister()", error );
      }
      else
      {
         printf( "SOFT_ASYNC_TRIGGER not implemented! Grab will timeout.\n" );
      }
#else
      //
      // Check that the camera actually supports the DCAM SOFTWARE_TRIGGER
      // method of software triggering    
      //
      error = checkSoftwareTriggerPresence( context, SOFTWARE_TRIGGER );
      if( error == FLYCAPTURE_OK )
      {
         error = checkTriggerReady( context );
         
         //
         // Camera is now ready to be triggered, so generate software trigger
         // by writing a '1' to bit 0
         //
         printf( "Press the Enter key to initiate a software trigger.\n" );
         getchar();
         error = flycaptureSetCameraRegister( 
            context, SOFTWARE_TRIGGER, 0x80000000 );
         HANDLE_ERROR( "flycaptureSetCameraRegister()", error );
      }
      else
      {
         printf( "SOFTWARE_TRIGGER not implemented! Grab will timeout.\n" );
      }
#endif

#endif
      //
      // Do an image grab.  This call will block until the camera
      // is externally triggered.
      //
      error = flycaptureGrabImage2( context, &image );
      if( error == FLYCAPTURE_TIMEOUT )
      {
	 printf( "Grab #%d timed out after %d milliseconds.\n", iImage, TIMEOUT );
      }
      else if( error != FLYCAPTURE_OK )
      {
	 HANDLE_ERROR( "flycaptureGrabImage2()", error );
      }      
      else
      {
	 printf( "Grab %d successful!\n", iImage );	 
      }
   }   

   //
   // Stop the camera and destroy the context.
   //
   flycaptureStop( context );
   flycaptureDestroyContext( context );

   printf( "Done!  (hit enter)" );
   getchar();

   return 0;
}
Exemplo n.º 4
0
int 
main( int argc, char* argv[] )
{
   FlyCaptureError   error;
   FlyCaptureContext context;

   // check the arguments of the call to make sure the utility is being called properly.
   if (argc != 2){
      PrintUsage();

      printf( "Done!  (hit enter)" );
      getchar();

      return 0;
   } else {
      if (!((strcmp(argv[1], "-retrieve") == 0) || (strcmp(argv[1], "-capture") == 0)))
      {
	 PrintUsage();  

	 printf( "Done!  (hit enter)" );
	 getchar();

	 return 0;
      }
   }

   //
   // Enumerate the cameras on the bus.
   //
   FlyCaptureInfoEx  arInfo[ _MAX_CAMS ];
   unsigned int	     uiSize = _MAX_CAMS;

   error = flycaptureBusEnumerateCamerasEx( arInfo, &uiSize );
   _HANDLE_ERROR( error, "flycaptureBusEnumerateCameras()" );

   for( unsigned int uiBusIndex = 0; uiBusIndex < uiSize; uiBusIndex++ )
   {
      FlyCaptureInfoEx* pinfo = &arInfo[ uiBusIndex ];
      printf( 
         "Index %u: %s (%u)\n",
         uiBusIndex,
         pinfo->pszModelName,
         pinfo->SerialNumber );
   }

   //
   // Create the context.
   //
   error = flycaptureCreateContext( &context );
   _HANDLE_ERROR( error, "flycaptureCreateContext()" );
   
   //
   // Initialize the camera.
   //
   printf( "Initializing camera %u.\n", _CAMERA_INDEX );
   error = flycaptureInitialize( context, _CAMERA_INDEX );
   _HANDLE_ERROR( error, "flycaptureInitialize()" );

   //
   // Reset the camera to default factory settings by asserting bit 0
   //
   error = flycaptureSetCameraRegister( context, INITIALIZE, 0x80000000 );
   _HANDLE_ERROR( error, "flycaptureSetCameraRegister()" );

   //
   // Power-up the camera (for cameras that support this feature)
   //
   error = flycaptureSetCameraRegister( context, CAMERA_POWER, 0x80000000 );
   _HANDLE_ERROR( error, "flycaptureSetCameraRegister()" );

   //
   // Report camera info.
   //
   FlyCaptureInfoEx info;
   error = flycaptureGetCameraInfo( context, &info );
   _HANDLE_ERROR( error, "flycaptureGetCameraInfo()" );

   printf( "Camera info:\n" );
   reportCameraInfo( &info );

   //
   // Query and report on the camera's ability to handle custom image modes.
   // We use the maximum and unit values to determine the size of the image
   // which can be saved to flash.
   //
   bool		  bAvailable;
   unsigned int	  uiMaxImageSizeCols;
   unsigned int	  uiMaxImageSizeRows;
   unsigned int	  uiImageUnitSizeHorz;
   unsigned int	  uiImageUnitSizeVert;
   unsigned int   uiOffsetUnitSizeHorz;
   unsigned int   uiOffsetUnitSizeVert;
   unsigned int   uiPixelFormats;

   error = ::flycaptureQueryCustomImageEx(
      context,
      MODE,
      &bAvailable,
      &uiMaxImageSizeCols,
      &uiMaxImageSizeRows,
      &uiImageUnitSizeHorz,
      &uiImageUnitSizeVert,
      &uiOffsetUnitSizeHorz,
      &uiOffsetUnitSizeVert,
      &uiPixelFormats );
   _HANDLE_ERROR( error, "flycaptureQueryCustomImageEx()" );

   //
   // Check to see if Flash Reading/Writing is supported by the camera
   //
   unsigned long ulRegVal;
   error = flycaptureGetCameraRegister(context, DATA_FLASH_CTRL, &ulRegVal);
   printf( "Data control register = %x\n", ulRegVal );

   if((0x80000000 & ulRegVal) == 0)
   {
      printf("This camera does not support the user data area feature. Exiting...\n");
     
      //
      // Destroy the context
      //
      flycaptureDestroyContext(context);

      printf( "Done!  (hit enter)" );
      getchar();

      return 0;
   }

   // Determine the available size of the Flash from the DATA_FLASH_CTRL register
   int iPageSize = (int)pow(2.0, (int)(ulRegVal & 0x00FFF000) >> 12);
   int iNumPages = (int)pow(2.0, (int)(ulRegVal & 0x00000FFF));
   unsigned int uiAvailableFlashSize = iPageSize * iNumPages;

   unsigned int uiHeight = uiMaxImageSizeCols;
   unsigned int uiWidth = uiMaxImageSizeRows;

   // If the Flash is not large enough to hold a full, high-res image, determine the
   // maximum image with a 4:3 aspect ration which can fit in the flash and use those
   // dimensions.
   if (uiMaxImageSizeCols*uiMaxImageSizeRows > uiAvailableFlashSize)
   {
      uiHeight = (int)sqrt(uiAvailableFlashSize*3.0/4);
      uiWidth = uiHeight*4/3;

      uiHeight -= (uiHeight % uiImageUnitSizeVert);
      uiWidth -= (uiWidth % uiImageUnitSizeHorz);
   }

   //
   // Determine the quadlet offset of the Flash area
   //
   unsigned long ulLUTLoc;
   error = flycaptureGetCameraRegister(context, DATA_FLASH_DATA, &ulLUTLoc);
   _HANDLE_ERROR( error, "flycaptureGetCameraRegister()" );   

   //
   // Start grabbing images in the current videomode and framerate.
   //
   printf( "Starting camera.\n\n" );
   error = flycaptureStartCustomImage(context, 0, 0, 0, uiWidth, uiHeight, 40, FLYCAPTURE_MONO8);
   _HANDLE_ERROR( error, "flycaptureStart()" );

   FlyCaptureImage image;
   memset( &image, 0x0, sizeof( FlyCaptureImage ) );

   // Here, we grab 10 images and only look at the last one, since after starting the camera,
   // it takes a few images for the the auto settings to stabilize (ie. exposure, gain, etc)
   for ( int iImage = 0; iImage < _IMAGES_TO_GRAB; iImage++ )
   {
      error = flycaptureGrabImage2( context, &image );
      _HANDLE_ERROR( error, "flycaptureGrabImage2()" );
   }

   // Check to see if we are capturing or retrieving
   if (strcmp(argv[1], "-capture") == 0)
   {

      printf( "Saving raw image to camera FLASH.\n\n" );

      //
      // Write the image to the cameras flash
      //
      error =  flycaptureWriteRegisterBlock(
	 context,
	 0xFFFF,
	 0xF0000000+ulLUTLoc*4,
	 (const unsigned long*)&(image.pData[0]),
	 (image.iRowInc*image.iRows/4));
      _HANDLE_ERROR( error, "flycaptureWriteRegisterBlock()" );

   } else if (strcmp(argv[1], "-retrieve") == 0){ // if we are not capturing (we are retrieving).

      printf( "Grabbing image from FLASH and saving it to disk as %s.\n\n", FILENAME_RAW_FROM_FLASH );

      // Create a flycapture image to save
      FlyCaptureImage savedImage;
      memset( &savedImage, 0x0, sizeof( FlyCaptureImage ) );

      // Fill in the savedImage structure
      savedImage.iCols = image.iCols;
      savedImage.iRows = image.iRows;
      savedImage.iRowInc = image.iRowInc;
      savedImage.videoMode = image.videoMode;
      savedImage.timeStamp = image.timeStamp;
      savedImage.bStippled = image.bStippled;
      savedImage.pixelFormat = image.pixelFormat;
      savedImage.iNumImages = image.iNumImages;
      savedImage.pData = (unsigned char*)malloc(savedImage.iRowInc*savedImage.iRows);

      // Read data from the flash into the SavedImage structure
      error = flycaptureReadRegisterBlock(
	 context,
	 0xFFFF,
	 0xF0000000+ulLUTLoc*4,
	 (unsigned long*)&(savedImage.pData[0]),
	 (savedImage.iRowInc*savedImage.iRows/4));
      _HANDLE_ERROR( error, "flycaptureReadRegisterBlock()" );   

      // Save the image to disk
      error = flycaptureSaveImage(
	 context,
	 &savedImage,
	 FILENAME_RAW_FROM_FLASH,
	 FLYCAPTURE_FILEFORMAT_PGM );
      _HANDLE_ERROR( error, "flycaptureSaveImage()" );

      free(savedImage.pData);

   } else {

      // We should never get here since the check at the top should catch this,
      // but this is here for completeness.
      PrintUsage();

   }

   //
   // Stop the camera
   //
   error = flycaptureStop( context );
   _HANDLE_ERROR( error, "flycaptureStop()" );

   //
   // Destroy the context.
   //
   error = flycaptureDestroyContext( context );
   _HANDLE_ERROR( error, "flycaptureDestroyContext()" );

   printf( "Done!  (hit enter)" );
   getchar();

   return 0;
}
Exemplo n.º 5
0
//=============================================================================
// Main Function
//=============================================================================
int 
main( int /* argc */, char* /* argv[] */ )
{
   FlyCaptureError   error;
   FlyCaptureContext context;   

   bool	             bOn;

   unsigned int      uiCurTime     = 0;
   unsigned int      uiLastTime    = 0;
   unsigned int      uiTotalTime   = 0;
   unsigned int      uiSeconds     = 0;
   unsigned int      uiCount	   = 0;
   unsigned int      uiOffset      = 0;

   double            dGrabTime     = 0.0;

   //
   // Create context
   //
   error = ::flycaptureCreateContext( &context );
   CHECK_ERROR( "flycaptureCreateContext()", error );

   //
   // Initialize first camera on the bus.
   //
   error = ::flycaptureInitialize( context, 0 );
   CHECK_ERROR( "flycaptureInitialize()", error );

   //
   // Reset the camera to default factory settings by asserting bit 0
   //
   error = flycaptureSetCameraRegister( context, INITIALIZE, 0x80000000 );
   CHECK_ERROR( "flycaptureSetCameraRegister()", error );

   //
   // Power-up the camera (for cameras that support this feature)
   //
   error = flycaptureSetCameraRegister( context, CAMERA_POWER, 0x80000000 );
   CHECK_ERROR( "flycaptureSetCameraRegister()", error );

   //
   // Enable image timestamping
   //
   error = ::flycaptureGetImageTimestamping( context, &bOn );
   CHECK_ERROR( "flycaptureGetImageTimestamping()", error );

   if( !bOn )
   {
      error = ::flycaptureSetImageTimestamping( context, true );
      CHECK_ERROR( "flycaptureSetImageTimestamping()", error );
   }

   //
   // Query and report on the camera's ability to handle custom image modes.
   //
   bool		  bAvailable;
   unsigned int	  uiMaxImageSizeCols;
   unsigned int	  uiMaxImageSizeRows;
   unsigned int	  uiImageUnitSizeHorz;
   unsigned int	  uiImageUnitSizeVert;
   unsigned int   uiOffsetUnitSizeHorz;
   unsigned int   uiOffsetUnitSizeVert;
   unsigned int   uiPixelFormats;

   error = ::flycaptureQueryCustomImageEx(
      context,
      MODE,
      &bAvailable,
      &uiMaxImageSizeCols,
      &uiMaxImageSizeRows,
      &uiImageUnitSizeHorz,
      &uiImageUnitSizeVert,
      &uiOffsetUnitSizeHorz,
      &uiOffsetUnitSizeVert,
      &uiPixelFormats );
   CHECK_ERROR( "flycaptureQueryCustomImage()", error );

   if( !bAvailable )
   {
      printf( 
         "Warning!  Camera reports that mode %u is not available.\n",
         MODE );
   }

   printf( 
      "Max image pizels: (%u, %u)\n"
      "Image Unit size: (%u, %u)\n"
      "Offset Unit size: (%u, %u)\n"
      "Pixel format bitfield: 0x%08x\n",
      uiMaxImageSizeCols,
      uiMaxImageSizeRows,
      uiImageUnitSizeHorz,
      uiImageUnitSizeVert,
      uiOffsetUnitSizeHorz,
      uiOffsetUnitSizeVert,
      uiPixelFormats );

   if( ( PIXEL_FORMAT & uiPixelFormats ) == 0 )
   {
      printf( 
         "Warning!  "
         "Camera reports that the requested pixel format is not supported!.\n",
         MODE );
   }

   //
   // Start camera using custom image size mode.
   //
   error = ::flycaptureStartCustomImage(
      context, 
      MODE, 
      START_COL, 
      START_ROW, 
      COLS, 
      ROWS, 
      SPEED, 
      PIXEL_FORMAT );
   CHECK_ERROR( "flycaptureStartCustomImage()", error );


   //
   // Grab a series of images, computing the time difference
   // between consecutive images.
   //
   FlyCaptureImage image = { 0 };
   for( int iImage = 0; iImage < IMAGES_TO_GRAB; iImage++ )
   {
      //
      // Grab an image
      //
      error = ::flycaptureGrabImage2( context, &image );
      CHECK_ERROR( "flycaptureGrabImage2()", error );

      //
      // Calculate the time difference between current and last image
      // in order to calculate actual frame rate
      //
      error = ::flycaptureParseImageTimestamp( context,
					       image.pData,
					       &uiSeconds,
					       &uiCount,
					       &uiOffset );
      CHECK_ERROR( "flycaptureParseImageTimestamp()", error );
      
      uiCurTime = (uiSeconds * 8000) + uiCount;
      
      if( iImage == 0 )
      {
	 uiLastTime = uiCurTime;
	 uiTotalTime = 0;
      }
      else
      {
	 uiTotalTime = uiTotalTime + (uiCurTime - uiLastTime);
	 uiLastTime = uiCurTime;
      }
      
      //
      // Print info.
      //
      printf(
	 "Image %03d: %d x %d %d %d %d %d\n",
	 iImage,
	 image.iCols,
	 image.iRows,
	 image.timeStamp.ulSeconds,
	 image.timeStamp.ulMicroSeconds,
	 image.timeStamp.ulCycleSeconds,
	 image.timeStamp.ulCycleCount  );
   }

   // 
   // Convert to a frames per second number
   //
   dGrabTime = (double)(1 / ( ((double)uiTotalTime / (double)8000) 
	       / IMAGES_TO_GRAB ));
   printf("Frame rate: %lfHz\n", dGrabTime );

   //
   // Save the last image to disk
   //
   printf( "Saving last image..." );
   saveFinalImage(context, &image);
   printf( "done\n" );

   //
   // stop the camera and destroy the context.
   //
   ::flycaptureStop( context );
   ::flycaptureDestroyContext( context );

   return 0;
}