void CCflycap_set_num_framebuffers( CCflycap *ccntxt,
				    int num_framebuffers ) {
  CHECK_CC(ccntxt);
  FlyCapture2::Camera *cam = (FlyCapture2::Camera *)ccntxt->inherited.cam;
  FlyCapture2::FC2Config cfg;
  CIPGRCHK(cam->StopCapture());
  CIPGRCHK(cam->GetConfiguration(&cfg));
  cfg.numBuffers = num_framebuffers;
  CIPGRCHK(cam->SetConfiguration(&cfg));
  CIPGRCHK(cam->StartCapture());
}
void CCflycap_close(CCflycap *ccntxt) {
  if (!ccntxt) {CAM_IFACE_THROW_ERROR("no CCflycap specified (NULL argument)");}
  FlyCapture2::Camera *cam = (FlyCapture2::Camera *)ccntxt->inherited.cam;
  CIPGRCHK(cam->StopCapture());
  CIPGRCHK(cam->Disconnect());

  cam_iface_backend_extras* backend_extras = (cam_iface_backend_extras*)(ccntxt->inherited.backend_extras);


  if (backend_extras!=NULL) {
    delete backend_extras;
    ccntxt->inherited.backend_extras = (void*)NULL;
  }

  delete cam;
  ccntxt->inherited.cam = (void*)NULL;
}
int main(int argc, char *argv[])
{

    get_param();

    std::string t_stamp = get_timestamp();

    sprintf (data_filename, "%s/data_%s.out", save_dir, t_stamp.c_str());
    std::cout << "data_filename: " << data_filename << std::endl;


    FlyCapture2::BusManager busMgr;

    PGRGuid guid;
    err = busMgr.GetCameraFromSerialNumber(cam_serial, &guid);
    if (err != PGRERROR_OK) {
      std::cout << "Index error" << std::endl;
      return false;
    }


    // Point Grey cam init
    init_cam_capture(pt_grey, guid);

    if (use_calib)
      load_calib_param();


    imageCallback(&pt_grey);


    printf("Stopping...\n");

    err = pt_grey.StopCapture();
    
    if ( err != PGRERROR_OK )
    {
        // This may fail when the camera was removed, so don't show 
        // an error message
    }  

    pt_grey.Disconnect();


}
void CCflycap_stop_camera( CCflycap *ccntxt ) {
  CHECK_CC(ccntxt);
  FlyCapture2::Camera *cam = (FlyCapture2::Camera *)ccntxt->inherited.cam;
  CIPGRCHK(cam->StopCapture());
}
void CCflycap_CCflycap( CCflycap * ccntxt, int device_number, int NumImageBuffers,
                        int mode_number) {

  // call parent
  CamContext_CamContext((CamContext*)ccntxt,device_number,NumImageBuffers,mode_number); // XXX cast error?
  ccntxt->inherited.vmt = (CamContext_functable*)&CCflycap_vmt;

  CAM_IFACE_CHECK_DEVICE_NUMBER(device_number);
  std::vector<CamMode> result;
  int myerr = get_mode_list(device_number, result);

  ccntxt->inherited.device_number = device_number;
  ccntxt->inherited.backend_extras = new cam_iface_backend_extras;
  memset(ccntxt->inherited.backend_extras,0,sizeof(cam_iface_backend_extras));

  FlyCapture2::Camera *cam = new FlyCapture2::Camera;
  FlyCapture2::PGRGuid guid;
  CIPGRCHK( BACKEND_GLOBAL(busMgr_ptr)->GetCameraFromIndex(device_number, &guid));
  CIPGRCHK(cam->Connect(&guid));

  FlyCapture2::FC2Config cfg;
  CIPGRCHK(cam->GetConfiguration(&cfg));
  cfg.numBuffers = NumImageBuffers;
  CIPGRCHK(cam->SetConfiguration(&cfg));

  // Set the settings to the camera
  CamMode target_mode = result[mode_number];

  if (target_mode.videomode == FlyCapture2::VIDEOMODE_FORMAT7) {
    CIPGRCHK(cam->SetFormat7Configuration(&target_mode.fmt7ImageSettings,
					  target_mode.fmt7PacketInfo.recommendedBytesPerPacket ));
  } else {
    CIPGRCHK(cam->SetVideoModeAndFrameRate(target_mode.videomode, target_mode.framerate));
  }

  ccntxt->inherited.cam = (void*)cam;

  // XXX move this to start camera and query camera for settings

  CIPGRCHK(cam->StartCapture());


  // Retrieve an image to get width, height. XXX change to query later.
  FlyCapture2::Image rawImage;
  CIPGRCHK( cam->RetrieveBuffer( &rawImage ));

  cam_iface_backend_extras *extras = (cam_iface_backend_extras *)ccntxt->inherited.backend_extras;

  ccntxt->inherited.depth = rawImage.GetBitsPerPixel();
  extras->buf_size = rawImage.GetDataSize();
  extras->current_height = rawImage.GetRows();
  extras->current_width = rawImage.GetCols();
  extras->max_height = rawImage.GetRows();
  extras->max_width = rawImage.GetCols();
  CIPGRCHK( cam->GetTriggerModeInfo( &extras->trigger_mode_info ));

  switch (rawImage.GetPixelFormat()) {
  case FlyCapture2::PIXEL_FORMAT_MONO8:
    ccntxt->inherited.coding = CAM_IFACE_MONO8;
    if (rawImage.GetBayerTileFormat()!=FlyCapture2::NONE) {
      NOT_IMPLEMENTED;
    }
    break;
  case FlyCapture2::PIXEL_FORMAT_RAW8:
    switch (rawImage.GetBayerTileFormat()) {
    case FlyCapture2::NONE:
      ccntxt->inherited.coding = CAM_IFACE_RAW8;
      break;
    case FlyCapture2::RGGB:
      ccntxt->inherited.coding = CAM_IFACE_MONO8_BAYER_RGGB;
      break;
    case FlyCapture2::GRBG:
      ccntxt->inherited.coding = CAM_IFACE_MONO8_BAYER_GRBG;
      break;
    case FlyCapture2::GBRG:
      ccntxt->inherited.coding = CAM_IFACE_MONO8_BAYER_GBRG;
      break;
    case FlyCapture2::BGGR:
      ccntxt->inherited.coding = CAM_IFACE_MONO8_BAYER_BGGR;
      break;
    default:
      NOT_IMPLEMENTED;
    }
  }
  CIPGRCHK(cam->StopCapture());

}