00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085 #include <sys/ioctl.h>
00086 #include <sys/types.h>
00087 #include <sys/stat.h>
00088 #include <sys/mman.h>
00089 #include <fcntl.h>
00090 #include <unistd.h>
00091 #include <stdio.h>
00092 #include <stdlib.h>
00093 #include <string.h>
00094 #include <linux/types.h>
00095 #include <libraw1394/raw1394.h>
00096 #include <libdc1394/dc1394_control.h>
00097 #include <AR/config.h>
00098 #include <AR/ar.h>
00099 #include <AR/video.h>
00100
00101
00102
00103
00104
00105 #undef LIBDC_8
00106 #undef LIBDC_9
00107 #undef LIBDC_10
00108 #undef LIBDC_11
00109 #undef LIBDC_DEF
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119 #define LIBDC_11
00120
00121
00122
00123
00124 #define DEFAULT_VIDEO_CARD -1
00125 #define MAX_PORTS 4
00126
00127
00128
00129
00130
00131 #define VIDEO_NODE_ANY -1
00132 #define DEFAULT_VIDEO_NODE VIDEO_NODE_ANY
00133 #define DEFAULT_VIDEO_MODE MODE_640x480_RGB
00134 #define DEFAULT_VIDEO_FRAME_RATE FRAMERATE_15
00135
00136
00137
00138
00139
00140 #ifdef LIBDC_8
00141 #warning Compiling using original 0.8.3 libDC library (single camera only) - debian: libdc1394-8-dev
00142 #warning The 0.8.3 libDC code is dangerous and has a number of bugs which will cause trouble - upgrade to 0.9.1 or later!
00143 #define LIBDC_DEF
00144 #endif
00145
00146 #ifdef LIBDC_9
00147 #warning Compiling using an older 0.9.1 libDC library (multiple camera support) - debian: libdc1394-9-dev
00148 #define LIBDC_DEF
00149 #endif
00150
00151 #ifdef LIBDC_10
00152 #warning Compiling using an older 0.9.5 libDC library (multiple camera support) - debian: libdc1394-10-dev
00153 #define LIBDC_DEF
00154 #endif
00155
00156 #ifdef LIBDC_11
00157
00158 #define LIBDC_DEF
00159 #endif
00160
00161 #ifndef LIBDC_DEF
00162 #error One of the LIBDC_[8,9,10,11] macros must be defined to compile this code properly!
00163 #endif
00164
00165
00166
00167
00168
00169 #include "conversions.h"
00170 int ar2Video_dragonfly = -1;
00171
00172
00173
00174
00175 static AR2VideoParamT *gVid = NULL;
00176
00177 int arVideoDispOption( void )
00178 {
00179 return ar2VideoDispOption();
00180 }
00181
00182 int arVideoOpen( char *config )
00183 {
00184 if( gVid != NULL ) {
00185 fprintf(stderr, "The device has already been opened!\n");
00186 exit (1);
00187 }
00188 gVid = ar2VideoOpen( config );
00189 if (gVid == NULL)
00190 return (-1);
00191
00192 return 0;
00193 }
00194
00195 int arVideoClose( void )
00196 {
00197 int result;
00198
00199 if( gVid == NULL ) return -1;
00200
00201 result = ar2VideoClose(gVid);
00202 gVid = NULL;
00203 return (result);
00204 }
00205
00206 int arVideoInqSize( int *x, int *y )
00207 {
00208 if( gVid == NULL ) return -1;
00209
00210 return ar2VideoInqSize( gVid, x, y );
00211 }
00212
00213 ARUint8 *arVideoGetImage( void )
00214 {
00215 if( gVid == NULL ) return NULL;
00216
00217 return ar2VideoGetImage( gVid );
00218 }
00219
00220 int arVideoCapStart( void )
00221 {
00222 if( gVid == NULL ) return -1;
00223
00224 return ar2VideoCapStart( gVid );
00225 }
00226
00227 int arVideoCapStop( void )
00228 {
00229 if( gVid == NULL ) return -1;
00230
00231 return ar2VideoCapStop( gVid );
00232 }
00233
00234 int arVideoCapNext( void )
00235 {
00236 if( gVid == NULL ) return -1;
00237
00238 return ar2VideoCapNext( gVid );
00239 }
00240
00241
00242
00243
00244 typedef struct __arVideo1394
00245 {
00246 raw1394handle_t handle;
00247 } ARVideo1394;
00248
00249 static ARVideo1394 arV1394;
00250 static int initFlag = 0;
00251
00252 static int ar2Video1394Init( int debug, int *card, int *node );
00253
00254
00255 int ar2VideoDispOption( void )
00256 {
00257 printf ("\n");
00258 printf("ARVideo may be configured using one or more of the following options, separated by a space:\n\n");
00259 printf(" -node=N\n");
00260 printf(" specifies detected node ID of a FireWire camera (-1: Any).\n");
00261 printf(" -card=N\n");
00262 printf(" specifies the FireWire adaptor id number (-1: Any).\n");
00263 printf(" -mode=[320x240_YUV422|640x480_RGB|640x480_YUV411]\n");
00264 printf(" specifies input image format.\n");
00265 printf(" -rate=N\n");
00266 printf(" specifies desired framerate of a FireWire camera. \n");
00267 printf(" (1.875, 3.75, 7.5, 15, 30, 60)\n");
00268 printf(" -[name]=N where name is 'iris' or 'gain'.\n");
00269 printf(" (value must be a legal value for this parameter - use coriander to find what they are\n");
00270 printf("\n");
00271 printf(" Note that if no config string is supplied, you can override it with the environment variable ARTOOLKIT_CONFIG\n");
00272 printf("\n");
00273
00274 return 0;
00275 }
00276
00277 AR2VideoParamT *ar2VideoOpen( char *config_in )
00278 {
00279 char video1394devname [128];
00280 AR2VideoParamT *vid;
00281 ARUint32 p1,p2;
00282 quadlet_t value;
00283 char *config, *a, line[256];
00284 int i;
00285
00286 int brightness = -1;
00287 int iris = -1;
00288 int shutter = -1;
00289 int gain = -1;
00290 int saturation = -1;
00291 int gamma = -1;
00292 int sharpness = -1;
00293
00294 arMalloc( vid, AR2VideoParamT, 1 );
00295 vid->node = DEFAULT_VIDEO_NODE;
00296 vid->card = DEFAULT_VIDEO_CARD;
00297 vid->mode = DEFAULT_VIDEO_MODE;
00298 vid->rate = DEFAULT_VIDEO_FRAME_RATE;
00299 vid->channel = 0;
00300 vid->speed = SPEED_400;
00301 vid->format = FORMAT_VGA_NONCOMPRESSED;
00302 vid->dma_buf_num = 16;
00303 vid->debug = 0;
00304 vid->status = 0;
00305
00306
00307 if (!config_in || !(config_in[0])) {
00308
00309 char *envconf = getenv ("ARTOOLKIT_CONFIG");
00310 if (envconf && envconf[0]) {
00311 config = envconf;
00312 printf ("Using config string from environment [%s].\n", envconf);
00313 } else {
00314 config = NULL;
00315 printf ("No video config string supplied, using default 640x480 RGB at 15 Hz.\n");
00316 }
00317 } else {
00318 config = config_in;
00319 printf ("Using supplied video config string [%s].\n", config_in);
00320 }
00321
00322 a = config;
00323 if( a != NULL) {
00324 for(;;) {
00325 while( *a == ' ' || *a == '\t' ) a++;
00326 if( *a == '\0' ) break;
00327
00328 if( strncmp( a, "-mode=", 6 ) == 0 ) {
00329 if ( strncmp( &a[6], "320x240_YUV422", 14 ) == 0 ) {
00330 vid->mode = MODE_320x240_YUV422;
00331 }
00332 else if ( strncmp( &a[6], "640x480_YUV411", 14 ) == 0 ) {
00333 vid->mode = MODE_640x480_YUV411;
00334 }
00335 else if ( strncmp( &a[6], "640x480_RGB", 11 ) == 0 ) {
00336 vid->mode = MODE_640x480_RGB;
00337 }
00338 else {
00339 fprintf (stderr, "Video mode string supplied [%s] is not parseable", &a[6]);
00340 ar2VideoDispOption();
00341 free( vid );
00342 return (NULL);
00343 }
00344 }
00345
00346 else if( strncmp( a, "-iris=", 6 ) == 0 ) {
00347 sscanf( a, "%s", line );
00348 if( sscanf( &line[6], "%d", &iris ) == 0 ) {
00349 fprintf (stderr, "Video iris value supplied [%s] is not parseable", &line[6]);
00350 ar2VideoDispOption();
00351 free( vid );
00352 return (NULL);
00353 }
00354 }
00355 else if( strncmp( a, "-gain=", 6 ) == 0 ) {
00356 sscanf( a, "%s", line );
00357 if( sscanf( &line[6], "%d", &gain ) == 0 ) {
00358 fprintf (stderr, "Video gain value supplied [%s] is not parseable", &line[6]);
00359 ar2VideoDispOption();
00360 free( vid );
00361 return (NULL);
00362 }
00363 }
00364
00365 else if( strncmp( a, "-node=", 6 ) == 0 ) {
00366 sscanf( a, "%s", line );
00367 if( sscanf( &line[6], "%d", &vid->node ) == 0 ) {
00368 fprintf (stderr, "Firewire node value supplied [%s] is not parseable", &line[6]);
00369 ar2VideoDispOption();
00370 free( vid );
00371 return (NULL);
00372 }
00373 }
00374 else if( strncmp( a, "-card=", 6 ) == 0 ) {
00375 sscanf( a, "%s", line );
00376 if( sscanf( &line[6], "%d", &vid->card ) == 0 ) {
00377 fprintf (stderr, "Firewire card value supplied [%s] is not parseable", &line[6]);
00378 ar2VideoDispOption();
00379 free( vid );
00380 return (NULL);
00381 }
00382 }
00383 else if( strncmp( a, "-rate=", 6 ) == 0 ) {
00384 if ( strncmp( &a[6], "1.875", 5 ) == 0 ) {
00385 vid->rate = FRAMERATE_1_875;
00386 }
00387 else if ( strncmp( &a[6], "3.75", 4 ) == 0 ) {
00388 vid->rate = FRAMERATE_3_75;
00389 }
00390 else if ( strncmp( &a[6], "7.5", 3 ) == 0 ) {
00391 vid->rate = FRAMERATE_7_5;
00392 }
00393 else if ( strncmp( &a[6], "15", 2 ) == 0 ) {
00394 vid->rate = FRAMERATE_15;
00395 }
00396 else if ( strncmp( &a[6], "30", 2 ) == 0 ) {
00397 vid->rate = FRAMERATE_30;
00398 }
00399 else if ( strncmp( &a[6], "60", 2 ) == 0 ) {
00400 vid->rate = FRAMERATE_60;
00401 }
00402 else {
00403 fprintf (stderr, "Frame rate value supplied [%s] is not parseable", &a[6]);
00404 ar2VideoDispOption();
00405 free( vid );
00406 return (NULL);
00407 }
00408 }
00409 else if( strncmp( a, "-debug", 6 ) == 0 ) {
00410 vid->debug = 1;
00411 }
00412 else if( strncmp( a, "-adjust", 7 ) == 0 ) {
00413
00414 }
00415 else {
00416 fprintf (stderr, "Unknown config option supplied [%s]", a);
00417 ar2VideoDispOption();
00418 free( vid );
00419 return (NULL);
00420 }
00421
00422 while( *a != ' ' && *a != '\t' && *a != '\0') a++;
00423 }
00424 }
00425
00426
00427 if( initFlag == 0 )
00428 {
00429 if( ar2Video1394Init(vid->debug, &vid->card, &vid->node) < 0 )
00430 {
00431 fprintf (stderr, "Could not initialise 1394 bus with card=%d and node=%d\n", vid->card, vid->node);
00432 return (NULL);
00433 }
00434 initFlag = 1;
00435 }
00436
00437
00438
00439
00440
00441 if( dc1394_get_camera_feature_set(arV1394.handle,
00442 vid->node,
00443 &(vid->features)) != DC1394_SUCCESS ) {
00444 fprintf( stderr, "Unable to get feature set from device\n");
00445 return (NULL);
00446 }
00447 else if( vid->debug ) {
00448 dc1394_print_feature_set( &(vid->features) );
00449 }
00450
00451
00452
00453 if (iris != -1)
00454 {
00455 fprintf (stderr, "Adjusting IRIS setting to %d\n", iris);
00456 dc1394_set_iris (arV1394.handle, vid->node, (unsigned int)iris);
00457 }
00458 if (gain != -1)
00459 {
00460 fprintf (stderr, "Adjusting GAIN setting to %d\n", gain);
00461 dc1394_set_gain (arV1394.handle, vid->node, (unsigned int)gain);
00462 }
00463
00464
00465
00466
00467
00468
00469
00470
00471
00472
00473 if( dc1394_query_supported_formats(arV1394.handle, vid->node, &value) != DC1394_SUCCESS ) {
00474 fprintf( stderr, "Unable to perform a query_supported_formats call\n");
00475 return (NULL);
00476 }
00477 i = 31 - (FORMAT_VGA_NONCOMPRESSED - FORMAT_MIN);
00478 p1 = 1 << i;
00479 p2 = value & p1;
00480 if( p2 == 0 ) {
00481 fprintf( stderr, "Unable to use this camera on VGA_NONCOMPRESSED format.\n");
00482 return (NULL);
00483 }
00484
00485
00486 dc1394_query_supported_modes(arV1394.handle, vid->node, FORMAT_VGA_NONCOMPRESSED, &value);
00487 i = 31 - (vid->mode - MODE_FORMAT0_MIN);
00488 p1 = 1 << i;
00489 p2 = value & p1;
00490 if( p2 == 0 )
00491 {
00492
00493 i = 31 - (MODE_640x480_MONO - MODE_FORMAT0_MIN);
00494 p1 = 1 << i;
00495 p2 = value & p1;
00496 if (p2 == 0)
00497 {
00498 fprintf( stderr, "Unsupported Mode for the specified camera.\n");
00499 ar2VideoDispOption();
00500 return (NULL);
00501 }
00502 else
00503 {
00504 fprintf (stderr, "Detected a mono camera, assuming DragonFly camera with Bayer image decoding\n");
00505 vid->mode = MODE_640x480_MONO;
00506 ar2Video_dragonfly = 1;
00507 }
00508 }
00509
00510 dc1394_query_supported_framerates(arV1394.handle, vid->node, FORMAT_VGA_NONCOMPRESSED, vid->mode, &value);
00511 i = 31 - (vid->rate - FRAMERATE_MIN);
00512 p1 = 1 << i;
00513 p2 = value & p1;
00514 if( p2 == 0 ) {
00515 fprintf( stderr, "Unsupported framerate for the specified mode.\n");
00516 ar2VideoDispOption();
00517 return (NULL);
00518 }
00519
00520
00521
00522
00523 struct stat video_stat;
00524 if (stat ("/dev/video1394", &video_stat) < 0)
00525 sprintf (video1394devname, "/dev/video1394-%d", vid->card);
00526 else
00527 sprintf (video1394devname, "/dev/video1394/%d", vid->card);
00528
00529
00530
00531
00532
00533 if( dc1394_dma_setup_capture(arV1394.handle,
00534 vid->node,
00535 #ifndef LIBDC_8
00536 vid->node,
00537 #else
00538 vid->channel,
00539 #endif
00540 vid->format,
00541 vid->mode,
00542 vid->speed,
00543 vid->rate,
00544 vid->dma_buf_num,
00545 #ifdef LIBDC_10
00546 0,
00547 #endif
00548 #ifndef LIBDC_8
00549 1, video1394devname,
00550 #endif
00551 &(vid->camera)) != DC1394_SUCCESS ) {
00552 fprintf (stderr, "Unable to setup initial camera DMA transfer, check that the video1394 module is loaded, and that the video mode, frame rate, and format are supported by your camera");
00553 return (NULL);
00554 }
00555
00556
00557 if( dc1394_set_trigger_mode(arV1394.handle, vid->node, TRIGGER_MODE_0) != DC1394_SUCCESS ) {
00558 fprintf( stderr, "unable to set camera trigger mode (ignored)\n");
00559 }
00560
00561 arMalloc( vid->image, ARUint8, (vid->camera.frame_width * vid->camera.frame_height * AR_PIX_SIZE_DEFAULT) );
00562
00563 return vid;
00564 }
00565
00566 int ar2VideoClose( AR2VideoParamT *vid )
00567 {
00568 int i;
00569
00570 if( vid->status > 0 ) ar2VideoCapStop( vid );
00571
00572 #if 0
00573 dc1394_dma_release_camera(arV1394.handle, &(vid->camera));
00574 #endif
00575 free( vid->image );
00576 free( vid );
00577
00578 raw1394_destroy_handle(arV1394.handle);
00579 initFlag = 0;
00580
00581 return 0;
00582 }
00583
00584 int ar2VideoCapStart( AR2VideoParamT *vid )
00585 {
00586 char video1394devname [128];
00587
00588
00589 if(vid->status != 0 && vid->status != 3){
00590 fprintf(stderr, "arVideoCapStart has already been called.\n");
00591 return -1;
00592 }
00593
00594
00595
00596
00597 struct stat video_stat;
00598 if (stat ("/dev/video1394", &video_stat) < 0)
00599 sprintf (video1394devname, "/dev/video1394-%d", vid->card);
00600 else
00601 sprintf (video1394devname, "/dev/video1394/%d", vid->card);
00602 if( vid->status == 3 ) {
00603 if( dc1394_dma_setup_capture(arV1394.handle,
00604 vid->node,
00605 #ifndef LIBDC_8
00606 vid->node,
00607 #else
00608 vid->channel,
00609 #endif
00610 vid->format,
00611 vid->mode,
00612 vid->speed,
00613 vid->rate,
00614 vid->dma_buf_num,
00615 #ifdef LIBDC_10
00616 0,
00617 #endif
00618 #ifndef LIBDC_8
00619 1, video1394devname,
00620 #endif
00621 &(vid->camera)) != DC1394_SUCCESS ) {
00622
00623 fprintf (stderr, "Unable to setup initial camera DMA transfer, check that the video1394 module is loaded, and that the video mode, frame rate, and format are supported by your camera");
00624 return (-1);
00625 }
00626 }
00627
00628 if( dc1394_start_iso_transmission(arV1394.handle, vid->node) != DC1394_SUCCESS ) {
00629 fprintf( stderr, "unable to start camera iso transmission\n");
00630 return -1;
00631 }
00632
00633 vid->status = 1;
00634
00635 return 0;
00636 }
00637
00638 int ar2VideoCapNext( AR2VideoParamT *vid )
00639 {
00640 if(vid->status == 0 || vid->status == 3){
00641 fprintf(stderr, "arVideoCapStart has never been called.\n");
00642 return -1;
00643 }
00644 if(vid->status == 2) vid->status = 1;
00645
00646 dc1394_dma_done_with_buffer( &(vid->camera) );
00647
00648 return 0;
00649 }
00650
00651 int ar2VideoCapStop( AR2VideoParamT *vid )
00652 {
00653 if(vid->status == 2){
00654 if( dc1394_dma_single_capture( &(vid->camera) ) != DC1394_SUCCESS ) {
00655 fprintf( stderr, "unable to capture a frame\n");
00656 }
00657 }
00658 if(vid->status == 0){
00659 fprintf(stderr, "arVideoCapStart has never been called.\n");
00660 return -1;
00661 }
00662 vid->status = 3;
00663
00664 if( dc1394_stop_iso_transmission(arV1394.handle, vid->node) != DC1394_SUCCESS ) {
00665 fprintf(stderr, "couldn't stop the camera?\n");
00666 return -1;
00667 }
00668
00669 dc1394_dma_release_camera(arV1394.handle, &(vid->camera));
00670
00671 return 0;
00672 }
00673
00674 int ar2VideoInqSize(AR2VideoParamT *vid, int *x,int *y)
00675 {
00676 *x = vid->camera.frame_width;
00677 *y = vid->camera.frame_height;
00678
00679 return 0;
00680 }
00681
00682 ARUint8 *ar2VideoGetImage( AR2VideoParamT *vid )
00683 {
00684 register ARUint8 *buf, *buf2;
00685 register int i, j;
00686 register int U, V, R, G, B, V2, U5, UV;
00687 register int Y0, Y1, Y2, Y3;
00688 register ARUint8 r, g, b;
00689
00690 if(vid->status == 0){
00691 fprintf(stderr, "arVideoCapStart has never been called.\n");
00692 return NULL;
00693 }
00694 if(vid->status == 2){
00695 fprintf(stderr, "arVideoCapNext has never been called since previous arVideoGetImage.\n");
00696 return NULL;
00697 }
00698
00699 if( dc1394_dma_single_capture( &(vid->camera) ) != DC1394_SUCCESS ) {
00700 fprintf(stderr, "unable to capture a frame\n");
00701 return NULL;
00702 }
00703 vid->status = 2;
00704
00705
00706 switch( vid->mode ) {
00707 case MODE_640x480_RGB:
00708 return (ARUint8 *)vid->camera.capture_buffer;
00709
00710
00711 case MODE_640x480_MONO:
00712 {
00713
00714 if (ar2Video_dragonfly < 0)
00715 {
00716 fprintf (stderr, "It is not possible to be in mono mode without the dragonfly flag being set previously - internal error\n");
00717 exit (1);
00718 }
00719
00720
00721 if (vid->camera.capture_buffer == NULL)
00722 return ((ARUint8 *)vid->camera.capture_buffer);
00723
00724
00725
00726
00727 quadlet_t qValue;
00728 GetCameraControlRegister (arV1394.handle, vid->node, 0x1040, &qValue);
00729 bayer_pattern_t pattern = BAYER_PATTERN_BGGR;
00730 static bayer_pattern_t prev_pattern = -1;
00731 switch( qValue )
00732 {
00733 case 0x42474752:
00734 pattern = BAYER_PATTERN_BGGR;
00735 break;
00736 case 0x47524247:
00737 pattern = BAYER_PATTERN_GRBG;
00738 break;
00739 case 0x52474742:
00740 pattern = BAYER_PATTERN_RGGB;
00741 break;
00742 case 0x47425247:
00743 pattern = BAYER_PATTERN_GBRG;
00744 break;
00745 case 0x59595959:
00746 fprintf (stderr, "Camera is black and white, Bayer conversion is not possible - internal error\n");
00747 exit (1);
00748 default:
00749 if (prev_pattern == -1)
00750 {
00751 fprintf (stderr, "Camera BAYER_TILE_MAPPING register has an unexpected value 0x%x on initial startup, which should not occur - internal error\n", qValue);
00752 exit (1);
00753 }
00754 else
00755 {
00756
00757 fprintf (stderr, "WARNING! The BAYER_TILE_MAPPING register has an unexpected value 0x%x, but I was able to use the previous stored result - this might be a bug\n", qValue);
00758 pattern = prev_pattern;
00759 }
00760 }
00761
00762
00763 prev_pattern = pattern;
00764
00765
00766 unsigned char *dest = vid->image;
00767 unsigned char *src = (ARUint8 *)vid->camera.capture_buffer;
00768 BayerNearestNeighbor( src,
00769 dest,
00770 vid->camera.frame_width,
00771 vid->camera.frame_height,
00772 pattern );
00773
00774
00775 return (vid->image);
00776 }
00777
00778
00779 case MODE_640x480_YUV411:
00780 buf = vid->image;
00781 buf2 = (ARUint8 *)vid->camera.capture_buffer;
00782 for( i = vid->camera.frame_height * vid->camera.frame_width / 4; i; i--) {
00783 U = ((ARUint8)*buf2++ - 128) * 0.354;
00784 U5 = 5*U;
00785 Y0 = (ARUint8)*buf2++;
00786 Y1 = (ARUint8)*buf2++;
00787 V = ((ARUint8)*buf2++ - 128) * 0.707;
00788 V2 = 2*V;
00789 Y2 = (ARUint8)*buf2++;
00790 Y3 = (ARUint8)*buf2++;
00791 UV = - U - V;
00792
00793
00794
00795
00796
00797 R = Y0 + V2;
00798 if ((R >> 8) > 0) R = 255; else if (R < 0) R = 0;
00799
00800 G = Y0 + UV;
00801 if ((G >> 8) > 0) G = 255; else if (G < 0) G = 0;
00802
00803 B = Y0 + U5;
00804 if ((B >> 8) > 0) B = 255; else if (B < 0) B = 0;
00805
00806 *buf++ = (ARUint8)R;
00807 *buf++ = (ARUint8)G;
00808 *buf++ = (ARUint8)B;
00809
00810
00811 R = Y1 + V2;
00812 if ((R >> 8) > 0) R = 255; else if (R < 0) R = 0;
00813
00814 G = Y1 + UV;
00815 if ((G >> 8) > 0) G = 255; else if (G < 0) G = 0;
00816
00817 B = Y1 + U5;
00818 if ((B >> 8) > 0) B = 255; else if (B < 0) B = 0;
00819
00820 *buf++ = (ARUint8)R;
00821 *buf++ = (ARUint8)G;
00822 *buf++ = (ARUint8)B;
00823
00824
00825 R = Y2 + V2;
00826 if ((R >> 8) > 0) R = 255; else if (R < 0) R = 0;
00827
00828 G = Y2 + UV;
00829 if ((G >> 8) > 0) G = 255; else if (G < 0) G = 0;
00830
00831 B = Y2 + U5;
00832 if ((B >> 8) > 0) B = 255; else if (B < 0) B = 0;
00833
00834 *buf++ = (ARUint8)R;
00835 *buf++ = (ARUint8)G;
00836 *buf++ = (ARUint8)B;
00837
00838
00839 R = Y3 + V2;
00840 if ((R >> 8) > 0) R = 255; else if (R < 0) R = 0;
00841
00842 G = Y3 + UV;
00843 if ((G >> 8) > 0) G = 255; else if (G < 0) G = 0;
00844
00845 B = Y3 + U5;
00846 if ((B >> 8) > 0) B = 255; else if (B < 0) B = 0;
00847
00848 *buf++ = (ARUint8)R;
00849 *buf++ = (ARUint8)G;
00850 *buf++ = (ARUint8)B;
00851 }
00852 return vid->image;
00853
00854 case MODE_320x240_YUV422:
00855 buf = vid->image;
00856 buf2 = (ARUint8 *)vid->camera.capture_buffer;
00857 for( i = vid->camera.frame_height * vid->camera.frame_width / 2; i; i-- ) {
00858 U = ((ARUint8)*buf2++ - 128) * 0.354;
00859 U5 = 5*U;
00860 Y0 = (ARUint8)*buf2++;
00861 V = ((ARUint8)*buf2++ - 128) * 0.707;
00862 V2 = 2*V;
00863 Y1 = (ARUint8)*buf2++;
00864 UV = - U - V;
00865
00866
00867 R = Y0 + V2;
00868 if ((R >> 8) > 0) R = 255; else if (R < 0) R = 0;
00869
00870 G = Y0 + UV;
00871 if ((G >> 8) > 0) G = 255; else if (G < 0) G = 0;
00872
00873 B = Y0 + U5;
00874 if ((B >> 8) > 0) B = 255; else if (B < 0) B = 0;
00875
00876 *buf++ = (ARUint8)R;
00877 *buf++ = (ARUint8)G;
00878 *buf++ = (ARUint8)B;
00879
00880
00881 R = Y1 + V2;
00882 if ((R >> 8) > 0) R = 255; else if (R < 0) R = 0;
00883
00884 G = Y1 + UV;
00885 if ((G >> 8) > 0) G = 255; else if (G < 0) G = 0;
00886
00887 B = Y1 + U5;
00888 if ((B >> 8) > 0) B = 255; else if (B < 0) B = 0;
00889
00890 *buf++ = (ARUint8)R;
00891 *buf++ = (ARUint8)G;
00892 *buf++ = (ARUint8)B;
00893 }
00894 return vid->image;
00895 }
00896
00897 return NULL;
00898 }
00899
00900
00901
00902
00903 static int ar2Video1394Init( int debug, int *card, int *node )
00904 {
00905 int i;
00906
00907
00908 if (((*card == -1) && (*node != -1)) ||
00909 ((*card != -1) && (*node == -1)))
00910 {
00911 fprintf (stderr, "Card value is %d and node value is %d, you must either auto-detect both or specify both\n", *card, *node);
00912 return (-1);
00913 }
00914
00915
00916 if ((*card == -1) && (*node == -1))
00917 {
00918
00919 int numPorts = MAX_PORTS;
00920 int p;
00921 struct raw1394_portinfo ports[MAX_PORTS];
00922 raw1394handle_t raw_handle = raw1394_new_handle ();
00923 if (raw_handle == NULL)
00924 {
00925 fprintf (stderr, "Could not acquire a raw1394 handle - driver not installed?\n");
00926 return (-1);
00927 }
00928 numPorts = raw1394_get_port_info (raw_handle, ports, numPorts);
00929 raw1394_destroy_handle (raw_handle);
00930
00931
00932 printf ("Auto-detecting firewire camera because card and node is not specified\n");
00933
00934
00935 for (p = 0; p < numPorts; p++)
00936 {
00937
00938 int numnodes, c;
00939 raw1394handle_t handle;
00940 handle = dc1394_create_handle (p);
00941 if (handle == NULL)
00942 continue;
00943
00944
00945 numnodes = raw1394_get_nodecount (handle);
00946 if (numnodes <= 1)
00947 continue;
00948
00949
00950 for (c = 0; c < numnodes; c++)
00951 {
00952 dc1394_camerainfo info;
00953 if (dc1394_get_camera_info (handle, c, &info) < 0)
00954 {
00955 printf ("1394 card %d node %d is not a camera [INVALID]\n", p, c);
00956 }
00957 else
00958 {
00959 printf ("1394 card %d node %d is a [%s - %s] --> ", p, c, info.vendor, info.model);
00960
00961
00962 if (*card == -1)
00963 {
00964 printf ("auto detected\n");
00965 *card = p;
00966 *node = c;
00967 }
00968 else
00969 printf ("not used\n");
00970 }
00971 }
00972 }
00973
00974
00975 if ((*card == -1) && (*node == -1))
00976 {
00977 fprintf (stderr, "Could not auto detect any cameras on the %d firewire cards available\n", numPorts);
00978 return (-1);
00979 }
00980 printf ("Using the firewire camera on card %d and node %d\n", *card, *node);
00981 }
00982
00983
00984
00985 arV1394.handle = dc1394_create_handle(*card);
00986 if (arV1394.handle==NULL)
00987 {
00988 fprintf (stderr, "Could not acquire a raw1394 handle, did you insmod the drivers?\n");
00989 return (-1);
00990 }
00991
00992
00993
00994 return 0;
00995 }
00996