Transformation normalize(LifeList *cells) { Transformation T; Transformation normalizeT; makeWorkSpace(cells->ncells); scratch1[0].position=INT_MAX; for (T.flipxF=1; T.flipxF>=0; T.flipxF--) { for (T.flipyF=1; T.flipyF>=0; T.flipyF--) { for (T.transposeF=1; T.transposeF>=0; T.transposeF--) { copyList(cells->cellList, cells->ncells, scratch2, 0); if (T.flipxF) flipx(scratch2, cells->ncells); if (T.flipyF) flipy(scratch2, cells->ncells); if (T.transposeF) transpose(scratch2, cells->ncells); T.translateBy=firstZero(scratch2, cells->ncells); if (compare(scratch2, scratch1, cells->ncells) <= 0) { copyList(scratch2, cells->ncells, scratch1, 0); normalizeT=T; } } } } copyList(scratch1, cells->ncells, cells->cellList, 0); return normalizeT; }
int x4dfp2analyze (float *imag, int *dim, int orientation) { switch (orientation) { case 4: flipx (imag, dim+0, dim+1, dim+2); /* sagittal */ case 3: flipz (imag, dim+0, dim+1, dim+2); /* coronal */ case 2: flipy (imag, dim+0, dim+1, dim+2); /* transverse */ return 0; default: return -1; /* none of the above */ } }
void rotate (void *iadr, XLONG **oadr, int type, int nx, int ny, int dir) { int nelem; static int first_time = 1, sv_nx=0, sv_ny=0;; static ulong *bufl = (ulong *) NULL; static ushort *bufs = (ushort *) NULL; static uchar *bufc = (uchar *) NULL; if ((nx*ny) != (sv_nx*sv_ny)) { if (type == 1 && bufc) free ((uchar *)bufc); if (type == 2 && bufs) free ((ushort *)bufs); if (type == 4 && bufl) free ((ulong *)bufl); sv_nx = nx; sv_ny = ny; first_time = 1; } nelem = nx * ny; switch (type) { case (1): /* uchar */ ubip = (uchar *) iadr; if (first_time) { ubop = bufc = (uchar *) calloc (1, nelem * sizeof(uchar)); *oadr = (XLONG *) ubop; first_time = 0; } else { ubop = (uchar *)bufc; *oadr = (XLONG *) bufc; } break; case (2): /* ushort */ usip = (ushort *) iadr; if (first_time) { usop = bufs = (ushort *) calloc (1, nelem * sizeof(ushort)); *oadr = (XLONG *) usop; first_time = 0; } else { usop = (ushort *)bufs; *oadr = (XLONG *) bufs; } break; case (4): /* ulong */ ulip = (ulong *) iadr; if (first_time) { ulop = bufl = (ulong *) calloc (1, nelem * sizeof(ulong)); *oadr = (XLONG *) ulop; first_time = 0; } else { ulop = (ulong *)bufl; *oadr = (XLONG *) bufl; } break; default: ; break; } switch (dir) { case (1): /* flip x axis */ flipx(type, nx, ny); break; case (2): /* flip y axis */ flipy(type, nx, ny); break; case (3): /* transpose 1st diag */ transpose1(type, nx, ny); break; case (4): /* transpose 2nd diag */ transpose2(type, nx, ny); break; default: break; } }
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); }