1 #include "mrilib.h"
2 
3 #define SFAC 0.0002f
4 
main(int argc,char * argv[])5 int main( int argc , char *argv[] )
6 {
7    THD_3dim_dataset *inset , *mask_dset ;
8    FILE *fpout=NULL ;
9    char *prefix="Xdataset" ;
10    char fpname[THD_MAX_NAME] ;
11    int nopt=1 , nvox,ngood , ii,jj , *ijkmask ;
12    byte *mask_vol ; float *inar , val ; short *outar ;
13 
14    if( argc < 3 || strcasecmp(argv[1],"-help") == 0 ){
15      printf("\n"
16        "Convert input datasets to the format needed for 3dClustSimX.\n"
17        "\n"
18        "Usage:\n"
19        "\n"
20        " 3dtoXdataset -prefix PPP maskdataset inputdataset ...\n"
21        "\n"
22        "The output file 'PPP.sdat' will be created, if it does not exist.\n"
23        "If it already exists, the input dataset value (inside the mask) will\n"
24        "be appended to this output file.\n"
25        "\n"
26      ) ;
27      exit(0) ;
28    }
29 
30    if( strcasecmp(argv[nopt],"-prefix") == 0 ){
31      prefix = strdup(argv[++nopt]) ;
32    }
33 
34    if( strstr(prefix,".sdat") == NULL ){
35      sprintf(fpname,"%s.sdat",prefix) ;
36    } else {
37      strcpy(fpname,prefix) ;
38    }
39 
40    mask_dset = THD_open_dataset(argv[++nopt]) ;
41    if( mask_dset == NULL )
42      ERROR_exit("can't open mask dataset '%s'",argv[nopt]) ;
43 
44    mask_vol = THD_makemask( mask_dset , 0 , 1.0,0.0 ) ;
45    if( mask_vol == NULL )
46      ERROR_exit("can't use -mask dataset '%s'",argv[nopt]) ;
47    DSET_unload(mask_dset) ;
48 
49    nvox = DSET_NVOX(mask_dset) ;
50    ngood = THD_countmask( nvox , mask_vol ) ;
51    INFO_message("mask has %d voxels",ngood) ;
52 
53    ijkmask = (int *  )malloc(sizeof(int)  *ngood) ;
54    outar   = (short *)malloc(sizeof(short)*ngood) ;
55    for( jj=ii=0 ; ii < nvox ; ii++ ){
56      if( mask_vol[ii] ) ijkmask[jj++] = ii ;
57    }
58 
59    for( ++nopt ; nopt < argc ; nopt++ ){
60      ININFO_message("process dataset %s",argv[nopt]) ;
61      inset = THD_open_dataset(argv[nopt]) ;
62      if( inset == NULL )
63        ERROR_exit("can't open dataset '%s'",argv[nopt]) ;
64      DSET_load(inset) ; CHECK_LOAD_ERROR(inset) ;
65      if( DSET_NVOX(inset) != nvox )
66        ERROR_exit("data grid mismatch at input %s",argv[nopt]) ;
67      if( !DSET_datum_constant(inset) || DSET_BRICK_TYPE(inset,0) != MRI_float )
68        ERROR_exit("data is not pure float type at input %s",argv[nopt]) ;
69 
70      if( fpout == NULL ){
71        fpout = fopen( fpname , "a" ) ;  /* open for write/append */
72        if( fpout == NULL )
73          ERROR_exit("can't open file '%s' for writing",fpname) ;
74      }
75      for( jj=0 ; jj < DSET_NVALS(inset) ; jj++ ){
76        inar = DSET_ARRAY(inset,jj) ;
77        for( ii=0 ; ii < ngood ; ii++ ){
78          val = inar[ijkmask[ii]] / SFAC ;
79          outar[ii] = SHORTIZE(val) ;
80        }
81        fwrite(outar,sizeof(short),ngood,fpout) ;
82      }
83      DSET_delete(inset) ;
84    }
85 
86    fclose(fpout) ;
87    INFO_message("output file is %s",fpname) ;
88    exit(0) ;
89 }
90