示例#1
0
static int dc1394_v2_read_header(AVFormatContext *c, AVFormatParameters * ap)
{
    dc1394_data* dc1394 = c->priv_data;
    dc1394camera_list_t *list;
    int res, i;
    struct dc1394_frame_format *fmt = NULL;
    struct dc1394_frame_rate *fps = NULL;

    if (dc1394_read_common(c,ap,&fmt,&fps) != 0)
       return -1;

    /* Now let us prep the hardware. */
    dc1394->d = dc1394_new();
    dc1394_camera_enumerate (dc1394->d, &list);
    if ( !list || list->num == 0) {
        av_log(c, AV_LOG_ERROR, "Unable to look for an IIDC camera\n\n");
        goto out;
    }

    /* FIXME: To select a specific camera I need to search in list its guid */
    dc1394->camera = dc1394_camera_new (dc1394->d, list->ids[0].guid);
    if (list->num > 1) {
        av_log(c, AV_LOG_INFO, "Working with the first camera found\n");
    }

    /* Freeing list of cameras */
    dc1394_camera_free_list (list);

    /* Select MAX Speed possible from the cam */
    if (dc1394->camera->bmode_capable>0) {
       dc1394_video_set_operation_mode(dc1394->camera, DC1394_OPERATION_MODE_1394B);
       i = DC1394_ISO_SPEED_800;
    } else {
       i = DC1394_ISO_SPEED_400;
    }

    for (res = DC1394_FAILURE; i >= DC1394_ISO_SPEED_MIN && res != DC1394_SUCCESS; i--) {
            res=dc1394_video_set_iso_speed(dc1394->camera, i);
    }
    if (res != DC1394_SUCCESS) {
        av_log(c, AV_LOG_ERROR, "Couldn't set ISO Speed\n");
        goto out_camera;
    }

    if (dc1394_video_set_mode(dc1394->camera, fmt->frame_size_id) != DC1394_SUCCESS) {
        av_log(c, AV_LOG_ERROR, "Couldn't set video format\n");
        goto out_camera;
    }

    if (dc1394_video_set_framerate(dc1394->camera,fps->frame_rate_id) != DC1394_SUCCESS) {
        av_log(c, AV_LOG_ERROR, "Couldn't set framerate %d \n",fps->frame_rate);
        goto out_camera;
    }
    if (dc1394_capture_setup(dc1394->camera, 10, DC1394_CAPTURE_FLAGS_DEFAULT)!=DC1394_SUCCESS) {
        av_log(c, AV_LOG_ERROR, "Cannot setup camera \n");
        goto out_camera;
    }

    if (dc1394_video_set_transmission(dc1394->camera, DC1394_ON) !=DC1394_SUCCESS) {
        av_log(c, AV_LOG_ERROR, "Cannot start capture\n");
        goto out_camera;
    }
    return 0;

out_camera:
    dc1394_capture_stop(dc1394->camera);
    dc1394_video_set_transmission(dc1394->camera, DC1394_OFF);
    dc1394_camera_free (dc1394->camera);
out:
    dc1394_free(dc1394->d);
    return -1;
}
示例#2
0
static gboolean
gst_dc1394_open_cam_with_best_caps (GstDc1394 * src)
{
    dc1394camera_list_t *cameras = NULL;
    gint err = 0;
    int framerateconst;

    GST_LOG_OBJECT (src, "Opening the camera!!!");


    if (dc1394_camera_enumerate (src->dc1394, &cameras) != DC1394_SUCCESS) {
        GST_ELEMENT_ERROR (src, RESOURCE, FAILED, ("Can't find cameras"),
                           ("Can't find cameras"));
        goto error;
    }

    GST_LOG_OBJECT (src, "Found  %d  cameras", cameras->num);

    if (src->camnum > (cameras->num - 1)) {
        GST_ELEMENT_ERROR (src, RESOURCE, FAILED, ("Invalid camera number"),
                           ("Invalid camera number"));
        goto error;
    }

    GST_LOG_OBJECT (src, "Opening camera : %d", src->camnum);

    src->camera =
        dc1394_camera_new_unit (src->dc1394, cameras->ids[src->camnum].guid,
                                cameras->ids[src->camnum].unit);

    dc1394_camera_free_list (cameras);
    cameras = NULL;

    // figure out mode
    framerateconst = gst_dc1394_framerate_frac_to_const (src->rate_numerator,
                     src->rate_denominator);

    GST_LOG_OBJECT (src, "The dma buffer queue size is %d  buffers",
                    src->bufsize);

    switch (src->iso_speed) {
    case 100:
        err = dc1394_video_set_iso_speed (src->camera, DC1394_ISO_SPEED_100);
        break;
    case 200:
        err = dc1394_video_set_iso_speed (src->camera, DC1394_ISO_SPEED_200);
        break;
    case 400:
        err = dc1394_video_set_iso_speed (src->camera, DC1394_ISO_SPEED_400);
        break;
    case 800:
        if (src->camera->bmode_capable > 0) {
            dc1394_video_set_operation_mode (src->camera,
                                             DC1394_OPERATION_MODE_1394B);
            err = dc1394_video_set_iso_speed (src->camera, DC1394_ISO_SPEED_800);
        }
        break;
    case 1600:
        if (src->camera->bmode_capable > 0) {
            dc1394_video_set_operation_mode (src->camera,
                                             DC1394_OPERATION_MODE_1394B);
            err = dc1394_video_set_iso_speed (src->camera, DC1394_ISO_SPEED_1600);
        }
        break;
    case 3200:
        if (src->camera->bmode_capable > 0) {
            dc1394_video_set_operation_mode (src->camera,
                                             DC1394_OPERATION_MODE_1394B);
            err = dc1394_video_set_iso_speed (src->camera, DC1394_ISO_SPEED_3200);
        }
        break;
    default:
        GST_ELEMENT_ERROR (src, RESOURCE, FAILED, ("Invalid ISO speed"),
                           ("Invalid ISO speed"));
        goto error;
        break;
    }

    if (err != DC1394_SUCCESS) {
        GST_ELEMENT_ERROR (src, RESOURCE, FAILED, ("Could not set ISO speed"),
                           ("Could not set ISO speed"));
        goto error;
    }

    GST_LOG_OBJECT (src, "Setting mode :  %d", src->vmode);
    err = dc1394_video_set_mode (src->camera, src->vmode);

    if (err != DC1394_SUCCESS) {
        GST_ELEMENT_ERROR (src, RESOURCE, FAILED, ("Could not set video mode %d",
                           src->vmode), ("Could not set video mode %d", src->vmode));
        goto error;
    }

    GST_LOG_OBJECT (src, "Setting framerate :  %d", framerateconst);
    dc1394_video_set_framerate (src->camera, framerateconst);

    if (err != DC1394_SUCCESS) {
        GST_ELEMENT_ERROR (src, RESOURCE, FAILED, ("Could not set framerate to %d",
                           framerateconst), ("Could not set framerate to %d", framerateconst));
        goto error;
    }
    // set any format-7 parameters if this is a format-7 mode
    if (src->vmode >= DC1394_VIDEO_MODE_FORMAT7_MIN &&
            src->vmode <= DC1394_VIDEO_MODE_FORMAT7_MAX) {
        // the big thing we care about right now is frame size
        err = dc1394_format7_set_image_size (src->camera, src->vmode,
                                             src->width, src->height);
        if (err != DC1394_SUCCESS) {
            GST_ELEMENT_ERROR (src, RESOURCE, FAILED,
                               ("Could not set format 7 image size to %d x %d", src->width,
                                src->height), ("Could not set format 7 image size to %d x %d",
                                               src->width, src->height));

            goto error;
        }

    }
    err = dc1394_capture_setup (src->camera, src->bufsize,
                                DC1394_CAPTURE_FLAGS_DEFAULT);
    if (err != DC1394_SUCCESS) {
        GST_ELEMENT_ERROR (src, RESOURCE, FAILED, ("Error setting capture mode"),
                           ("Error setting capture mode"));
    }
    if (err != DC1394_SUCCESS) {
        if (err == DC1394_NO_BANDWIDTH) {
            GST_LOG_OBJECT (src, "Capture setup_dma failed."
                            "Trying to cleanup the iso_channels_and_bandwidth and retrying");

            // try to cleanup the bandwidth and retry
            err = dc1394_iso_release_all (src->camera);
            if (err != DC1394_SUCCESS) {
                GST_ELEMENT_ERROR (src, RESOURCE, FAILED,
                                   ("Could not cleanup bandwidth"), ("Could not cleanup bandwidth"));
                goto error;
            } else {
                err =
                    dc1394_capture_setup (src->camera, src->bufsize,
                                          DC1394_CAPTURE_FLAGS_DEFAULT);
                if (err != DC1394_SUCCESS) {
                    GST_ELEMENT_ERROR (src, RESOURCE, FAILED,
                                       ("unable to setup camera error %d", err),
                                       ("unable to setup camera error %d", err));
                    goto error;
                }
            }
        } else {
            GST_ELEMENT_ERROR (src, RESOURCE, FAILED,
                               ("unable to setup camera error %d", err),
                               ("unable to setup camera error %d", err));
            goto error;

        }
    }


    return TRUE;

error:

    if (src->camera) {
        dc1394_camera_free (src->camera);
        src->camera = NULL;
    }

    return FALSE;

}
示例#3
0
//--------------------------------------------------------------------
int of1394VideoGrabber::initGrabber()
{
	// moved from constructor
	int i;
	
	printf("Making new camera\n");
	
	d = dc1394_new ();
	
	err=dc1394_camera_enumerate (d, &list);
    DC1394_ERR_RTN(err,"Failed to enumerate cameras");
	
	if (list->num == 0) {
		if(err!=0 && bVerbose)printf("\n No cameras found\n ");
		return 1;
	}
	
	camera = dc1394_camera_new (d, list->ids[0].guid);
	if (!camera) {
		if(err!=0 && bVerbose)printf("\n Failed to initialize camera with guid %PRIx64", list->ids[0].guid);
		return 1;
	}
	dc1394_camera_free_list (list);
	

	
	/*-----------------------------------------------------------------------
	 *  setup capture
	 *-----------------------------------------------------------------------*/
	

	

	dc1394_video_set_mode(camera, DC1394_VIDEO_MODE_FORMAT7_0);
	uint32_t f, f2;
	dc1394_format7_get_image_size(camera, DC1394_VIDEO_MODE_FORMAT7_0, &f, &f2);
	
	err=dc1394_capture_setup(camera,4, DC1394_CAPTURE_FLAGS_DEFAULT);
    DC1394_ERR_CLN_RTN(err,cleanup_and_exit(camera),"Could not setup camera-\nmake sure that the video mode and framerate are\nsupported by your camera\n");

	
	
	/*-----------------------------------------------------------------------
	 *  have the camera start sending us data
	 *-----------------------------------------------------------------------*/
	err=dc1394_video_set_transmission(camera, DC1394_ON);
    DC1394_ERR_CLN_RTN(err,cleanup_and_exit(camera),"Could not start camera iso transmission\n");

	dc1394_get_image_size_from_video_mode(camera, video_mode, &width, &height);
	if(err != DC1394_SUCCESS){
		cout<<"get image size error: "<<err<<endl;
	}
	printf("Size from video mode = %i wide %i high\n", width, height);

	pixels	= new unsigned char[width*height*3]; // x3 for RGB
	pixels2	= new unsigned char[width*height*3]; // x3 for RGB

	tex.allocate(width,height,GL_LUMINANCE);

	err = dc1394_capture_dequeue( camera, DC1394_CAPTURE_POLICY_WAIT, &frame ) ;
    DC1394_ERR_CLN_RTN(err,cleanup_and_exit(camera),"Could not capture a frame\n");

	memcpy( pixels, frame->image, width * height*3) ;
	
	dc1394_capture_enqueue( camera, frame ) ;


	
	ofSleepMillis(10);
	startThread(true, false);
	
	failedToInit = false;
	dc1394_feature_set_mode(camera, DC1394_FEATURE_EXPOSURE, DC1394_FEATURE_MODE_MANUAL);
	dc1394_feature_set_mode(camera, DC1394_FEATURE_BRIGHTNESS, DC1394_FEATURE_MODE_MANUAL);
	dc1394_feature_set_mode(camera, DC1394_FEATURE_GAMMA, DC1394_FEATURE_MODE_AUTO);
	dc1394_feature_set_mode(camera, DC1394_FEATURE_SHUTTER, DC1394_FEATURE_MODE_MANUAL);
	dc1394_feature_set_mode(camera, DC1394_FEATURE_GAIN, DC1394_FEATURE_MODE_AUTO);
	dc1394_feature_set_mode(camera, DC1394_FEATURE_IRIS, DC1394_FEATURE_MODE_AUTO);
	dc1394_feature_set_mode(camera, DC1394_FEATURE_WHITE_BALANCE, DC1394_FEATURE_MODE_AUTO);
	dc1394_feature_set_mode(camera, DC1394_FEATURE_TEMPERATURE, DC1394_FEATURE_MODE_AUTO);
	dc1394_feature_set_mode(camera, DC1394_FEATURE_WHITE_SHADING, DC1394_FEATURE_MODE_AUTO);	
	dc1394_feature_set_mode(camera, DC1394_FEATURE_SATURATION, DC1394_FEATURE_MODE_AUTO);		
	dc1394_feature_set_mode(camera, DC1394_FEATURE_HUE, DC1394_FEATURE_MODE_AUTO);		
	
	//Print features
	dc1394_feature_get_all(camera, &features);
	dc1394_feature_print_all(&features, stdout);

	return 0;
}
bool CvCaptureCAM_DC1394_v2_CPP::startCapture()
{
    int i;
    int code = 0;
    if (!dcCam)
        return false;
    if (isoSpeed > 0)
    {
        code = dc1394_video_set_iso_speed(dcCam,
                                          isoSpeed <= 100 ? DC1394_ISO_SPEED_100 :
                                          isoSpeed <= 200 ? DC1394_ISO_SPEED_200 :
                                          isoSpeed <= 400 ? DC1394_ISO_SPEED_400 :
                                          isoSpeed <= 800 ? DC1394_ISO_SPEED_800 :
                                          isoSpeed == 1600 ? DC1394_ISO_SPEED_1600 :
                                          DC1394_ISO_SPEED_3200);
    }

    if (frameWidth > 0 || frameHeight > 0)
    {
        dc1394video_mode_t bestMode = (dc1394video_mode_t) - 1;
        dc1394video_modes_t videoModes;
        dc1394_video_get_supported_modes(dcCam, &videoModes);
        for (i = 0; i < (int)videoModes.num; i++)
        {
            dc1394video_mode_t mode = videoModes.modes[i];
			if (mode >= DC1394_VIDEO_MODE_FORMAT7_MIN && mode <= DC1394_VIDEO_MODE_FORMAT7_MAX)
				continue;
            int pref = -1;
            dc1394color_coding_t colorCoding;
            dc1394_get_color_coding_from_video_mode(dcCam, mode, &colorCoding);

            uint32_t width, height;
            dc1394_get_image_size_from_video_mode(dcCam, mode, &width, &height);
            if ((int)width == frameWidth || (int)height == frameHeight)
            {
                if (colorCoding == DC1394_COLOR_CODING_RGB8 ||
                        colorCoding == DC1394_COLOR_CODING_RGB16 ||
                        colorCoding == DC1394_COLOR_CODING_RAW8 ||
                        colorCoding == DC1394_COLOR_CODING_RAW16)
                {
                    bestMode = mode;
                    break;
                }

                if (colorCoding == DC1394_COLOR_CODING_YUV411 ||
                        colorCoding == DC1394_COLOR_CODING_YUV422 ||
                        (colorCoding == DC1394_COLOR_CODING_YUV444 &&
                        pref < 1))
                {
                    bestMode = mode;
                    pref = 1;
                }

                if (colorCoding == DC1394_COLOR_CODING_MONO8 ||
                        (colorCoding == DC1394_COLOR_CODING_MONO16 &&
                        pref < 0))
                {
                    bestMode = mode;
                    pref = 0;
                }
            }
        }
        if ((int)bestMode >= 0)
            code = dc1394_video_set_mode(dcCam, bestMode);
    }

    if (fps > 0)
    {
        dc1394video_mode_t mode;
        dc1394framerates_t framerates;
        double minDiff = DBL_MAX;
        dc1394framerate_t bestFps = (dc1394framerate_t) - 1;

        dc1394_video_get_mode(dcCam, &mode);
        dc1394_video_get_supported_framerates(dcCam, mode, &framerates);

        for (i = 0; i < (int)framerates.num; i++)
        {
            dc1394framerate_t ifps = framerates.framerates[i];
            double fps1 = (1 << (ifps - DC1394_FRAMERATE_1_875)) * 1.875;
            double diff = fabs(fps1 - fps);
            if (diff < minDiff)
            {
                minDiff = diff;
                bestFps = ifps;
            }
        }
        if ((int)bestFps >= 0)
            code = dc1394_video_set_framerate(dcCam, bestFps);
    }

    if (cameraId == VIDERE)
    {
        bayerFilter = DC1394_COLOR_FILTER_GBRG;
        nimages = 2;
        uint32_t value = 0;
        dc1394_get_control_register(dcCam, 0x50c, &value);
        colorStereo = (value & 0x80000000) != 0;
    }

    code = dc1394_capture_setup(dcCam, nDMABufs, DC1394_CAPTURE_FLAGS_DEFAULT);
    if (code >= 0)
    {
        FD_SET(dc1394_capture_get_fileno(dcCam), &dc1394.camFds);
        dc1394_video_set_transmission(dcCam, DC1394_ON);
        if (cameraId == VIDERE)
        {
            enum { PROC_MODE_OFF, PROC_MODE_NONE, PROC_MODE_TEST, PROC_MODE_RECTIFIED, PROC_MODE_DISPARITY, PROC_MODE_DISPARITY_RAW };
            int procMode = PROC_MODE_RECTIFIED;
            usleep(100000);
            uint32_t qval1 = 0x08000000 | (0x90 << 16) | ((procMode & 0x7) << 16);
            uint32_t qval2 = 0x08000000 | (0x9C << 16);
            dc1394_set_control_register(dcCam, 0xFF000, qval1);
            dc1394_set_control_register(dcCam, 0xFF000, qval2);
        }
        started = true;
    }

    return code >= 0;
}
示例#5
0
int main(int argc,char *argv[])
{
    XEvent xev;
    XGCValues xgcv;
    long background=0x010203;
    int i, j;
    dc1394_t * d;
    dc1394camera_list_t * list;

    get_options(argc,argv);
    /* process options */
    switch(fps) {
    case 1: fps =        DC1394_FRAMERATE_1_875; break;
    case 3: fps =        DC1394_FRAMERATE_3_75; break;
    case 15: fps = DC1394_FRAMERATE_15; break;
    case 30: fps = DC1394_FRAMERATE_30; break;
    case 60: fps = DC1394_FRAMERATE_60; break;
    default: fps = DC1394_FRAMERATE_7_5; break;
    }
    switch(res) {
    case 1:
        res = DC1394_VIDEO_MODE_640x480_YUV411;
        device_width=640;
        device_height=480;
        format=XV_YUY2;
        break;
    case 2:
        res = DC1394_VIDEO_MODE_640x480_RGB8;
        device_width=640;
        device_height=480;
        format=XV_YUY2;
        break;
    default:
        res = DC1394_VIDEO_MODE_320x240_YUV422;
        device_width=320;
        device_height=240;
        format=XV_UYVY;
        break;
    }

    dc1394error_t err;

    d = dc1394_new ();
    if (!d)
        return 1;
    err=dc1394_camera_enumerate (d, &list);
    DC1394_ERR_RTN(err,"Failed to enumerate cameras");

    if (list->num == 0) {
        dc1394_log_error("No cameras found");
        return 1;
    }

    j = 0;
    for (i = 0; i < list->num; i++) {
        if (j >= MAX_CAMERAS)
            break;
        cameras[j] = dc1394_camera_new (d, list->ids[i].guid);
        if (!cameras[j]) {
            dc1394_log_warning("Failed to initialize camera with guid %llx", list->ids[i].guid);
            continue;
        }
        j++;
    }
    numCameras = j;
    dc1394_camera_free_list (list);

    if (numCameras == 0) {
        dc1394_log_error("No cameras found");
        exit (1);
    }

    /* setup cameras for capture */
    for (i = 0; i < numCameras; i++) {

        err=dc1394_video_set_iso_speed(cameras[i], DC1394_ISO_SPEED_400);
        DC1394_ERR_CLN_RTN(err,cleanup(),"Could not set ISO speed");

        err=dc1394_video_set_mode(cameras[i], res);
        DC1394_ERR_CLN_RTN(err,cleanup(),"Could not set video mode");

        err=dc1394_video_set_framerate(cameras[i], fps);
        DC1394_ERR_CLN_RTN(err,cleanup(),"Could not set framerate");

        err=dc1394_capture_setup(cameras[i],NUM_BUFFERS, DC1394_CAPTURE_FLAGS_DEFAULT);
        DC1394_ERR_CLN_RTN(err,cleanup(),"Could not setup camera-\nmake sure that the video mode and framerate are\nsupported by your camera");

        err=dc1394_video_set_transmission(cameras[i], DC1394_ON);
        DC1394_ERR_CLN_RTN(err,cleanup(),"Could not start camera iso transmission");

    }

    fflush(stdout);
    if (numCameras < 1) {
        perror("no cameras found :(\n");
        cleanup();
        exit(-1);
    }

    switch(format){
    case XV_YV12:
        set_frame_length(device_width*device_height*3/2, numCameras);
        break;
    case XV_YUY2:
    case XV_UYVY:
        set_frame_length(device_width*device_height*2, numCameras);
        break;
    default:
        dc1394_log_error("Unknown format set (internal error)");
        exit(255);
    }

    /* make the window */
    display=XOpenDisplay(getenv("DISPLAY"));
    if(display==NULL) {
        dc1394_log_error("Could not open display \"%s\"",getenv("DISPLAY"));
        cleanup();
        exit(-1);
    }

    QueryXv();

    if ( adaptor < 0 ) {
        cleanup();
        exit(-1);
    }

    width=device_width;
    height=device_height * numCameras;

    window=XCreateSimpleWindow(display,DefaultRootWindow(display),0,0,width,height,0,
                               WhitePixel(display,DefaultScreen(display)),
                               background);

    XSelectInput(display,window,StructureNotifyMask|KeyPressMask);
    XMapWindow(display,window);
    connection=ConnectionNumber(display);

    gc=XCreateGC(display,window,0,&xgcv);

    /* main event loop */
    while(1){

        for (i = 0; i < numCameras; i++) {
            if (dc1394_capture_dequeue(cameras[i], DC1394_CAPTURE_POLICY_WAIT, &frames[i])!=DC1394_SUCCESS)
                dc1394_log_error("Failed to capture from camera %d", i);
        }

        display_frames();
        XFlush(display);

        while(XPending(display)>0){
            XNextEvent(display,&xev);
            switch(xev.type){
            case ConfigureNotify:
                width=xev.xconfigure.width;
                height=xev.xconfigure.height;
                display_frames();
                break;
            case KeyPress:
                switch(XKeycodeToKeysym(display,xev.xkey.keycode,0)){
                case XK_q:
                case XK_Q:
                    cleanup();
                    exit(0);
                    break;
                case XK_comma:
                case XK_less:
                    width=width/2;
                    height=height/2;
                    XResizeWindow(display,window,width,height);
                    display_frames();
                    break;
                case XK_period:
                case XK_greater:
                    width=2*width;
                    height=2*height;
                    XResizeWindow(display,window,width,height);
                    display_frames();
                    break;
                case XK_0:
                    freeze = !freeze;
                    break;
                case XK_1:
                    fps =        DC1394_FRAMERATE_1_875;
                    for (i = 0; i < numCameras; i++)
                        dc1394_video_set_framerate(cameras[i], fps);
                    break;
                case XK_2:
                    fps =        DC1394_FRAMERATE_3_75;
                    for (i = 0; i < numCameras; i++)
                        dc1394_video_set_framerate(cameras[i], fps);
                    break;
                case XK_4:
                    fps = DC1394_FRAMERATE_15;
                    for (i = 0; i < numCameras; i++)
                        dc1394_video_set_framerate(cameras[i], fps);
                    break;
                case XK_5:
                    fps = DC1394_FRAMERATE_30;
                    for (i = 0; i < numCameras; i++)
                        dc1394_video_set_framerate(cameras[i], fps);
                    break;
                case XK_3:
                    fps = DC1394_FRAMERATE_7_5;
                    for (i = 0; i < numCameras; i++)
                        dc1394_video_set_framerate(cameras[i], fps);
                    break;
                }
                break;
            }
        } /* XPending */

        for (i = 0; i < numCameras; i++) {
            if (frames[i])
                dc1394_capture_enqueue (cameras[i], frames[i]);
        }

    } /* while not interrupted */

    exit(0);
}
示例#6
0
    VideoIIDC1394::VideoIIDC1394(int source, uint8_t iso, uint8_t vid_mode, uint8_t f_rate, uint32_t w, uint32_t h)
    {
#if defined(DUNE_WITH_DC1394)

      std::cout << " Entered constructor " << std:: endl;

      width = w;

      height = h;

      iidc = dc1394_new();

      frame = NULL;

      std::cout << " Built Object " << std:: endl;

      dc1394error_t error_code;

      if (!iidc)
      {
        throw std::runtime_error("Failed to start IEEE 1394 IIDC interface");
      }

      error_code = dc1394_camera_enumerate(iidc, &list);

      std::cout << " Enumerated camera objects " << std:: endl;

      if (error_code < 0)
      {
        throw std::runtime_error("Failed to enumerate cameras");
      }

      std::cout << "Number of cameras found: " << list->num << std:: endl;

      camera = dc1394_camera_new(iidc, list->ids[0].guid);

      std::cout << " Built new camera object " << std:: endl;

      if (!camera)
      {
        throw std::runtime_error("Failed to initialize camera");
      }

      dc1394_camera_free_list(list);

      error_code = dc1394_video_set_iso_speed(camera, DC1394_ISO_SPEED_400);

      if (error_code < 0)
      {
        throw std::runtime_error("Failed to set ISO SPEED");
      }

      error_code = dc1394_video_set_mode(camera, DC1394_VIDEO_MODE_640x480_YUV422);
      if (error_code < 0)
      {
        throw std::runtime_error("Failed to set video mode");
      }

      error_code = dc1394_video_set_framerate(camera, DC1394_FRAMERATE_15);
      if (error_code < 0)
      {
        free(this);
        throw std::runtime_error("Failed to set ISO SPEED");
      }

      error_code = dc1394_capture_setup(camera, 8, DC1394_CAPTURE_FLAGS_DEFAULT);
      if (error_code < 0)
      {
        throw std::runtime_error("Failed to set ISO SPEED");
      }

      error_code = dc1394_video_set_transmission(camera, DC1394_ON);
      if (error_code < 0)
      {
        throw std::runtime_error("Could not start video capture");
      }

      converted_frame = new dc1394video_frame_t;
      std::memset(converted_frame, 0, sizeof(dc1394video_frame_t));

      std::cout << " Finished setting everything up " << std:: endl;

#else
      (void)source;
      (void)iso;
      (void)vid_mode;
      (void)f_rate;
      (void)w;
      (void)h;

      throw std::runtime_error("IIDC Firewire Video interface is not yet implemented in this system.");

#endif
    }
示例#7
0
int main(int argc, char *argv[])
{
    double lapse=0;
    double fps;
    double ntime=0, ptime=0, tmp;
    int index;
    dc1394video_frame_t *frame;
    dc1394error_t err;
    GLuint texture;
    TimeLapse_t timer;
    int cam_id=0;

    if ( argc!=1 && argc!=2) {
        fprintf(stderr,"Usage:\n\tdc1394_iso <optional camera_ID>\n");
        fprintf(stderr,"\t\tDefault values for histogram are min=%.0f, max=%.0f and %d bins\n\n",DEF_BIN_MIN, DEF_BIN_MAX, (int)DEF_BIN_N);
        exit(0);
    }

    signal(SIGINT, DisplayStatsAndExit);

    if (argc==2)
        cam_id=atoi(argv[1]);

    bin_n=DEF_BIN_N;
    bin_max=DEF_BIN_MAX;
    bin_min=DEF_BIN_MIN;
    bins = calloc(bin_n,sizeof(unsigned int));

    // init 1394
    d = dc1394_new();
    if (!d) return 1;
    // find cameras
    dc1394camera_list_t * list;
    err=dc1394_camera_enumerate (d, &list);
    DC1394_ERR_RTN(err,"Failed to enumerate cameras");
    if (list->num == 0) {
        fprintf(stderr,"No cameras found\n");
        return 1;
    }
    else {
        if (list->num<cam_id+1) {
            fprintf(stderr,"Not enough cameras found for id\n");
            return 1;
        }
    }
    // use selected camera
    camera = dc1394_camera_new (d, list->ids[cam_id].guid);
    if (!camera) {
        dc1394_log_error("Failed to initialize camera with guid %llx", list->ids[cam_id].guid);
        return 1;
    }
    dc1394_camera_free_list (list);
    // setup video format
    dc1394_video_set_transmission(camera, DC1394_OFF); // just in case
    dc1394_video_set_mode(camera, MODE);
    dc1394_video_set_iso_speed(camera, DC1394_ISO_SPEED_400);
    err=dc1394_format7_set_roi(camera, MODE, DC1394_COLOR_CODING_MONO8, PKT, 0, 0, SX, SY);
    if (err!=DC1394_SUCCESS) {
        fprintf(stderr,"Could not set ROI!\n");
        return 0;
    }
    // setup catpure
    err=dc1394_capture_setup(camera, 10, DC1394_CAPTURE_FLAGS_DEFAULT);
    if (err!=DC1394_SUCCESS) {
        fprintf(stderr,"Could not set capture!\n");
        return 0;
    }

    fps_min=9e6;
    fps_max=0;

    fprintf(stderr,"\t\t\t\t------------- FPS --------------        \n");
    fprintf(stderr,"Time:\t\tFrames:\t\tInst:\tMin:\tMax:\tAvg:\n");
    fprintf(stderr,"Stabilizing...\r");
    unsigned int stabilized=0;

    video=SDL_Start_GL(SX, SY);

    // start image transmission
    dc1394_video_set_transmission(camera, DC1394_ON);

    ResetTimer(&timer);

    // ----- ready to roll on all images! -----
    int quit = 0;
    while (!quit) {
        // capture image
        err=dc1394_capture_dequeue(camera, DC1394_CAPTURE_POLICY_WAIT, &frame);/* Capture */
        DC1394_ERR_RTN(err,"Problem getting an image");

        // display image
        glGenTextures( 1, &texture);
        glBindTexture( GL_TEXTURE_2D, texture);
        glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
        glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
        glTexImage2D( GL_TEXTURE_2D, 0, GL_LUMINANCE8, SX, SY, 0, GL_LUMINANCE, GL_UNSIGNED_BYTE, frame->image );

        // return buffer
        dc1394_capture_enqueue(camera, frame);

        glBegin( GL_QUADS );
        {
            glTexCoord2f(0, 0);
            glVertex3f(0, 0, 1 );
            glTexCoord2f(0, 1);
            glVertex3f(0, SY, 1 );
            glTexCoord2f(1, 1);
            glVertex3f(SX, SY, 1 );
            glTexCoord2f(1, 0);
            glVertex3f(SX, 0, 1 );
        }
        glEnd();
        SDL_GL_SwapBuffers();
        glDeleteTextures(1, &texture);

        if (ntime>0.1) // if ntime is valid...
            ptime=ntime;
        ntime=(double)frame->timestamp/1000000.0;

        // display the framerate
        nframes++;
        fps=1.0/(ntime-ptime+1e-20);
        tmp=GetTimeLapseSec(&timer);
        lapse+=tmp;
        if (lapse>STABILIZING_TIME && stabilized==0) {
            stabilized=1;
            lapse=tmp;
            nframes=1;
            fprintf(stderr,"                                       \r");
        }

        if (stabilized>0) {

            if (nframes>1) {
                fprintf(stderr,"%9.3f\t%7d   \t%-3.2f \t%3.2f \t%3.2f \t%3.3f     \r", lapse, nframes, fps, fps_min, fps_max, ((float)nframes)/(lapse+1e-10));
            }
            index=(int)((fps-bin_min)/((double)(bin_max-bin_min)/(double)bin_n));

            if (index>bin_n-1)
                index=bin_n-1;
            if (index<0)
                index=0;
            bins[index]++;
            if (fps_min>fps)
                fps_min=fps;
            if (fps_max<fps)
                fps_max=fps;
        }

        SDL_Event event;
        while (SDL_PollEvent(&event)) {
            switch (event.type) {
            case SDL_QUIT:
                quit = 1;
                break;
            }
        }
    } // --------------- END OF BIG IMAGE LOOP -------------------

    DisplayStatsAndExit(0);

    return 0;
}
示例#8
0
//=============================================================================
// setStereoVideoCapture()
//
// Given that the camera handle is a stereo camera, query the camera for 
// stereo specific information about this camera and populate the 
// PGRStereoCamera_t handle structure
//
// note: currently always allocated maximum framerate
dc1394error_t
setStereoVideoCapture( PGRStereoCamera_t* stereoCamera )
{
   dc1394error_t 	err;
   dc1394color_coding_t	coding;

   switch( stereoCamera->model )
   {
      case BUMBLEBEE:
	 dc1394video_mode_t 	videoMode;
	 dc1394framerate_t 	fps;

	 // note: both B&W and Bayer Bumblebees transmit in mono mode
	 if ( stereoCamera->nCols == 640 )
	 {
	    // lo res 
            printf("capture mode: DC1394_VIDEO_MODE_640x480_MONO16 ");
            printf("capture mode: %d\n", DC1394_VIDEO_MODE_640x480_MONO16);
	    videoMode 	= DC1394_VIDEO_MODE_640x480_MONO16;
	    fps		= DC1394_FRAMERATE_30;
	 }
	 else
	 {
            printf("capture mode: DC1394_VIDEO_MODE_1024x768_MONO16 ");
            printf("capture mode: %d\n", DC1394_VIDEO_MODE_1024x768_MONO16);
	    // assume hi res 
	    videoMode 	= DC1394_VIDEO_MODE_1024x768_MONO16;
	    fps		= DC1394_FRAMERATE_15;
	 }
	 
	 // make the calls to set up the capture mode
	 dc1394_video_set_iso_speed( stereoCamera->camera, DC1394_ISO_SPEED_400 );
	 dc1394_video_set_mode(      stereoCamera->camera, videoMode );
	 dc1394_video_set_framerate( stereoCamera->camera, fps );
	 err = dc1394_capture_setup( stereoCamera->camera, 8, DC1394_CAPTURE_FLAGS_DEFAULT );
	 if ( err != DC1394_SUCCESS ) 
	 {
	    fprintf( stderr, "Can't setup Bumblebee capture\n" );
	    return err;
	 }
	 break;

      case BUMBLEBEE2:
	 // Bumblebee2 transmits stereo images in Format 7

	 // load the factory defaults - this is auto-everything
	 err = dc1394_memory_load( stereoCamera->camera, 0 );
	 if ( err != DC1394_SUCCESS ) 
	 {
	    fprintf( stderr, "Can't load default memory channel\n" );
	    return err;
	 }

	 // set 16-bit transmission to be PGR-default little endian mode
	 err = setEndian( stereoCamera->camera, false );
	 if ( err != DC1394_SUCCESS ) 
	 {
	    fprintf( stderr, "Can't set Bumblebee2 into little-endian mode\n" );
	    return err;
	 }

	 // color cameras transmit in "RAW16", mono cameras in "MONO16"
	 coding	= DC1394_COLOR_CODING_MONO16;
	 if ( stereoCamera->bColor )
	    coding = DC1394_COLOR_CODING_RAW16;
	 
	 dc1394_video_set_iso_speed( stereoCamera->camera, DC1394_ISO_SPEED_400 );
	 dc1394_video_set_mode( stereoCamera->camera, DC1394_VIDEO_MODE_FORMAT7_3 );

	 err = dc1394_format7_set_roi( stereoCamera->camera,
				       DC1394_VIDEO_MODE_FORMAT7_3,
				       coding,
                                       // bytes per packet - sets frame rate
				       DC1394_USE_MAX_AVAIL, 
				       0, 
				       0,
				       stereoCamera->nCols,
				       stereoCamera->nRows );
	 if ( err != DC1394_SUCCESS ) 
	 {
	    fprintf( stderr, "Can't setup Bumblebee2 capture\n" );
	    return err;
	 }

	 err = dc1394_capture_setup( stereoCamera->camera, 8, DC1394_CAPTURE_FLAGS_DEFAULT );
	 if ( err != DC1394_SUCCESS ) 
	 {
	    fprintf( stderr, "Can't setup Bumblebee capture\n" );
	    return err;
	 }
	 break;

      case BUMBLEBEEXB3:
	 // Bumblebee3 transmits stereo images in Format 7

	 // load the factory defaults - this is auto-everything
	 err = dc1394_memory_load( stereoCamera->camera, 0 );
	 if ( err != DC1394_SUCCESS ) 
	 {
	    fprintf( stderr, "Can't load default memory channel\n" );
	    return err;
	 }


	 if ( stereoCamera->nBytesPerPixel == 2 )
	 {
	    // run in 2 camera mode

	    // NOTE: this code will always only set up the wide-baseline pair
	    // To change to the inner baseline pair you need to set the PAN
	    // register to 1.
	    // PAN = 0 is the wide baseline pair which is set by default.
	    // To change to all 3 images being transmitted you need to change
	    // modes to "coding	= DC1394_COLOR_CODING_RGB8;"
	    //

	    // set 16-bit transmission to be PGR-default little endian mode
	    err = setEndian( stereoCamera->camera, false );
	    if ( err != DC1394_SUCCESS ) 
	    {
	       fprintf( stderr, "Can't set Bumblebee2 into little-endian mode\n" );
	       return err;
	    }

	    // color cameras transmit in "RAW16", mono cameras in "MONO16"
	    coding	= DC1394_COLOR_CODING_MONO16;
	    if ( stereoCamera->bColor )
	       coding = DC1394_COLOR_CODING_RAW16;
	 }
	 else
	 {
	    // 3 camera mode transmits in RGB8
	    coding	= DC1394_COLOR_CODING_RGB8;
	 }

	 // assume the XB is plugged into a 1394B network
	 // XB3 can work with a 1394A bus but code changes will be required
	 dc1394_video_set_operation_mode( stereoCamera->camera, DC1394_OPERATION_MODE_1394B );
	 dc1394_video_set_iso_speed( stereoCamera->camera, DC1394_ISO_SPEED_800 );
	 dc1394_video_set_mode( stereoCamera->camera, DC1394_VIDEO_MODE_FORMAT7_3 );
	 err = dc1394_format7_set_roi( stereoCamera->camera,
				       DC1394_VIDEO_MODE_FORMAT7_3,
				       coding,
                                       // bytes per packet - sets frame rate
				       DC1394_USE_MAX_AVAIL, 
				       0, 
				       0,
				       stereoCamera->nCols,
				       stereoCamera->nRows );
	 if ( err != DC1394_SUCCESS ) 
	 {
	    fprintf( stderr, "Can't setup Bumblebee XB3 capture\n" );
	    return err;
	 }

	 err = dc1394_capture_setup( stereoCamera->camera, 4, DC1394_CAPTURE_FLAGS_DEFAULT );
	 if ( err != DC1394_SUCCESS ) 
	 {
	    fprintf( stderr, "Can't setup Bumblebee capture\n" );
	    return err;
	 }
	 break;

      default:
	 fprintf( stderr, 
		  "setStereoVideoCapture() - \n"
		  "\tThis camera is not a supported stereo camera!\n" );
	 return DC1394_FAILURE;
   }

   // get the bayer tile info so we will know how to color process
   // this mode
   err = getBayerTile( stereoCamera->camera,
		       &stereoCamera->bayerTile );
   if ( err != DC1394_SUCCESS )
   {
      fprintf( stderr, "Could not query the Bayer Tile Register!\n" );
      return err;
   }

   return DC1394_SUCCESS;
}
示例#9
0
// Note:
// the following was tested on a IIDC camera over USB therefore might not work as
// well on a camera over proper firewire transport
void FirewireVideo::init_format7_camera(
    uint64_t guid, int dma_frames,
    dc1394speed_t iso_speed,
    dc1394video_mode_t video_mode,
    int framerate,
    uint32_t width, uint32_t height,
    uint32_t left, uint32_t top, bool reset_at_boot
    ) {

    if(video_mode< DC1394_VIDEO_MODE_FORMAT7_0)
        throw VideoException("roi can be specified only for format7 modes");

    camera = dc1394_camera_new (d, guid);
    if (!camera)
        throw VideoException("Failed to initialize camera");

    // Attempt to stop camera if it is already running
    dc1394switch_t is_iso_on = DC1394_OFF;
    dc1394_video_get_transmission(camera, &is_iso_on);
    if (is_iso_on==DC1394_ON) {
        dc1394_video_set_transmission(camera, DC1394_OFF);
    }

    cout << "Using camera with GUID " << camera->guid << endl;

    if(reset_at_boot){
      dc1394_camera_reset(camera);
    }

    //-----------------------------------------------------------------------
    //  setup mode and roi
    //-----------------------------------------------------------------------

    if(iso_speed >= DC1394_ISO_SPEED_800)
    {
        err=dc1394_video_set_operation_mode(camera, DC1394_OPERATION_MODE_1394B);
        if( err != DC1394_SUCCESS )
           throw VideoException("Could not set DC1394_OPERATION_MODE_1394B");
    }

    err=dc1394_video_set_iso_speed(camera, iso_speed);
    if( err != DC1394_SUCCESS )
        throw VideoException("Could not set iso speed");

    // check that the required mode is actually supported
    dc1394format7mode_t format7_info;

    err = dc1394_format7_get_mode_info(camera, video_mode, &format7_info);
    if( err != DC1394_SUCCESS )
      throw VideoException("Could not get format7 mode info");

    // safely set the video mode
    err=dc1394_video_set_mode(camera, video_mode);
    if( err != DC1394_SUCCESS )
        throw VideoException("Could not set format7 video mode");

    // set position to 0,0 so that setting any size within min and max is a valid command
    err = dc1394_format7_set_image_position(camera, video_mode,0,0);
    if( err != DC1394_SUCCESS )
      throw VideoException("Could not set format7 image position");

    // work out the desired image size
    width = nearest_value(width, format7_info.unit_pos_x, 0, format7_info.max_size_x - left);
    height = nearest_value(height, format7_info.unit_pos_y, 0, format7_info.max_size_y - top);

    // set size
    err = dc1394_format7_set_image_size(camera,video_mode,width,height);
    if( err != DC1394_SUCCESS )
      throw VideoException("Could not set format7 size");

    // get the info again since many parameters depend on image size
    err = dc1394_format7_get_mode_info(camera, video_mode, &format7_info);
    if( err != DC1394_SUCCESS )
      throw VideoException("Could not get format7 mode info");

    // work out position of roi
    left = nearest_value(left, format7_info.unit_size_x, format7_info.unit_size_x, format7_info.max_size_x - width);
    top = nearest_value(top, format7_info.unit_size_y, format7_info.unit_size_y, format7_info.max_size_y - height);

    // set roi position
    err = dc1394_format7_set_image_position(camera,video_mode,left,top);
    if( err != DC1394_SUCCESS )
      throw VideoException("Could not set format7 size");

    this->width = width;
    this->height = height;
    this->top = top;
    this->left = left;

    cout<<"roi: "<<left<<" "<<top<<" "<<width<<" "<<height<<"  ";


    //-----------------------------------------------------------------------
    //  setup frame rate
    //-----------------------------------------------------------------------

    if((framerate == MAX_FR)||(framerate == EXT_TRIG)){

      err=dc1394_format7_set_packet_size(camera,video_mode, format7_info.max_packet_size);
      if( err != DC1394_SUCCESS )
        throw VideoException("Could not set format7 packet size");

    } else {

      // setting packet size to get the desired frame rate according to the libdc docs
      // does not do the trick, so for now we support only max frame rate

        throw VideoException("In format 7 only max frame rate is currently supported");
      //      uint32_t depth;
      //      err = dc1394_format7_get_data_depth(camera, video_mode, &depth);
      //      if( err != DC1394_SUCCESS )
      //        throw VideoException("Could not get format7 depth");
      //
      //      // the following is straight from the libdc docs
      //      double bus_period = bus_period_from_iso_speed(iso_speed);
      //
      //      // work out the max number of packets that the bus can deliver
      //      int num_packets = (int) (1.0/(bus_period*framerate) + 0.5);
      //
      //      if((num_packets > 4095)||(num_packets < 0))
      //        throw VideoException("number of format7 packets out of range");
      //
      //      // work out what the packet size should be for the requested size and framerate
      //      uint32_t packet_size = (width*964*depth + (num_packets*8) - 1)/(num_packets*8);
      //      packet_size = nearest_value(packet_size,format7_info.unit_packet_size,format7_info.unit_packet_size,format7_info.max_packet_size);
      //
      //      if(packet_size > format7_info.max_packet_size){
      //        throw VideoException("format7 requested frame rate and size exceed bus bandwidth");
      //      }
      //
      //      err=dc1394_format7_set_packet_size(camera,video_mode, packet_size);
      //      if( err != DC1394_SUCCESS ){
      //        throw VideoException("Could not set format7 packet size");
      //      }
    }

    // ask the camera what is the resulting framerate (this assume that such a rate is actually
    // allowed by the shutter time)
    err = dc1394_feature_set_power(camera,DC1394_FEATURE_FRAME_RATE,DC1394_OFF);
    if( err != DC1394_SUCCESS )
        throw VideoException("Could not turn off frame rate");

    float value;
    err=dc1394_feature_get_absolute_value(camera,DC1394_FEATURE_FRAME_RATE,&value);
    if( err != DC1394_SUCCESS )
        throw VideoException("Could not get framerate");

    cout<<" framerate(shutter permitting):"<<value<<endl;

    //-----------------------------------------------------------------------
    //  setup capture
    //-----------------------------------------------------------------------

    err=dc1394_capture_setup(camera,dma_frames, DC1394_CAPTURE_FLAGS_DEFAULT);
    if( err != DC1394_SUCCESS )
        throw VideoException("Could not setup camera - check settings");

    Start();

}
示例#10
0
int main(int argc, const char * argv[]) {

    dc1394_t *d;
    dc1394camera_list_t *list;
    dc1394error_t err;
    dc1394camera_t *camera;
    dc1394format7modeset_t modeset;
    dc1394video_frame_t *frame;
    FILE* imagefile;
    char filename[256];
    int i = 0;

    d = dc1394_new();
    if (!d) {
        return 1;
    }

    err = dc1394_camera_enumerate(d, &list);
    DC1394_ERR_RTN(err, "Failed to enumerate cameras");
    if (list->num == 0) {
        dc1394_log_error("No cameras found");
        dc1394_free(d);
        return 1;
    }
    printf("Detected %d cameras\n", list->num);

    // Assume that Ladybug 5 is detected as camera #0
    camera = dc1394_camera_new(d, list->ids[0].guid);
    if (!camera) {
        dc1394_log_error("Failed to initialize camera with guid %llx", list->ids[0].guid);
        dc1394_free(d);
    }
    dc1394_camera_free_list(list);
    printf("Using camera %s %s\n", camera->vendor, camera->model);

    // Report camera info
    err = dc1394_camera_print_info(camera, stdout);
    DC1394_ERR_RTN(err, "Could not print camera info");


    // Setup video mode, etc...
    err = dc1394_video_set_operation_mode(camera, DC1394_OPERATION_MODE_1394B);
    DC1394_ERR_RTN(err, "Could not set B mode");
    err = dc1394_video_set_iso_speed(camera, DC1394_ISO_SPEED_MAX);
    DC1394_ERR_RTN(err, "Could not set max speed");
    err = dc1394_video_set_mode(camera, DC1394_VIDEO_MODE_FORMAT7_0);
    DC1394_ERR_RTN(err, "Could not set DC1394_VIDEO_MODE_FORMAT7_0");

    // Get format7 mode info
    err = dc1394_format7_get_modeset(camera, &modeset);
    DC1394_ERR_RTN(err, "Could not get format 7 mode info\n");
    print_format7_info(&modeset);


    // Set format 7 roi
    err = dc1394_format7_set_roi(camera,
                                 DC1394_VIDEO_MODE_FORMAT7_0,
                                 modeset.mode[0].color_coding,
                                 modeset.mode[0].max_packet_size,
                                 modeset.mode[0].pos_x,
                                 modeset.mode[0].pos_y,
                                 modeset.mode[0].max_size_x,
                                 modeset.mode[0].max_size_y);
    DC1394_ERR_RTN(err, "Could not set max ROI");

    // Set capture
    err = dc1394_capture_setup(camera, 10, DC1394_CAPTURE_FLAGS_DEFAULT);
    DC1394_ERR_RTN(err, "Could not setup capture");
    err = dc1394_video_set_transmission(camera, DC1394_ON);
    DC1394_ERR_RTN(err, "Could not start transmission");

    while (i < NFRAMES) {
        // Capture image
        printf("Capturing image %d\n", i);
        err = dc1394_capture_dequeue(camera, DC1394_CAPTURE_POLICY_WAIT, &frame);
        DC1394_ERR_RTN(err, "Could not dequeue a frame");
        
        // Do something with the image
        print_frame_info(frame);

        // Save the image
        sprintf(filename, "%05d.pgm",i);
        imagefile = fopen(filename, "wb");
        if ( imagefile == NULL ) {
            printf("Could not save image\n");
            continue;
        }
        fprintf(imagefile, "P5\n%u %u 255\n", frame->size[0], frame->size[1]);
        fwrite(frame->image, 1, frame->image_bytes, imagefile);
        fclose(imagefile);
        printf("Saved image %s\n", filename);

        err = dc1394_capture_enqueue(camera, frame);
        DC1394_ERR_RTN(err, "Could enqueue a frame");

        i++;
    }

    dc1394_camera_free(camera);
    dc1394_free(d);
    return 0;
}
int main(int argc, char *argv[])
{
    // first check for the correct number of arguments
    if (argc != numArguments) {
        printf("Wrong number of arguments, exiting\n");
        exit(0);
    }
    
    time_t theTime = time(NULL);
    struct tm *aTime = localtime(&theTime);
    
    int day = aTime->tm_mday;
    int month = aTime->tm_mon + 1;
    int year = aTime->tm_year + 1900;
    int hour = aTime->tm_hour;
    int min = aTime->tm_min;
    int sec = aTime->tm_sec;
    
    char directory[128];
    sprintf(directory, "/opt/data_collection/%d_%d_%d_%d_%d_%d", year, month, day, hour, min, sec);
    char cmd[128];
    sprintf(cmd, "mkdir %s", directory);
    system(cmd);
    
    double lapse=0;
    double fps;
    double ntime=0, ptime=0, tmp;
    int index;
    dc1394video_frame_t *frame;
    dc1394error_t err;
    int cam_id=0;
    
    char fileName[128];

    // init 1394
    d = dc1394_new();
    if (!d) return 1;
    
    // find cameras
    dc1394camera_list_t * list;
    err=dc1394_camera_enumerate (d, &list);
    DC1394_ERR_RTN(err,"Failed to enumerate cameras");
    if (list->num == 0) {
        fprintf(stderr,"No cameras found\n");
        return 1;
    }
    else {
        if (list->num<cam_id+1) {
            fprintf(stderr,"Not enough cameras found for id\n");
            return 1;
        }
    }
    // use selected camera
    camera = dc1394_camera_new (d, list->ids[cam_id].guid);
    if (!camera) {
        dc1394_log_error("Failed to initialize camera with guid %llx", list->ids[cam_id].guid);
        return 1;
    }
    dc1394_camera_free_list (list);
    
    // setup video format
    dc1394video_mode_t video_mode;
    if (color == 0) {
        printf("video mode is MONO\n");
        video_mode = DC1394_VIDEO_MODE_640x480_MONO8;
    } else {
        printf("video mode is RGB8\n");
        video_mode = DC1394_VIDEO_MODE_640x480_RGB8;
    }
    
    dc1394framerate_t framerate = DC1394_FRAMERATE_7_5;
    
    //dc1394_video_set_transmission(video_mode, DC1394_OFF); // just in case
    dc1394_video_set_mode(camera, video_mode);
    dc1394_video_set_framerate(camera, framerate);
    dc1394_video_set_iso_speed(camera, DC1394_ISO_SPEED_400);
        
    // setup catpure
    err=dc1394_capture_setup(camera, 10, DC1394_CAPTURE_FLAGS_DEFAULT);
    if (err!=DC1394_SUCCESS) {
        fprintf(stderr,"Could not set capture!\n");
        return 0;
    }
    
    // start image transmission
    dc1394_video_set_transmission(camera, DC1394_ON);
    
    // first, see what we are controlling manually vs. auto
    autoExposure = atoi(argv[1]);
    autoGain = atoi(argv[3]);
    autoShutter = atoi(argv[5]);
    autoBrightness = atoi(argv[7]);

    // set what we want to control to manual...
    if (autoExposure == 1) {
        dc1394_feature_set_mode(camera, DC1394_FEATURE_EXPOSURE, DC1394_FEATURE_MODE_AUTO);
    } else {
        dc1394_feature_set_mode(camera, DC1394_FEATURE_EXPOSURE, DC1394_FEATURE_MODE_MANUAL);
    }
    
    if (autoGain == 1) {
        dc1394_feature_set_mode(camera, DC1394_FEATURE_GAIN, DC1394_FEATURE_MODE_AUTO);
    } else {
        dc1394_feature_set_mode(camera, DC1394_FEATURE_GAIN, DC1394_FEATURE_MODE_MANUAL);
    }

    if (autoShutter == 1) {
        dc1394_feature_set_mode(camera, DC1394_FEATURE_SHUTTER, DC1394_FEATURE_MODE_AUTO);
    } else {
        dc1394_feature_set_mode(camera, DC1394_FEATURE_SHUTTER, DC1394_FEATURE_MODE_MANUAL);
    }

    if (autoBrightness == 1) {
        dc1394_feature_set_mode(camera, DC1394_FEATURE_BRIGHTNESS, DC1394_FEATURE_MODE_AUTO);
    } else {
        dc1394_feature_set_mode(camera, DC1394_FEATURE_BRIGHTNESS, DC1394_FEATURE_MODE_MANUAL);
    }
        
    // print out the current features
    dc1394featureset_t  features;
    err=dc1394_feature_get_all(camera, &features);
    if (err!=DC1394_SUCCESS) {
        dc1394_log_warning("Could not get the feature set");
    }
    else {
        dc1394_feature_print_all(&features, stdout);
    }
    
    
    // set the exposure level
    if (argc == numArguments) {
        exposure = atoi(argv[2]);
    }
    
    err=dc1394_feature_set_value(camera, DC1394_FEATURE_EXPOSURE, exposure);
    if (err!=DC1394_SUCCESS) {
        printf("Could NOT set the exposure!\n");
    } else {
        printf("exposure set to %d\n", exposure);
    }

    // set the gain
    if (argc == numArguments) {
        gain = atoi(argv[4]);
    }
    
    err=dc1394_feature_set_value(camera, DC1394_FEATURE_GAIN, gain);
    if (err!=DC1394_SUCCESS) {
        printf("Could NOT set the gain!\n");
    } else {
        printf("gain set to %d\n", gain);
    }

    // set the shutter
    if (argc == numArguments) {
        shutter = atoi(argv[6]);
    }

    err=dc1394_feature_set_value(camera, DC1394_FEATURE_SHUTTER, shutter);
    if (err!=DC1394_SUCCESS) {
        printf("Could NOT set the shutter!\n");
    } else {
        printf("shutter set to %d\n", shutter);
    }
    
    // set the brightness
    if (argc == numArguments) {
        brightness = atoi(argv[8]);
    }
    
    err=dc1394_feature_set_value(camera, DC1394_FEATURE_BRIGHTNESS, brightness);
    if (err!=DC1394_SUCCESS) {
        printf("Could NOT set the brightness!\n");
    } else {
        printf("shutter set to %d\n", brightness);
    }

    // print out the current features    
    //dc1394featureset_t  features;
    err=dc1394_feature_get_all(camera, &features);
    if (err!=DC1394_SUCCESS) {
        dc1394_log_warning("Could not get the feature set");
    }
    else {
        dc1394_feature_print_all(&features, stdout);
    }
    
    // go ahead and log our settings to a text file in the directory we created
    FILE *file;
    char fileNameSettings[256];
    sprintf(fileNameSettings, "%s/settings.txt", directory);
    file = fopen(fileNameSettings, "a+");
    fprintf(file,"exposure=%d,%d\n", autoExposure, exposure);
    fprintf(file,"gain=%d,%d\n", autoGain, gain);
    fprintf(file,"shutter=%d,%d\n",autoShutter, shutter);
    fprintf(file,"brightness=%d,%d\n",autoBrightness, brightness);
    fclose(file);
    
    // start the main loop
    int quit = 0;
    while (!quit) {
        
        // capture image
        err=dc1394_capture_dequeue(camera, DC1394_CAPTURE_POLICY_WAIT, &frame);/* Capture */
        DC1394_ERR_RTN(err,"Problem getting an image");

        // return buffer
        dc1394_capture_enqueue(camera, frame);
        
        int t=0;
        if (color == 1) {
            t=3;
        } else {
            t=1;
        }
        
        IplImage *tmp = cvCreateImage(cvSize(640,480),8,t);
        memcpy(tmp->imageData, frame->image, 640*480*t);
        cvShowImage("image", tmp);
        char c = cvWaitKey(10);
        
        // save the image
        sprintf(fileName, "%s/frame%06d.bmp", directory, count);
        count++;
        cvSaveImage(fileName, tmp, 0);
        cvReleaseImage(&tmp);
        
    } // end loop

    return 0;
}
示例#12
0
int main(int argc, char *argv[])
{
    FILE* imagefile;
    dc1394camera_t *camera;
    unsigned int width, height;
    dc1394video_frame_t *frame=NULL;
    //dc1394featureset_t features;
    dc1394_t * d;
    dc1394camera_list_t * list;
    dc1394error_t err;

    d = dc1394_new ();
    err=dc1394_camera_enumerate (d, &list);
    DC1394_ERR_RTN(err,"Failed to enumerate cameras");

    if (list->num == 0) {
        dc1394_log_error("No cameras found");
        return 1;
    }

    camera = dc1394_camera_new (d, list->ids[0].guid);
    if (!camera) {
        dc1394_log_error("Failed to initialize camera with guid %llx", list->ids[0].guid);
        return 1;
    }
    dc1394_camera_free_list (list);

    printf("Using camera with GUID %"PRIx64"\n", camera->guid);

    /*-----------------------------------------------------------------------
     *  setup capture
     *-----------------------------------------------------------------------*/

    err=dc1394_video_set_iso_speed(camera, DC1394_ISO_SPEED_400);
    DC1394_ERR_CLN_RTN(err,cleanup_and_exit(camera),"Could not set iso speed");

    err=dc1394_video_set_mode(camera, DC1394_VIDEO_MODE_1280x960_RGB8);
    DC1394_ERR_CLN_RTN(err,cleanup_and_exit(camera),"Could not set video mode\n");

    err=dc1394_video_set_framerate(camera, DC1394_FRAMERATE_7_5);
    DC1394_ERR_CLN_RTN(err,cleanup_and_exit(camera),"Could not set framerate\n");

    err=dc1394_capture_setup(camera,4, DC1394_CAPTURE_FLAGS_DEFAULT);
    DC1394_ERR_CLN_RTN(err,cleanup_and_exit(camera),"Could not setup camera-\nmake sure that the video mode and framerate are\nsupported by your camera\n");

    /*-----------------------------------------------------------------------
     *  have the camera start sending us data
     *-----------------------------------------------------------------------*/
    err=dc1394_video_set_transmission(camera, DC1394_ON);
    DC1394_ERR_CLN_RTN(err,cleanup_and_exit(camera),"Could not start camera iso transmission\n");

    /*-----------------------------------------------------------------------
     *  capture one frame
     *-----------------------------------------------------------------------*/
    err=dc1394_capture_dequeue(camera, DC1394_CAPTURE_POLICY_WAIT, &frame);
    DC1394_ERR_CLN_RTN(err,cleanup_and_exit(camera),"Could not capture a frame\n");

    /*-----------------------------------------------------------------------
     *  stop data transmission
     *-----------------------------------------------------------------------*/
    err=dc1394_video_set_transmission(camera,DC1394_OFF);
    DC1394_ERR_CLN_RTN(err,cleanup_and_exit(camera),"Could not stop the camera?\n");

    /*-----------------------------------------------------------------------
     *  save image as 'Image.pgm'
     *-----------------------------------------------------------------------*/
    imagefile=fopen(IMAGE_FILE_NAME, "wb");

    if( imagefile == NULL) {
        perror( "Can't create output file");
        cleanup_and_exit(camera);
    }

    dc1394_get_image_size_from_video_mode(camera, DC1394_VIDEO_MODE_1280x960_RGB8, &width, &height);
    fprintf(imagefile,"P6\n%u %u\n255\n", width, height);
    fwrite(frame->image, 1, height*width*3, imagefile);
    fclose(imagefile);
    printf("wrote: " IMAGE_FILE_NAME " (%d image bytes)\n",height*width*3);

    /*-----------------------------------------------------------------------
     *  close camera
     *-----------------------------------------------------------------------*/
    dc1394_video_set_transmission(camera, DC1394_OFF);
    dc1394_capture_stop(camera);
    dc1394_camera_free(camera);
    dc1394_free (d);
    return 0;
}
示例#13
0
文件: pgr.c 项目: jbmulligan/quip
static int set_default_video_mode(PGR_Cam *pgcp)
{
	dc1394camera_t*	cam_p;
	dc1394video_mode_t	video_mode;
	int i;

	cam_p = pgcp->pc_cam_p;

	//  get the best video mode and highest framerate. This can be skipped
	//  if you already know which mode/framerate you want...
	// get video modes:

	if( dc1394_video_get_supported_modes( cam_p, &pgcp->pc_video_modes )
						!= DC1394_SUCCESS ){
		return -1;
	}

	// select highest res mode that is greyscale (MONO8)
	/*
	printf("Searching for the highest resolution MONO8 mode available (of %d modes)...\n",
		video_modes.num);
		*/

	dc1394color_coding_t coding;

	// assign an invalid value to video_mode to quiet compiler,
	// then check below to make sure a mode we want was found...

	video_mode = BAD_VIDEO_MODE;

//fprintf(stderr,"Checking %d video modes...\n",pgcp->pc_video_modes.num);
	for ( i = pgcp->pc_video_modes.num-1; i >= 0; i-- ) {
		// don't consider FORMAT 7 modes (i.e. "scalable")
		if ( !dc1394_is_video_mode_scalable( pgcp->pc_video_modes.modes[i] ) ) {
			dc1394_get_color_coding_from_video_mode( cam_p,
				pgcp->pc_video_modes.modes[i], &coding );
//fprintf(stderr,"Checking non-scalable mode %d\n",pgcp->pc_video_modes.modes[i]);
			if ( coding == DC1394_COLOR_CODING_MONO8 ) {
				video_mode = pgcp->pc_video_modes.modes[i];
				break;
			}
		} else {
//fprintf(stderr,"Not checking scalable mode %d\n",pgcp->pc_video_modes.modes[i]);
		}
	}
	if( video_mode == BAD_VIDEO_MODE ){	// only scalable modes?
		for ( i = pgcp->pc_video_modes.num-1; i >= 0; i-- ) {
			dc1394_get_color_coding_from_video_mode( cam_p, 
				pgcp->pc_video_modes.modes[i], &coding );
			if( coding == DC1394_COLOR_CODING_MONO8  ||
					coding == DC1394_COLOR_CODING_RAW8 ) {
				video_mode = pgcp->pc_video_modes.modes[i];
				break;
			}
		}
	}

	assert( video_mode != BAD_VIDEO_MODE );

#ifdef FOOBAR
	// double check that we found a video mode  that is MONO8
	dc1394_get_color_coding_from_video_mode( cam_p, video_mode, &coding );
	if ( ( dc1394_is_video_mode_scalable( video_mode ) ) ||
			( coding != DC1394_COLOR_CODING_MONO8 &&
			  coding != DC1394_COLOR_CODING_RAW8 ) ) {
		warn("Could not get a valid MONO8 mode" );
		return -1;
	}
#endif // FOOBAR

	dc1394_video_set_mode( pgcp->pc_cam_p, video_mode );
	pgcp->pc_video_mode = video_mode;

	return 0;
}
示例#14
0
int main(int argc, char *argv[]) {
    fitsfile *fptr;
    long fpixel=1, nelements, naxes[2];
    dc1394camera_t *camera;
    int grab_n_frames;
    struct timeval start_time, end_time;
    int i, j, status;
    unsigned int max_height, max_width;
    uint64_t total_bytes = 0;
    unsigned int width, height;
    dc1394video_frame_t *frame=NULL;
    dc1394_t * d;
    dc1394camera_list_t * list;
    dc1394error_t err;
    char *filename;
    unsigned char *buffer;
    float *average;

    grab_n_frames = atoi(argv[1]);
    filename = argv[2];
    status = 0;
    width = 320;
    height = 240;
    naxes[0] = width;
    naxes[1] = height;
    nelements = naxes[0]*naxes[1];

    stderr = freopen("grab_cube.log", "w", stderr);

    d = dc1394_new();
    if (!d)
        return 1;
    err = dc1394_camera_enumerate(d, &list);
    DC1394_ERR_RTN(err, "Failed to enumerate cameras.");

    if (list->num == 0) {
        dc1394_log_error("No cameras found.");
        return 1;
    }

    camera = dc1394_camera_new (d, list->ids[0].guid);
    if (!camera) {
        dc1394_log_error("Failed to initialize camera with guid %"PRIx64,
                         list->ids[0].guid);
        return 1;
    }
    dc1394_camera_free_list(list);

    printf("Using camera with GUID %"PRIx64"\n", camera->guid);

    /*-----------------------------------------------------------------------
     *  setup capture for format 7
     *-----------------------------------------------------------------------*/
    // err=dc1394_video_set_operation_mode(camera, DC1394_OPERATION_MODE_1394B);
    // DC1394_ERR_CLN_RTN(err,dc1394_camera_free (camera),"cannot operate at 1394B");

    // libdc1394 doesn't work well with firewire800 yet so set to legacy 400 mode
    dc1394_video_set_iso_speed(camera, DC1394_ISO_SPEED_400);

    // configure camera for format7
    err = dc1394_video_set_mode(camera, DC1394_VIDEO_MODE_FORMAT7_1);
    DC1394_ERR_CLN_RTN(err,dc1394_camera_free (camera),"cannot choose format7_0");

    err = dc1394_format7_get_max_image_size (camera, DC1394_VIDEO_MODE_FORMAT7_1,
            &max_width, &max_height);
    DC1394_ERR_CLN_RTN(err,dc1394_camera_free (camera),"cannot get max image size for format7_0");

    err = dc1394_format7_set_roi (camera,
                                  DC1394_VIDEO_MODE_FORMAT7_1,
                                  DC1394_COLOR_CODING_MONO8, // not sure why RAW8/16 don't work
                                  DC1394_USE_MAX_AVAIL,
                                  0, 0, // left, top
                                  width, height); // width, height
    DC1394_ERR_CLN_RTN(err,dc1394_camera_free (camera),"cannot set roi");

    // set the frame rate to absolute value in frames/sec
    err = dc1394_feature_set_mode(camera, DC1394_FEATURE_FRAME_RATE, DC1394_FEATURE_MODE_MANUAL);
    DC1394_ERR_CLN_RTN(err,dc1394_camera_free (camera),"cannot set framerate to manual");
    err = dc1394_feature_set_absolute_control(camera, DC1394_FEATURE_FRAME_RATE, DC1394_TRUE);
    DC1394_ERR_CLN_RTN(err,dc1394_camera_free (camera),"cannot set framerate to absolute mode");
    err = dc1394_feature_set_absolute_value(camera, DC1394_FEATURE_FRAME_RATE, 100.0);
    DC1394_ERR_CLN_RTN(err,dc1394_camera_free (camera),"cannot set framerate");

    // set the shutter speed to absolute value in seconds
    err = dc1394_feature_set_mode(camera, DC1394_FEATURE_SHUTTER, DC1394_FEATURE_MODE_MANUAL);
    DC1394_ERR_CLN_RTN(err,dc1394_camera_free (camera),"cannot set shutter to manual");
    err = dc1394_feature_set_absolute_control(camera, DC1394_FEATURE_SHUTTER, DC1394_TRUE);
    DC1394_ERR_CLN_RTN(err,dc1394_camera_free (camera),"cannot set shutter to absolute mode");
    err = dc1394_feature_set_absolute_value(camera, DC1394_FEATURE_SHUTTER, 1.0e-2);
    DC1394_ERR_CLN_RTN(err,dc1394_camera_free (camera),"cannot set shutter");

    // set gain manually.  use relative value here in range 48 to 730.
    err = dc1394_feature_set_mode(camera, DC1394_FEATURE_GAIN, DC1394_FEATURE_MODE_MANUAL);
    DC1394_ERR_CLN_RTN(err,dc1394_camera_free (camera),"cannot set gain to manual");
    err = dc1394_feature_set_value(camera, DC1394_FEATURE_GAIN, 200);
    DC1394_ERR_CLN_RTN(err,dc1394_camera_free (camera),"cannot set gain");

    // set brightness manually.  use relative value in range 0 to 1023.
    err = dc1394_feature_set_mode(camera, DC1394_FEATURE_BRIGHTNESS, DC1394_FEATURE_MODE_MANUAL);
    DC1394_ERR_CLN_RTN(err,dc1394_camera_free (camera),"cannot set brightness to manual");
    err = dc1394_feature_set_value(camera, DC1394_FEATURE_BRIGHTNESS, 50);
    DC1394_ERR_CLN_RTN(err,dc1394_camera_free (camera),"cannot set brightness");

    err = dc1394_format7_get_total_bytes (camera, DC1394_VIDEO_MODE_FORMAT7_1, &total_bytes);
    DC1394_ERR_CLN_RTN(err,dc1394_camera_free (camera),"cannot get total bytes");

    // err = dc1394_feature_set_value (camera, DC1394_FEATURE_GAIN, 24);
    //DC1394_ERR_CLN_RTN(err, dc1394_camera_free(camera), "Error setting gain");

    err = dc1394_capture_setup(camera, 16, DC1394_CAPTURE_FLAGS_DEFAULT);
    DC1394_ERR_CLN_RTN(err, dc1394_camera_free(camera), "Error capturing");

    /*-----------------------------------------------------------------------
     *  have the camera start sending us data
     *-----------------------------------------------------------------------*/
    err = dc1394_video_set_transmission(camera, DC1394_ON);
    if (err != DC1394_SUCCESS) {
        dc1394_log_error("unable to start camera iso transmission");
        dc1394_capture_stop(camera);
        dc1394_camera_free(camera);
        exit(1);
    }

    /* allocate the buffers */
    if (!(buffer = malloc(nelements*sizeof(char)))) {
        printf("Couldn't Allocate Image Buffer\n");
        exit(-1);
    }

    if (!(average = calloc(nelements, sizeof(float)))) {
        printf("Couldn't Allocate Average Image Buffer\n");
        exit(-1);
    }

    // set up FITS image and capture
    fits_create_file(&fptr, filename, &status);
    dc1394_get_image_size_from_video_mode(camera, DC1394_VIDEO_MODE_FORMAT7_1, &width, &height);
    fits_create_img(fptr, FLOAT_IMG, 2, naxes, &status);

    /*-----------------------------------------------------------------------
     *  capture frames and measure the time for this operation
     *-----------------------------------------------------------------------*/
    gettimeofday(&start_time, NULL);

    printf("Start capture:\n");

    for (i=0; i<grab_n_frames; ++i) {
        /*-----------------------------------------------------------------------
         *  capture one frame
         *-----------------------------------------------------------------------*/
        err = dc1394_capture_dequeue(camera, DC1394_CAPTURE_POLICY_WAIT, &frame);
        if (err != DC1394_SUCCESS) {
            dc1394_log_error("unable to capture");
            dc1394_capture_stop(camera);
            dc1394_camera_free(camera);
            exit(1);
        }

        memcpy(buffer, frame->image, nelements*sizeof(char));

        // release buffer
        dc1394_capture_enqueue(camera,frame);

        for (j=0; j<nelements; j++) {
            average[j] += (1.0/grab_n_frames)*(buffer[j]);
        }

    }

    gettimeofday(&end_time, NULL);
    printf("End capture.\n");

    /*-----------------------------------------------------------------------
     *  stop data transmission
     *-----------------------------------------------------------------------*/

    err = dc1394_video_set_transmission(camera, DC1394_OFF);
    DC1394_ERR_RTN(err,"couldn't stop the camera?");

    /*-----------------------------------------------------------------------
     *  save FITS image to disk
     *-----------------------------------------------------------------------*/
    fits_write_img(fptr, TFLOAT, fpixel, naxes[0]*naxes[1], average, &status);
    fits_close_file(fptr, &status);
    fits_report_error(stderr, status);
    free(buffer);
    free(average);

    printf("wrote: %s\n", filename);
    printf("Readout is %d bits/pixel.\n", frame->data_depth);

    /*-----------------------------------------------------------------------
     *  close camera, cleanup
     *-----------------------------------------------------------------------*/
    dc1394_capture_stop(camera);
    dc1394_video_set_transmission(camera, DC1394_OFF);
    dc1394_camera_free(camera);
    dc1394_free(d);
    return 0;
}
示例#15
0
static int capture_loop(dc1394camera_t *camera, const char *basename, float delay, bool testonly)
{
	uint16_t average;
	uint32_t num_saturated, num_half_saturated;
	float shutter = SHUTTER_GOOD;
	float gain = GAIN_GOOD;

	CHECK(dc1394_video_set_iso_speed(camera, DC1394_ISO_SPEED_400));
	CHECK(dc1394_video_set_mode(camera, DC1394_VIDEO_MODE_1280x960_MONO16));
	CHECK(dc1394_video_set_framerate(camera, DC1394_FRAMERATE_3_75));

	CHECK(dc1394_capture_setup(camera, 16, DC1394_CAPTURE_FLAGS_DEFAULT));

	CHECK(dc1394_feature_set_power(camera, DC1394_FEATURE_EXPOSURE, DC1394_OFF));

	CHECK(dc1394_feature_set_power(camera, DC1394_FEATURE_BRIGHTNESS, DC1394_ON));
	CHECK(dc1394_feature_set_mode(camera, DC1394_FEATURE_BRIGHTNESS, DC1394_FEATURE_MODE_MANUAL));
	CHECK(dc1394_feature_set_value(camera, DC1394_FEATURE_BRIGHTNESS, 0));

	CHECK(dc1394_feature_set_power(camera, DC1394_FEATURE_GAIN, DC1394_ON));
	CHECK(dc1394_feature_set_mode(camera, DC1394_FEATURE_GAIN, DC1394_FEATURE_MODE_MANUAL));
	CHECK(dc1394_feature_set_value(camera, DC1394_FEATURE_GAIN, 500));
	CHECK(dc1394_feature_set_absolute_control(camera, DC1394_FEATURE_GAIN, DC1394_ON));
	CHECK(dc1394_feature_set_absolute_value(camera, DC1394_FEATURE_GAIN, GAIN_GOOD));

	CHECK(dc1394_feature_set_power(camera, DC1394_FEATURE_SHUTTER, DC1394_ON));
	CHECK(dc1394_feature_set_mode(camera, DC1394_FEATURE_SHUTTER, DC1394_FEATURE_MODE_MANUAL));
	CHECK(dc1394_feature_set_value(camera, DC1394_FEATURE_SHUTTER, 500));
	CHECK(dc1394_feature_set_absolute_control(camera, DC1394_FEATURE_SHUTTER, DC1394_ON));
	CHECK(dc1394_feature_set_absolute_value(camera, DC1394_FEATURE_SHUTTER, SHUTTER_GOOD));

	CHECK(dc1394_video_set_transmission(camera, DC1394_ON));

	while (true) {
		alarm(10+(unsigned)delay);

		if (capture_image(camera, basename, shutter, gain, &average, 
				  &num_saturated, &num_half_saturated, testonly) == -1) {
			return -1;
		}

		if (num_saturated > SATURATED_HIGH) {
			/* too much saturation */
			if (gain > GAIN_MIN) {
				gain = new_gain(average, average*0.5, gain);
			} else if (shutter > SHUTTER_MIN) {
				shutter = new_shutter(average, average*0.5, shutter, SHUTTER_MAX);
			}
		} else if (average < AVERAGE_LOW && 
			   num_saturated == 0 && 
			   num_half_saturated < SATURATED_HIGH) {
			/* too dark */
			if (shutter < SHUTTER_GOOD) {
				float shutter2 = new_shutter(average, AVERAGE_TARGET, shutter, SHUTTER_GOOD);
				average = (shutter2/shutter)*average;
				shutter = shutter2;
			}
			if (average < AVERAGE_LOW) {
				if (gain < GAIN_MAX) {
					gain = new_gain(average, AVERAGE_TARGET, gain);
				} else if (shutter < SHUTTER_MAX) {
					shutter = new_shutter(average, AVERAGE_TARGET, shutter, SHUTTER_MAX);
				}
			}
		} else if (average > AVERAGE_HIGH) {
			/* too light */
			if (shutter > SHUTTER_GOOD) {
				float shutter2 = new_shutter(average, AVERAGE_TARGET, shutter, SHUTTER_GOOD);
				average = (shutter2/shutter)*average;
				shutter = shutter2;
			}
			if (average > AVERAGE_HIGH) {
				if (gain > GAIN_MIN) {
					gain = new_gain(average, AVERAGE_TARGET, gain);
				} else if (shutter > SHUTTER_MIN) {
					shutter = new_shutter(average, AVERAGE_TARGET, shutter, SHUTTER_MAX);
				}
			}
		}

		fflush(stdout);

		usleep(delay*1.0e6);
	}

	return 0;
}
示例#16
0
int main(int argc, char *argv[]) 
{
  // assumes your camera can output 640x480 with 8-bit monochrome
  video_mode = DC1394_VIDEO_MODE_640x480_MONO8;

  d = dc1394_new();
  if (!d)
    return 1;
  err=dc1394_camera_enumerate (d, &list);
  DC1394_ERR_RTN(err,"Failed to enumerate cameras");

  if (list->num == 0) {
    dc1394_log_error("No cameras found");
    return 1;
  }

  // use two counters so tht cameras array does not contain gaps in the case of errors
  j = 0;
  for (i = 0; i < list->num; i++) {
    if (j >= MAX_CAMERAS)
      break;
    cameras[j] = dc1394_camera_new (d, list->ids[i].guid);
    if (!cameras[j]) {
      dc1394_log_warning("Failed to initialize camera with guid %llx", list->ids[i].guid);
      continue;
    }
    j++;
  }
  numCameras = j;
  dc1394_camera_free_list (list);

  if (numCameras == 0) {
    dc1394_log_error("No cameras found");
    exit (1);
  }

  // setup cameras for capture
  for (i = 0; i < numCameras; i++) {
    err=dc1394_video_set_iso_speed(cameras[i], DC1394_ISO_SPEED_800);
    DC1394_ERR_CLN_RTN(err,cleanup(),"Could not set ISO speed");
    
    err=dc1394_video_set_mode(cameras[i], video_mode);
    DC1394_ERR_CLN_RTN(err,cleanup(),"Could not set video mode");
    
    err=dc1394_capture_setup(cameras[i],NUM_BUFFERS, DC1394_CAPTURE_FLAGS_DEFAULT);
    DC1394_ERR_CLN_RTN(err,cleanup(),"Could not setup camera-\nmake sure that the video mode and framerate are\nsupported by your camera");

    err=dc1394_get_image_size_from_video_mode(cameras[i], video_mode, &video_mode_width, &video_mode_height);
    DC1394_ERR_CLN_RTN(err,cleanup(),"Could not query video mode width and height");

    err=dc1394_video_set_one_shot(cameras[i], DC1394_ON);
    DC1394_ERR_CLN_RTN(err,cleanup(),"Could not use one shot mode");
  }

    fflush(stdout);
    if (numCameras < 1) {
        perror("no cameras found :(\n");
        cleanup();
        exit(-1);
    }

    for (i = 0; i < numCameras; i++) {
      if (dc1394_capture_dequeue(cameras[i], DC1394_CAPTURE_POLICY_WAIT, &frames[i])!=DC1394_SUCCESS)
	dc1394_log_error("Failed to capture from camera %d", i);

      // save image as '[GUID].pgm'
      char filename[256];
      sprintf(filename, "%" PRIu64 "%s",list->ids[i].guid,IMAGE_FILE_EXTENSION);
      imagefile=fopen(filename, "w");

      if( imagefile == NULL) {
	  dc1394_log_error("Can't create %s", filename);
	}
  
      // adding the pgm file header
      fprintf(imagefile,"P5\n%u %u 255\n", video_mode_width, video_mode_height);

      // writing to the file
      fwrite((const char *)frames[i]->image, 1, \
	     video_mode_width * video_mode_height, imagefile);
      fclose(imagefile);                                    
    }

  // exit cleanly
  cleanup();
  return(0);
}
示例#17
0
bool Libdc1394SequenceGrabber::initVideoMode(unsigned int w, unsigned int h, bool grey, unsigned int videomode, unsigned int color_mode)
{
	msg(osg::INFO) << "initVideoMode" << std::endl;

	std::vector<dc1394video_mode_t> suitable_modes;
	if (videomode != 0) 
	{
		suitable_modes.push_back((dc1394video_mode_t)videomode);
	}
	else 
	{
		if (w == 320) {
			if (!grey) suitable_modes.push_back(DC1394_VIDEO_MODE_320x240_YUV422);
		} else if (w == 640) {
			//if (!grey) suitable_modes.push_back(DC1394_VIDEO_MODE_640x480_RGB8);
			//if (!grey) suitable_modes.push_back(DC1394_VIDEO_MODE_640x480_YUV411);
			if (!grey) suitable_modes.push_back(DC1394_VIDEO_MODE_640x480_YUV422);
			if (grey) suitable_modes.push_back(DC1394_VIDEO_MODE_640x480_MONO8);
			//if (grey) suitable_modes.push_back(DC1394_VIDEO_MODE_640x480_MONO16);
		} else if (w == 800) {
			//if (!grey) suitable_modes.push_back(DC1394_VIDEO_MODE_800x600_RGB8);
			if (!grey) suitable_modes.push_back(DC1394_VIDEO_MODE_800x600_YUV422);
			if (grey) suitable_modes.push_back(DC1394_VIDEO_MODE_800x600_MONO8);
			//if (grey) suitable_modes.push_back(DC1394_VIDEO_MODE_800x600_MONO16);
		} else if (w == 1024) {
			//if (!grey) suitable_modes.push_back(DC1394_VIDEO_MODE_1024x768_RGB8);
			if (!grey) suitable_modes.push_back(DC1394_VIDEO_MODE_1024x768_YUV422);
			if (grey) suitable_modes.push_back(DC1394_VIDEO_MODE_1024x768_MONO8);
			//if (grey) suitable_modes.push_back(DC1394_VIDEO_MODE_1024x768_MONO16);
		} else if (w == 1280) {
			//if (!grey) suitable_modes.push_back(DC1394_VIDEO_MODE_1280x960_RGB8);
			if (!grey) suitable_modes.push_back(DC1394_VIDEO_MODE_1280x960_YUV422);
			if (grey) suitable_modes.push_back(DC1394_VIDEO_MODE_1280x960_MONO8);
			//if (grey) suitable_modes.push_back(DC1394_VIDEO_MODE_1280x960_MONO16);
		} else if (w == 1600) {
			//if (!grey) suitable_modes.push_back(DC1394_VIDEO_MODE_1600x1200_RGB8);
			if (!grey) suitable_modes.push_back(DC1394_VIDEO_MODE_1600x1200_YUV422);
			if (grey) suitable_modes.push_back(DC1394_VIDEO_MODE_1600x1200_MONO8);
			//if (grey) suitable_modes.push_back(DC1394_VIDEO_MODE_1600x1200_MONO16);
		}
	
	}
	dc1394video_modes_t video_modes;
	
    dc1394error_t err=dc1394_video_get_supported_modes(_camera,&video_modes);
    checkSuccess(err, "dc1394_video_get_supported_modes failed");
    
	for (unsigned int i = 0;i < video_modes.num;i++) 
	{
		for(unsigned int j=0; j < suitable_modes.size(); ++j) {
			if (video_modes.modes[i] == suitable_modes[j])
			{
				// videmodus gefunden, gleich setzen
				_videomode = video_modes.modes[i];
				err = dc1394_video_set_mode(_camera, video_modes.modes[i]);
				checkSuccess(err,"dc1394_video_set_mode failed");
                
                if (color_mode == 0) {
                    err = dc1394_get_color_coding_from_video_mode(_camera, _videomode, &_sourceFormat);
                    checkSuccess(err, "dc1394_get_color_coding_from_video_mode failed");
                } else {
                    _sourceFormat = (dc1394color_coding_t)(color_mode);
                }
                return true;
			}
		}
	}
	
	return false;
}
示例#18
0
 int main(int argc, char *argv[])
 {
     FILE* imagefile;
     dc1394camera_t *camera;
     unsigned int width, height;
     dc1394video_frame_t *frame=NULL;
     //dc1394featureset_t features;
     dc1394_t * d;
     dc1394camera_list_t * list;
     dc1394error_t err;
     int counter = 0;
     d = dc1394_new ();
     if (!d)
         return 1;
     err=dc1394_camera_enumerate (d, &list);
     DC1394_ERR_RTN(err,"Failed to enumerate cameras");

     if (list->num == 0) {
         dc1394_log_error("No cameras found");
         return 1;
     }

     camera = dc1394_camera_new (d, list->ids[0].guid);
     if (!camera) {
         dc1394_log_error("Failed to initialize camera with guid %llx", list->ids[0].guid);
         return 1;
     }
     dc1394_camera_free_list (list);

     printf("Using camera with GUID %" PRIx64 "\n", camera->guid);

     /*-----------------------------------------------------------------------
      *  setup capture
      *-----------------------------------------------------------------------*/

     err=dc1394_video_set_iso_speed(camera, DC1394_ISO_SPEED_400);
     DC1394_ERR_CLN_RTN(err,cleanup_and_exit(camera),"Could not set iso speed");

     err=dc1394_video_set_mode(camera, DC1394_VIDEO_MODE_FORMAT7_7);
     DC1394_ERR_CLN_RTN(err,cleanup_and_exit(camera),"Could not set video mode\n");

     err=dc1394_video_set_framerate(camera, DC1394_FRAMERATE_7_5);
     DC1394_ERR_CLN_RTN(err,cleanup_and_exit(camera),"Could not set framerate\n");

     err=dc1394_capture_setup(camera,4, DC1394_CAPTURE_FLAGS_DEFAULT);
     DC1394_ERR_CLN_RTN(err,cleanup_and_exit(camera),"Could not setup camera-\nmake sure that the video mode and framerate are\nsupported by your camera\n");

     /*-----------------------------------------------------------------------
      *  have the camera start sending us data
      *-----------------------------------------------------------------------*/
     err=dc1394_video_set_transmission(camera, DC1394_ON);
     DC1394_ERR_CLN_RTN(err,cleanup_and_exit(camera),"Could not start camera iso transmission\n");
     for(;;){
         /*-----------------------------------------------------------------------
          *  capture one frame
          *-----------------------------------------------------------------------*/
         err=dc1394_capture_dequeue(camera, DC1394_CAPTURE_POLICY_WAIT, &frame);
         DC1394_ERR_CLN_RTN(err,cleanup_and_exit(camera),"Could not capture a frame\n");

         dc1394_get_image_size_from_video_mode(camera, DC1394_VIDEO_MODE_FORMAT7_7, &width, &height);

         cv::Mat img = cv::Mat(height, width, CV_8U, frame->image);

         err = dc1394_capture_enqueue(camera, frame);
         frame=NULL;
         cv::imshow("Image", img);

         printf("Frame %d\n", counter);
         counter++;
         cv::waitKey(10);

      }
     /*-----------------------------------------------------------------------
      *  stop data transmission
      *-----------------------------------------------------------------------*/
     err=dc1394_video_set_transmission(camera,DC1394_OFF);
     DC1394_ERR_CLN_RTN(err,cleanup_and_exit(camera),"Could not stop the camera?\n");

     /*-----------------------------------------------------------------------
      *  close camera
      *-----------------------------------------------------------------------*/
     dc1394_video_set_transmission(camera, DC1394_OFF);
     dc1394_capture_stop(camera);
     dc1394_camera_free(camera);
     dc1394_free (d);
     return 0;
 }
示例#19
0
// Note:
// the following was tested on a IIDC camera over USB therefore might not work as
// well on a camera over proper firewire transport
void FirewireVideo::init_format7_camera(
    uint64_t guid, int dma_frames,
    dc1394speed_t iso_speed,
    dc1394video_mode_t video_mode,
    float framerate,
    uint32_t width, uint32_t height,
    uint32_t left, uint32_t top, bool reset_at_boot
    ) {

    if(video_mode< DC1394_VIDEO_MODE_FORMAT7_0)
        throw VideoException("roi can be specified only for format7 modes");

    camera = dc1394_camera_new (d, guid);
    if (!camera)
        throw VideoException("Failed to initialize camera");

    // Attempt to stop camera if it is already running
    dc1394switch_t is_iso_on = DC1394_OFF;
    dc1394_video_get_transmission(camera, &is_iso_on);
    if (is_iso_on==DC1394_ON) {
        dc1394_video_set_transmission(camera, DC1394_OFF);
    }

    cout << "Using camera with GUID " << camera->guid << endl;

    if(reset_at_boot){
      dc1394_camera_reset(camera);
    }

    //-----------------------------------------------------------------------
    //  setup mode and roi
    //-----------------------------------------------------------------------

    if(iso_speed >= DC1394_ISO_SPEED_800)
    {
        err=dc1394_video_set_operation_mode(camera, DC1394_OPERATION_MODE_1394B);
        if( err != DC1394_SUCCESS )
           throw VideoException("Could not set DC1394_OPERATION_MODE_1394B");
    }

    err=dc1394_video_set_iso_speed(camera, iso_speed);
    if( err != DC1394_SUCCESS )
        throw VideoException("Could not set iso speed");

    // check that the required mode is actually supported
    dc1394format7mode_t format7_info;

    err = dc1394_format7_get_mode_info(camera, video_mode, &format7_info);
    if( err != DC1394_SUCCESS )
      throw VideoException("Could not get format7 mode info");

    // safely set the video mode
    err=dc1394_video_set_mode(camera, video_mode);
    if( err != DC1394_SUCCESS )
        throw VideoException("Could not set format7 video mode");

    // set position to 0,0 so that setting any size within min and max is a valid command
    err = dc1394_format7_set_image_position(camera, video_mode,0,0);
    if( err != DC1394_SUCCESS )
      throw VideoException("Could not set format7 image position");

    // work out the desired image size
    width = nearest_value(width, format7_info.unit_pos_x, 0, format7_info.max_size_x - left);
    height = nearest_value(height, format7_info.unit_pos_y, 0, format7_info.max_size_y - top);

    // set size
    err = dc1394_format7_set_image_size(camera,video_mode,width,height);
    if( err != DC1394_SUCCESS )
      throw VideoException("Could not set format7 size");

    // get the info again since many parameters depend on image size
    err = dc1394_format7_get_mode_info(camera, video_mode, &format7_info);
    if( err != DC1394_SUCCESS )
      throw VideoException("Could not get format7 mode info");

    // work out position of roi
    left = nearest_value(left, format7_info.unit_size_x, format7_info.unit_size_x, format7_info.max_size_x - width);
    top = nearest_value(top, format7_info.unit_size_y, format7_info.unit_size_y, format7_info.max_size_y - height);

    // set roi position
    err = dc1394_format7_set_image_position(camera,video_mode,left,top);
    if( err != DC1394_SUCCESS )
      throw VideoException("Could not set format7 size");

    this->width = width;
    this->height = height;
    this->top = top;
    this->left = left;

    cout<<"roi: "<<left<<" "<<top<<" "<<width<<" "<<height<<"  ";


    //-----------------------------------------------------------------------
    //  setup frame rate
    //-----------------------------------------------------------------------

    err=dc1394_format7_set_packet_size(camera,video_mode, format7_info.max_packet_size);
    if( err != DC1394_SUCCESS )
      throw VideoException("Could not set format7 packet size");

    if((framerate != MAX_FR) && (framerate != EXT_TRIG)){
      //set the framerate by using the absolute feature as suggested by the
      //folks at PointGrey
      err = dc1394_feature_set_absolute_control(camera,DC1394_FEATURE_FRAME_RATE,DC1394_ON);
      if( err != DC1394_SUCCESS )
          throw VideoException("Could not turn on absolute frame rate control");

      err = dc1394_feature_set_mode(camera,DC1394_FEATURE_FRAME_RATE,DC1394_FEATURE_MODE_MANUAL);
      if( err != DC1394_SUCCESS )
          throw VideoException("Could not make frame rate manual ");

      err=dc1394_feature_set_absolute_value(camera,DC1394_FEATURE_FRAME_RATE,framerate);
      if( err != DC1394_SUCCESS )
          throw VideoException("Could not set format7 framerate ");
    }

    // ask the camera what is the resulting framerate (this assume that such a rate is actually
    // allowed by the shutter time)
    float value;
    err=dc1394_feature_get_absolute_value(camera,DC1394_FEATURE_FRAME_RATE,&value);
    if( err != DC1394_SUCCESS )
        throw VideoException("Could not get framerate");

    cout<<" framerate(shutter permitting):"<<value<<endl;

    //-----------------------------------------------------------------------
    //  setup capture
    //-----------------------------------------------------------------------

    err=dc1394_capture_setup(camera,dma_frames, DC1394_CAPTURE_FLAGS_DEFAULT);
    if( err != DC1394_SUCCESS )
        throw VideoException("Could not setup camera - check settings");

    Start();

}
bool Camera::open(int deviceIdx) {

    if (numCams < 1) {
        std::cerr << "Camera not found or could not be opened." << std::endl;
        return false;
    }

    camera = dc1394_camera_new(camDict, camList->ids[deviceIdx].guid);
    if (!camera) {
        dc1394_log_error("Failed to initialize camera with guid %.", camList->ids[deviceIdx].guid);
        return false;
    }

    /* reset camera to initial state with default values */
    resetCameraRegister();

    /* prepare camera for ringlight leds */
    configureOutputPins();
    configureClockDelay();
    configureClockDuration();
    configureResetDelay();
    configureResetDuration();

    /* set video mode */
    error = dc1394_video_set_mode(camera,  DC1394_VIDEO_MODE_640x480_MONO8);
    if (error != DC1394_SUCCESS) {
        std::cout << "Could not set video mode" << std::endl;
        cleanup(camera);
        return false;
    }

    /* camera parameter calibrated with FlyCapture2 */

    /* brightness */
    error = dc1394_feature_set_mode(camera, DC1394_FEATURE_BRIGHTNESS, DC1394_FEATURE_MODE_MANUAL);
    error = dc1394_feature_set_value(camera, DC1394_FEATURE_BRIGHTNESS, 0);

    /* exposure */
    error = dc1394_feature_set_mode(camera, DC1394_FEATURE_EXPOSURE, DC1394_FEATURE_MODE_MANUAL);
    error = dc1394_feature_set_value(camera, DC1394_FEATURE_EXPOSURE, 149);

    /* gamma */
    error = dc1394_feature_set_mode(camera, DC1394_FEATURE_GAMMA, DC1394_FEATURE_MODE_MANUAL);
    error = dc1394_feature_set_value(camera, DC1394_FEATURE_GAMMA, 1024);

    /* shutter */
    error = dc1394_feature_set_mode(camera, DC1394_FEATURE_SHUTTER, DC1394_FEATURE_MODE_MANUAL);
    error = dc1394_feature_set_value(camera, DC1394_FEATURE_SHUTTER, 61);

    /* gain */
    error = dc1394_feature_set_mode(camera, DC1394_FEATURE_GAIN, DC1394_FEATURE_MODE_MANUAL);
    error = dc1394_feature_set_value(camera, DC1394_FEATURE_GAIN, 740);

    /* frame rate */
    error = dc1394_feature_set_mode(camera, DC1394_FEATURE_FRAME_RATE, DC1394_FEATURE_MODE_MANUAL);
    error = dc1394_feature_set_absolute_value(camera, DC1394_FEATURE_FRAME_RATE, FRAME_RATE);

    /* setup capture */
    error = dc1394_capture_setup(camera, numDMABuffers, DC1394_CAPTURE_FLAGS_DEFAULT);
    if (error != DC1394_SUCCESS) {
        std::cerr << "Could not setup camera-\nmake sure that the video mode and framerate are\nsupported by your camera" << std::endl;
        cleanup(camera);
        return false;
    }

    /* starting camera sending data */
    error = dc1394_video_set_transmission(camera, DC1394_ON);
    if (error != DC1394_SUCCESS) {
        std::cerr << "Could not start camera iso transmission" << std::endl;
        cleanup(camera);
        return false;
    }
    
    /* capture image with no LEDs to subtract ambient light */
    captureAmbientImage();
    
    /* set average image intensity used by ps process for adjustment */
    avgImgIntensity = cv::mean(ambientImage)[0];

    return true;
}
示例#21
0
//--------------------------------------------------------------------
bool ofxVideoGrabberPtgrey::initGrabber(int w, int h, bool setUseTexture){
    width = w;
    height = h;
    bUseTexture = setUseTexture;
	
	// In format_7 the framerate is set by setting the packet_size
	// this is used in dc1394_format7_set_roi()
	// The following formula is from the libdc1394 faq
	// http://damien.douxchamps.net/ieee1394/libdc1394/v2.x/faq/#How_can_I_work_out_the_packet_size_for_a_wanted_frame_rate
	float bus_period = 0.000125;  // for firewire 400
	int frame_rate = 60;
	int depth = 3;
	int num_packets = (int)(1.0/(bus_period*frame_rate) + 0.5);
	//packet_size =  (width*height*depth + (num_packets*8) - 1)/(num_packets*8);
	packet_size = DC1394_USE_MAX_AVAIL;	

    //first choose a device to use
    if( bChooseDevice ) {
        bool bFound = false;
        for (uint32_t index = 0; index < deviceList->num; index++) {
            if (deviceList->ids[index].unit == deviceID) {
                bFound = true;
                deviceID = deviceList->ids[index].guid;
            }
        }
        if (!bFound) {
            printf("initGrabber warning: Device ID for unit %x not found, using first device\n", deviceID);
            deviceID = deviceList->ids[0].guid;
        }
    } else if(deviceList->num > 0) {
        deviceID = deviceList->ids[0].guid;
    } else {
        ofLog(OF_LOG_ERROR, "in initGrabber, No cameras found");
    }


    camera = dc1394_camera_new(driver, deviceID);
	if (!camera) {
		ofLog(OF_LOG_ERROR, "Failed to initialize camera with guid %x", deviceID);
		return 1;
	}

    /*-----------------------------------------------------------------------
     *  setup capture
     *-----------------------------------------------------------------------*/

    err = dc1394_video_set_iso_speed(camera, DC1394_ISO_SPEED_400);
    if( err != DC1394_SUCCESS ) { ofLog(OF_LOG_ERROR, "failed to set iso speed"); }
	
	err = dc1394_video_set_mode( camera, video_mode );
	if( err != DC1394_SUCCESS ) { ofLog(OF_LOG_ERROR, "failed to set format 7 video mode"); }
	
	err = dc1394_format7_set_color_coding(camera, video_mode, color_coding);
	if( err != DC1394_SUCCESS ) { ofLog(OF_LOG_ERROR, "failed to set format 7 color coding"); }
	
	err = dc1394_format7_set_packet_size(camera, video_mode, packet_size);
	if( err != DC1394_SUCCESS ) { ofLog(OF_LOG_ERROR, "failed to set format 7 packet_size"); }
	
	err = dc1394_format7_set_roi(camera, video_mode, color_coding, packet_size, 0,0, width,height);
	if( err != DC1394_SUCCESS ) {
		ofLog(OF_LOG_ERROR, "failed to set format 7");
	}

    //err = dc1394_video_set_framerate(camera, framerate);
	//DC1394_ERR_CLN_RTN(err,cleanup_and_exit(camera),"Could not set framerate");

    err = dc1394_capture_setup(camera,2, DC1394_CAPTURE_FLAGS_DEFAULT);
	if( err != DC1394_SUCCESS ) { ofLog(OF_LOG_ERROR, "failed to setup dma capturing"); }


	/*-----------------------------------------------------------------------
     *  set camera's features
     *-----------------------------------------------------------------------*/

	err = dc1394_feature_set_mode(camera, DC1394_FEATURE_BRIGHTNESS, DC1394_FEATURE_MODE_MANUAL);
	err = dc1394_feature_set_value(camera, DC1394_FEATURE_BRIGHTNESS, 200);  //1-255

	err = dc1394_feature_set_mode(camera, DC1394_FEATURE_EXPOSURE, DC1394_FEATURE_MODE_MANUAL);
	err = dc1394_feature_set_value(camera, DC1394_FEATURE_EXPOSURE, 62);  //7-62

	err = dc1394_feature_set_mode(camera, DC1394_FEATURE_SHUTTER, DC1394_FEATURE_MODE_MANUAL);
	err = dc1394_feature_set_value(camera, DC1394_FEATURE_SHUTTER, 60);  //1-263, 64 being the max for 60fps

	err = dc1394_feature_set_mode(camera, DC1394_FEATURE_GAIN, DC1394_FEATURE_MODE_MANUAL);
	err = dc1394_feature_set_value(camera, DC1394_FEATURE_GAIN, 50);  //16-64

	//err = dc1394_feature_set_mode(camera, DC1394_FEATURE_PAN, DC1394_FEATURE_MODE_MANUAL);
	//err = dc1394_feature_set_value(camera, DC1394_FEATURE_PAN, 56);


    /*-----------------------------------------------------------------------
     *  report camera's features
     *-----------------------------------------------------------------------*/
    err=dc1394_feature_get_all(camera,&features);
    if (err!=DC1394_SUCCESS) {
        dc1394_log_warning("Could not get feature set");
    }
    else {
        dc1394_feature_print_all(&features, stdout);
    }

    /*-----------------------------------------------------------------------
     *  have the camera start sending us data
     *-----------------------------------------------------------------------*/
    err=dc1394_video_set_transmission(camera, DC1394_ON);
    //DC1394_ERR_CLN_RTN(err,cleanup_and_exit(camera),"Could not start camera iso transmission");

	//create pixel buffer
    pixels	= new unsigned char[width * height * 3];

    //create texture
    if (bUseTexture) {
        tex.allocate(width,height,GL_LUMINANCE);
        memset(pixels, 0, width*height);
        tex.loadData(pixels, width, height, GL_LUMINANCE);
    }

}
示例#22
0
文件: ffmv_ccd.cpp 项目: A-j-K/indi
/**************************************************************************************
** Client is asking us to establish connection to the device
***************************************************************************************/
bool FFMVCCD::Connect()
{
    dc1394camera_list_t *list;
    dc1394error_t err;
    bool supported;
    bool settings_valid;
    uint32_t val;
    dc1394format7mode_t fm7;
    dc1394feature_info_t feature;
    float min, max;

    dc1394 = dc1394_new();
    if (!dc1394) {
        return false;
    }

    err = dc1394_camera_enumerate(dc1394, &list);
    if (err != DC1394_SUCCESS) {
        IDMessage(getDeviceName(), "Could not find DC1394 cameras!");
        return false;
    }
    if (!list->num) {
        IDMessage(getDeviceName(), "No DC1394 cameras found!");
        return false;
    }
    dcam = dc1394_camera_new(dc1394, list->ids[0].guid);
    if (!dcam) {
        IDMessage(getDeviceName(), "Unable to connect to camera!");
        return false;
    }

    /* Reset camera */
    err = dc1394_camera_reset(dcam);
    if (err != DC1394_SUCCESS) {
        IDMessage(getDeviceName(), "Unable to reset camera!");
        return false;
    }

    /* Set mode */
    err = dc1394_video_set_mode(dcam, DC1394_VIDEO_MODE_640x480_MONO16);
    if (err != DC1394_SUCCESS) {
        IDMessage(getDeviceName(), "Unable to connect to set videomode!");
        return false;
    }
    /* Disable Auto exposure control */
    err = dc1394_feature_set_power(dcam, DC1394_FEATURE_EXPOSURE, DC1394_OFF);
    if (err != DC1394_SUCCESS) {
        IDMessage(getDeviceName(), "Unable to disable auto exposure control");
        return false;
    }

    /* Set frame rate to the lowest possible */
    err = dc1394_video_set_framerate(dcam, DC1394_FRAMERATE_7_5);
    if (err != DC1394_SUCCESS) {
        IDMessage(getDeviceName(), "Unable to connect to set framerate!");
        return false;
    }
    /* Turn frame rate control off to enable extended exposure (subs of 512ms) */
    err = dc1394_feature_set_power(dcam, DC1394_FEATURE_FRAME_RATE, DC1394_OFF);
    if (err != DC1394_SUCCESS) {
        IDMessage(getDeviceName(), "Unable to disable framerate!");
        return false;
    }

    /* Get the longest possible exposure length */
    err = dc1394_feature_set_mode(dcam, DC1394_FEATURE_SHUTTER, DC1394_FEATURE_MODE_MANUAL);
    if (err != DC1394_SUCCESS) {
        IDMessage(getDeviceName(), "Failed to enable manual shutter control.");
    } 
    err = dc1394_feature_set_absolute_control(dcam, DC1394_FEATURE_SHUTTER, DC1394_ON);
    if (err != DC1394_SUCCESS) {
        IDMessage(getDeviceName(), "Failed to enable absolute shutter control.");
    } 
    err = dc1394_feature_get_absolute_boundaries(dcam, DC1394_FEATURE_SHUTTER, &min, &max);
    if (err != DC1394_SUCCESS) {
        IDMessage(getDeviceName(), "Could not get max shutter length");
    } else {
        max_exposure = max;
    }

    /* Set gain to max. By setting the register directly, we can achieve a
     * gain of 24 dB...compared to a gain of 12 dB which is reported as the
     * max
     */
    err = dc1394_set_control_register(dcam, 0x820, 0x40);
    //err = dc1394_set_control_register(dcam, 0x820, 0x7f);
    if (err != DC1394_SUCCESS) {
            return err;
    }
#if 0
    /* Set absolute gain to max */
    err = dc1394_feature_set_absolute_control(dcam, DC1394_FEATURE_GAIN, DC1394_ON);
    if (err != DC1394_SUCCESS) {
        IDMessage(getDeviceName(), "Failed to enable ansolute gain control.");
    } 
    err = dc1394_feature_get_absolute_boundaries(dcam, DC1394_FEATURE_GAIN, &min, &max);
    if (err != DC1394_SUCCESS) {
        IDMessage(getDeviceName(), "Could not get max gain value");
    } else {
        err = dc1394_feature_set_absolute_value(dcam, DC1394_FEATURE_GAIN, max);
        if (err != DC1394_SUCCESS) {
            IDMessage(getDeviceName(), "Could not set max gain value");
        }
    }
#endif

    /* Set brightness */
    err = dc1394_feature_set_mode(dcam, DC1394_FEATURE_BRIGHTNESS, DC1394_FEATURE_MODE_MANUAL);
    if (err != DC1394_SUCCESS) {
        IDMessage(getDeviceName(), "Failed to enable manual brightness control.");
    } 
    err = dc1394_feature_set_absolute_control(dcam, DC1394_FEATURE_BRIGHTNESS, DC1394_ON);
    if (err != DC1394_SUCCESS) {
        IDMessage(getDeviceName(), "Failed to enable ansolute brightness control.");
    } 
    err = dc1394_feature_set_absolute_value(dcam, DC1394_FEATURE_BRIGHTNESS, 1);
    if (err != DC1394_SUCCESS) {
            IDMessage(getDeviceName(), "Could not set max brightness value");
    }

    /* Turn gamma control off */
    err = dc1394_feature_set_absolute_value(dcam, DC1394_FEATURE_GAMMA, 1);
    if (err != DC1394_SUCCESS) {
            IDMessage(getDeviceName(), "Could not set gamma value");
    }
    err = dc1394_feature_set_power(dcam, DC1394_FEATURE_GAMMA, DC1394_OFF);
    if (err != DC1394_SUCCESS) {
        IDMessage(getDeviceName(), "Unable to disable gamma!");
        return false;
    }

    /* Turn off white balance */
    err = dc1394_feature_set_power(dcam, DC1394_FEATURE_WHITE_BALANCE, DC1394_OFF);
    if (err != DC1394_SUCCESS) {
        IDMessage(getDeviceName(), "Unable to disable white balance!");
        return false;
    }

    err=dc1394_capture_setup(dcam,10, DC1394_CAPTURE_FLAGS_DEFAULT);

    return true;
}
示例#23
0
文件: dc1394.c 项目: cobr123/qtVlc
/*****************************************************************************
 * Open:
 *****************************************************************************/
static int Open( vlc_object_t *p_this )
{
    demux_t      *p_demux = (demux_t*)p_this;
    demux_sys_t  *p_sys;
    es_format_t   fmt;
    dc1394error_t res;
    int           i_width;
    int           i_height;

    if( strncmp(p_demux->psz_access, "dc1394", 6) != 0 )
        return VLC_EGENERIC;

    /* Set up p_demux */
    p_demux->pf_demux = Demux;
    p_demux->pf_control = Control;
    p_demux->info.i_update = 0;
    p_demux->info.i_title = 0;
    p_demux->info.i_seekpoint = 0;

    p_demux->p_sys = p_sys = calloc( 1, sizeof( demux_sys_t ) );
    if( !p_sys )
        return VLC_ENOMEM;

    memset( &fmt, 0, sizeof( es_format_t ) );

    /* DEFAULTS */
    p_sys->video_mode      = DC1394_VIDEO_MODE_640x480_YUV422;
    p_sys->width           = 640;
    p_sys->height          = 480;
    p_sys->frame_rate      = DC1394_FRAMERATE_15;
    p_sys->brightness      = 200;
    p_sys->focus           = 0;
    p_sys->fd_audio        = -1;
    p_sys->p_dccontext     = NULL;
    p_sys->camera          = NULL;
    p_sys->selected_camera = -1;
    p_sys->dma_buffers     = 1;
    p_sys->reset_bus       = 0;

    /* PROCESS INPUT OPTIONS */
    if( process_options(p_demux) != VLC_SUCCESS )
    {
        msg_Err( p_demux, "Bad MRL, please check the option line "
                          "(MRL was: %s)",
                          p_demux->psz_path );
        free( p_sys );
        return VLC_EGENERIC;
    }

    p_sys->p_dccontext = dc1394_new();
    if( !p_sys->p_dccontext )
    {
        msg_Err( p_demux, "Failed to initialise libdc1394");
        free( p_sys );
        return VLC_EGENERIC;
    }

    if( FindCamera( p_sys, p_demux ) != VLC_SUCCESS )
    {
        dc1394_free( p_sys->p_dccontext );
        free( p_sys );
        return VLC_EGENERIC;
    }

    if( !p_sys->camera )
    {
        msg_Err( p_demux, "No camera found !!" );
        dc1394_free( p_sys->p_dccontext );
        free( p_sys );
        return VLC_EGENERIC;
    }

    if( p_sys->reset_bus )
    {
        if( dc1394_reset_bus( p_sys->camera ) != DC1394_SUCCESS )
        {
            msg_Err( p_demux, "Unable to reset IEEE 1394 bus");
            Close( p_this );
            return VLC_EGENERIC;
        }
        else msg_Dbg( p_demux, "Successfully reset IEEE 1394 bus");
    }

    if( dc1394_camera_reset( p_sys->camera ) != DC1394_SUCCESS )
    {
        msg_Err( p_demux, "Unable to reset camera");
        Close( p_this );
        return VLC_EGENERIC;
    }

    if( dc1394_camera_print_info( p_sys->camera,
                  stderr ) != DC1394_SUCCESS )
    {
        msg_Err( p_demux, "Unable to print camera info");
        Close( p_this );
        return VLC_EGENERIC;
    }

    if( dc1394_feature_get_all( p_sys->camera,
                &p_sys->features ) != DC1394_SUCCESS )
    {
        msg_Err( p_demux, "Unable to get feature set");
        Close( p_this );
        return VLC_EGENERIC;
    }
    // TODO: only print features if verbosity increased
    dc1394_feature_print_all(&p_sys->features, stderr);

#if 0 //"Note that you need to execute this function only if you use exotic video1394 device names. /dev/video1394, /dev/video1394/* and /dev/video1394-* are automatically recognized." http://damien.douxchamps.net/ieee1394/libdc1394/v2.x/api/capture/
    if( p_sys->video_device )
    {
        if( dc1394_capture_set_device_filename( p_sys->camera,
                        p_sys->video_device ) != DC1394_SUCCESS )
        {
            msg_Err( p_demux, "Unable to set video device");
            Close( p_this );
            return VLC_EGENERIC;
        }
    }
#endif

    if( p_sys->focus )
    {
        if( dc1394_feature_set_value( p_sys->camera,
                      DC1394_FEATURE_FOCUS,
                      p_sys->focus ) != DC1394_SUCCESS )
        {
            msg_Err( p_demux, "Unable to set initial focus to %u",
                     p_sys->focus );
        }
        else
            msg_Dbg( p_demux, "Initial focus set to %u", p_sys->focus );
    }

    if( dc1394_feature_set_value( p_sys->camera,
                  DC1394_FEATURE_FOCUS,
                  p_sys->brightness ) != DC1394_SUCCESS )
    {
        msg_Err( p_demux, "Unable to set initial brightness to %u",
                 p_sys->brightness );
    }
    else
        msg_Dbg( p_demux, "Initial brightness set to %u", p_sys->brightness );

    if( dc1394_video_set_framerate( p_sys->camera,
                    p_sys->frame_rate ) != DC1394_SUCCESS )
    {
        msg_Err( p_demux, "Unable to set framerate");
        Close( p_this );
        return VLC_EGENERIC;
    }

    if( dc1394_video_set_mode( p_sys->camera,
                   p_sys->video_mode ) != DC1394_SUCCESS )
    {
        msg_Err( p_demux, "Unable to set video mode");
        Close( p_this );
        return VLC_EGENERIC;
    }

    if( dc1394_video_set_iso_speed( p_sys->camera,
                    DC1394_ISO_SPEED_400 ) != DC1394_SUCCESS )
    {
        msg_Err( p_demux, "Unable to set iso speed");
        Close( p_this );
        return VLC_EGENERIC;
    }

    /* and setup capture */
    res = dc1394_capture_setup( p_sys->camera,
                    p_sys->dma_buffers,
                DC1394_CAPTURE_FLAGS_DEFAULT);
    if( res != DC1394_SUCCESS )
    {
        if( res == DC1394_NO_BANDWIDTH )
        {
            msg_Err( p_demux ,"No bandwidth: try adding the "
             "\"resetbus\" option" );
        }
        else
        {
            msg_Err( p_demux ,"Unable to setup capture" );
        }
        Close( p_this );
        return VLC_EGENERIC;
    }

    /* TODO - UYV444 chroma converter is missing, when it will be available
     * fourcc will become variable (and not just a fixed value for UYVY)
     */
    i_width = p_sys->width;
    i_height = p_sys->height;

    if( picture_Setup( &p_sys->pic, VLC_CODEC_UYVY,
                       i_width, i_height, 1, 1 ) )
    {
        msg_Err( p_demux ,"unknown chroma" );
        Close( p_this );
        return VLC_EGENERIC;
    }

    es_format_Init( &fmt, VIDEO_ES, VLC_CODEC_UYVY );

    fmt.video.i_width = i_width;
    fmt.video.i_height = i_height;

    msg_Dbg( p_demux, "Added new video es %4.4s %dx%d",
             (char*)&fmt.i_codec, fmt.video.i_width, fmt.video.i_height );

    p_sys->p_es_video = es_out_Add( p_demux->out, &fmt );

    if( p_sys->audio_device )
    {
        if( OpenAudioDev( p_demux ) == VLC_SUCCESS )
        {
            es_format_t fmt;
            es_format_Init( &fmt, AUDIO_ES, VLC_CODEC_S16L ); /* FIXME: hmm, ?? */

            fmt.audio.i_channels = p_sys->channels ? p_sys->channels : 1;
            fmt.audio.i_rate = p_sys->i_sample_rate;
            fmt.audio.i_bitspersample = 16;
            fmt.audio.i_blockalign = fmt.audio.i_channels *
                                     fmt.audio.i_bitspersample / 8;
            fmt.i_bitrate = fmt.audio.i_channels * fmt.audio.i_rate *
                            fmt.audio.i_bitspersample;

            msg_Dbg( p_demux, "New audio es %d channels %dHz",
            fmt.audio.i_channels, fmt.audio.i_rate );

            p_sys->p_es_audio = es_out_Add( p_demux->out, &fmt );
        }
    }

    /* have the camera start sending us data */
    if( dc1394_video_set_transmission( p_sys->camera,
                       DC1394_ON ) != DC1394_SUCCESS )
    {
        msg_Err( p_demux, "Unable to start camera iso transmission" );
        dc1394_capture_stop( p_sys->camera );
        Close( p_this );
        return VLC_EGENERIC;
    }
    msg_Dbg( p_demux, "Set iso transmission" );
    // TODO: reread camera
    return VLC_SUCCESS;
}
示例#24
0
bool CvCaptureCAM_DC1394_v2_CPP::startCapture()
{
    int i;
    int code = 0;
    if (!dcCam)
        return false;
    if (isoSpeed > 0)
    {
        code = dc1394_video_set_iso_speed(dcCam,
                                          isoSpeed <= 100 ? DC1394_ISO_SPEED_100 :
                                          isoSpeed <= 200 ? DC1394_ISO_SPEED_200 :
                                          isoSpeed <= 400 ? DC1394_ISO_SPEED_400 :
                                          isoSpeed <= 800 ? DC1394_ISO_SPEED_800 :
                                          isoSpeed == 1600 ? DC1394_ISO_SPEED_1600 :
                                          DC1394_ISO_SPEED_3200);
    }

    // should a specific mode be used
    if (userMode >= 0)

    {
        dc1394video_mode_t wantedMode;
        dc1394video_modes_t videoModes;
        dc1394_video_get_supported_modes(dcCam, &videoModes);

        //set mode from number, for example the second supported mode, i.e userMode = 1

        if (userMode < (int)videoModes.num)
        {
            wantedMode = videoModes.modes[userMode];
        }

        //set modes directly from DC134 constants (from dc1394video_mode_t)
        else if ((userMode >= DC1394_VIDEO_MODE_MIN) && (userMode <= DC1394_VIDEO_MODE_MAX ))
        {
            //search for wanted mode, to check if camera supports it
            int j = 0;
            while ((j< (int)videoModes.num) && videoModes.modes[j]!=userMode)
            {
                j++;
            }

            if ((int)videoModes.modes[j]==userMode)
            {
                wantedMode = videoModes.modes[j];
            }
            else
            {
                userMode = -1;  // wanted mode not supported, search for best mode
            }
        }
        else
        {
            userMode = -1;      // wanted mode not supported, search for best mode
        }
        //if userMode is available: set it and update size
        if (userMode != -1)
        {
            code = dc1394_video_set_mode(dcCam, wantedMode);
            uint32_t width, height;
            dc1394_get_image_size_from_video_mode(dcCam, wantedMode, &width, &height);
            frameWidth  = (int)width;
            frameHeight = (int)height;
        }
    }

    if (userMode == -1 && (frameWidth > 0 || frameHeight > 0))
    {
        dc1394video_mode_t bestMode = (dc1394video_mode_t) - 1;
        dc1394video_modes_t videoModes;
        dc1394_video_get_supported_modes(dcCam, &videoModes);
        for (i = 0; i < (int)videoModes.num; i++)
        {
            dc1394video_mode_t mode = videoModes.modes[i];
            if (mode >= DC1394_VIDEO_MODE_FORMAT7_MIN && mode <= DC1394_VIDEO_MODE_FORMAT7_MAX)
                continue;
            int pref = -1;
            dc1394color_coding_t colorCoding;
            dc1394_get_color_coding_from_video_mode(dcCam, mode, &colorCoding);

            uint32_t width, height;
            dc1394_get_image_size_from_video_mode(dcCam, mode, &width, &height);
            if ((int)width == frameWidth || (int)height == frameHeight)
            {
                if (colorCoding == DC1394_COLOR_CODING_RGB8 ||
                        colorCoding == DC1394_COLOR_CODING_RAW8)
                {
                    bestMode = mode;
                    break;
                }

                if (colorCoding == DC1394_COLOR_CODING_YUV411 ||
                        colorCoding == DC1394_COLOR_CODING_YUV422 ||
                        (colorCoding == DC1394_COLOR_CODING_YUV444 &&
                         pref < 1))
                {
                    bestMode = mode;
                    pref = 1;
                    break;
                }

                if (colorCoding == DC1394_COLOR_CODING_MONO8)
                {
                    bestMode = mode;
                    pref = 0;
                }
            }
        }
        if ((int)bestMode >= 0)
            code = dc1394_video_set_mode(dcCam, bestMode);
    }

    if (fps > 0)
    {
        dc1394video_mode_t mode;
        dc1394framerates_t framerates;
        double minDiff = DBL_MAX;
        dc1394framerate_t bestFps = (dc1394framerate_t) - 1;

        dc1394_video_get_mode(dcCam, &mode);
        dc1394_video_get_supported_framerates(dcCam, mode, &framerates);

        for (i = 0; i < (int)framerates.num; i++)
        {
            dc1394framerate_t ifps = framerates.framerates[i];
            double fps1 = (1 << (ifps - DC1394_FRAMERATE_1_875)) * 1.875;
            double diff = fabs(fps1 - fps);
            if (diff < minDiff)
            {
                minDiff = diff;
                bestFps = ifps;
            }
        }
        if ((int)bestFps >= 0)
            code = dc1394_video_set_framerate(dcCam, bestFps);
    }

    if (cameraId == VIDERE)
    {
        bayerFilter = DC1394_COLOR_FILTER_GBRG;
        nimages = 2;
        uint32_t value = 0;
        dc1394_get_control_register(dcCam, 0x50c, &value);
        colorStereo = (value & 0x80000000) != 0;
    }

    code = dc1394_capture_setup(dcCam, nDMABufs, DC1394_CAPTURE_FLAGS_DEFAULT);
    if (code >= 0)
    {
        FD_SET(dc1394_capture_get_fileno(dcCam), &dc1394.camFds);
        dc1394_video_set_transmission(dcCam, DC1394_ON);
        if (cameraId == VIDERE)
        {
            enum { PROC_MODE_OFF, PROC_MODE_NONE, PROC_MODE_TEST, PROC_MODE_RECTIFIED, PROC_MODE_DISPARITY, PROC_MODE_DISPARITY_RAW };
            int procMode = PROC_MODE_RECTIFIED;
            usleep(100000);
            uint32_t qval1 = 0x08000000 | (0x90 << 16) | ((procMode & 0x7) << 16);
            uint32_t qval2 = 0x08000000 | (0x9C << 16);
            dc1394_set_control_register(dcCam, 0xFF000, qval1);
            dc1394_set_control_register(dcCam, 0xFF000, qval2);
        }
        started = true;
    }

    return code >= 0;
}
示例#25
0
void DC1394Camera::init(core::ConfigNode config, uint64_t guid)
{
    LOGGER.infoStream() << guid << ": Initializing camera ";
    m_guid = guid;

    // Grab our camera 
    m_camera = dc1394_camera_new(s_libHandle, guid);
    if (!m_camera) {
        LOGGER.errorStream() <<
            "Failed to initialize camera with guid: " << guid;
        assert(m_camera && "Couldn't initialize camera");
    }

    // Determines settings and frame size
    dc1394error_t err = DC1394_FAILURE;
    dc1394video_mode_t videoMode = DC1394_VIDEO_MODE_640x480_RGB8;
    dc1394framerate_t frameRate = DC1394_FRAMERATE_15;

    // Check for the whitebalance feature
    dc1394feature_info_t whiteBalance;
    whiteBalance.id = DC1394_FEATURE_WHITE_BALANCE;
    err = dc1394_feature_get(m_camera, &whiteBalance);
    assert(DC1394_SUCCESS == err && "Could not get white balance feature info");
    bool hasWhiteBalance = (whiteBalance.available == DC1394_TRUE);

    uint32_t uValue, vValue;
    if (hasWhiteBalance)
    {
        err = dc1394_feature_whitebalance_get_value(m_camera, &uValue, &vValue);
        LOGGER.infoStream() << m_guid
                            << ": Current U: " << uValue
                            << " V: " << vValue;
    }
    
    // Actually set the values if the user wants to
    if (config.exists("uValue") && config.exists("vValue"))
    {
        bool uAuto =
            boost::to_lower_copy(config["uValue"].asString("auto")) == "auto";
        bool vAuto =
            boost::to_lower_copy(config["vValue"].asString("auto")) == "auto";
        bool autoVal = uAuto && vAuto;

        if ((uAuto || vAuto) && !autoVal)
        {
            assert(false && "Both Whitebalance values must either be auto or manual");
        }

        if (autoVal)
        {
            setWhiteBalance(0, 0, true);
        }
        else
        {
            // Read in config values
            uint32_t u_b_value = static_cast<uint32_t>(config["uValue"].asInt());
            uint32_t v_r_value = static_cast<uint32_t>(config["vValue"].asInt());

            // Set values
            setWhiteBalance(u_b_value, v_r_value);
        }
    }
    else if (config.exists("uValue") || config.exists("vValue"))
    {
        assert(false && "Must set both the U and V values for white balance");
    }

    err = dc1394_feature_whitebalance_get_value(m_camera, &uValue, &vValue);
    LOGGER.infoStream() << m_guid
                        << ": Set U: " << uValue 
                        << " V: " << vValue;


    
    if (config.exists("brightness"))
    {
        // Read in and set values
        if (boost::to_lower_copy(config["brightness"].asString("auto")) == "auto")
        {
            setBrightness(0, true);
        }
        else
        {
            uint32_t value = static_cast<uint32_t>(config["brightness"].asInt());
            setBrightness(value);
        }
    }

    if (config.exists("exposure"))
    {
        // Read in and set values
        if (boost::to_lower_copy(config["exposure"].asString("auto")) == "auto")
        {
            setExposure(0, true);
        }
        else
        {
            uint32_t value = static_cast<uint32_t>(config["exposure"].asInt());
            setExposure(value);
        }
    }


    
    // Grab image size
    err = dc1394_get_image_size_from_video_mode(m_camera, videoMode,
                                                &m_width, &m_height);
    assert(DC1394_SUCCESS == err && "Could not get image size");
    
    float fRate;
    err = dc1394_framerate_as_float(frameRate, &fRate);
    assert(DC1394_SUCCESS == err && "Could not get framerate as float");
    m_fps = fRate;

    // Setup the capture
    err = dc1394_video_set_iso_speed(m_camera, DC1394_ISO_SPEED_400);
    assert(DC1394_SUCCESS == err && "Could not set iso speed");
           
    err = dc1394_video_set_mode(m_camera, videoMode);
    assert(DC1394_SUCCESS == err && "Could not set video mode");

    err = dc1394_video_set_framerate(m_camera, frameRate);
    assert(DC1394_SUCCESS == err && "Could not set framerate");

    // Start data transfer
    err = dc1394_video_set_transmission(m_camera, DC1394_ON);
    assert(DC1394_SUCCESS == err && "Could not start camera iso transmission");

    err = dc1394_capture_setup(m_camera, DMA_BUFFER_SIZE,
                               DC1394_CAPTURE_FLAGS_DEFAULT);
    assert(DC1394_SUCCESS == err && "Could not setup camera make sure"
           " that the video mode and framerate are supported by your camera");
}