File: fsl_api_driver.c

package info (click to toggle)
insighttoolkit 3.20.1%2Bgit20120521-3
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 80,652 kB
  • sloc: cpp: 458,133; ansic: 196,223; fortran: 28,000; python: 3,839; tcl: 1,811; sh: 1,184; java: 583; makefile: 430; csh: 220; perl: 193; xml: 20
file content (142 lines) | stat: -rw-r--r-- 3,897 bytes parent folder | download | duplicates (9)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/*******************************************************************
 *
 * fsl_api_driver.c
 *
 * Test fslio API 
 *
 * Usage: fsl_api_driver <command> <dataset(s)> [params]
 *
 * 
 *      print <dataset>
 *              print dataset header fields
 *      peek <dataset> X Y Z T
 *              print value at voxel location X Y Z T (0-based index)
 *      timecourse <dataset> X Y Z
 *              print timecourse at voxel location X Y Z (0-based index)
 *
 * Kate Fissell, University of Pittsburgh
 * 12/04
 *******************************************************************/


#include <stdio.h>
#include <strings.h>

#include <nifti1.h>
#include <fslio.h>

void pusage(char *cmd);

int main(int argc, char * argv[])
{

FSLIO *fslio;
void *buffer;
char *f1name;
double ***vol;
int x,y,z,t;


/*** process commandline parameters */
if (argc < 2) {
        pusage(argv[0]);
        exit(1);
}


/************************* PRINT ***************************/
if (!strncmp(argv[1],"print",5)) {
        if (argc != 3) {
                fprintf(stderr, "\nError, print command takes one parameter: print <dataset>\n");
                exit(1);
        }

        f1name = argv[2];
        /** open nifti dataset */
        fslio = FslInit();
        buffer = FslReadAllVolumes(fslio,f1name);
        if (buffer == NULL) {
                fprintf(stderr, "\nError opening and reading %s.\n",f1name);
                exit(1);
        }

        nifti_image_infodump(fslio->niftiptr);

        exit(0);
}



/************************* PEEK ***************************/
if (!strncmp(argv[1],"peek",4)) {
        if (argc != 7) {
                fprintf(stderr, "\nError, peek command takes five parameters: peek <dataset> X Y Z T\n");
                exit(1);
        }

        /**** get inputs */
        f1name = argv[2];
        x = atoi(argv[3]);
        y = atoi(argv[4]);
        z = atoi(argv[5]);
        t = atoi(argv[6]);

        /** open nifti dataset header */
        fslio = FslReadHeader(f1name);
        if (fslio == NULL) {
                fprintf(stderr, "\nError, could not read header info for %s.\n",f1name);
                exit(1);
        }

        /**** check inputs */
        if ( (x<0) || (x>=fslio->niftiptr->nx) ) {
                fprintf(stderr, "\nError: x index (%d) out of range [0..%d]\n",x,fslio->niftiptr->nx-1);
                exit(1);
        }
        if ( (y<0) || (y>=fslio->niftiptr->ny) ) {
                fprintf(stderr, "\nError: y index (%d) out of range [0..%d]\n",y,fslio->niftiptr->ny-1);
                exit(1);
        }
        if ( (z<0) || (z>=fslio->niftiptr->nz) ) {
                fprintf(stderr, "\nError: z index (%d) out of range [0..%d]\n",z,fslio->niftiptr->nz-1);
                exit(1);
        }
        if ( (t<0) || (t>=fslio->niftiptr->nt) ) {
                fprintf(stderr, "\nError: t index (%d) out of range [0..%d]\n",t,fslio->niftiptr->nt-1);
                exit(1);
        }

        /*** get volume data as scaled doubles */

        vol = FslGetVolumeAsScaledDouble(fslio,t);
        if (vol == NULL) {
                fprintf(stderr, "\nError accessing %s\n",f1name);
                exit(1);
        }
        else {
                fprintf(stderr, "\nLocation %d %d %d %d: %.4f\n",x,y,z,t,vol[z][y][x]);
                exit(0);
        }

}



fprintf(stderr, "\nError, unrecognized command %s\n",argv[1]);
pusage(argv[0]);


exit(1);
}


void pusage(char *cmd) {
        fprintf(stderr, "\n%s is a small driver program to test out the fslio API.\n",cmd);
        fprintf(stderr, "\nUsage: %s <command> <dataset(s)> <parameters>",cmd);
        fprintf(stderr, "\n\n\tCommands:");
        fprintf(stderr, "\n\tprint <dataset>\t\t\tprint dataset header");
        fprintf(stderr, "\n\tpeek <dataset> X Y Z T\t\tprint dataset value at location (0-based) (x,y,z,t).");

        fprintf(stderr, "\n");
return;
}