int write_image_4dfp(char *filespc, IMAGE_4dfp *image) { int isbig, vdim, status = 0; char imgroot[MAXL], imgfile[MAXL], command[MAXL]; FILE *imgfp; char control = '\0'; isbig = strcmp (image->ifh.imagedata_byte_order, "littleendian"); if (!control) control = (isbig) ? 'b' : 'l'; vdim=image->ifh.matrix_size[0]*image->ifh.matrix_size[1]*image->ifh.matrix_size[2]*image->ifh.matrix_size[3]; getroot(filespc, imgroot); sprintf(imgfile, "%s.4dfp.img", imgroot); if (!(imgfp = fopen (imgfile, "wb"))) errw("write_image_4dfp", imgfile); if (ewrite(image->image, vdim, control, imgfp)) errw("write_image_4dfp", imgfile); /* ifh hdr rec */ if (fclose(imgfp)) errw("write_image_4dfp", imgfile); if (Writeifh("write_image_4dfp", imgfile, &image->ifh, control)) errw("write_image_4dfp", imgroot); sprintf(command, "ifh2hdr %s", imgroot); status |= system (command); return status; }
int main (int argc, char *argv[]) { /*************/ /* image I/O */ /*************/ FILE *fp_img, *fp_out; IFH ifh; char imgfile[MAXL], outfile[MAXL], imgroot[MAXL], outroot[MAXL]; char *str, command[MAXL], program[MAXL]; /**************/ /* processing */ /**************/ char control = '\0'; int dimension, c, i, j, k; int imgdim[4], orient, isbig; float voxdim[3], voxdimn[3]; float *imgz, *imgp; double q; /*********/ /* flags */ /*********/ int status = 0; int p_flag = 0; int two_sided = 0; int defined; int debug = 0; printf ("%s\n", rcsid); setprog (program, argv); /************************/ /* process command line */ /************************/ for (k = 0, i = 1; i < argc; i++) { if (*argv[i] == '-') { strcpy (command, argv[i]); str = command; while (c = *str++) switch (c) { case 'd': debug++; break; case 'p': p_flag++; break; case '2': two_sided++; break; case '@': control = *str++; *str = '\0'; break; } } else switch (k) { case 0: getroot (argv[i], imgroot); k++; break; } } if (k < 1) usage (program); /**************************/ /* get input 4dfp z-image */ /**************************/ sprintf (imgfile, "%s.4dfp.img", imgroot); if (get_4dfp_dimoe (imgfile, imgdim, voxdim, &orient, &isbig) < 0) errr (program, imgfile); if (Getifh (imgfile, &ifh)) errr (program, imgfile); if (!control) control = (isbig) ? 'b' : 'l'; dimension = imgdim[0] * imgdim[1] * imgdim[2]; if (!(imgz = (float *) malloc (dimension * sizeof (float)))) errm (program); if (!(imgp = (float *) malloc (dimension * sizeof (float)))) errm (program); /***************************/ /* prepare 4dfp read/write */ /***************************/ fprintf (stdout, "Reading: %s\n", imgfile); if (!(fp_img = fopen (imgfile, "rb"))) errr (program, imgfile); sprintf (outroot, "%s_%s", imgroot, (p_flag) ? "p" : "log10p"); sprintf (outfile, "%s.4dfp.img", outroot); fprintf (stdout, "Writing: %s\n", outfile); if (!(fp_out = fopen (outfile, "wb"))) errw (program, outfile); /***********/ /* process */ /***********/ for (j = 0; j < imgdim[3]; j++) { if (eread (imgz, dimension, isbig, fp_img)) errr (program, imgfile); for (i = 0; i < dimension; i++) { q = (double) imgz[i]; defined = !isnan (q) && imgz[i] != (float) 1.e-37 && finite (q); if (defined) { if (two_sided) { q = fabs (q); imgp[i] = (p_flag) ? 2.*p_z (q) : (-1./M_LN10)*(lnp_z (q) + M_LN2); } else { if (q > 0) { imgp[i] = (p_flag) ? p_z (q) : (-1./M_LN10)*lnp_z (q); } else { imgp[i] = (p_flag) ? 1.0 : 0.0; } } } else { imgp[i] = 1.e-37; } } if (ewrite (imgp, dimension, control, fp_out)) errw (program, outfile); } if (fclose (fp_img)) errr (program, imgfile); if (fclose (fp_out)) errw (program, outfile); /*******/ /* ifh */ /*******/ if (Writeifh (program, outfile, &ifh, control)) errw (program, outroot); /*******/ /* hdr */ /*******/ sprintf (command, "ifh2hdr %s", outroot); status |= system (command); /*******/ /* rec */ /*******/ startrece (outfile, argc, argv, rcsid, control); catrec (imgfile); endrec (); free (imgz); free (imgp); exit (status); }
int main (int argc, char *argv[]) { FILE *imgfp; /* input file */ FILE *outfp; /* output file */ /*************/ /* image i/o */ /*************/ char *ptr, string[MAXL], program[MAXL], command[MAXL]; char imgroot[MAXL], imgfile[MAXL], ifhfile[MAXL]; char mskroot[MAXL], mskfile[MAXL]; char tmproot[MAXL], tmpfile[MAXL]; char trailer[MAXL] = "", outroot[MAXL], outfile[MAXL], recfile[MAXL]; /***************/ /* computation */ /***************/ float *imgv, *imgr, *imgm; float v, frac, t, del2v, dmin; float fndex[3], x[3], w[3], dvdx[3], d2vdx2[3]; float ctneg = 0.0, ctpos = 0.0; float vtneg = 0.0, vtpos = 0.0; float srad = 0.0, orad = 0; int ix, iy, iz, dim, nx, ny, nz; int c, i, j, k, l, jmin; int isbig, isbigm; char control = '\0'; /**************/ /* peak lists */ /**************/ EXTREMUM *ppos, *pneg, *pall, *pallN, ptmp[1]; int mpos = 0, mneg = 0, npos = 0, nneg = 0, nall, nallN, Nvoxcrit = 1; int npos0, npos1, npos2; int nneg0, nneg1, nneg2; /*********/ /* flags */ /*********/ int mask = 0; int status = 0; int debug = 0; int quiet = 0; int forceblur = 0; printf ("%s\n", rcsid); if (!(ptr = strrchr (argv[0], '/'))) ptr = argv[0]; else ptr++; strcpy (program, ptr); /******************************/ /* get command line arguments */ /******************************/ for (k = 0, i = 1; i < argc; i++) { if (*argv[i] == '-') { strcpy (command, argv[i]); ptr = command; while (c = *ptr++) switch (c) { case 'q': quiet++; break; case 'F': forceblur++; break; case 's': srad = atof (ptr); *ptr = '\0'; break; case 'd': dthresh = atof (ptr); *ptr = '\0'; break; case 'n': ntop = atoi (ptr); *ptr = '\0'; break; case 'N': Nvoxcrit = atoi (ptr); *ptr = '\0'; break; case 'o': orad = atof (ptr); *ptr = '\0'; break; case '@': control = *ptr++; *ptr = '\0'; break; case 'c': getrange (ptr, &ctneg, &ctpos); *ptr = '\0'; break; case 'v': getrange (ptr, &vtneg, &vtpos); *ptr = '\0'; break; case 'm': getroot (ptr, mskroot); mask++; *ptr = '\0'; break; case 'a': strncpy (trailer, ptr, MAXL - 1); *ptr = '\0'; break; } } else switch (k) { case 0: getroot (argv[i], imgroot); k++; break; } } if (k < 1) { printf ("Usage: %s <file_4dfp>\n", program); printf (" e.g., %s grand_average_222[.4dfp.img] -s10\n", program); printf ("\toption\n"); printf ("\t-s<flt>\tpreblur with hard sphere kernel of specified radius\n"); printf ("\t-n<int>\tlimit initial pos and neg peak list lengths (default=%d)\n", NTOP); printf ("\t-c<flt>[to<flt>] specify sign inverted curvature thresholds (default none)\n"); printf ("\t-v<flt>[to<flt>] specify peak value thresholds (default none)\n"); printf ("\t-d<flt>\tconsolidate extremum pairs closer than specified distance\n"); printf ("\t-o<flt>\toutput a fidl compatible 4dfp format ROI file with regions of specified radius\n"); printf ("\t-m<str>\tapply named mask file to output ROIs\n"); printf ("\t-N<int>\tspecify output ROI minimum voxel count (default = 1)\n"); printf ("\t-a<str>\tappend specified string to ROI output filename\n"); printf ("\t-q\tquiet mode (suppress rec file listing)\n"); printf ("\t-F\tforce preblur image creation even if hsphere_4dfp result exists\n"); printf ("\t-@<b|l>\toutput big or little endian (default input endian)\n"); printf ("N.B.:\toperations controlled by options -s, -n, -c, -v, -d, -o, -m, -N are applied serially in listed order\n"); printf ("N.B.:\tall distances are in mm\n"); printf ("N.B.:\toption -s<flt> creates a blurred image by invoking hsphere_4dfp\n"); printf ("N.B.:\toption -F is intended for use in iterative scripts and has no effect absent -s<flt>\n"); exit (1); } /************/ /* read ifh */ /************/ sprintf (imgfile, "%s.4dfp.img", imgroot); if (srad > 0.0) { sprintf (tmproot, "%s_%.0fmm", imgroot, srad); } else { strcpy (tmproot, imgroot); } sprintf (tmpfile, "%s.4dfp.img", tmproot); sprintf (ifhfile, "%s.4dfp.ifh", imgroot); fprintf (stdout, "Reading: %s\n", ifhfile); if (Getifh (ifhfile, &imgifh)) errr (program, ifhfile); isbig = strcmp (imgifh.imagedata_byte_order, "littleendian"); if (!control) control = (isbig) ? 'b' : 'l'; printf ("image dimensions \t%10d%10d%10d%10d\n", imgifh.matrix_size[0], imgifh.matrix_size[1], imgifh.matrix_size[2], imgifh.matrix_size[3]); printf ("image mmppix \t%10.6f%10.6f%10.6f\n", imgifh.mmppix[0], imgifh.mmppix[1], imgifh.mmppix[2]); printf ("image center \t%10.4f%10.4f%10.4f\n", imgifh.center[0], imgifh.center[1], imgifh.center[2]); printf ("image orientation\t%10d\n", imgifh.orientation); /********************************************************************/ /* check that optionally specified mask is dimensionally consistent */ /********************************************************************/ if (mask) { sprintf (mskfile, "%s.4dfp.img", mskroot); sprintf (ifhfile, "%s.4dfp.ifh", mskroot); fprintf (stdout, "Reading: %s\n", ifhfile); if (Getifh (ifhfile, &mskifh)) errr (program, ifhfile); isbigm = strcmp (mskifh.imagedata_byte_order, "littleendian"); status = imgifh.orientation - mskifh.orientation; for (k = 0; k < 3; k++) { status |= imgifh.matrix_size[k] - mskifh.matrix_size[k]; status |= (fabs ((double) (imgifh.mmppix[k] - mskifh.mmppix[k])) > 0.0001); } if (status) { fprintf (stderr, "%s: %s %s dimension mismatch\n", program, imgroot, mskroot); exit (-1); } } dim = 1; for (k = 0; k < 3; k++) dim *= imgifh.matrix_size[k]; if (!(imgv = (float *) malloc (dim * sizeof (float)))) errm (program); if (!(imgr = (float *) malloc (dim * sizeof (float)))) errm (program); if (!(imgm = (float *) malloc (dim * sizeof (float)))) errm (program); nx = imgifh.matrix_size[0]; ny = imgifh.matrix_size[1]; nz = imgifh.matrix_size[2]; /*****************************************/ /* virtual flip instead of x4dfp2ecat () */ /*****************************************/ vrtflip_ (&imgifh.orientation, imgifh.matrix_size, imgifh.center, imgifh.mmppix, centerr, mmppixr); printf ("atlas mmppix \t%10.6f%10.6f%10.6f\n", mmppixr[0], mmppixr[1], mmppixr[2]); printf ("atlas center \t%10.4f%10.4f%10.4f\n", centerr[0], centerr[1], centerr[2]); /****************************************************/ /* read filtered image or create using hpshere_4dfp */ /****************************************************/ if (forceblur || access (tmpfile, R_OK)) { sprintf (command, "hsphere_4dfp %s %.4f", imgfile, srad); printf ("%s\n", command); if (system (command)) errw (program, tmpfile); } printf ("Reading: %s\n", tmpfile); if (!(imgfp = fopen (tmpfile, "rb")) || eread (imgv, dim, isbig, imgfp) || fclose (imgfp)) errr (program, tmpfile); /************************************************************************************************/ /* intial extremum memory allocation (realloc on unassigned pointer => unpredictable core dump) */ /************************************************************************************************/ if (!(ppos = (EXTREMUM *) malloc ((mpos = MSIZE) * sizeof (EXTREMUM)))) errm (program); if (!(pneg = (EXTREMUM *) malloc ((mneg = MSIZE) * sizeof (EXTREMUM)))) errm (program); /*******************/ /* compile extrema */ /*******************/ printf ("peak value thresholds %10.4f to %10.4f\n", vtneg, vtpos); printf ("peak curvature thresholds %10.4f to %10.4f\n", ctneg, ctpos); printf ("compiling extrema slice"); for (iz = 1; iz < imgifh.matrix_size[2] - 1; iz++) {printf (" %d", iz + 1); fflush (stdout); for (iy = 1; iy < imgifh.matrix_size[1] - 1; iy++) { for (ix = 1; ix < imgifh.matrix_size[0] - 1; ix++) { i = ix + nx*(iy + ny*iz); dvdx[0] = 0.5*(-imgv[i - 1] + imgv[i + 1]); dvdx[1] = 0.5*(-imgv[i - nx] + imgv[i + nx]); dvdx[2] = 0.5*(-imgv[i - nx*ny] + imgv[i + nx*ny]); d2vdx2[0] = -2.0*imgv[i] + imgv[i - 1] + imgv[i + 1]; d2vdx2[1] = -2.0*imgv[i] + imgv[i - nx] + imgv[i + nx]; d2vdx2[2] = -2.0*imgv[i] + imgv[i - nx*ny] + imgv[i + nx*ny]; for (k = 0; k < 3; k++) w[k] = -dvdx[k] / d2vdx2[k]; for (del2v = k = 0; k < 3; k++) del2v += d2vdx2[k] / (mmppixr[k] * mmppixr[k]); fndex[0] = (float) (ix + 1) + w[0]; fndex[1] = (float) (iy + 1) + w[1]; fndex[2] = (float) (iz + 1) + w[2]; for (k = 0; k < 3; k++) x[k] = fndex[k] * mmppixr[k] - centerr[k]; if (imgv[i] > 0.0 && del2v <= -ctpos && imgv[i] > imgv[i - 1] && imgv[i] > imgv[i + 1] && imgv[i] > imgv[i - nx] && imgv[i] > imgv[i + nx] && imgv[i] > imgv[i - nx*ny] && imgv[i] > imgv[i + nx*ny]) { imgvalx_ (imgv, &nx, &ny, &nz, centerr, mmppixr, x, &t, &l); if (l < 1) {printf ("undefined imgvalx point\n"); continue;} if (t < vtpos) continue; if (mpos <= npos) { mpos += MSIZE; if (!(ppos = (EXTREMUM *) realloc (ppos, mpos * sizeof (EXTREMUM)))) errm (program); } for (k = 0; k < 3; k++) ppos[npos].x[k] = x[k]; ppos[npos].v = t; ppos[npos].del2v = del2v; ppos[npos].weight = 1.0; ppos[npos].nvox = ppos[npos].killed = 0; npos++; } if (imgv[i] < 0.0 && del2v >= -ctneg && imgv[i] < imgv[i - 1] && imgv[i] < imgv[i + 1] && imgv[i] < imgv[i - nx] && imgv[i] < imgv[i + nx] && imgv[i] < imgv[i - nx*ny] && imgv[i] < imgv[i + nx*ny]) { imgvalx_ (imgv, &nx, &ny, &nz, centerr, mmppixr, x, &t, &l); if (l < 1) {printf ("undefined imgvalx point\n"); continue;} if (t > vtneg) continue; if (mneg <= nneg) { mneg += MSIZE; if (!(pneg = (EXTREMUM *) realloc (pneg, mneg * sizeof (EXTREMUM)))) errm (program); } for (k = 0; k < 3; k++) pneg[nneg].x[k] = x[k]; pneg[nneg].v = t; pneg[nneg].del2v = del2v; pneg[nneg].weight = 1.0; pneg[nneg].nvox = pneg[nneg].killed = 0; nneg++; } }}} printf ("\n"); fflush (stdout); printf ("before sorting npos=%d nneg=%d\n", npos, nneg); npos0 = npos; nneg0 = nneg; qsort ((void *) ppos, npos, sizeof (EXTREMUM), pcompare); qsort ((void *) pneg, nneg, sizeof (EXTREMUM), pcompare); printf ("positive peaks\n"); ntot = 0; peaklist (stdout, ppos, npos, 0); npos1 = ntot; if (dthresh > 0.0) consolidate (ppos, npos); printf ("negative peaks\n"); ntot = 0; peaklist (stdout, pneg, nneg, 0); nneg1 = ntot; if (dthresh > 0.0) consolidate (pneg, nneg); printf ("final peak list\n"); ntot = 0; peaklist (stdout, ppos, npos, 0); peaklist (stdout, pneg, nneg, 0); /********************************************/ /* combine positive and negative peak lists */ /********************************************/ if (!(pall = (EXTREMUM *) malloc (ntot * sizeof (EXTREMUM)))) errm (program); nall = 0; for (npos2 = i = 0; i < ntop && i < npos; i++) if (!ppos[i].killed) {pall[nall++] = ppos[i]; npos2++;} for (nneg2 = i = 0; i < ntop && i < nneg; i++) if (!pneg[i].killed) {pall[nall++] = pneg[i]; nneg2++;} assert (ntot == nall); free (ppos); free (pneg); if (orad == 0.0) goto DONE; /***************************/ /* create output ROI image */ /***************************/ printf ("Reading: %s\n", imgfile); if (!(imgfp = fopen (imgfile, "rb")) || eread (imgv, dim, isbig, imgfp) || fclose (imgfp)) errr (program, imgfile); if (mask) { printf ("Reading: %s\n", mskfile); if (!(imgfp = fopen (mskfile, "rb")) || eread (imgm, dim, isbigm, imgfp) || fclose (imgfp)) errr (program, mskfile); } /****************************/ /* count voxels in each ROI */ /****************************/ for (iz = 0; iz < imgifh.matrix_size[2]; iz++) { for (iy = 0; iy < imgifh.matrix_size[1]; iy++) { for (ix = 0; ix < imgifh.matrix_size[0]; ix++) { i = ix + nx*(iy + ny*iz); fndex[0] = (float) (ix + 1); fndex[1] = (float) (iy + 1); fndex[2] = (float) (iz + 1); for (k = 0; k < 3; k++) ptmp[0].x[k] = fndex[k]*mmppixr[k] - centerr[k]; dmin = 1.e6; for (j = 0; j < nall; j++) { t = (float) pdist (ptmp, pall + j); if (t < dmin) { jmin = j; dmin = t; } } k = !mask || (fabs (imgm[i]) > 1.e-37); if (k && (dmin < orad)) pall[jmin].nvox++; }}} /*******************************/ /* apply voxel count criterion */ /*******************************/ for (i = 0; i < ntot; i++) if (pall[i].nvox < Nvoxcrit) pall[i].killed = 1; if (!(pallN = (EXTREMUM *) malloc (ntot * sizeof (EXTREMUM)))) errm (program); for (nallN = i = 0; i < ntot; i++) if (!pall[i].killed) pallN[nallN++] = pall[i]; free (pall); printf ("creating ROI slice"); for (iz = 0; iz < imgifh.matrix_size[2]; iz++) {printf (" %d", iz + 1); fflush (stdout); for (iy = 0; iy < imgifh.matrix_size[1]; iy++) { for (ix = 0; ix < imgifh.matrix_size[0]; ix++) { i = ix + nx*(iy + ny*iz); imgr[i] = 0.0; fndex[0] = (float) (ix + 1); fndex[1] = (float) (iy + 1); fndex[2] = (float) (iz + 1); for (k = 0; k < 3; k++) ptmp[0].x[k] = fndex[k]*mmppixr[k] - centerr[k]; dmin = 1.e6; for (j = 0; j < nallN; j++) { t = (float) pdist (ptmp, pallN + j); if (t < dmin) { jmin = j; dmin = t; } } k = !mask || (fabs (imgm[i]) > 1.e-37); if (k && (dmin < orad)) imgr[i] = jmin + 2; }}} printf ("\n"); fflush (stdout); if (!(ptr = strrchr (imgroot, '/'))) ptr = imgroot; else ptr++; if (strlen (trailer)) { sprintf (outroot, "%s_ROI_%s", ptr, trailer); } else { sprintf (outroot, "%s_ROI", ptr); } sprintf (outfile, "%s.4dfp.img", outroot); printf ("Writing: %s\n", outfile); if (!(outfp = fopen (outfile, "wb")) || ewrite (imgr, dim, control, outfp) || fclose (outfp)) errw (program, outfile); /********************************************/ /* output ROI ifh and make analyze hdr file */ /********************************************/ if (Writeifh (program, outfile, &imgifh, control)) errw (program, outroot); sprintf (outfile, "%s.4dfp.ifh", outroot); if (!(outfp = fopen (outfile, "a"))) errw (program, outfile); for (i = 0; i < nallN; i++) { fprintf (outfp, "region names\t:= %-5droi_%+03d_%+03d_%+03d%10d\n", i, nint (pallN[i].x[0]), nint (pallN[i].x[1]), nint (pallN[i].x[2]), pallN[i].nvox); } fclose (outfp); /********************/ /* make analyze hdr */ /********************/ sprintf (command, "ifh2hdr %s -r%d", outroot, nallN + 1); printf ("%s\n", command); status |= system (command); /******************/ /* output ROI rec */ /******************/ sprintf (outfile, "%s.4dfp.img", outroot); startrece (outfile, argc, argv, rcsid, control); sprintf (recfile, "%s.rec", outfile); if (!(outfp = fopen (recfile, "a"))) errw (program, recfile); fprintf (outfp, "Peak value thresholds %10.4f to %10.4f\n", vtneg, vtpos); fprintf (outfp, "Peak curvature thresholds %10.4f to %10.4f\n", ctneg, ctpos); fprintf (outfp, "Peak counts before sorting pos=%d neg=%d\n", npos0, nneg0); fprintf (outfp, "Peak counts after sorting pos=%d neg=%d\n", npos1, nneg1); fprintf (outfp, "Peak counts after %.4f mm threshold consolidation pos=%d neg=%d\n", dthresh, npos2, nneg2); ntot = 0; peaklist (outfp, pallN, nallN, 1); free (pallN); fprintf (outfp, "N.B.: indices count from 1 and include orientation specific 4dfptoanalyze flips\n"); fclose (outfp); catrec (tmpfile); if (mask) catrec (mskfile); endrec (); sprintf (command, "brec %s -2", recfile); if (!quiet) system (command); DONE: free (imgv); free (imgr); free (imgm); exit (status); }
int main (int argc, char *argv[]) { FILE *imgfp, *datfp; char imgroot[MAXL], imgfile[MAXL], gfcfile[MAXL], ifhfile[MAXL]; char outroot[MAXL], outfile[MAXL], datfile[MAXL], volfile[MAXL], recfile[MAXL]; char program[MAXL], string[MAXL], command[MAXL], *ptr; int isbig; char control = '\0'; IFH ifh; UCHAR ix, iy, iz; XYZN xyzn; int iv, iv0, jv, imgdim[3]; int ir, jr, nn; int c, i, j, k, m, n; double dela, delg, u1; float vmin, vmax, fwhm = 0., alpha = ALPHA, *img_orig=NULL; float maxfac = 0.0; /**************/ /* gain field */ /**************/ int norder = NORDER, nterm = NTERM; int niter = 0, limiter = LIMITER; int eight = 8; /* field length for call to powstring_ () */ float a[NTERM], /* polynomial coefficients */ a0[NTERM]; float drms, diffcrit = DIFFCRIT; float u1gfc[MAXR]; double sum0, sum, q, t; /*********/ /* flags */ /*********/ int test = 0; int status = 0; int debug = 0; int gfreeze = 0; int verbose = 0; int negdef = 0; if (!(ptr = strrchr (argv[0], '/'))) ptr = argv[0]; else ptr++; strcpy (program, ptr); /************************/ /* 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 'v': verbose++; break; case 'n': negdef++; break; case 'g': gfreeze++; break; case '@': control = *ptr++; *ptr = '\0'; break; case 'b': g_bandwidth = atof (ptr); *ptr = '\0'; break; case 'p': fwhm = atof (ptr); *ptr = '\0'; break; case 'e': diffcrit = atof (ptr); *ptr = '\0'; break; case 'i': g_sigma = atof (ptr); *ptr = '\0'; break; case 'l': limiter = atoi (ptr); *ptr = '\0'; break; case 'm': g_mr = atoi (ptr); *ptr = '\0'; break; case 's': g_sconst = atof (ptr); *ptr = '\0'; break; case 'z': g_zthresh = atof (ptr); *ptr = '\0'; break; case 'M': maxfac = atof (ptr); *ptr = '\0'; break; case 'r': getrange (ptr, &g_gfc_thresh, &g_gfc_ceil); *ptr = '\0'; break; } } else switch (k) { case 0: getroot (argv[i], imgroot); k++; break; } if (k < 1) { fprintf (stderr, "Usage:\t%s <imgroot>\n", program); fprintf (stderr, " e.g.,\t%s vc1440_mpr_n4_111_t88.4dfp\n", program); /*??*/ fprintf (stderr, "\toption\n"); fprintf (stderr, "\t-g freeze initial gain field\n"); fprintf (stderr, "\t-n force negative definite quadratic gain field\n"); fprintf (stderr, "\t-v verbose mode\n"); fprintf (stderr, "\t-p<flt> pre-blur by specified FWHM in mm\n"); fprintf (stderr, "\t-b<flt>\tspecify bandwidth in intensity units (default=%.1f)\n", BANDWIDTH); fprintf (stderr, "\t-e<flt>\tspecify drms convergence criterion (default=%f)\n", DIFFCRIT); fprintf (stderr, "\t-i<flt>\tspecify sigma (default=%f)\n", SIGMA); fprintf (stderr, "\t-l<int>\tspecify iteration limit (default=%d)\n", LIMITER); fprintf (stderr, "\t-m<flt>\tspecify gfc computation region count (default=%d)\n", MR); fprintf (stderr, "\t-s<flt>\tspecify space constant in mm (default=%f)\n", SCONST); fprintf (stderr, "\t-z<flt>\tspecify background threshold (default=%.1f)\n", ZTHRESH); fprintf (stderr, "\t-M<flt>\tspecify maximum correction factor\n"); fprintf (stderr, "\t-r<flt>[to<flt>]\tspecify gfc range (default=%.1fto%.1f)\n", GFC_THRESH, GFC_CEIL); exit (1); } /*******************************************/ /* read/initialize gain field coefficients */ /*******************************************/ sprintf (gfcfile, "%s.4dfp.gfc", imgroot); if (imgfp = fopen (gfcfile, "r")) { printf ("Reading: %s\n", gfcfile); for (k = 0; k < nterm; k++) fscanf (imgfp, "%f", a + k); fclose (imgfp); } else { a[0] = 1.0; for (k = 1; k < nterm; k++) a[k] = 0.0; } for (k = 0; k < nterm; k++) fprintf (stdout, "%8.4f", a[k]); fprintf (stdout, "\n"); /******************************/ /* get input image dimensions */ /******************************/ if (Getifh (imgroot, &ifh) != 0) errr (program, imgroot); isbig = strcmp (ifh.imagedata_byte_order, "littleendian"); if (!control) control = (isbig) ? 'b' : 'l'; for (k = 0; k < 3; k++) g_voxdim[k] = ifh.scaling_factor[k]; if (debug) printf ("%10.6f%10.6f%10.6f\n", g_voxdim[0], g_voxdim[1], g_voxdim[2]); g_dimension = ((g_xdim = ifh.matrix_size[0]) *(g_ydim = ifh.matrix_size[1]) *(g_zdim = ifh.matrix_size[2])); /************************************************/ /* check image index limits against XYZN typedef */ /*************************************************/ if (g_xdim > MAXD || g_ydim > MAXD || g_zdim > MAXD) { fprintf (stderr, "%s: %s dimension exceeds %d\n", program, imgroot, MAXD); exit (-1); } if (ifh.number_of_bytes_per_pixel != 4) { printf ("%s: cannot process %d bytes per pixel", program, ifh.number_of_bytes_per_pixel); exit (-1); } /************/ /* allocate */ /************/ if (!(g_img1 = (float *) malloc (g_dimension * sizeof (float))) || !(g_img0 = (float *) malloc (g_dimension * sizeof (float))) || !(g_imgg = (float *) malloc (g_dimension * sizeof (float))) || !(g_imgo = (short *) calloc (g_dimension, sizeof (short))) || !(g_imgb = (UCHAR *) calloc (g_dimension, sizeof (UCHAR)))) errm (program); sprintf (imgfile, "%s.4dfp.img", imgroot); printf ("Reading: %s\n", imgfile); if (!(imgfp = fopen (imgfile, "rb")) || eread (g_img0, g_dimension, isbig, imgfp) || fclose (imgfp)) errr (program, imgfile); /******************/ /* pre-blur image */ /******************/ if (!gfreeze && fwhm > 0) { if (!(img_orig = (float *) malloc (g_dimension * sizeof (float)))) errm (program); /*************************************/ /* img_orig will hold original image */ /*************************************/ memcpy (img_orig, g_img0, g_dimension*sizeof(float)); memcpy (g_img1, g_img0, g_dimension*sizeof(float)); printf ("blur FWHM = %.2f mm\n", fwhm); imgdim[0] = g_xdim; imgdim[1] = g_ydim; imgdim[2] = g_zdim; imgblur3d_ (&fwhm, &alpha, g_voxdim, g_img1, imgdim, g_img0); /* g_img1 returned unusable */ /************************************************************************/ /* g_img0 now points to blurred image to be used in further computation */ /************************************************************************/ } /*******************/ /* compute outroot */ /*******************/ if (ptr = strrchr (imgroot, '/')) ptr++; else ptr = imgroot; getroot (ptr, outroot); /*********************/ /* apply initial gfc */ /*********************/ evalgain3d_ (g_imgg, &g_xdim, &g_ydim, &g_zdim, &norder, a); for (sum = sum0 = i = 0; i < g_dimension; i++) { sum0 += g_img0[i]; sum += (g_img1[i] = g_img0[i]/g_imgg[i]); } for (i = 0; i < g_dimension; i++) g_img1[i] *= (sum0/sum); if (gfreeze) goto WRITE; /****************/ /* open datfile */ /****************/ sprintf (datfile, "%s_gfc.dat", outroot); if (!(datfp = fopen (datfile, "a"))) errw (program, datfile); powstring_ (&norder, &eight, string); fprintf (datfp, "#\t%s\n", string); fprintf (datfp, "%d\t", niter); for (k = 0; k < nterm; k++) fprintf (datfp, "%8.4f", a[k]); fprintf (datfp, "%8.4f\n", 0.0); fflush (datfp); /***********************/ /* clear region buffer */ /***********************/ memset (&g_region, '\0', MAXR * sizeof (REGION)); iv0 = ir = 0; /***************************/ /* find contiguous regions */ /***************************/ CONTIG: while (ir < MAXR) { for (iv = iv0; iv < g_dimension; iv++) if (!g_imgo[iv]) break; if (iv == g_dimension) break; iv0 = iv; g_imgo[iv] = ir + 1; g_region[ir].u1 = (floor (g_img1[iv] / g_bandwidth) + 0.5) * g_bandwidth; k = iv; j = g_dimension; j /= g_zdim; iz = k / j; k -= iz * j; j /= g_ydim; iy = k / j; k -= iy * j; j /= g_xdim; ix = k / j; k -= ix * j; xyzn.ix = ix; xyzn.iy = iy; xyzn.iz = iz; xyzn.nn = 0; assign (ir, xyzn); do { m = g_region[ir].ne++; iv = g_region[ir].xyzn[m].ix + g_xdim*(g_region[ir].xyzn[m].iy + g_ydim*g_region[ir].xyzn[m].iz); for (j = 0; j < 6; j++) { xyzn = g_region[ir].xyzn[m]; switch (j) { case 0: k = -1; if (xyzn.ix < 1) continue; xyzn.ix--; break; case 1: k *= -1; if (xyzn.ix > g_xdim - 2) continue; xyzn.ix++; break; case 2: k *= -g_xdim; if (xyzn.iy < 1) continue; xyzn.iy--; break; case 3: k *= -1; if (xyzn.iy > g_ydim - 2) continue; xyzn.iy++; break; case 4: k *= -g_ydim; if (xyzn.iz < 1) continue; xyzn.iz--; break; case 5: k *= -1; if (xyzn.iz > g_zdim - 2) continue; xyzn.iz++; break; } jv = iv + k; if (g_imgo[jv] || g_img0[jv] < g_zthresh) continue; dela = g_img1[jv] - g_region[ir].u1; if (fabs (dela) < 0.5 * g_bandwidth) { g_imgo[jv] = ir + 1; assign (ir, xyzn); } } if (verbose && !(g_region[ir].count % 1000)) { xyzn = g_region[ir].xyzn[m]; printf ("checkr0: region=%4d unexa=%8d coords=%4d%4d%4d u1=%8.2f\n", ir, g_region[ir].count - m, xyzn.ix, xyzn.iy, xyzn.iz, g_region[ir].u1); } } while (g_region[ir].count > g_region[ir].ne); if (g_region[ir].count == 1) { iv = g_region[ir].xyzn[0].ix + g_xdim*(g_region[ir].xyzn[0].iy + g_ydim*g_region[ir].xyzn[0].iz); g_imgo[iv] = -1; g_region[ir].count = g_region[ir].ne = 0; } else { ir++; } } /************************/ /* sort regions by size */ /************************/ qsort ((void *) g_region, ir, sizeof (REGION), rcompare); if (ir == MAXR) { if (verbose) printf ("ir %d -> %d iv0=%10d ivmax=%d\n", ir, MAXR/2, iv0, g_dimension); while (--ir > MAXR/2) { for (m = 0; m < g_region[ir].count; m++) { xyzn = g_region[ir].xyzn[m]; iv = xyzn.ix + g_xdim*(xyzn.iy + g_ydim*xyzn.iz); g_imgo[iv] = 0; } g_region[ir].count = g_region[ir].ne = 0; } goto CONTIG; } g_nr = ir; for (ir = g_nr; ir < MAXR; ir++) if (g_region[ir].xyzn) free (g_region[ir].xyzn); test = 1; ITER: getmean (); /* analyze g_img1 */ getxyzn0 (); /* analyze g_img1 */ /********************/ /* reassign regions */ /********************/ for (i = 0; i < g_dimension; i++) g_imgo[i] = 0; printf ("total number of regions %d\n", g_nr); for (ir = 0; ir < g_nr; ir++) { g_region[ir].ne = g_region[ir].count = 0; if (g_region[ir].u1 < g_gfc_thresh || g_region[ir].u1 > g_gfc_ceil) continue; assign (ir, g_region[ir].xyzn0); do { m = g_region[ir].ne++; xyzn = g_region[ir].xyzn[m]; iv = xyzn.ix + g_xdim*(xyzn.iy + g_ydim*xyzn.iz); for (j = 0; j < 6; j++) { xyzn = g_region[ir].xyzn[m]; switch (j) { case 0: k = -1; if (xyzn.ix < 1) continue; xyzn.ix--; break; case 1: k *= -1; if (xyzn.ix > g_xdim - 2) continue; xyzn.ix++; break; case 2: k *= -g_xdim; if (xyzn.iy < 1) continue; xyzn.iy--; break; case 3: k *= -1; if (xyzn.iy > g_ydim - 2) continue; xyzn.iy++; break; case 4: k *= -g_ydim; if (xyzn.iz < 1) continue; xyzn.iz--; break; case 5: k *= -1; if (xyzn.iz > g_zdim - 2) continue; xyzn.iz++; break; } jv = iv + k; if (g_imgo[jv] || g_img0[jv] < g_zthresh) continue; dela = g_img1[jv] - g_region[ir].u1; delg = g_sconst * (g_img1[jv] - g_img1[iv]) / g_voxdim[j/2]; if (sqrt (dela*dela + delg*delg) < g_sigma*g_bandwidth) { g_imgo[jv] = ir + 1; assign (ir, xyzn); } } if (verbose && !(g_region[ir].count % 1000)) { xyzn = g_region[ir].xyzn[m]; printf ("checkr1: region=%4d unexa=%8d coords=%4d%4d%4d u1=%8.2f\n", ir, g_region[ir].count - m, xyzn.ix, xyzn.iy, xyzn.iz, g_region[ir].u1); } } while (g_region[ir].count > g_region[ir].ne); } getmean (); getxyzn0 (); /* analyze g_img1 */ listreg (stdout); if (g_region[0].u1 < g_bandwidth) { fprintf (stderr, "%s: algorithmic failure\n", program); exit (-1); } if (++niter > limiter || !test) goto COUNT; fprintf (stdout, "%s: iteration %d\n", program, niter); /***************/ /* compute gfc */ /***************/ for (ir = 0; ir < g_nr; ir++) u1gfc[ir] = g_region[ir].u1; for (k = 0; k < nterm; k++) a0[k] = a[k]; fitgain3dd_ (g_img0, &g_xdim, &g_ydim, &g_zdim, g_imgo, u1gfc, &g_mr, &norder, a, &drms); if (negdef) fnegdef_ (a); printf ("niter=%d drms=%.6f diffcrit=%.6f\n", niter, drms, diffcrit); fprintf (datfp, "%d\t", niter); for (k = 0; k < nterm; k++) fprintf (datfp, "%8.4f", a[k]); fprintf (datfp, "%8.4f\n", 100*drms); fflush (datfp); evalgain3d_ (g_imgg, &g_xdim, &g_ydim, &g_zdim, &norder, a); for (sum = i = 0; i < g_dimension; i++) sum += (g_img1[i] = g_img0[i]/g_imgg[i]); for ( i = 0; i < g_dimension; i++) g_img1[i] *= (sum0/sum); /********************/ /* convergence test */ /********************/ test = (drms > diffcrit); goto ITER; COUNT: fclose (datfp); /*******************/ /* count neighbors */ /*******************/ for (ir = 0; ir < g_nr; ir++) { for (g_nl = m = 0; m < g_region[ir].count; m++) { ix = g_region[ir].xyzn[m].ix; iy = g_region[ir].xyzn[m].iy; iz = g_region[ir].xyzn[m].iz; iv = ix + g_xdim*(iy + g_ydim*iz); nn = 0; k = -1; if (ix > 0) {if ((j = g_imgo[iv + k]) == ir + 1) nn++; else enter (j);} k *= -1; if (ix < g_xdim - 1) {if ((j = g_imgo[iv + k]) == ir + 1) nn++; else enter (j);} k *= -g_xdim; if (iy > 0) {if ((j = g_imgo[iv + k]) == ir + 1) nn++; else enter (j);} k *= -1; if (iy < g_ydim - 1) {if ((j = g_imgo[iv + k]) == ir + 1) nn++; else enter (j);} k *= -g_ydim; if (iz > 0) {if ((j = g_imgo[iv + k]) == ir + 1) nn++; else enter (j);} k *= -1; if (iz < g_zdim - 1) {if ((j = g_imgo[iv + k]) == ir + 1) nn++; else enter (j);} if (nn > g_region[ir].nnmax) g_region[ir].nnmax = nn; g_imgb[iv] = g_region[ir].xyzn[m].nn = nn; } if (verbose) { printf ("%5d%10d%10d%5d%2d |", ir, g_region[ir].count, k, j, g_region[ir].nnmax); for (i = 0; i < g_nl; i++) printf (" %d", g_list[i]); printf ("\n"); } } /*********************************/ /* write gain field coefficients */ /*********************************/ if (!(imgfp = fopen (gfcfile, "w"))) errw (program, gfcfile); printf ("Writing: %s\n", gfcfile); for (k = 0; k < nterm; k++) fprintf (imgfp, "%8.4f", a[k]); fprintf (imgfp, "\n"); fclose (imgfp); /*************************/ /* write corrected image */ /*************************/ WRITE: for (sum0 = sum = i = 0; i < g_dimension; i++) { if (!gfreeze && fwhm > 0) g_img0[i] = img_orig[i]; sum0 += g_img0[i]; q = 1./g_imgg[i]; t = (maxfac > 0.0) ? 2.*maxfac*tanh(.5*q/maxfac) : q; if (0) printf ("%10.6f %10.6f\n", q, t); sum += (g_img1[i] = g_img0[i]*t); } for (i = 0; i < g_dimension; i++) g_img1[i] *= (sum0/sum); vmin = FLT_MAX; vmax = -vmin; for (i = 0; i < g_dimension; i++) { if (g_img1[i] < vmin) vmin = g_img1[i]; if (g_img1[i] > vmax) vmax = g_img1[i]; } sprintf (outfile, "%s_gfc.4dfp.img", outroot); fprintf (stdout, "Writing: %s\n", outfile); if (!(imgfp = fopen (outfile, "wb")) || ewrite (g_img1, g_dimension, control, imgfp) || fclose (imgfp)) errw (program, outfile); /***************/ /* ifh and hdr */ /***************/ sprintf (ifhfile, "%s_gfc.4dfp.ifh", outroot); if (Writeifh (program, ifhfile, &ifh, control)) errw (program, ifhfile); sprintf (command, "ifh2hdr %s -r%.0f", ifhfile, vmax); system (command); /*******/ /* rec */ /*******/ startrece (outfile, argc, argv, g_rcsid, control); sprintf (string, "%s gain field coefficients\n", imgroot); printrec (string); powstring_ (&norder, &eight, string); printrec (string); for (k = 0; k < nterm; k++) {sprintf (string, "%8.4f", a[k]); printrec (string);} printrec ("\n"); if (!gfreeze) { sprintf (recfile, "%s_gfc.4dfp.img.rec", outroot); if (!(imgfp = fopen (recfile, "a"))) errw (program, recfile); listreg (imgfp); fclose (imgfp); } catrec (imgfile); endrec (); if (gfreeze) goto FREE; /*************************/ /* compute regions image */ /*************************/ for (i = 0; i < g_dimension; i++) g_img1[i] = 0; for (ir = 0; ir < g_mr; ir++) { for (m = 0; m < g_region[ir].count; m++) { xyzn = g_region[ir].xyzn[m]; iv = xyzn.ix + g_xdim*(xyzn.iy + g_ydim*xyzn.iz); g_img1[iv] = g_region[ir].u1; } } /******************************/ /* write processing QA images */ /******************************/ sprintf (volfile, "%s_gfc_vols.4dfp.img", outroot); printf ("Writing: %s\n", volfile); if (!(imgfp = fopen (volfile, "w"))) errw (program, volfile); /*****************/ /* regions image */ /*****************/ if (ewrite (g_img1, g_dimension, control, imgfp)) errw (program, volfile); /****************/ /* border image */ /****************/ for (i = 0; i < g_dimension; i++) g_img1[i] = g_imgb[i]; if (ewrite (g_img1, g_dimension, control, imgfp)) errw (program, volfile); /**************/ /* gain field */ /**************/ for (i = 0; i < g_dimension; i++) g_img1[i] = g_imgg[i]; if (ewrite (g_img1, g_dimension, control, imgfp)) errw (program, volfile); fclose (imgfp); /***************/ /* ifh and hdr */ /***************/ sprintf (ifhfile, "%s_gfc_vols.4dfp.ifh", outroot); ifh.matrix_size[3] = 3; if (Writeifh (program, ifhfile, &ifh, control)) errw (program, ifhfile); sprintf (string, "ifh2hdr %s -r%.0f", ifhfile, vmax); system (string); /*******/ /* rec */ /*******/ startrece (volfile, argc, argv, g_rcsid, control); sprintf (string, "%s_gfc QA (3 volumes)\n", imgroot); printrec (string); catrec (outfile); printrec ("Volume 1: regions\nVolume 2: border\nVolume 3: gain field\n"); endrec (); /************/ /* clean up */ /************/ FREE: free (g_img1); free (g_img0); free (g_imgg); free (g_imgo); free (g_imgb); if (img_orig) free (img_orig); for (ir = 0; ir < g_nr; ir++) free (g_region[ir].xyzn); exit (status); }
int main (int argc, char *argv[]) { FILE *fp; IFH ifh; char imgroot[2][MAXR][MAXL], Nmgroot[2][MAXL], lstfile[2][MAXL]; int nimg[2], isbig[2][MAXR], isbigN[2]; int orient, orient1, imgdim[4], imgdim1[4], dimension, vdim; char outroot[MAXL] = "", imgfile[MAXL], outfile[MAXL]; float voxdim[3], voxdim1[3]; float amin = FLT_MAX, amax = -FLT_MAX; float *img1, **imgN; double **imgs, **imgv; int **imgm, ngroup; char control = '\0'; /***********/ /* utility */ /***********/ double u; int c, i, j, k, m; char *ptr, *str, command[MAXL]; char *srgv[MAXL]; /* string field pointers */ /*********/ /* flags */ /*********/ int defined; int debug = 0; int NaN_flag = 'E'; /* 'E' 1.e-37; 'Z' 0.0; 'N' NaN; */ int status = 0; int saverec = 1; printf ("%s\n", rcsid); setprog (program, argv); /************************/ /* process command line */ /************************/ k = 0; for (i = 1; i < argc; i++) { if (*argv[i] == '-') { strcpy (command, argv[i]); ptr = command; while (c = *ptr++) switch (c) { case 'R': saverec = 0; break; case 'd': debug++; break; case 'o': getroot (ptr, outroot); *ptr = '\0'; break; case '@': control = *ptr++; *ptr = '\0'; break; } } else switch (k) { case 0: strcpy (lstfile[0], argv[i]); k++; break; case 1: getroot (argv[i], Nmgroot[0]); k++; break; case 2: strcpy (lstfile[1], argv[i]); k++; break; case 3: getroot (argv[i], Nmgroot[1]); k++; break; } } if (k != 2 && k != 4) usage (); ngroup = k/2; /********************/ /* parse list files */ /********************/ for (k = 0; k < ngroup; k++) { nimg[k] = 0; if (!(fp = fopen (lstfile[k], "r"))) errr (program, lstfile[k]); printf ("Reading: %s\n", lstfile[k]); while (fgets (command, MAXL, fp)) { if (nimg[k] >= MAXR) { fprintf (stderr, "%s: maximum number of input images (%d) exceeded\n", program, MAXR); exit (-1); } if (ptr = strchr (command, '#')) *ptr = '\0'; if (!strlen (command)) continue; /* skip blank lines */ if (ptr = strchr (command, '\n')) *ptr = '\0'; /* strip terminal nl */ i = m = 0; while (m < MAXL && i < MAXL) { while (!isgraph ((int) command[i]) && command[i]) i++; if (!command[i]) break; srgv[m++] = command + i; while (isgraph ((int) command[i])) i++; if (!command[i]) break; command[i++] = '\0'; } getroot (srgv[0], imgroot[k][nimg[k]++]); } fclose (fp); if (!nimg[k]) usage (); /*********************************/ /* check dimensional consistency */ /*********************************/ for (i = 0; i < nimg[k]; i++) { printf ("image %3d: %s\n", i + 1, imgroot[k][i]); if (!i && !k) { if (get_4dfp_dimoe (imgroot[k][i], imgdim, voxdim, &orient, isbig[k] + i)) exit (-1); if (Getifh (imgroot[k][i], &ifh)) errr (program, imgroot[k][i]); } else { if (get_4dfp_dimoe (imgroot[k][i], imgdim1, voxdim1, &orient1, isbig[k] + i)) exit (-1); status = (orient1 != orient); for (j = 0; j < 4; j++) status |= (imgdim1[j] != imgdim[j]); for (j = 0; j < 3; j++) status |= (fabs (voxdim1[j] - voxdim[j]) > 1.e-4); } if (status) { fprintf (stderr, "%s: %s %s dimension mismatch\n", program, imgroot[0][0], imgroot[k][i]); exit (-1); } } if (get_4dfp_dimoe (Nmgroot[k], imgdim1, voxdim1, &orient1, isbigN + k)) exit (-1); status = (orient1 != orient); for (j = 0; j < 3; j++) status |= (imgdim1[j] != imgdim[j]); for (j = 0; j < 3; j++) status |= (fabs (voxdim1[j] - voxdim[j]) > 1.e-4); if (status) { fprintf (stderr, "%s: %s %s dimension mismatch\n", program, imgroot[0][0], Nmgroot[k]); exit (-1); } } if (!control) control = (isbig[0][0]) ? 'b' : 'l'; vdim = imgdim[0]*imgdim[1]*imgdim[2]; dimension = vdim*imgdim[3]; if (!(img1 = (float *) calloc (dimension, sizeof (float)))) errm (program); imgv = calloc_double2 (ngroup, dimension); /* variance accumulator */ imgs = calloc_double2 (ngroup, dimension); /* sum accumulator */ imgm = calloc_int2 (ngroup, dimension); /* voxel counter */ imgN = calloc_float2 (ngroup, vdim); /* input N image */ for (k = 0; k < ngroup; k++) { /****************/ /* read N image */ /****************/ printf ("Reading: %s\n", Nmgroot[k]); sprintf (imgfile, "%s.4dfp.img", Nmgroot[k]); if (!(fp = fopen (imgfile, "rb")) || eread (imgN[k], vdim, isbigN[k], fp) || fclose (fp)) errr (program, imgfile); /**********************************/ /* read and accumulate image data */ /**********************************/ for (j = 0; j < nimg[k]; j++) { printf ("Reading: %s\n", imgroot[k][j]); sprintf (imgfile, "%s.4dfp.img", imgroot[k][j]); if (!(fp = fopen (imgfile, "rb")) || eread (img1, dimension, isbig[k][j], fp) || fclose (fp)) errr (program, imgfile); for (i = 0; i < dimension; i++) { defined = isnormal (img1[i]) && img1[i] != (float) 1.e-37; if (!defined) continue; imgm[k][i]++; imgs[k][i] += img1[i]; imgv[k][i] += img1[i]*img1[i]; } } for (i = 0; i < dimension; i++) { u = (imgs[k][i] /= imgm[k][i]); imgv[k][i] -= u*u*imgm[k][i]; if (imgv[k][i] < 0.0) imgv[k][i] = 0.0; imgv[k][i] /= (imgm[k][i] - 1); } /***************************************/ /* divide variance estimate by N image */ /***************************************/ for (i = 0; i < vdim; i++) if (imgN[k][i]) for (j = 0; j < imgdim[3]; j++) { imgv[k][j*vdim + i] /= imgN[k][i]; } } /***************************************************************************/ /* compute voxelwize t statistic (Welch's approximate t' when ngroup == 2) */ /***************************************************************************/ for (i = 0; i < dimension; i++) { if (ngroup == 2) { imgs[0][i] -= imgs[1][i]; imgv[0][i] += imgv[1][i]; } if (isnormal (imgs[0][i]) && isnormal (imgv[0][i])) { img1[i] = imgs[0][i] / sqrt (imgv[0][i]); if (img1[i] > amax) amax = img1[i]; if (img1[i] < amin) amin = img1[i]; } else switch (NaN_flag) { case 'Z': img1[i] = 0.0; break; case 'E': img1[i] = (float) 1.e-37; break; case 'N': img1[i] = rnan (); break; } } if (!strlen (outroot)) { /*******************/ /* compute outroot */ /*******************/ strcpy (command, lstfile[0]); if (!(ptr = strrchr (command, '/'))) ptr = command; else ptr++; if (str = strrchr (ptr, '.')) *str = '\0'; sprintf (outroot, "RFX_%s", ptr); if (ngroup == 2) { strcat (outroot, "_vs_"); strcpy (command, lstfile[1]); if (!(ptr = strrchr (command, '/'))) ptr = command; else ptr++; if (str = strrchr (ptr, '.')) *str = '\0'; strcat (outroot, ptr); } strcat (outroot, "_tmap"); } sprintf (outfile, "%s.4dfp.img", outroot); /****************/ /* write result */ /****************/ printf ("Writing: %s\n", outfile); if (!(fp = fopen (outfile, "wb")) || ewrite (img1, dimension, control, fp) || fclose (fp)) errw (program, outfile); /*******/ /* ifh */ /*******/ Writeifh (program, outfile, &ifh, control); /*******/ /* hdr */ /*******/ sprintf (command, "ifh2hdr %s -r%.0fto%.0f", outroot, amin, amax); printf ("%s\n", command); status |= system (command); /*******/ /* rec */ /*******/ startrecle (outfile, argc, argv, rcsid, control); for (k = 0; k < ngroup; k++) { sprintf (command, "imglist %d\n", k + 1); printrec (command); catrec (lstfile[k]); printrec ("endimglist\n"); if (saverec) { sprintf (imgfile, "%s.4dfp.img", Nmgroot[k]); catrec (imgfile); for (j = 0; j < nimg[k]; j++) { sprintf (imgfile, "%s.4dfp.img", imgroot[k][j]); catrec (imgfile); } } } endrec (); free (img1); free_double2 (imgs); free_double2 (imgv); free_int2 (imgm); free_float2 (imgN); exit (status); }
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); }
int main (int argc, char **argv) { /*************/ /* image I/O */ /*************/ FILE *fp_img, *fp_out; IFH ifh; char imgfile[MAXL], imgroot[MAXL]; char outfile[MAXL], outroot[MAXL] = ""; /**************/ /* processing */ /**************/ int imgdim[4], dimension, orient, isbig; float voxdim[3]; float *imgr; float thresh=0.0; char control ='\0'; /***********/ /* utility */ /***********/ int c, i, k; char *ptr, command[MAXL], program[MAXL]; /*********/ /* flags */ /*********/ int status = 0; printf ("%s\n", rcsid); setprog (program, argv); /************************/ /* process command line */ /************************/ for (k = 0, i = 1; i < argc; i++) { if (i > 1 && *argv[i] == '-') { strcpy (command, argv[i]); ptr = command; while ((c = *ptr++)) switch (c) { case '@': control = *ptr++; *ptr = '\0'; break; } } else switch (k) { case 0: thresh = atof (argv[i]); k++; break; case 1: getroot (argv[i], imgroot); k++; break; case 2: getroot (argv[i], outroot); k++; break; } } if (k < 2) { printf ("Usage: %s <flt> <file_4dfp> [outroot]\n", program); printf (" option\n"); printf (" -@<b|l>\toutput big or little endian (default input endian)\n"); printf ("e.g., %s 90 pt349_study9to9\n", program); printf ("e.g., %s 90 pt349_study9to9 pt349_study9to9z\n", program); printf ("N.B.: default output 4dfp root is <file_4dfp>\"z\"\n"); exit (1); } sprintf (imgfile, "%s.4dfp.img", imgroot); /***************************************/ /* create output filename if not given */ /***************************************/ if (!strlen (outroot)) sprintf (outroot, "%sz", imgroot); sprintf (outfile, "%s.4dfp.img", outroot); /*****************************/ /* get 4dfp input dimensions */ /*****************************/ if (get_4dfp_dimoe (imgfile, imgdim, voxdim, &orient, &isbig) < 0) errr (program, imgroot); if (Getifh (imgfile, &ifh)) errr (program, imgroot); if (!control) control = (isbig) ? 'b' : 'l'; dimension = imgdim[0] * imgdim[1] * imgdim[2]; /*****************/ /* alloc buffers */ /*****************/ if (!(imgr = (float *) malloc (dimension * sizeof (float)))) errm (program); /***********/ /* process */ /***********/ if (!(fp_img = fopen (imgfile, "rb"))) errr (program, imgfile); if (!(fp_out = fopen (outfile, "wb"))) errw (program, outfile); fprintf (stdout, "Reading: %s\n", imgfile); fprintf (stdout, "Writing: %s\n", outfile); for (k = 0; k < imgdim[3]; k++) { if (gread ((char *) imgr, sizeof (float), dimension, fp_img, isbig)) errr (program, imgfile); for (i = 0; i < dimension; i++) if (imgr[i] < thresh) imgr[i] = 0.0; if (gwrite ((char *) imgr, sizeof (float), dimension, fp_out, control)) errw (program, outfile); } fclose (fp_img); fclose (fp_out); /***************/ /* ifh and hdr */ /***************/ if (Writeifh (program, outfile, &ifh, control)) errw (program, outfile); sprintf (command, "ifh2hdr %s", outroot); printf ("%s\n", command); status = system (command); /*******************/ /* create rec file */ /*******************/ startrece (outfile, argc, argv, rcsid, control); catrec (imgfile); endrec (); free (imgr); exit (status); }