1 /******************************* DTIStudioFibertoSegments.c **************************************/
2 
3 #if 0
4 # format from Hangyi Jiang for DTIStudio fibers
5 File Header (128 bytes):
6 char     sFiberFileTag[8] = "FiberDat";
7 int        nFiberNr;	// number of fibers in this file
8 int        nFiberLenMax;	// max-length of fibers
9 float     fFiberLenMean;	// mean-length of fibers
10 int        nImgWidth;		// image dimension
11 int        nImgHeight;
12 int        nImgSlices;
13 float	fPixelSizeWidth;	// voxel size
14 float	fPixelSizeHeight;
15 float	fSliceThickness;
16 
17 int	enumSliceOrientation;  // orientation:  0=Coronal, 1=Axial,
18 2=Sagittal
19 int	enumSliceSequencing;  // sequencing:  0=Normal,  1=
20 
21 Fiber Data (starts from offset 128 bytes)
22 for each fiber:
23   int      nLength;    // fiber length;
24   int      nReserved;
25   int      nFiberStartIndex;   // the start-point of the selected fiber
26   int      nFiberEndIndex;     // the end-point of the selected fiber
27   XYZ_TRIPLE  xyzFiberChain[];	// the fiber data, in x-y-z format.
28 then, next fiber..
29 #endif
30 
31 #include "mrilib.h"
32 
33 static char outfname[THD_MAX_PREFIX] = "rawxyzseg.dat";
34 static char infname[THD_MAX_PREFIX];
35 
36 /*! convert DTIStudio fiber format data to SUMA segment data */
main(int argc,char * argv[])37 int main( int argc , char * argv[] )
38 {
39    int nopt;
40    int i,j;
41    FILE *fout, *fin;
42    float fxyz[3];
43    char fiberheaderstring[32];
44    int totalpts=0;
45    int npts, nfibers;
46    int statcode, ii;
47    int swapdata = 0;
48 
49    /*----- Read command line -----*/
50    if( argc < 2 || strcmp(argv[1],"-help") == 0 ){
51       printf("Usage: DTIStudioFibertoSegments [options] dataset\n"
52              "Convert a DTIStudio Fiber file to a SUMA segment file\n"
53              "Options:\n"
54              "  -output / -prefix = name of the output file (not an AFNI dataset prefix)\n"
55              "    the default output name will be rawxyzseg.dat\n"
56 	     "  -swap - swap bytes in data\\n\n"
57 	     );
58       exit(0) ;
59    }
60    fout = NULL;
61    mainENTRY("DTIStudioFibertoSegments main"); machdep(); AFNI_logger("DTIStudioFibertoSegments",argc,argv);
62    nopt = 1;
63    while( nopt < argc && argv[nopt][0] == '-' ){
64     if( (strcmp(argv[nopt],"-output") == 0 ) || (strcmp(argv[nopt],"-prefix")==0))
65       {
66        if (++nopt >= argc)
67 	 {
68 	  ERROR_exit("output / prefix needs an argument!\n");
69 	 }
70        MCW_strncpy (outfname, argv[nopt], THD_MAX_PREFIX);	/* change name from default prefix */
71        if (!THD_filename_ok (outfname))
72 	 {
73 	  ERROR_exit("%s is not a valid output name!\n", outfname);
74 	 }
75        if (THD_is_file(outfname))
76         {
77 	  ERROR_exit("%s already exists!\n", outfname);
78 	 }
79        nopt++; continue;
80       }
81 
82      if(strcmp(argv[nopt],"-swap")==0) {
83          swapdata = 1;
84 	 nopt++; continue;
85      }
86 
87      ERROR_exit("unknown option %s\n", argv[nopt]);
88    }
89 
90    fout = fopen (outfname, "w") ;
91    if( fout == NULL ){
92      ERROR_exit("can not create %s for some reason!\n", outfname);
93    }
94 
95 
96    /* get fiber file as input and check if we can open it */
97    MCW_strncpy (infname, argv[nopt], THD_MAX_PREFIX);	/* get name of fiber file */
98    if (!THD_filename_ok (infname))
99       {
100        ERROR_exit("%s is not a valid input name!\n", infname);
101       }
102    if (!THD_is_file(infname))
103       {
104        ERROR_exit("%s does not exist!\n", infname);
105       }
106    fin = fopen (infname, "r") ;
107    if( fin == NULL ){
108       ERROR_exit("can not open %s for some reason!\n", infname);
109    }
110 
111 
112 
113    /* read the header */
114 
115    statcode = fread(fiberheaderstring, 8, 1, fin);
116    if(!strcmp(fiberheaderstring,"FiberDat")) {
117      fclose(fin);
118      fclose(fout);
119      ERROR_exit("file does not have correct header format\n header does not start with FiberDat");
120    }
121 
122    statcode = fread(&nfibers, sizeof(int), 1, fin);
123 
124    if((statcode>=1) && (swapdata)) {
125        swap_fourbytes(1, &nfibers ) ;
126    }
127 
128    if((statcode<1)||(nfibers<1)) {
129      fclose(fin);
130      fclose(fout);
131      ERROR_exit("file does not have correct header format\nNumber of fibers is %u\n", nfibers);
132    }
133 
134    fseek(fin, 128, SEEK_SET);    /* go to start of data - 128 byte header */
135    PRINT_VERSION("DTIStudioFibertoSegments"); AUTHOR("Daniel Glen");
136    INFO_message("Number of fibers = %d", nfibers);
137    for(i=0;i<nfibers;i++) {                    /* get all the fibers */
138       statcode = fread(&npts,sizeof(int),1, fin);
139       if((statcode>=1) && (swapdata)) {
140         swap_fourbytes(1,&npts) ;
141       }
142 
143       if((statcode<1)||(npts<1)) {
144          fclose(fin);
145          fclose(fout);
146          ERROR_exit("can not read fiber info for segment number %d\n", i);
147       }
148 
149       totalpts += npts;
150       statcode = fseek(fin, 12,  SEEK_CUR); /* skip unused byte,RGB bytes,Start and End point indices */
151 
152       for(j=0;j<npts;j++) {                     /* get each point in each fiber */
153 	statcode = fread(fxyz, sizeof(float),3, fin);      /* xyz floating point triplet */
154         if(statcode<3) {
155            fclose(fin);
156            fclose(fout);
157            ERROR_exit("Can not read fiber data");
158         }
159         if(swapdata) {
160            swap_fourbytes(3,fxyz) ;
161 	}
162 
163         if(j!=0) {     /* after first point put space and write x,y,z, then repeat x,y,z on next line */
164 	  statcode = fprintf(fout," %10.3f %10.3f %10.3f\n",fxyz[0],fxyz[1],fxyz[2]);
165         }
166         if(j<(npts-1)) {  /* for all but the last point, write point again (first time for 1st pt*/
167 	  statcode = fprintf(fout,"%10.3f %10.3f %10.3f",fxyz[0],fxyz[1],fxyz[2]);
168         }
169         if(statcode==0) {
170            fclose(fin);
171            fclose(fout);
172 	   ERROR_exit("writing output file!");
173         }
174      }
175    }
176 
177    INFO_message("Total number of points = %d", totalpts);
178    fclose(fout);
179    fclose(fin);
180    exit(0);
181 }
182