take.c

Go to the documentation of this file.
00001 
00020 #include "edtinc.h"
00021 
00022 
00023 /*
00024  * defines
00025  */
00026 #define STARTSIG 0xa5
00027 #define ENDSIG   0x5a
00028 
00029 int
00030 get_usps_lines_xferred(PdvDev *pdv_p, int buffer)
00031 {
00032         u_int rasterlines;
00033 
00034         rasterlines = buffer % pdv_p->ring_buffer_numbufs;
00035 
00036         edt_ioctl(pdv_p, EDTG_LINES_XFERRED, &rasterlines);
00037 
00038         return rasterlines;
00039 
00040 }
00041 
00042 /*
00043  * fwd ref
00044  */
00045 void
00046 crop_and_subsample(int subsample, int src_width, int src_height,
00047                 int x0, int y0, int x1, int y1, u_char * src, u_char * dst);
00048 void
00049         resolve_fname(char *fname, char *newname, int loops, char *ext);
00050 static void usage(char *progname);
00051 
00052 /*
00053  * globals
00054  */
00055 int     ASnum = 0;
00056 int     ASinc = 1;
00057 
00058 void
00059 dumpbuf(char *name, u_short * buf, int size)
00060 {
00061     FILE   *fp = fopen(name, "wb");
00062 
00063     fwrite(buf, sizeof(short), size / 2, fp);
00064     fclose(fp);
00065 }
00066 
00067 static int quiet = 0;
00068 
00069 /*******************************
00070  *
00071  * simple method to suppress messages
00072  * using quiet variable
00073  *
00074  *******************************/
00075 
00076 int
00077 take_printf(char *format,...)
00078 
00079 {
00080     va_list stack;
00081 
00082     if (quiet)
00083         return 0;
00084 
00085     va_start(stack, format);
00086 
00087     return vprintf(format, stack);
00088 
00089 }
00090 
00091 int
00092 write_image_file(char *tmpname, u_char * image_p,
00093                  int s_width, int s_height, int s_depth)
00094 {
00095     int     s_db = bits2bytes(s_depth);
00096     u_char *bbuf = NULL;
00097         int retval;
00098 
00099     /*
00100      * write bmp file on Windows systems, or Sun Raster on Unux/Linux
00101      * systems. Switch on number of bytes per pixel
00102      */
00103     switch (s_db)
00104     {
00105     case 1:
00106 #ifdef _NT_
00107         take_printf("writing %dx%dx%d bmp file to %s\n",
00108                     s_width, s_height, s_depth, tmpname);
00109 
00110         retval = dvu_write_bmp(tmpname, image_p, s_width, s_height);
00111 #else
00112         take_printf("writing %dx%dx%d raster file to %s\n",
00113                     s_width, s_height, s_depth, tmpname);
00114 
00115         retval = dvu_write_rasfile(tmpname, (u_char *) image_p, s_width, s_height);
00116 #endif
00117         break;
00118 
00119     case 2:
00120         take_printf("converting %dx%dx%d image to 8 bits, writing to %s\n",
00121                     s_width, s_height, s_depth, tmpname);
00122 
00123 #ifdef _NT_
00124         if (!bbuf)
00125             bbuf = (u_char *) pdv_alloc(s_width * s_height);
00126 
00127         if (bbuf == NULL)
00128         {
00129             pdv_perror("data buf malloc");
00130             exit(1);
00131         }
00132         dvu_word2byte((u_short *) image_p, (u_char *) bbuf,
00133                       s_width * s_height, s_depth);
00134         retval = dvu_write_bmp(tmpname, bbuf, s_width, s_height);
00135 #else
00136         retval = dvu_write_rasfile16(tmpname, (u_char *) image_p, s_width, s_height, s_depth);
00137 #endif
00138         break;
00139 
00140     case 3:
00141         take_printf("writing %dx%dx%d bmp file to %s\n",
00142                     s_width, s_height, s_depth, tmpname);
00143 
00144 #ifdef _NT_
00145         retval = dvu_write_bmp_24(tmpname, (u_char *) image_p, s_width, s_height);
00146 #else
00147         retval = dvu_write_rasfile24(tmpname, (u_char *) image_p, s_width, s_height);
00148 #endif
00149 
00150         break;
00151 
00152     default:
00153         take_printf("invalid image depth for file write...!\n");
00154         return -1;
00155         break;
00156     }
00157 
00158     return retval;
00159 }
00160 
00161 /* keep this until we find out why without it, process cannot exit */
00162 /* even though wakeup from WAITBUF detects interrupt and does wakeup */
00163 int GetOut = 0 ;
00164 static int
00165 gotit()
00166 {
00167         GetOut = 1 ;
00168         return 0;
00169 }
00170 
00171 char cmdstr[80] ;
00172 
00173 /*
00174  * main
00175  */
00176 #ifdef NO_MAIN
00177 #include "opt_util.h"
00178 char   *argument;
00179 int     option;
00180 
00181 take(char *command_line)
00182 #else
00183 int
00184 main(int argc, char *argv[])
00185 #endif
00186 {
00187     int     i, lc;
00188     int     waited = 0;
00189     int     unit = 0;
00190     char   *unitstr = "0";
00191     int     loops = 1;
00192     int     wait_for_enter = 0;
00193     int     user_delay = 0;
00194     int     numbufs = 1;
00195     int     do_ring = 0;
00196     int     read_cfgfile = 1;
00197     int     got_cfgfile = 0;
00198     int     verbose = 0;
00199     int     do_file = 0;
00200     int     rbtimeout = 0;
00201     int     bytesread;
00202     int     started;
00203     int     done;
00204     char    cfgfname[128];
00205     char    outfname[128];
00206     char    rawfname[128];
00207     char    edt_devname[256];
00208     char   *progname = "take";
00209     u_char *imagebuf = NULL;
00210     u_char *bbuf = NULL;
00211     u_char *subimage = NULL;
00212     u_char *image_p = NULL;
00213     PdvDev *pdv_p;
00214     int     timeouts=0, last_timeouts=0;
00215     int     in_timeout_cleanup = 0;
00216     int     overruns=0, overrun;
00217     char    tmpname[128];
00218     int     subsample = 0, crop = 0;
00219     int     debug_timeout = 0;
00220     int     width, height, depth, imagesize, dmasize, db;
00221     int     s_width, s_height, s_depth, s_size, s_db;
00222     int     crop_x0, crop_x1, crop_y0, crop_y1;
00223     int     exposure, gain, offset;
00224     int     set_exposure = 0, set_gain = 0, set_offset = 0;
00225     int     set_timeout = 0;
00226     int     do_timestamp = 0;
00227     int     do_timetype = 0;
00228     int     timetype = 0;
00229     int     do_lasttest = 0;
00230     int     timeout_val = 0;
00231     int     enable_burst_en = 1;
00232     int     si = -1;
00233     int     sc = -1;
00234     int     vskip, vactv, hskip, hactv;
00235     int     set_roi = 0;
00236     int     unset_roi = 0;
00237     int     checksig = 0, checkcount = 0;
00238     int     checksim = 0;
00239     int     checkclsim = 0;
00240     int     checkclk = 0;
00241     int     badstart = 0, badend = 0;
00242     int     just_print_the_last_one = 0;
00243     u_short startsig = STARTSIG, endsig = ENDSIG;
00244     u_int   sigcount;
00245     int     siginit = 0;
00246     char    errstr[64];
00247     u_int   timestamp[2];
00248     u_int   freqbuf[2];
00249     double  dtime;
00250     double  svtime = 0;
00251     double  curtime = 0;
00252     double  frequency = 0;
00253     int     checkfrm = 0;
00254     int     binning = 0;
00255     int     opto_trigger = 0;
00256     int     fldid_trigger = 0;
00257 
00258     int     foi_unit = -1;
00259     int     channel = 0;
00260 
00261 
00262     int     mosaic = 0;
00263     int     mosaic_sample = 0;
00264     int     sample_height = 10;
00265     int     sample_start = 0;
00266     int     mosaic_size = 0;
00267     int     mosaic_width = 0;
00268     int     mosaic_height = 0;
00269     char    dvcmode[8];
00270     unsigned char *mosaic_ptr = NULL;
00271     unsigned char *mosaic_image = NULL;
00272     int     arg_channel = -1;
00273     int     dl;
00274 
00275     int noreset = 0;
00276 
00277 #ifdef NO_MAIN
00278     char  **argv = 0;
00279     int     argc = 0;
00280 
00281     opt_create_argv("take", command_line, &argc, &argv);
00282 #endif
00283 
00284 
00285     cfgfname[0] = '\0';
00286     rawfname[0] = '\0';
00287     outfname[0] = '\0';
00288     edt_devname[0] = '\0';
00289     dvcmode[0] = '\0';
00290     tmpname[0] = '\0';
00291     ASnum = 0;
00292     quiet = 0 ;
00293     siginit = 0 ;
00294 
00295 #ifdef __APPLE__
00296 /* debug for interrupted wakeup */
00297 signal(SIGINT,gotit);
00298 #endif
00299 
00300     --argc;
00301     ++argv;
00302     while (argc && ((argv[0][0] == '-') || (argv[0][0] == '/')))
00303     {
00304         switch (argv[0][1])
00305         {
00306         case 'u':               /* device unit number */
00307             ++argv;
00308             --argc;
00309             unitstr = argv[0];
00310             break;
00311 
00312         case 'B':               /* turn off burst enable */
00313             ++argv;
00314             --argc;
00315             if (argc > 0)
00316                 enable_burst_en = atoi(argv[0]);
00317             else
00318                 enable_burst_en = 1;
00319             break;
00320 
00321         case 'v':               /* verbose */
00322             verbose = 1;
00323             break;
00324 
00325         case 'w':               /* wait for user <cr> between frames */
00326             wait_for_enter = 1;
00327             break;
00328 
00329         case 'T':               /* on timeout, dump data to timeout.raw */
00330             debug_timeout = 1;
00331             break;
00332 
00333         case 'd':               /* timestamp */
00334             do_timestamp = 1;
00335             break;
00336 
00337         case 'D':               /* dvc mode setting (diagnostic) */
00338             ++argv;
00339             --argc;
00340             strcpy(dvcmode, argv[0]);
00341             break;
00342 
00343         case 'X':               /* use timer */
00344             ++argv;
00345             --argc;
00346             timetype = atoi(argv[0]);
00347             do_timetype = 1;
00348             break;
00349 
00350         case 'y':
00351             opto_trigger = 1;
00352             break;
00353 
00354         case 'Y':
00355             fldid_trigger = 1;
00356             break;
00357 
00358         case 'j':
00359             just_print_the_last_one = 1;
00360             break;
00361 
00362         case 'L':
00363             do_lasttest = 1;    /* freerun/bytecount with wait for next frame */
00364             break;
00365 
00366         case 'm':               /* microsec sleep between frames */
00367             ++argv;
00368             --argc;
00369             if ((argv[0][0] >= '0') && (argv[0][0] <= '9'))
00370                 user_delay = atoi(argv[0]);
00371             break;
00372 
00373         case 'N':               /* specify numbufs */
00374             ++argv;
00375             --argc;
00376             do_ring = 1;
00377             numbufs = atoi(argv[0]);
00378             break;
00379 
00380         case 'H':               /* check count signature from testgen */
00381             checksim = 1;
00382             checksig = 0;
00383             checkcount = 0;
00384             break;
00385 
00386         case 'P':               /* check count signature from PCI CD sim */
00387             checkcount = 1;
00388             checksig = 0;
00389             break;
00390 
00391         case 'p':               /* check fixed signature from PCI CD sim */
00392             checksig = 1;
00393             checkcount = 0;
00394             break;
00395 
00396         case 'G':               /* check count signature from testgen */
00397             checkclsim = 1;
00398             checksim = 0;
00399             checksig = 0;
00400             checkcount = 0;
00401             break;
00402 
00403         case 'q':               /* binning */
00404             ++argv;
00405             --argc;
00406             binning = atoi(argv[0]);
00407             break;
00408 
00409         case 'Q':               /* turn on quiet mode */
00410             quiet = 1;
00411             break;
00412 
00413         case 't':               /* microsec sleep between frames */
00414             /* if no subarg, set to -1 (auto timeout) */
00415             if ((argc < 2) || (argv[1][0] == '-'))
00416             {
00417                 set_timeout = 1;
00418                 timeout_val = -1;
00419             }
00420             else
00421             {
00422                 ++argv;
00423                 --argc;
00424                 set_timeout = 1;
00425                 if ((argv[0][0] >= '0') && (argv[0][0] <= '9'))
00426                     timeout_val = atoi(argv[0]);
00427             }
00428             break;
00429 
00430         case 'l':               /* loops */
00431             ++argv;
00432             --argc;
00433             loops = atoi(argv[0]);
00434             break;
00435 
00436         case 'f':               /* save filename */
00437             ++argv;
00438             --argc;
00439             strcpy(rawfname, argv[0]);
00440             do_file = 1;
00441             break;
00442 
00443         case 'F':               /* foi unit */
00444             ++argv;
00445             --argc;
00446             foi_unit = atoi(argv[0]);
00447 
00448 
00449             break;
00450 
00451         case 'S':               /* subsample */
00452             ++argv;
00453             --argc;
00454             subsample = atoi(argv[0]);
00455             break;
00456 
00457         case 'C':               /* crop x0, y0, x1, y1 */
00458             crop = 1;
00459             ++argv;
00460             --argc;
00461             crop_x0 = atoi(argv[0]);
00462 
00463             ++argv;
00464             --argc;
00465             crop_y0 = atoi(argv[0]);
00466 
00467             ++argv;
00468             --argc;
00469             crop_x1 = atoi(argv[0]);
00470 
00471             ++argv;
00472             --argc;
00473             crop_y1 = atoi(argv[0]);
00474             break;
00475 
00476         case 'n':               /* autosequence start only */
00477             ++argv;
00478             --argc;
00479             ASnum = atoi(argv[0]);
00480             break;
00481 
00482         case 's':               /* autosequence start and inc */
00483             ++argv;
00484             --argc;
00485             ASnum = atoi(argv[0]);
00486             break;
00487 
00488         case 'e':               /* exposure time */
00489             ++argv;
00490             --argc;
00491             set_exposure = 1;
00492             exposure = atoi(argv[0]);
00493             break;
00494 
00495         case 'g':               /* gain */
00496             ++argv;
00497             --argc;
00498             set_gain = 1;
00499             gain = atoi(argv[0]);
00500             break;
00501 
00502         case 'o':               /* offset (blacklevel) */
00503             ++argv;
00504             --argc;
00505             set_offset = 1;
00506             offset = atoi(argv[0]);
00507             break;
00508 
00509         case 'I':               /* # to increment for multiple file writes */
00510             ++argv;
00511             --argc;
00512             ASinc = atoi(argv[0]);
00513             break;
00514 
00515         case 'b':               /* bitmap or sun raster save filename */
00516         case 'i':
00517             ++argv;
00518             --argc;
00519             strcpy(outfname, argv[0]);
00520             do_file = 1;
00521             break;
00522 
00523         case 'c':               /* channel */
00524             ++argv;
00525             --argc;
00526             arg_channel = atoi(argv[0]);
00527             break;
00528 
00529         case 'r':               /* ROI */
00530             if (argc < 5)
00531             {
00532                 usage(progname);
00533                 exit(1);
00534             }
00535             ++argv;
00536             --argc;
00537             hskip = atoi(argv[0]);
00538             ++argv;
00539             --argc;
00540             hactv = atoi(argv[0]);
00541             ++argv;
00542             --argc;
00543             vskip = atoi(argv[0]);
00544             ++argv;
00545             --argc;
00546             vactv = atoi(argv[0]);
00547             set_roi = 1;
00548             break;
00549 
00550         case 'R':               /* disable ROI */
00551             unset_roi = 1;
00552             break;
00553 
00554         case 'O':               /* try noreset - will be obsolete on clink? */
00555             noreset = 1 ;
00556             break;
00557 
00558         case 'x':               /* experimental -- strobe testing */
00559             ++argv;
00560             --argc;
00561             si = atoi(argv[0]);
00562             ++argv;
00563             --argc;
00564             sc = atoi(argv[0]);
00565             break;
00566 
00567         case 'M':               /* mosaic */
00568             mosaic = 1;
00569             break;
00570 
00571         case 'a':               /* call pdv_checkfrm */
00572             checkfrm = 1;
00573             break;
00574         case 'A':
00575             checkclk = 1;
00576             break;
00577 
00578         default:
00579             fprintf(stderr, "unknown flag -'%c'\n", argv[0][1]);
00580         case '?':
00581         case 'h':
00582             usage(progname);
00583             exit(0);
00584         }
00585         argc--;
00586         argv++;
00587     }
00588 
00589 
00590     unit = edt_parse_unit_channel(unitstr, edt_devname, "pdv", &channel);
00591 
00592     if (arg_channel != -1)
00593     {
00594         channel = arg_channel;
00595     }
00596 
00597     unit = edt_parse_unit(unitstr, edt_devname, "pdv");
00598 
00599     /*
00600      * pdv_open_channel is just pdv_open with an extra argument, the channel,
00601      * which is normally 0 unless you're running multiple cameras (2nd base
00602      * mode on a PCI DV C-Link or dasy-chained RCI boxes off a single PCI
00603      * FOI)
00604      */
00605     if ((pdv_p = pdv_open_channel(edt_devname, unit, channel)) == NULL)
00606     {
00607         sprintf(errstr, "pdv_open(%s%d_%d)", edt_devname, unit, channel);
00608         pdv_perror(errstr);
00609         return (1);
00610     }
00611 
00612     if (*dvcmode)
00613         pdv_set_mode_dvc(pdv_p, dvcmode);
00614 
00615     if (foi_unit != -1)
00616     {
00617         edt_set_foiunit(pdv_p, foi_unit);
00618 
00619     }
00620     if (set_exposure)
00621         pdv_set_exposure(pdv_p, exposure);
00622 
00623     if (set_gain)
00624         pdv_set_gain(pdv_p, gain);
00625 
00626     if (set_offset)
00627         pdv_set_blacklevel(pdv_p, offset);
00628 
00629     if (set_timeout)
00630         pdv_set_timeout(pdv_p, timeout_val);
00631 
00632     if (opto_trigger)
00633         pdv_enable_external_trigger(pdv_p, PDV_PHOTO_TRIGGER);
00634 
00635     else if (fldid_trigger)
00636         pdv_enable_external_trigger(pdv_p, PDV_FLDID_TRIGGER);
00637 
00638 
00639     if (set_roi)
00640     {
00641         pdv_set_roi(pdv_p, hskip, hactv, vskip, vactv);
00642         pdv_enable_roi(pdv_p, 1);
00643     }
00644     else if (unset_roi)
00645     {
00646         pdv_enable_roi(pdv_p, 0);
00647     }
00648 
00649     if (binning)
00650     {
00651         pdv_set_binning(pdv_p, binning, binning);
00652     }
00653 
00654     if (checksig)
00655         take_printf("checking PCI CD Simulator fixed signature -- use simple_putframe -p\n");
00656     if (checksim)
00657         take_printf("checking testgen signature\n");
00658     if (checkclsim)
00659         take_printf("checking cl simulator signature\n");
00660     if (checkcount)
00661         take_printf("checking PCI CD Simulator count signature -- use simple_putframe -P\n");
00662 
00663     if (do_timetype)
00664     {
00665         edt_set_timetype(pdv_p, EDT_TM_FREQ);
00666         edt_get_reftime(pdv_p, freqbuf);
00667         frequency = (double) freqbuf[0] * 0x100000000L + freqbuf[1];
00668         edt_set_timetype(pdv_p, timetype);
00669     }
00670 
00671     /*
00672      * set size variables for image and initial subimage
00673      */
00674     width = s_width = pdv_get_width(pdv_p);
00675     height = s_height = pdv_get_height(pdv_p);
00676     depth = s_depth = pdv_get_depth(pdv_p);
00677     db = s_db = bits2bytes(depth);
00678     imagesize = s_size = pdv_get_imagesize(pdv_p);
00679     dmasize = s_size = pdv_get_dmasize(pdv_p);
00680     subimage = NULL;
00681     /*
00682      * turn off merge
00683      */
00684     edt_set_merge(pdv_p, 0, 0, 0, 0);
00685 
00686     if (s_size <= 0)
00687     {
00688         printf("%s%d: Invalid image size. Make sure device has been initialized.\n", edt_devname, unit);
00689         exit(1);
00690     }
00691 
00692     /*
00693      * allocate memory for the image, and make sure it's aligned on a page
00694      * boundary
00695      */
00696 
00697     imagesize = pdv_get_imagesize(pdv_p);
00698 
00699  
00700     if (mosaic)
00701     {
00702         mosaic_sample = width * sample_height * db;
00703 
00704         mosaic_size = loops * mosaic_sample;
00705 
00706         mosaic_image = pdv_alloc(mosaic_size);
00707 
00708         mosaic_ptr = mosaic_image;
00709 
00710         mosaic_width = width * db;
00711 
00712         mosaic_height = loops * sample_height;
00713 
00714     }
00715  
00716     if (do_ring)
00717     {
00718         /* Construct a ring buffer */
00719         if (pdv_multibuf(pdv_p, numbufs) == -1)
00720         {
00721             char    msg[64];
00722 
00723             sprintf(msg, "pdv_multibuf(0x%x, %d)", pdv_p, numbufs);
00724             pdv_perror(msg);
00725             return (0);
00726         }
00727     }
00728     else
00729     {
00730         imagebuf = (u_char *) pdv_alloc(imagesize);
00731         if (imagebuf == NULL)
00732         {
00733             pdv_perror("data buf malloc");
00734             exit(1);
00735         }
00736     }
00737 
00738     pdv_cl_reset_fv_counter(pdv_p) ;
00739 
00740     /*
00741      * check for crazy subsample and crop values and sort out the params as
00742      * needed
00743      */
00744 
00745     if (do_file && (subsample || crop))
00746     {
00747         if ((subsample < 0)
00748             || (subsample > height / 4)
00749             || (subsample > width / 4))
00750         {
00751             printf("-S <subsample> arg out of range\n");
00752             usage(progname);
00753             exit(1);
00754         }
00755 
00756         /* force subsample since its used as an arg in processing */
00757         if (crop && (subsample == 0))
00758             subsample = 1;
00759 
00760         if (crop)
00761         {
00762             int     tmp;
00763 
00764             if (crop_x0 > crop_x1)
00765             {
00766                 tmp = crop_x1;
00767                 crop_x1 = crop_x0;
00768                 crop_x0 = tmp;
00769             }
00770             if (crop_y0 > crop_y1)
00771             {
00772                 tmp = crop_y1;
00773                 crop_y1 = crop_y0;
00774                 crop_y0 = tmp;
00775             }
00776 
00777             if (crop_x0 < 0)
00778                 crop_x0 = 0;
00779             if (crop_y0 < 0)
00780                 crop_y0 = 0;
00781             if (crop_x1 > width - 1)
00782                 crop_x1 = width - 1;
00783             if (crop_y1 > height - 1)
00784                 crop_y1 = height - 1;
00785         }
00786         else
00787         {
00788             crop_x0 = 0;
00789             crop_y0 = 0;
00790             crop_x1 = width - 1;
00791             crop_y1 = height - 1;
00792         }
00793 
00794         s_width = ((crop_x1 - crop_x0) + subsample) / subsample;
00795         s_height = ((crop_y1 - crop_y0) + subsample) / subsample;
00796         s_size = s_width * s_height * s_db;
00797         subimage = (u_char *) malloc(s_size);
00798     }
00799 
00800     /*
00801      * should normally only be set from the config file as "start_delay:",
00802      * but for testing allow it to be set here too
00803      */
00804     if (user_delay)
00805         pdv_p->dd_p->start_delay = user_delay;
00806 
00807     if (pdv_p->dd_p->start_delay)
00808         take_printf("%d millisecond delay added between images\n", pdv_p->dd_p->start_delay);
00809     if (pdv_p->dd_p->force_single)
00810         take_printf("force_single set; prestarting one image at a time\n");
00811 
00812 #ifdef WIN2K
00813     edt_get_kernel_event(pdv_p, EDT_EVENT_BUF);
00814 #endif
00815 
00816     /*
00817      * acquire the image
00818      */
00819     take_printf("reading image from %s\nwidth %d height %d depth %d total bytes %d\n",
00820                 pdv_get_cameratype(pdv_p),
00821                 pdv_get_width(pdv_p),
00822                 pdv_get_height(pdv_p),
00823                 pdv_get_depth(pdv_p),
00824                 imagesize);
00825 
00826     if (do_ring)
00827     {
00828         if (pdv_p->dd_p->force_single)
00829         {
00830             pdv_start_image(pdv_p);
00831             started = 1;
00832         }
00833         else if (do_lasttest)
00834         {
00835             /* start freerun */
00836             pdv_start_images(pdv_p, 0);
00837         }
00838         else if (wait_for_enter)
00839         {
00840             printf("hit enter for first image");
00841             getchar();
00842             pdv_start_images(pdv_p, 1);
00843             started = 1;
00844         }
00845         else
00846         {
00847             pdv_start_images(pdv_p, numbufs);
00848             started = numbufs;
00849         }
00850     }
00851     if (do_lasttest)
00852     {
00853         for (;;)
00854         {
00855             int     ret;
00856             u_char  tmpbuf[80];
00857 
00858 #if 0
00859             printf("return to check");
00860             getchar();
00861 #endif
00862             take_printf("%d done count %x\n", edt_done_count(pdv_p), edt_get_bytecount(pdv_p));
00863             edt_send_msg(pdv_p, 0, "w a07a 6", 8);
00864             ret = edt_serial_wait(pdv_p, 1000, 0);
00865             if (ret > 0)
00866             {
00867                 take_printf("got %d resp\n", ret);
00868                 edt_get_msg(pdv_p, (char *) tmpbuf, sizeof(tmpbuf));
00869                 take_printf("resp %x %x %x\n", tmpbuf[0], tmpbuf[1], tmpbuf[2]);
00870                 if (tmpbuf[2])
00871                 {
00872                     printf("got %s - return to continue", &tmpbuf[2]);
00873                     getchar();
00874                 }
00875             }
00876             else
00877             {
00878                 printf("bad resp from serial wait %d\n", ret);
00879                 getchar();
00880             }
00881             edt_send_msg(pdv_p, 0, "w a07a 4", 8);
00882             ret = edt_serial_wait(pdv_p, 1000, 0);
00883             if (ret > 0)
00884             {
00885                 printf("got %d resp\n", ret);
00886                 edt_get_msg(pdv_p, (char *) tmpbuf, sizeof(tmpbuf));
00887                 printf("resp %x %x %x\n", tmpbuf[0], tmpbuf[1], tmpbuf[2]);
00888                 if (tmpbuf[2])
00889                 {
00890                     printf("got %s - return to continue", &tmpbuf[2]);
00891                     getchar();
00892                 }
00893             }
00894             else
00895             {
00896                 printf("bad resp from serial wait %d\n", ret);
00897                 getchar();
00898             }
00899         }
00900     }
00901     done = 0;
00902     (void) edt_dtime();         /* init time for check */
00903     for (lc = 0; lc < loops && !GetOut; lc++)
00904     {
00905         if (wait_for_enter)
00906         {
00907             printf("hit enter for next image");
00908             getchar();
00909         }
00910 
00911         if (do_ring)
00912         {
00913 
00914             if (do_timestamp)
00915             {
00916                 imagebuf = pdv_wait_image_timed(pdv_p, timestamp);
00917                 switch (timetype)
00918                 {
00919                 case EDT_TM_SEC_NSEC:
00920                     /* secs, nsec */
00921                     curtime = (double) timestamp[0] * 1000000000L + timestamp[1];
00922                     curtime /= 1000000000.0;
00923                     break;
00924                 case EDT_TM_CLICKS:
00925                     /* cpu cycles high32/low32 */
00926                     curtime = (double) timestamp[0] * 0x100000000L + timestamp[1];
00927                     break;
00928                 case EDT_TM_COUNTER:
00929                     /* counter high32/low32 */
00930                     curtime = (double) timestamp[0] * 0x100000000L + timestamp[1];
00931                     curtime /= frequency;
00932                     break;
00933                 }
00934                 take_printf("\ntime %3.9f ", curtime);
00935                 if (svtime > 0)
00936                 {
00937                     take_printf(" delta %3.9f ", (curtime - svtime));
00938                 }
00939                 svtime = curtime;
00940             }
00941             else
00942                 imagebuf = pdv_wait_image(pdv_p);
00943             ++waited;
00944         }
00945         else
00946         {
00947             if (wait_for_enter && done == 0)
00948             {
00949                 printf("hit enter for first image");
00950                 getchar();
00951             } bytesread = pdv_read(pdv_p, imagebuf, imagesize);
00952             if (bytesread != dmasize)
00953             {
00954                 printf("dmasize 0x%x read 0x%x\n", dmasize, bytesread);
00955             }
00956             if (bytesread < 0)
00957             {
00958                 pdv_perror(edt_devname);
00959                 exit(1);
00960             }
00961         }
00962         if (checkfrm)
00963             pdv_checkfrm(pdv_p, (u_short *) imagebuf, width * height, verbose);
00964         timeouts = pdv_timeouts(pdv_p);
00965         if (overrun = (edt_reg_read(pdv_p, PDV_STAT) & PDV_OVERRUN))
00966             ++overruns;
00967 
00968         if (timeouts > last_timeouts)
00969         {
00970             bytesread = edt_get_timeout_count(pdv_p);
00971             if (debug_timeout)
00972             {
00973                 FILE   *tmpf;
00974 
00975                 tmpf = fopen("timeout.raw", "wb");
00976                 if (tmpf)
00977                 {
00978                     printf("saving timeout data\n");
00979                     fwrite(imagebuf, imagesize, 1, tmpf);
00980                     fclose(tmpf);
00981                 }
00982                 printf("got timeout xfer %d - return to continue: ",
00983                        edt_get_bytecount(pdv_p));
00984                 getchar();
00985             }
00986 
00987             /*
00988              * pdv_timeout_cleanup recovers from the timeout, in particular
00989              * when you've prestarted multiple buffers returns how many
00990              * buffers were pending before the reset so we can restart that
00991              * many again
00992              */
00993             if (do_ring)
00994             {
00995                 pdv_timeout_restart(pdv_p,TRUE);
00996                 in_timeout_cleanup = TRUE;
00997             }
00998 
00999             last_timeouts = timeouts;
01000             rbtimeout = 1;
01001 
01002         }
01003         else if (in_timeout_cleanup)
01004         {
01005             pdv_timeout_restart(pdv_p,TRUE);
01006             in_timeout_cleanup = FALSE;
01007         }
01008         
01009         done++;
01010 
01011         if (verbose)
01012         {
01013             int     j;
01014             u_char *tmp_p;
01015             int     lastval = 0;
01016 
01017             tmp_p = imagebuf;
01018 
01019             for (j = 0; j < 16; j++)
01020                 take_printf("%02x ", imagebuf[j]);
01021             take_printf("\n");
01022             for (j = imagesize - 16; j < imagesize; j++)
01023                 take_printf("%02x ", imagebuf[j]);
01024             take_printf("\n");
01025             if (checkclk)
01026             {
01027                 for (j = 0; j < 512; j++)
01028                 {
01029                     static int k = 0;
01030                     u_short tmpval = tmp_p[1] << 8 | tmp_p[0];
01031 
01032                     if (tmpval != lastval + 1)
01033                         printf("*");
01034                     else
01035                         printf(" ");
01036                     lastval = tmpval;
01037 
01038                     printf("%04x", tmpval);
01039 
01040                     if ((++k % 16) == 0)
01041                         printf("\n");
01042                     tmp_p += 4;
01043                 }
01044                 printf("\n");
01045             }
01046         }
01047         /*
01048          * check signature pixels from PCI CD simulator if -p or -P flag
01049          * specified -- expects first and last pixel to be count if -p, or
01050          * 0xA5 and 0x5A,  if -P, bombs out on first frame where this is not
01051          * the case
01052          */
01053         if (checksig || checkcount || checksim || checkclsim)
01054         {
01055             u_short bitmask = 0;
01056 
01057             for (i = 0; i < s_depth; i++)
01058                 bitmask += 1 << i;
01059 
01060             /*
01061              * signature counter -- pick up the first one we come to
01062              */
01063             if (checkcount)
01064             {
01065                 if (!siginit)
01066                 {
01067                     startsig = endsig = sigcount = ((u_short *) imagebuf)[0] & bitmask;
01068                     siginit = 1;
01069                 }
01070                 else
01071                     startsig = endsig = sigcount & bitmask;
01072                 ++sigcount;
01073             }
01074             if (checksim)
01075             {
01076                 if (!siginit)
01077                 {
01078                     u_short *sp = (u_short *) imagebuf;
01079 
01080                     startsig = sp[0];
01081                     endsig = sp[(imagesize / 2) - 1];
01082                     siginit = 1;
01083                 }
01084                 else
01085                 {
01086                     startsig ^= 0xffff;
01087                     endsig ^= 0xffff;
01088                 }
01089             }
01090             if (checkclsim)
01091             {
01092                 if (!siginit)
01093                 {
01094                     u_short *sp = (u_short *) imagebuf;
01095 
01096                     startsig = sp[0];
01097                     /* find 3333 */
01098                     if (sp[1] == 0x3333)
01099                     printf("init start  to %04x\n", startsig) ;
01100                     else
01101                     printf("buf is already not aligned\n") ;
01102                     
01103                     siginit = 1;
01104                 }
01105                 else
01106                 {
01107                     if (startsig == 0x7fff) startsig = 0 ;
01108                     else startsig = startsig + 1 ;
01109                 }
01110             }
01111 
01112 
01113             if (depth > 8 || checkclsim)
01114             {
01115                 u_short *sp = (u_short *) imagebuf;
01116                 u_short firstword = sp[0];
01117 
01118                 /*
01119                  * -4 because of one test xilinx which went into grid mode
01120                  * when fifo empty
01121                  */
01122                 u_short lastword = sp[(imagesize / 2) - 4];
01123 
01124                 if (checkclsim)
01125                 {
01126                     u_int frmcnt = pdv_cl_get_fv_counter(pdv_p) ;
01127                     int k ;
01128 
01129                     if (firstword != startsig)
01130                         badstart = 1;
01131                     k = 0 ;
01132                     /* deal with 3333rd image */
01133                     if (sp[0] == 0x3333 && sp[1] == 0x3333) k = 1 ;
01134                     for (; k < imagesize/2 ; k++)
01135                     {
01136                         if (sp[k] == 0x3333)
01137                         {
01138                              if (k != 1)
01139                             {
01140                              printf("image1 3333 at %x\n",k) ;
01141                             badend = 1 ;
01142                             }
01143                             printf("frm %x count %04x stat %04x reg %08x\n",
01144                                     frmcnt,
01145                                     sp[k-1],
01146                                     edt_reg_read(pdv_p,PDV_STAT),
01147                                     edt_reg_read(pdv_p,PDV_CL_JERRYDBG)) ;
01148                             break ;
01149                         }
01150                     }
01151                 }
01152                 else
01153                 {
01154                     if (firstword != startsig)
01155                         badstart = 1;
01156                     if (lastword != endsig)
01157                         badend = 1;
01158                 }
01159 
01160                 if (badstart || badend)
01161                 {
01162                     u_char **buf = edt_buffer_addresses(pdv_p);
01163 
01164                     printf("\n\nstat %04x reg %08x\n",
01165                     edt_reg_read(pdv_p,PDV_STAT),
01166                     edt_reg_read(pdv_p,PDV_CL_JERRYDBG)) ;
01167 
01168                     /*
01169                      * if we just missed a frame, report it and go on
01170                      */
01171                     if (checksim
01172                         && (firstword == (startsig ^ 0xffff))
01173                         && (lastword == (endsig ^ 0xffff)))
01174                     {
01175                         printf("\nMISSED a frame! start %04x s/b %04x, end %04x s/b %04x\n", firstword, startsig, lastword, endsig);
01176                         printf("buffers %d donecount %d PDV_STAT %02x\n",
01177                                waited, edt_done_count(pdv_p), edt_reg_read(pdv_p, PDV_STAT));
01178                         startsig ^= 0xffff;
01179                         endsig ^= 0xffff;
01180                     }
01181                     else
01182                     {
01183                         printf("\nbad signature! start %04x s/b %04x, end %04x s/b %04x\n",
01184                                firstword, startsig, lastword, endsig);
01185                         printf("bad buf addr at %x %04x %04x\n", sp, firstword, lastword);
01186                         printf("buffers %d donecount %d PDV_STAT %02x\n",
01187                                waited, edt_done_count(pdv_p), edt_reg_read(pdv_p, PDV_STAT));
01188 
01189                         edt_stop_buffers(pdv_p);
01190 
01191                         sprintf(cmdstr,"setdebug -d 0 -u %d -g >setdebug.out\n",unit);
01192                         printf("%s\n",cmdstr) ;
01193                         system(cmdstr) ;
01194 
01195 
01196                         printf("dumping %d buffers to buf01.raw, buf02.raw, ....\n",
01197                                numbufs);
01198                         for (i = 0; i < numbufs; i++)
01199                         {
01200                             char    bufname[32];
01201 
01202                             sprintf(bufname, "buf%02d.raw", i);
01203                             printf("dump buf %d from %x\n", i, buf[i]);
01204                             dumpbuf(bufname, (u_short *) buf[i], imagesize);
01205                         }
01206 
01207                         printf("return to continue");
01208                         getchar();
01209                         if (!(checkcount||checkclsim))
01210                         {
01211                             for (i = 0; i < imagesize / 2; i++)
01212                                 if (((sp[i] & bitmask) == startsig)
01213                                     || (sp[i] & bitmask) == endsig)
01214                                     printf("found %04x at %d\n", sp[i], i);
01215                         }
01216                         exit(1);
01217                     }
01218                 }
01219                 /*
01220                  * sp[0] = 0xaaaa ; sp[imagesize/2 - 1] = 0x5555 ;
01221                  */
01222             }
01223             else
01224             {
01225                 u_char *cp = imagebuf;
01226 
01227                 if (cp[0] != startsig)
01228                     badstart = 1;
01229                 if (cp[imagesize - 1] != endsig)
01230                     badend = 1;
01231                 if (badstart || badend)
01232                 {
01233                     printf("\nbad signature! start %02x s/b %02x, end %02x s/b %02x\n",
01234                            cp[0], startsig, cp, endsig);
01235 
01236                     if (!checkcount)
01237                     {
01238                         for (i = 0; i < imagesize; i++)
01239                             if ((cp[i] == startsig)
01240                                 || (cp[i] == endsig))
01241                                 printf("found %02x at %d\n", cp[i], i);
01242                     }
01243                     exit(1);
01244                 }
01245             }
01246         }
01247         if (do_ring && started < loops)
01248         {
01249             if (!do_lasttest)
01250             {
01251                 pdv_start_images(pdv_p, 1);
01252             }
01253             started++;
01254         }
01255 
01256         /*
01257          * process subimage if needed, set up args for write
01258          */
01259         if (do_file && (subimage != NULL))
01260         {
01261             crop_and_subsample(subsample, width, height,
01262                     crop_x0, crop_y0, crop_x1, crop_y1, imagebuf, subimage);
01263             image_p = subimage;
01264         }
01265         else
01266             image_p = imagebuf;
01267 
01268         /*
01269          * write the image to raw data file (no formatting)
01270          */
01271         if (*rawfname)
01272         {
01273             resolve_fname(rawfname, tmpname, loops, ".raw");
01274             take_printf("writing %dx%dx%d raw file to %s\n",
01275                         s_width, s_height, s_depth, tmpname);
01276 
01277             (void) dvu_write_raw(s_size, image_p, tmpname);
01278         }
01279 
01280         /*
01281          * write the image to Windows bitmap or Sun raster format file and
01282          * fix up the filename extension
01283          */
01284         if (*outfname && !mosaic)
01285         {
01286 #ifdef _NT_
01287             resolve_fname(outfname, tmpname, loops, ".bmp");
01288 #else
01289             resolve_fname(outfname, tmpname, loops, ".ras");
01290 #endif
01291             write_image_file(tmpname, image_p,
01292                              s_width, s_height, s_depth);
01293 
01294 
01295         }
01296         else if (mosaic)
01297         {
01298             memcpy(mosaic_ptr, image_p, mosaic_sample);
01299             mosaic_ptr += mosaic_sample;
01300         }
01301 
01302         dl = pdv_debug_level();
01303 
01304         if (rbtimeout)
01305         {
01306             if (bytesread == 0)
01307                 take_printf("%d images %d timeouts %d overruns 0 bytes", done, timeouts, overruns);
01308             else take_printf("%d images %d timeouts %d overruns %d bytes (%d short)", done, timeouts, overruns, bytesread, dmasize-bytesread);
01309         }
01310         else if (just_print_the_last_one)
01311         {
01312             if (lc == loops-1)
01313             take_printf("%d images %d timeouts %d overruns\n", done, timeouts, overruns);
01314         }
01315         else take_printf("%d images %d timeouts %d overruns", done, timeouts, overruns);
01316 
01317         if (!just_print_the_last_one)
01318         {
01319             if (verbose | rbtimeout | wait_for_enter | (pdv_debug_level() > 0))
01320                 take_printf("\n");
01321             else take_printf("                                \r");
01322         }
01323 
01324         rbtimeout = 0;
01325         fflush(stdout);
01326     }
01327     take_printf("\n");
01328 
01329     if (mosaic && *outfname)
01330     {
01331         write_image_file(outfname, mosaic_image,
01332                          s_width, mosaic_height, s_depth);
01333     }
01334 
01335     if (loops > 3)
01336     {
01337         dtime = edt_dtime();
01338         printf("%f bytes/sec\n", (double) (loops * dmasize) / dtime);
01339         printf("%f frames/sec\n", (double) (loops) / dtime);
01340     }
01341 
01342     if (!do_ring)
01343         pdv_free(imagebuf);
01344     /* else */
01345     pdv_stop_continuous(pdv_p);
01346 
01347     if (binning)
01348     {
01349         pdv_set_binning(pdv_p, 1, 1);
01350     }
01351 
01352     pdv_close(pdv_p);
01353 #ifdef NO_MAIN
01354     return (0);
01355 #else
01356     exit(0);
01357 #endif
01358 }
01359 
01360 void
01361 resolve_fname(char *fname, char *newname, int loops, char *ext)
01362 {
01363     int     l = strlen(fname);
01364     char    tmpname[256];
01365     char    tmpext[5];
01366 
01367     strcpy(tmpname, fname);
01368     if ((l > 4) && (tmpname[l - 4] == '.'))
01369     {
01370         strcpy(tmpext, &tmpname[l - 4]);
01371         tmpname[l - 4] = '\0';
01372     }
01373     else
01374         strcpy(tmpext, ext);
01375 
01376     if (loops > 1)
01377     {
01378         sprintf(newname, "%s%04d%s", tmpname, ASnum, tmpext);
01379         ASnum += ASinc;
01380     }
01381     else
01382         sprintf(newname, "%s%s", tmpname, tmpext);
01383 }
01384 
01385 
01386 /*
01387  * process the image, return in subimage. Handles both subsampling and
01388  * cropping.
01389  * 
01390  * NOTE: this only works on single-byte images (so far)
01391  */
01392 
01393 void
01394 crop_and_subsample(int subsample, int src_width, int src_height,
01395                  int x0, int y0, int x1, int y1, u_char * src, u_char * dst)
01396 {
01397     u_char *next_fromptr = src + (y0 * src_width) + x0;
01398     int     to_width = (x1 - x0) + 1;
01399     int     lines = (y1 - y0) + 1;
01400     u_char *to = dst;
01401     u_char *from;
01402     u_char *from_end;
01403 
01404     while (lines > 0)
01405     {
01406         from = (u_char *) next_fromptr;
01407         from_end = from + to_width;
01408 
01409         while (from < from_end)
01410         {
01411             *to++ = *from;
01412             from += subsample;
01413         }
01414         next_fromptr += src_width * subsample;
01415         lines -= subsample;
01416     }
01417 }
01418 
01419 
01420 void
01421 usage(char *progname)
01422 {
01423     printf("\n%s: Image acquisition and optional save for EDT PCI DV family digital\n", progname);
01424     printf("DMA interface products. Many options make it useful for diagnostics and\n");
01425     printf("also as an example for various methods of doing acquisition with EDT PCI DV\n");
01426     printf("family products. For a much a simpler example, see simple_take.c\n\n");
01427     printf("usage: %s\n", progname);
01428     printf("  -e exposuretime   set exposure time before acquiring (if supported)\n");
01429     printf("  -g gain           set camera gain (if supported)\n");
01430     printf("  -o offset         set camera offset (if supported)\n");
01431     printf("  -l loopcount      loop for loopcount images\n");
01432     printf("  -m msecs          delay msecs milliseconds between images\n");
01433     printf("  -f out_fname      output to raw file(s)\n");
01434 #ifdef _NT_
01435     printf("  -b fname.bmp      output to Microsoft bitmap file(s)\n");
01436 #else
01437     printf("  -i fname.ras      output to Sun Raster file(s)\n");
01438 #endif
01439     printf("  -C x0 y0 x1 y1    crop the output image to the coordinates given\n");
01440     printf("  -N n              number of multiple buffers (forces multibuf mode)\n");
01441     printf("  -S n              subsample -- take every nth pixel\n");
01442     printf("  -r [hs ha vs va]  region of interest -- to disable use -r alone (no subargs)\n");
01443     printf("  -t [msecs]        timeout # of msecs. if 0, disables timeouts, if no subarg,\n");
01444     printf("  -T                if image times out, dump any recieved data to timeout.raw\n");
01445     printf("                    re-enables auto timeouts\n");
01446     printf("  -w                wait for <CR> between images\n");
01447     printf("  -s start inc      specify start and increment for auto-numbered file-\n");
01448     printf("                    names (use with -l of 2 or more for auto-numbered filenames)\n");
01449     printf("  -I inc            specify increment for auto-numbered file (see -s)\n");
01450     printf("  -v                verbose output\n");
01451     printf("  -q binval         set binning (applicable cameras only)\n");
01452     printf("  -p                check pci dv simulator signature, exit if bad\n");
01453     printf("  -d                get and print a driver timestamp with every image\n");
01454     printf("  -j                just output the last take line (for output to file)\n");
01455     printf("  -c channel        channel # if multiple channels (2nd base Camera Link\n");
01456     printf("                    connector or multiple RCI units on one FOI card (default 0)\n");
01457     printf("  -u unit           pdv unit number (default 0)\n");
01458     printf("  -F foi_unit       RCI module number (default no set)\n");
01459     printf("                    A full device pathname can also be used\n");
01460     printf("  -h                this help message\n");
01461 }

Generated on Mon May 12 16:38:59 2008 by  doxygen 1.5.1