Doxygen Source Code Documentation
Main Page Alphabetical List Data Structures File List Data Fields Globals Search
DTIStudioFibertoSegments.c
Go to the documentation of this file.00001
00002
00003 #if 0
00004 # format from Hangyi Jiang for DTIStudio fibers
00005 File Header (128 bytes):
00006 char sFiberFileTag[8] = "FiberDat";
00007 int nFiberNr;
00008 int nFiberLenMax;
00009 float fFiberLenMean;
00010 int nImgWidth;
00011 int nImgHeight;
00012 int nImgSlices;
00013 float fPixelSizeWidth;
00014 float fPixelSizeHeight;
00015 float fSliceThickness;
00016
00017 int enumSliceOrientation;
00018 2=Sagittal
00019 int enumSliceSequencing;
00020
00021 Fiber Data (starts from offset 128 bytes)
00022 for each fiber:
00023 int nLength;
00024 int nReserved;
00025 int nFiberStartIndex;
00026 int nFiberEndIndex;
00027 XYZ_TRIPLE xyzFiberChain[];
00028 then, next fiber..
00029 #endif
00030
00031 #include "mrilib.h"
00032
00033 static char outfname[THD_MAX_PREFIX] = "rawxyzseg.dat";
00034 static char infname[THD_MAX_PREFIX];
00035
00036
00037 int main( int argc , char * argv[] )
00038 {
00039 int nopt;
00040 int i,j;
00041 FILE *fout, *fin;
00042 float fxyz[3];
00043 char fiberheaderstring[32];
00044 int totalpts=0;
00045 int npts, nfibers;
00046 int statcode;
00047
00048
00049 if( argc < 2 || strcmp(argv[1],"-help") == 0 ){
00050 printf("Usage: DTIStudioFibertoSegments [options] dataset\n"
00051 "Convert a DTIStudio Fiber file to a SUMA segment file\n"
00052 "Options:\n"
00053 " -output / -prefix = name of the output file (not an AFNI dataset prefix)\n"
00054 " the default output name will be rawxyzseg.dat\n\n"
00055 );
00056 exit(0) ;
00057 }
00058 fout = NULL;
00059 mainENTRY("DTIStudioFibertoSegments main"); machdep(); AFNI_logger("DTIStudioFibertoSegments",argc,argv);
00060 nopt = 1;
00061 while( nopt < argc && argv[nopt][0] == '-' ){
00062 if( (strcmp(argv[nopt],"-output") == 0 ) || (strcmp(argv[nopt],"-prefix")==0))
00063 {
00064 if (++nopt >= argc)
00065 {
00066 fprintf (stderr, "*** Error - output / prefix needs an argument!\n");
00067 exit (1);
00068 }
00069 MCW_strncpy (outfname, argv[nopt], THD_MAX_PREFIX);
00070 if (!THD_filename_ok (outfname))
00071 {
00072 fprintf (stderr, "*** Error - %s is not a valid output name!\n", outfname);
00073 exit (1);
00074 }
00075 if (THD_is_file(outfname))
00076 {
00077 fprintf (stderr, "*** Error - %s already exists!\n", outfname);
00078 exit (1);
00079 }
00080 nopt++; continue;
00081 }
00082
00083 fprintf(stderr, "*** Error - unknown option %s\n", argv[nopt]);
00084 exit(1);
00085 }
00086
00087 fout = fopen (outfname, "w") ;
00088 if( fout == NULL ){
00089 fprintf (stderr, "*** Error - can not create %s for some reason!\n", outfname);
00090 exit (1);
00091 }
00092
00093
00094
00095 MCW_strncpy (infname, argv[nopt], THD_MAX_PREFIX);
00096 if (!THD_filename_ok (infname))
00097 {
00098 fprintf (stderr, "*** Error - %s is not a valid input name!\n", infname);
00099 exit (1);
00100 }
00101 if (!THD_is_file(infname))
00102 {
00103 fprintf (stderr, "*** Error - %s does not exist!\n", infname);
00104 exit (1);
00105 }
00106 fin = fopen (infname, "r") ;
00107 if( fin == NULL ){
00108 fprintf (stderr, "*** Error - can not open %s for some reason!\n", infname);
00109 exit (1);
00110 }
00111
00112
00113
00114
00115
00116 statcode = fread(fiberheaderstring, 8, 1, fin);
00117 if(!strcmp(fiberheaderstring,"FiberDat")) {
00118 fprintf(stderr, "*** Error - file does not have correct header format\n");
00119 fclose(fin);
00120 fclose(fout);
00121 exit(1);
00122 }
00123
00124 statcode = fread(&nfibers, sizeof(int), 1, fin);
00125 if((statcode<1)||(nfibers<1)) {
00126 fprintf(stderr, "*** Error - file does not have correct header format\n");
00127 fclose(fin);
00128 fclose(fout);
00129 exit(1);
00130 }
00131
00132 fseek(fin, 128, SEEK_SET);
00133
00134 fprintf(stderr, "Number of fibers = %d \n", nfibers);
00135 for(i=0;i<nfibers;i++) {
00136 statcode = fread(&npts,sizeof(int),1, fin);
00137 if((statcode<1)||(npts<1)) {
00138 fprintf(stderr, "*** can not read fiber info.\n");
00139 fclose(fin);
00140 fclose(fout);
00141 exit(1);
00142 }
00143
00144 totalpts += npts;
00145 statcode = fseek(fin, 12, SEEK_CUR);
00146
00147 for(j=0;j<npts;j++) {
00148 statcode = fread(fxyz, sizeof(float),3, fin);
00149 if(statcode<3) {
00150 fprintf(stderr, "*** can not read fiber data.\n");
00151 fclose(fin);
00152 fclose(fout);
00153 exit(1);
00154 }
00155
00156 if(j!=0) {
00157 statcode = fprintf(fout," %10.3f %10.3f %10.3f\n",fxyz[0],fxyz[1],fxyz[2]);
00158 }
00159 if(j<(npts-1)) {
00160 statcode = fprintf(fout,"%10.3f %10.3f %10.3f",fxyz[0],fxyz[1],fxyz[2]);
00161 }
00162 if(statcode==0) {
00163 fprintf(stderr, "*** Error - writing output file!\n");
00164 fclose(fout);
00165 exit(1);
00166 }
00167 }
00168 }
00169
00170 fprintf(stderr, "Total number of points = %d \n", totalpts);
00171 fclose(fout);
00172 fclose(fin);
00173 exit(0);
00174 }