/* * Display some text on somebody's window, processing some control * characters while we are at it. */ int display (register xwin_t * win, register char *text, int size) { register int i; unsigned char cch; for (i = 0; i < size; i++) { if (*text == '\n') { xscroll (win, 0); text++; continue; } if (*text == '\a') { beep (); wrefresh (curscr); text++; continue; } /* erase character */ if (*text == win->cerase) { wmove (win->x_win, win->x_line, max (--win->x_col, 0)); getyx (win->x_win, win->x_line, win->x_col); waddch (win->x_win, ' '); wmove (win->x_win, win->x_line, win->x_col); getyx (win->x_win, win->x_line, win->x_col); text++; continue; } /* * On word erase search backwards until we find * the beginning of a word or the beginning of * the line. */ if (*text == win->werase) { int endcol, xcol, i, c; endcol = win->x_col; xcol = endcol - 1; while (xcol >= 0) { c = readwin (win->x_win, win->x_line, xcol); if (c != ' ') break; xcol--; } while (xcol >= 0) { c = readwin (win->x_win, win->x_line, xcol); if (c == ' ') break; xcol--; } wmove (win->x_win, win->x_line, xcol + 1); for (i = xcol + 1; i < endcol; i++) waddch (win->x_win, ' '); wmove (win->x_win, win->x_line, xcol + 1); getyx (win->x_win, win->x_line, win->x_col); text++; continue; } /* line kill */ if (*text == win->kill) { wmove (win->x_win, win->x_line, 0); wclrtoeol (win->x_win); getyx (win->x_win, win->x_line, win->x_col); text++; continue; } if (*text == '\f') { if (win == &my_win) wrefresh (curscr); text++; continue; } if (win->x_col == COLS - 1) { /* check for wraparound */ xscroll (win, 0); } if (*text < ' ' && *text != '\t') { waddch (win->x_win, '^'); getyx (win->x_win, win->x_line, win->x_col); if (win->x_col == COLS - 1) /* check for wraparound */ xscroll (win, 0); cch = (*text & 63) + 64; waddch (win->x_win, cch); } else waddch (win->x_win, *text); getyx (win->x_win, win->x_line, win->x_col); text++; } wrefresh (win->x_win); return 0; }
int main (int argc, char *argv[]) { /************/ /* 4dfp I/O */ /************/ FILE *fp_img, *fp_out; IFH ifh; char outfile[MAXL], imgfile[MAXL]; char imgroot[MAXL], outroot[MAXL] = ""; char control = '\0'; /****************/ /* image arrays */ /****************/ float *img1, *imgc; float voxdim[3]; int imgdim[4], outdim[4], ix, iy, iz, index, vdim, isbig; int ix0 = 1, ix1 = 0, iy0 = 1, iy1 = 0, iz0 = 1, iz1 = 0; int vdim_crop, orient; /***********/ /* utility */ /***********/ int c, i, j, k; char *ptr, string[MAXL]; /*********/ /* flags */ /*********/ int doflip = 0; int docrop = 1; int scrollx = 0, scrolly = 0, scrollz = 0; printf ("%s\n", rcsid); setprog (program, argv); /************************/ /* process command line */ /************************/ for (k = 0, i = 1; i < argc; i++) { if (*argv[i] == '-') { strcpy (string, argv[i]); ptr = string; while (c = *ptr++) switch (c) { case 'f': doflip++; break; case 'Z': docrop = 0; break; case '@': control = *ptr++; *ptr = '\0'; break; case 'x': getirange (ptr, &ix0, &ix1); *ptr = '\0'; break; case 'y': getirange (ptr, &iy0, &iy1); *ptr = '\0'; break; case 'z': getirange (ptr, &iz0, &iz1); *ptr = '\0'; break; case 's': switch (*ptr++) { case 'x': scrollx = atoi (ptr); break; case 'y': scrolly = atoi (ptr); break; case 'z': scrollz = atoi (ptr); break; } *ptr = '\0'; break; } } else switch (k) { case 0: getroot (argv[i], imgroot); k++; break; case 1: getroot (argv[i], outroot); k++; break; } } if (k < 1) { printf ("Usage:\t%s <(4dfp) inroot> [(4dfp) outroot]\n", program); printf ("\toption\n"); printf ("\t-<x|y|z><int>[to[<int>]\tspecify x y z crop limits\n"); printf ("\t-s<x|y|z><int>\tscroll specified axis by specified number of pixels\n"); printf ("\t-f\tinterpret specifications under 4dfp<->analyze flips\n"); printf ("\t-Z\tzero voxels instead of physically cropping\n"); printf ("\t-@<b|l>\toutput big or little endian (default input endian)\n"); printf ("N.B.:\tcrop limit indices count from 1\n"); printf ("N.B.:\tscrolling is done after cropping\n"); printf ("N.B.:\tdefault (4dfp) output root is <(4dfp) inroot>\"_crop\"\n"); exit (1); } sprintf (imgfile, "%s.4dfp.img", imgroot); if (get_4dfp_dimoe (imgfile, imgdim, voxdim, &orient, &isbig) < 0 || Getifh (imgfile, &ifh)) errr (program, imgfile); if (!control) control = (isbig) ? 'b' : 'l'; printf ("input dimensions %10d%10d%10d%10d\n", imgdim[0], imgdim[1], imgdim[2], imgdim[3]); printf ("voxel dimensions %10.6f%10.6f%10.6f\n", voxdim[0], voxdim[1], voxdim[2]); if (ix1 > imgdim[0] || ix1 < 1) ix1 = imgdim[0]; if (iy1 > imgdim[1] || iy1 < 1) iy1 = imgdim[1]; if (iz1 > imgdim[2] || iz1 < 1) iz1 = imgdim[2]; if (ix0 > imgdim[0] || ix0 < 1) ix0 = 1; if (iy0 > imgdim[1] || iy0 < 1) iy0 = 1; if (iz0 > imgdim[2] || iz0 < 1) iz0 = 1; if (ix1 < ix0) {k = ix1; ix1 = ix0; ix0 = k;} if (iy1 < iy0) {k = iy1; iy1 = iy0; iy0 = k;} if (iz1 < iz0) {k = iz1; iz1 = iz0; iz0 = k;} printf ("crop limits x %3d to %3d\n", ix0, ix1); printf ("crop limits y %3d to %3d\n", iy0, iy1); printf ("crop limits z %3d to %3d\n", iz0, iz1); printf ("scroll x %3d\n", scrollx); printf ("scroll y %3d\n", scrolly); printf ("scroll z %3d\n", scrollz); outdim[0] = (docrop) ? ix1 - ix0 + 1 : imgdim[0]; outdim[1] = (docrop) ? iy1 - iy0 + 1 : imgdim[1]; outdim[2] = (docrop) ? iz1 - iz0 + 1 : imgdim[2]; outdim[3] = imgdim[3]; printf ("output dimensions %10d%10d%10d%10d\n", outdim[0], outdim[1], outdim[2], outdim[3]); vdim = imgdim[0] * imgdim[1] * imgdim[2]; if (!(img1 = (float *) malloc (vdim * sizeof (float)))) errm (program); if (!(fp_img = fopen (imgfile, "rb"))) errr (program, imgfile); printf ("Reading: %s\n", imgfile); if (!strlen (outroot)) sprintf (outroot, "%s_crop", imgroot); sprintf (outfile, "%s.4dfp.img", outroot); vdim_crop = outdim[0] * outdim[1] * outdim[2]; if (!(imgc = (float *) calloc (vdim_crop, sizeof (float)))) errm (program); if (!(fp_out = fopen (outfile, "wb"))) errw (program, outfile); printf ("Writing: %s\n", outfile); /***********/ /* process */ /***********/ printf ("processing volume"); for (k = 0; k < imgdim[3]; k++) {printf (" %d", k + 1); fflush (stdout); if (eread (img1, vdim, isbig, fp_img)) errr (program, imgfile); if (doflip) { if (x4dfp2analyze (img1, imgdim, ifh.orientation)) { fprintf (stderr, "%s: invalid %s orientation\n", program, imgroot); exit (-1); } } i = 0; for (iz = iz0 - 1; iz < iz1; iz++) { for (iy = iy0 - 1; iy < iy1; iy++) { for (ix = ix0 - 1; ix < ix1; ix++) { index = ix + imgdim[0]*(iy + imgdim[1]*iz); if (docrop) { imgc[i++] = img1[index]; } else { imgc[index] = img1[index]; } }}} if (scrollx) xscroll (img1, imgc, outdim, scrollx); if (scrolly) yscroll (img1, imgc, outdim, scrolly); if (scrollz) zscroll (img1, imgc, outdim, scrollz); if (doflip) x4dfp2analyze (imgc, outdim, ifh.orientation); if (ewrite (imgc, vdim_crop, control, fp_out)) errw (program, outfile); } printf ("\n"); fflush (stdout); fclose (fp_img); fclose (fp_out); /**************/ /* output ifh */ /**************/ if (docrop) { if (writeifhe (program, outroot, outdim, voxdim, ifh.orientation, control)) errw (program, outroot); } else { Writeifh (program, outroot, &ifh, control); } /***************/ /* run ifh2hdr */ /***************/ sprintf (string, "ifh2hdr %s", outroot); status |= system (string); /************/ /* rec file */ /************/ startrece (outfile, argc, argv, rcsid, control); if (doflip) printrec ("indices subjected to 4dfp<->analyze orientation dependent flips\n"); if (!docrop) printrec ("voxels zeroed instead of physically cropped\n"); sprintf (string, "crop limits x %3d to %3d\n", ix0, ix1); printrec (string); sprintf (string, "crop limits y %3d to %3d\n", iy0, iy1); printrec (string); sprintf (string, "crop limits z %3d to %3d\n", iz0, iz1); printrec (string); if (scrollx) {sprintf (string, "scroll x %3d\n", scrollx); printrec (string);} if (scrolly) {sprintf (string, "scroll y %3d\n", scrolly); printrec (string);} if (scrollz) {sprintf (string, "scroll z %3d\n", scrollz); printrec (string);} catrec (imgfile); endrec (); /************/ /* clean up */ /************/ free (img1); free (imgc); exit (status); }