Issues with libdc1394

748 views Asked by At

I am trying to get images from Point Grey firewire 1394 camera using libdc1394. Here is the code.

#include <stdio.h>
#include <stdint.h>
#include <dc1394/dc1394.h>
#include <stdlib.h>
#include <inttypes.h>

#ifndef _WIN32
#include <unistd.h>
#endif

#define IMAGE_FILE_NAME "image.ppm"

/*-----------------------------------------------------------------------
 *  Releases the cameras and exits
 *-----------------------------------------------------------------------*/
void cleanup_and_exit(dc1394camera_t *camera)
{
    dc1394_video_set_transmission(camera, DC1394_OFF);
    dc1394_capture_stop(camera);
    dc1394_camera_free(camera);
    exit(1);
}

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 ();
    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_operation_mode(camera, DC1394_OPERATION_MODE_1394B);
    DC1394_ERR_CLN_RTN(err,cleanup_and_exit(camera),"Could not set 1394B mode");

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

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

    err=dc1394_format7_set_roi(camera, DC1394_VIDEO_MODE_FORMAT7_0, DC1394_COLOR_CODING_RAW16, DC1394_USE_MAX_AVAIL, 0, 0, 1024, 726);       
    DC1394_ERR_CLN_RTN(err,cleanup_and_exit(camera),"Could not ROI\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");


    /*-----------------------------------------------------------------------
     *  GIJS edit: show F7 info
     *-----------------------------------------------------------------------*/
    uint32_t unit_bytes;
    uint32_t max_bytes;
    err=dc1394_format7_get_packet_parameters(camera, DC1394_VIDEO_MODE_FORMAT7_0, &unit_bytes, &max_bytes );
    printf("\n[DEBUG] F7 Info\n");
    printf("[DEBUG] unit_byte: %d (error %d)\n", unit_bytes, err );   
    printf("[DEBUG] max_bytes: %d (error %d)\n", max_bytes, err );
    uint32_t packet_size;
    err=dc1394_format7_get_packet_size(camera, DC1394_VIDEO_MODE_FORMAT7_0, &packet_size );
    printf("[DEBUG] packet_size: %d (error %d)\n", packet_size, err );
    uint32_t packets_per_frame;
    err=dc1394_format7_get_packets_per_frame(camera, DC1394_VIDEO_MODE_FORMAT7_0, &packets_per_frame );     
    printf("[DEBUG] packets_per_frame: %d (error %d)\n", packets_per_frame, err );
    uint32_t pixels_per_frame;
    err=dc1394_format7_get_pixel_number(camera, DC1394_VIDEO_MODE_FORMAT7_0, &pixels_per_frame );   
    printf("[DEBUG] pixels_per_frame: %d (error %d)\n", pixels_per_frame, err );
    uint32_t recommended_packet_size;
    err=dc1394_format7_get_recommended_packet_size(camera, DC1394_VIDEO_MODE_FORMAT7_0, &recommended_packet_size );
    printf("[DEBUG] recommended_packet_size: %d (error %d)\n", recommended_packet_size, err );
    uint32_t total_bytes;
    err=dc1394_format7_get_total_bytes(camera, DC1394_VIDEO_MODE_FORMAT7_0, &total_bytes );
    printf("[DEBUG] total_size: %d (error %d)\n", total_bytes, err );

    /*-----------------------------------------------------------------------
     *  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");


    /*-----------------------------------------------------------------------
     *  check if frame is corrupt
     *-----------------------------------------------------------------------*/
    printf("\n[DEBUG] is frame corrupt: ");
    if( dc1394_capture_is_frame_corrupt( camera, frame) )
    printf("YES\n");
    else
    printf("NO\n");


    /*-----------------------------------------------------------------------
     *  show frame info
     *-----------------------------------------------------------------------*/
      printf("\n[DEBUG] Frame Info\n");
      printf("[DEBUG] image_bytes: %d\n",           frame->image_bytes);
      printf("[DEBUG] size[0]: %d\n",               frame->size[0]);
      printf("[DEBUG] size[1]: %d\n",               frame->size[1]);      
      printf("[DEBUG] allocated_image_bytes: %d\n", frame->allocated_image_bytes );
      printf("[DEBUG] total_bytes: %d\n",           frame->total_bytes );
      printf("[DEBUG] color_coding: %d\n",          frame->color_coding);     
      printf("[DEBUG] color_filter: %d\n",          frame->color_filter);
      printf("[DEBUG] packet_size: %d\n",           frame->packet_size);      
      printf("[DEBUG] packets_per_frame: %d\n",     frame->packets_per_frame);
      printf("[DEBUG] padding_bytes: %d\n",         frame->padding_bytes);
      printf("[DEBUG] timestamp: %d\n",             frame->timestamp);
      printf("[DEBUG] stride: %d\n",                frame->stride);
      printf("[DEBUG] data_depth: %d\n",            frame->data_depth);
      printf("[DEBUG] id: %d\n",                    frame->id);
      printf("[DEBUG] frames_behind: %d\n",         frame->frames_behind);
      printf("[DEBUG] image: %u\n",                 frame->image);    


    /*-----------------------------------------------------------------------
     *  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);
    }

    width  = 1104;
    height = 624;    
    fprintf(imagefile,"P6\n%u %u\n65536\n", width, height);
    int nbytes = fwrite(frame->image, 2, height*width, imagefile);
    fclose(imagefile);
    printf("wrote: " IMAGE_FILE_NAME " (%d image bytes)\n",nbytes*2);

    /*-----------------------------------------------------------------------
     *  close camera
     *-----------------------------------------------------------------------*/
    dc1394_video_set_transmission(camera, DC1394_OFF);
    dc1394_capture_stop(camera);
    dc1394_camera_free(camera);
    dc1394_free (d);
    return 0;
}

It compiled perfectly but when I tried to execute it. I got the following the following error.

libdc1394 error: Format_7 Error_flag_1 is set: in _dc1394_v130_handshake (format7.c, line 122): invalid image position, size, color coding or ISO speed

libdc1394 error: Format_7 Error_flag_1 is set: in dc1394_format7_set_roi (format7.c, line 803): Handshaking failed after setting color_coding

libdc1394 error: Format_7 Error_flag_1 is set: in main (test.c, line 90): Could not ROI

Can some one tell me how I can set dc1394_format7_set_roi(parametre) and dc1394_video_set_mode (parameter) to get stero images.

0

There are 0 answers