int main (int argc, char *argv[]) { FILE *anafp; char in1root[MAXL]; char in2root[MAXL]; char outroot[MAXL]; char imgfile[MAXL]; char program[MAXL], string[MAXL], *ptr; struct dsr hdr; short *img1, *img2, *imgo; int xdim, ydim, zdim, dimension; int img1max = 0, img2max = 0; int img1min = 0, img2min = 0; int swab_img1 = 0, swab_img2 = 0, swab_imgo = 0; char orient, control = '\0'; int nbin = NBIN; int *hist, *mrg1, *mrg2; int histmax = 0; int range1, range2; int binwid1, binwid2; int grid1, grid2; short valmax = 0; float ftmp; int c, i, j, k; /*********/ /* flags */ /*********/ int status = 0; int debug = 0; int do_grid = 1; if (ptr = strrchr (argv[0], '/')) ptr++; else ptr = argv[0]; strcpy (program, ptr); printf ("%s\n", rcsid); /************************/ /* 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 'd': debug++; break; case 'g': do_grid = 0; break; case '@': control = *ptr++; *ptr = '\0'; break; case 'r': j = atoi (ptr++); ptr++; /* skip over ":" */ switch (j) { case 1: getrangei (ptr, &img1min, &img1max); break; case 2: getrangei (ptr, &img2min, &img2max); break; default: usage (program); break; } *ptr = '\0'; break; } } else switch (k) { case 0: getroot (argv[i], in1root); k++; break; case 1: getroot (argv[i], in2root); k++; break; case 2: getroot (argv[i], outroot); k++; break; } if (k < 3) usage (program); fprintf (stdout, "img1: %s\nimg2: %s\n", in1root, in2root); sprintf (imgfile, "%s.hdr", in1root); if (!(anafp = fopen (imgfile, "rb")) || fread (&hdr, sizeof (struct dsr), 1, anafp) != 1 || fclose (anafp)) errr (program, imgfile); if (hdr.hk.sizeof_hdr != sizeof (struct dsr)) { printf ("converting %s byte order\n", in1root); swab_hdr (&hdr); swab_img1++; } if (hdr.dime.bitpix != 16) erri (program, imgfile); xdim = hdr.dime.dim[1]; ydim = hdr.dime.dim[2]; zdim = hdr.dime.dim[3]; orient = hdr.hist.orient; dimension = xdim * ydim * zdim; sprintf (imgfile, "%s.hdr", in2root); if (!(anafp = fopen (imgfile, "rb")) || fread (&hdr, sizeof (struct dsr), 1, anafp) != 1 || fclose (anafp)) errr (program, imgfile); if (hdr.hk.sizeof_hdr != sizeof (struct dsr)) { printf ("converting %s byte order\n", in2root); swab_hdr (&hdr); swab_img2++; } if (hdr.dime.bitpix != 16) erri (program, imgfile); if (xdim != hdr.dime.dim[1] || ydim != hdr.dime.dim[2] || zdim != hdr.dime.dim[3] || orient != hdr.hist.orient) { fprintf (stderr, "%s: %s %s dimension/orientation mismatch\n", program, in1root, in2root); exit (-1); } img1 = (short *) malloc (dimension * sizeof (short)); img2 = (short *) malloc (dimension * sizeof (short)); mrg1 = (int *) calloc (nbin, sizeof (int)); mrg2 = (int *) calloc (nbin, sizeof (int)); hist = (int *) calloc (nbin*nbin, sizeof (int)); imgo = (short *) malloc (nbin*nbin * sizeof (short)); if (!img1 || !img2 || !hist || !mrg1 || !mrg2 || !imgo) errm (program); sprintf (imgfile, "%s.img", outroot); startrece (imgfile, argc, argv, rcsid, control); sprintf (imgfile, "%s.img", in1root); catrec (imgfile); printf ("Reading: %s\n", imgfile); if (!(anafp = fopen (imgfile, "rb")) || fread (img1, sizeof (short), dimension, anafp) != dimension || fclose (anafp)) errr (program, imgfile); if (swab_img1) for (i = 0; i < dimension; i++) swab2 ((char *) &img1[i]); sprintf (imgfile, "%s.img", in2root); catrec (imgfile); printf ("Reading: %s\n", imgfile); if (!(anafp = fopen (imgfile, "rb")) || fread (img2, sizeof (short), dimension, anafp) != dimension || fclose (anafp)) errr (program, imgfile); if (swab_img2) for (i = 0; i < dimension; i++) swab2 ((char *) &img2[i]); if (!img1max && !img1min) { img1max -32768; img1min = 32767; for (i = 0; i < dimension; i++) { if (img1[i] > img1max) img1max = img1[i]; if (img1[i] < img1min) img1min = img1[i]; } } else { for (i = 0; i < dimension; i++) { if (img1[i] > img1max) img1[i] = img1max; if (img1[i] < img1min) img1[i] = img1min; } } range1 = img1max - img1min; if (!img2max && !img2min) { img2max -32768; img2min = 32767; for (i = 0; i < dimension; i++) { if (img2[i] > img2max) img2max = img2[i]; if (img2[i] < img2min) img2min = img2[i]; } } else { for (i = 0; i < dimension; i++) { if (img2[i] > img2max) img2[i] = img2max; if (img2[i] < img2min) img2[i] = img2min; } } range2 = img2max - img2min; fprintf (stdout, "img1: min=%6d\tmax=%6d\trange=%6d\n", img1min, img1max, range1); fprintf (stdout, "img2: min=%6d\tmax=%6d\trange=%6d\n", img2min, img2max, range2); sprintf (string, "before auto range adjust\n"); printrec (string); sprintf (string, "img1: min=%6d\tmax=%6d\trange=%6d\n", img1min, img1max, range1); printrec (string); sprintf (string, "img2: min=%6d\tmax=%6d\trange=%6d\n", img2min, img2max, range2); printrec (string); for (i = 0; i < dimension; i++) { k = (nbin * (img1[i] - img1min)) / range1; if (k < nbin && k > 0) mrg1[k]++; k = (nbin * (img2[i] - img2min)) / range2; if (k < nbin && k > 0) mrg2[k]++; } for (k = 2; k < nbin; k++) { mrg1[k] += mrg1[k - 1]; mrg2[k] += mrg2[k - 1]; } if (debug) for (k = 0; k < nbin; k++) { printf ("%6d %10.6f %10.6f\n", k, (float) mrg1[k] / (float) mrg1[nbin - 1], (float) mrg2[k] / (float) mrg2[nbin - 1]); } for (i = 1; i < nbin; i++) if (((float) mrg1[i] / (float) mrg1[nbin - 1]) > CUM_D) break; for (j = 1; j < nbin; j++) if (((float) mrg1[j] / (float) mrg1[nbin - 1]) > 1.0-CUM_D) break; if (debug) printf ("img1: first_bin=%d\tlast_bin=%d\n", i, j); img1min += (float) (range1 * i) / nbin; range1 *= (float) (j - i) / nbin; binwid1 = (float) range1 / nbin; if (!binwid1) binwid1++; range1 = binwid1 * nbin; img1min += binwid1 - (img1min + range1) % binwid1; for (i = 1; i < nbin; i++) if (((float) mrg2[i] / (float) mrg2[nbin - 1]) > CUM_D) break; for (j = 1; j < nbin; j++) if (((float) mrg2[j] / (float) mrg2[nbin - 1]) > 1.0-CUM_D) break; if (debug) printf ("img2: first_bin=%d\tlast_bin=%d\n", i, j); img2min += (float) (range2 * i) / nbin; range2 *= (float) (j - i) / nbin; binwid2 = (float) range2 / nbin; if (!binwid2) binwid2++; range2 = binwid2 * nbin; img2min += binwid2 - (img2min + range2) % binwid2; fprintf (stdout, "img1: min=%6d\tmax=%6d\trange=%6d\tbinwidth=%d\n", img1min, img1min + range1, range1, binwid1); fprintf (stdout, "img2: min=%6d\tmax=%6d\trange=%6d\tbinwidth=%d\n", img2min, img2min + range2, range2, binwid2); sprintf (string, "after auto range adjust\n"); printrec (string); sprintf (string, "img1: min=%6d\tmax=%6d\trange=%6d\tbinwidth=%d\n", img1min, img1min + range1, range1, binwid1); printrec (string); sprintf (string, "img2: min=%6d\tmax=%6d\trange=%6d\tbinwidth=%d\n", img2min, img2min + range2, range2, binwid2); printrec (string); if (!range1 || !range2) exit (-1); for (k = 0; k < dimension; k++) { i = (img1[k] - img1min) / binwid1; j = (img2[k] - img2min) / binwid2; if (i < 0 || i >= nbin || j < 0 || j >= nbin) continue; hist[nbin * j + i]++; if (!hist[nbin * j + i]) { fprintf (stderr, "2Dhist: histogram bin overflow\n"); exit (-1); } if (hist[nbin * j + i] > histmax) histmax = hist[nbin * j + i]; } fprintf (stdout, "maximum bin count=%d\n", histmax); /***************************/ /* write histogram to imgo */ /***************************/ for (k = 0; k < nbin*nbin; k++) { if (hist[k]) { ftmp = 100 * log ((double) hist[k]); imgo[k] = (short) ftmp; } else imgo[k] = 0; if (imgo[k] > valmax) valmax = imgo[k]; } if (do_grid) { grid1 = range1 / 10; grid1 /= 10; grid1 *= 10; for (i = k = 0; i < nbin; k += grid1) { i = (k - img1min) / binwid1; if (i >= 0 && i < nbin) for (j = 0; j < nbin; j++) imgo[nbin * j + i] = valmax / 2; } sprintf (string, "img1: grid interval=%d\n", grid1); printrec (string); grid2 = range2 / 10; grid2 /= 10; grid2 *= 10; for (j = k = 0; j < nbin; k += grid2) { j = (k - img2min) / binwid2; if (j >= 0 && j < nbin) for (i = 0; i < nbin; i++) imgo[nbin * j + i] = valmax / 2; } sprintf (string, "img2: grid interval=%d\n", grid2); printrec (string); } swab_imgo = ((CPU_is_bigendian() != 0) && (control == 'l' || control == 'L')) || ((CPU_is_bigendian() == 0) && (control == 'b' || control == 'B')); if (swab_imgo) for (i = 0; i < nbin*nbin; i++) swab2 ((char *) &imgo[i]); sprintf (imgfile, "%s.img", outroot); fprintf (stdout, "Writing: %s\n", imgfile); if (!(anafp = fopen (imgfile, "wb")) || fwrite (imgo, sizeof (short), nbin*nbin, anafp) != nbin*nbin || fclose (anafp)) errw (program, imgfile); hdr.dime.dim[0] = 2; hdr.dime.dim[1] = nbin; hdr.dime.dim[2] = nbin; hdr.dime.dim[3] = 1; hdr.dime.datatype = 4; hdr.dime.pixdim[1] = 1; hdr.dime.pixdim[2] = 1; hdr.dime.pixdim[3] = 1; hdr.dime.glmax = valmax; hdr.dime.glmin = 0; hdr.hist.orient = 0; if (swab_imgo) swab_hdr (&hdr); sprintf (imgfile, "%s.hdr", outroot); fprintf (stdout, "Writing: %s\n", imgfile); if (!(anafp = fopen (imgfile, "wb")) || fwrite (&hdr, sizeof (struct dsr), 1, anafp) != 1 || fclose (anafp)) errw (program, imgfile); free (img1); free (img2); free (imgo); free (mrg1); free (mrg2); free (hist); endrec (); exit (status); }
int main (int argc, char *argv[]) { /*******/ /* i/o */ /*******/ FILE *fpimg, *fpout; IFH ifh; struct dsr hdr; /* ANALYZE hdr */ char imgroot[MAXL], imgfile[MAXL], outfile[MAXL]; char trailer[8] = ".4dint"; /****************/ /* image arrays */ /****************/ float *imgf, cscale = 1.0; short int *imgi=NULL; unsigned char *imgu=NULL; float voxsiz[3]; int imgdim[4], vdim, orient, isbig; int imin = 32767, imax = -32768; char control = '\0'; short int origin[3]; /* used in SPM99 conversions */ /***********/ /* utility */ /***********/ int c, i, j, k; char *str, command[MAXL], program[MAXL]; /*********/ /* flags */ /*********/ int uchar = 0; int debug = 0; int spm99 = 0; int swab_flag = 0; fprintf (stdout, "%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 '8': uchar++; strcpy (trailer, "_8bit"); break; case 'd': debug++; break; case 'c': cscale = atof (str); *str = '\0'; break; case 'S': if (!strcmp (str, "PM99")) spm99++; *str = '\0'; break; case '@': control = *str++; *str = '\0'; break; } } else switch (k) { case 0: getroot (argv[i], imgroot); k++; break; } } if (k < 1) { printf ("Usage:\t%s <(4dfp) filename>\n", program); printf ("\toption\n"); printf ("\t-c<flt>\tscale output values by specified factor\n"); printf ("\t-8\toutput 8 bit unsigned char\n"); printf ("\t-SPM99\tinclude origin and scale in hdr (http:/wideman-one.com/gw/brain/analyze/format.doc)\n"); printf ("\t-@<b|l>\toutput big or little endian (default CPU endian)\n"); exit (1); } /*****************************/ /* get input 4dfp dimensions */ /*****************************/ if (get_4dfp_dimoe (imgroot, imgdim, voxsiz, &orient, &isbig)) errr (program, imgroot); vdim = imgdim[0] * imgdim[1] * imgdim[2]; if (uchar) { if (!(imgu = (unsigned char *) malloc (vdim * sizeof (char)))) errm (program); } else { if (!(imgi = (short *) malloc (vdim * sizeof (short)))) errm (program); } if (!(imgf = (float *) malloc (vdim * sizeof (float)))) errm (program); /*************************/ /* open input and output */ /*************************/ sprintf (imgfile, "%s.4dfp.img", imgroot); printf ("Reading: %s\n", imgfile); if (!(fpimg = fopen (imgfile, "rb"))) errr (program, imgfile); sprintf (outfile, "%s%s.img", imgroot, trailer); if (!(fpout = fopen (outfile, "wb"))) errw (program, outfile); printf ("Writing: %s\n", outfile); /**********************/ /* process all frames */ /**********************/ for (k = 0; k < imgdim[3]; k++) { if (eread (imgf, vdim, isbig, fpimg)) errr (program, imgfile); switch (orient) { case 4: flipx (imgf, imgdim + 0, imgdim + 1, imgdim + 2); /* sagittal */ case 3: flipz (imgf, imgdim + 0, imgdim + 1, imgdim + 2); /* coronal */ case 2: flipy (imgf, imgdim + 0, imgdim + 1, imgdim + 2); /* transverse */ break; default: fprintf (stderr, "%s: %s image orientation not recognized\n", program, imgfile); exit (-1); break; } for (i = 0; i < vdim; i++) { j = nint (cscale*imgf[i]); if (debug) printf ("%10.6f%10d\n", imgf[i], j); if (uchar) { if (j < 0) j = 0; if (j > 255) j = 255; imgu[i] = j; } else { imgi[i] = j; } if (j > imax) imax = j; if (j < imin) imin = j; } if (uchar) { if (fwrite ( imgu, sizeof (char), vdim, fpout) != vdim) errw (program, outfile); } else { if (gwrite ((char *) imgi, sizeof (short), vdim, fpout, control)) errw (program, outfile); } } fclose (fpimg); fclose (fpout); /**************************/ /* create ANALYZE 7.5 hdr */ /**************************/ Inithdr (&hdr, imgdim, voxsiz, ""); if (uchar) { hdr.dime.datatype = 2; /* unsigned char */ hdr.dime.bitpix = 8; } else { hdr.dime.datatype = 4; /* signed integer */ hdr.dime.bitpix = 16; } hdr.dime.glmax = imax; hdr.dime.glmin = imin; hdr.hist.orient = orient - 2; swab_flag = ((CPU_is_bigendian() != 0) && (control == 'l' || control == 'L')) || ((CPU_is_bigendian() == 0) && (control == 'b' || control == 'B')); if (spm99) { if (Getifh (imgroot, &ifh)) errr (program, imgroot); for (i = 0; i < 3; i++) origin[i] = 0.4999 + ifh.center[i]/ifh.mmppix[i]; /*************************************************/ /* flip 4dfp->analyze assuming transverse orient */ /*************************************************/ origin[1] = imgdim[1] + 1 - origin[1]; /*******************************************************************/ /* origin field officially text and so not affected by swab_hdr () */ /*******************************************************************/ if (swab_flag) for (i = 0; i < 3; i++) swab2 ((char *) (origin + i)); memcpy ((char *) &hdr + 253, (char *) origin, 3*sizeof (short int)); memcpy ((char *) &hdr + 112, (char *) &cscale, sizeof (float)); hdr.hk.extents = 0; } if (swab_flag) swab_hdr (&hdr); sprintf (outfile, "%s%s.hdr", imgroot, trailer); printf ("Writing: %s\n", outfile); if (!(fpout = fopen (outfile, "wb")) || fwrite (&hdr, sizeof (struct dsr), 1, fpout) != 1 || fclose (fpout)) errw (program, outfile); /*******************/ /* create rec file */ /*******************/ sprintf (outfile, "%s%s.img", imgroot, trailer); startrece (outfile, argc, argv, rcsid, control); sprintf (command, "Voxel values scaled by %f\n", cscale); printrec (command); catrec (imgfile); endrec (); free (imgf); if (uchar) { free (imgu); } else { free (imgi); } exit (0); }