Camera.C

00001 #include "Camera.H"
00002 #include <iostream>
00003 
00004 Camera::Camera(dc1394camera_t* _camera) :
00005 camera(_camera), error(DC1394_SUCCESS)
00006 {
00007   if (camera) error = setupCameraDefaults();
00008   else error = DC1394_CAMERA_NOT_INITIALIZED;  
00009 }
00010 
00011 Camera::~Camera() {
00012   if (camera) {
00013     dc1394_video_set_transmission(camera, DC1394_OFF);
00014     dc1394_capture_stop(camera);
00015   }
00016 }
00017 
00018 Image<byte> Camera::getImage() {
00019 
00020   dc1394video_frame_t* frame = NULL;
00021   dc1394error_t err;
00022   Image<byte> output;
00023   int width = 0;
00024   int height = 0;
00025 
00026 
00027   
00028   err = dc1394_capture_dequeue(camera, DC1394_CAPTURE_POLICY_WAIT, &frame);
00029   DC1394_WRN(err, "Could not capture frame");
00030   if (err != DC1394_SUCCESS)  return output;
00031   
00032   width = (int)frame->size[0];
00033   height = (int)frame->size[1];
00034   output.attach(frame->image, width, height);
00035 
00036         err = dc1394_capture_enqueue(camera, frame);
00037   DC1394_WRN(err, "Could not free frame");
00038   return output.deepcopy(); 
00039 }
00040 
00041 ImageIceMod::ImageIce Camera::getIceImage() {
00042   dc1394video_frame_t* frame = NULL;
00043   dc1394error_t err;
00044   ImageIceMod::ImageIce output;
00045 
00046   
00047   err = dc1394_capture_dequeue(camera, DC1394_CAPTURE_POLICY_WAIT, &frame);
00048   DC1394_WRN(err, "Could not capture frame");
00049   if (err != DC1394_SUCCESS)  return output;
00050   //printf("Frames behind: %d\n", frame->frames_behind);
00051         output = dc2Ice(frame);
00052         err = dc1394_capture_enqueue(camera, frame);
00053   DC1394_WRN(err, "Could not free frame");
00054   return output; 
00055 
00056     /*while (!frame) {
00057       dc1394_capture_dequeue(camera, DC1394_CAPTURE_POLICY_POLL, &frame);
00058       if (!frame) usleep(20000);
00059     }
00060 
00061 
00062     dc1394_capture_dequeue(camera, DC1394_CAPTURE_POLICY_POLL, &frame);
00063     int numFrames = frame->frames_behind;
00064 
00065     dc1394_capture_enqueue(camera, frame);
00066     for (int i = 0; i < numFrames; i++) {
00067       dc1394_capture_dequeue(camera, DC1394_CAPTURE_POLICY_POLL, &frame);
00068       if (frame != NULL) {
00069         printf("%d\n", frame->frames_behind);
00070         dc1394_capture_enqueue(camera, frame);
00071       }
00072     }*/
00073 }
00074 
00075 ImageIceMod::ImageIce Camera::dc2Ice(dc1394video_frame_t* input) {
00076         ImageIceMod::ImageIce output;
00077         int height, width, pixSize;
00078   width = (int)input->size[0];
00079   height = (int)input->size[1];
00080   pixSize = (int)input->data_depth / 8;
00081   //std::cout << input->data_depth << std::endl;
00082   //printf("Width: %d", width);
00083   //printf("Height: %d", height);
00084         //dc1394_get_image_size_from_video_mode(camera, cameraMode, &width, &height);
00085         output.height = height;
00086         output.width = width;
00087         output.pixSize = pixSize;
00088         
00089         int size = height*width*pixSize;
00090   output.data.resize(size);
00091   std::copy(input->image, input->image + size, output.data.begin());
00092   /*
00093   if (size == EXPECTED_SIZE*ICE_IMAGE_DEPTH) {
00094     output.data.resize(size);
00095     std::copy(input->image, input->image + size, output.data.begin());
00096   }
00097   */
00098         return output;
00099 }
00100 
00101 dc1394video_frame_t Camera::getdc1394Frame() {
00102   dc1394video_frame_t* frame;
00103   dc1394video_frame_t output; 
00104   dc1394_capture_dequeue(camera, DC1394_CAPTURE_POLICY_WAIT, &frame);
00105   output = *frame;
00106   //output = dc2ice(frame);
00107   dc1394_capture_enqueue(camera, frame);
00108   return output;
00109 }
00110 
00111 dc1394error_t Camera::setupCameraDefaults() {
00112   dc1394error_t err; 
00113   err=dc1394_video_set_framerate(camera, DC1394_FRAMERATE_30); //7_5,15, 30, 60
00114   DC1394_ERR_RTN(err, "Failed to set framerate");
00115   err=dc1394_video_set_iso_speed(camera, DC1394_ISO_SPEED_400);
00116   DC1394_ERR_RTN(err, "Failed to set iso speed");
00117   err = dc1394_video_set_mode(camera, DC1394_VIDEO_MODE_640x480_MONO8);
00118   DC1394_ERR_RTN(err, "Failed to set video mode");
00119   err = dc1394_capture_setup(camera, 5, DC1394_CAPTURE_FLAGS_DEFAULT);
00120   DC1394_ERR_RTN(err, "Failed to setup capture");
00121   err = dc1394_video_set_transmission(camera, DC1394_ON);
00122   DC1394_ERR_RTN(err, "Failed to start camera iso transmission");
00123   return DC1394_SUCCESS;
00124 }
00125 
Generated on Sun May 8 08:05:31 2011 for iLab Neuromorphic Vision Toolkit by  doxygen 1.6.3