Doxygen Source Code Documentation
Main Page Alphabetical List Data Structures File List Data Fields Globals Search
3dAFNItoRaw.c
Go to the documentation of this file.00001
00002 #include "mrilib.h"
00003
00004 static char outfname[THD_MAX_PREFIX] = "rawxyz.dat";
00005
00006
00007 int main( int argc , char * argv[] )
00008 {
00009 THD_3dim_dataset * old_dset;
00010 int nopt, nxyz;
00011 int force_float = 0;
00012 float voxval;
00013 int ival;
00014 short sval;
00015 double dval;
00016 byte bval;
00017 void *voxptr;
00018 int i,j;
00019 FILE *fout;
00020 double fac;
00021 MRI_IMAGE *data_im = NULL;
00022 MRI_TYPE out_kind = MRI_float;
00023 int statcode;
00024
00025
00026 if( argc < 2 || strcmp(argv[1],"-help") == 0 ){
00027 printf("Usage: 3dAFNItoRaw [options] dataset\n"
00028 "Convert an AFNI brik file with multiple sub-briks to a raw file with\n"
00029 " each sub-brik voxel concatenated voxel-wise.\n"
00030 "For example, a dataset with 3 sub-briks X,Y,Z with elements x1,x2,x3,...,xn,\n"
00031 " y1,y2,y3,...,yn and z1,z2,z3,...,zn will be converted to a raw dataset with\n"
00032 " elements x1,y1,z1, x2,y2,z2, x3,y3,z3, ..., xn,yn,zn \n"
00033 "The dataset is kept in the original data format (float/short/int)\n"
00034 "Options:\n"
00035 " -output / -prefix = name of the output file (not an AFNI dataset prefix)\n"
00036 " the default output name will be rawxyz.dat\n\n"
00037 " -datum float = force floating point output. Floating point forced if any\n"
00038 " sub-brik scale factors not equal to 1.\n\n"
00039 );
00040 printf("\n" MASTER_SHORTHELP_STRING ) ;
00041 exit(0) ;
00042 }
00043
00044 mainENTRY("3dAFNItoRaw main"); machdep(); AFNI_logger("3dAFNItoRaw",argc,argv);
00045 PRINT_VERSION("3dAFNItoRaw") ;
00046 nopt = 1;
00047 while( nopt < argc && argv[nopt][0] == '-' ){
00048 if( (strcmp(argv[nopt],"-output") == 0 ) || (strcmp(argv[nopt],"-prefix")==0))
00049 {
00050 if (++nopt >= argc)
00051 {
00052 fprintf (stderr, "*** Error - output / prefix needs an argument!\n");
00053 exit (1);
00054 }
00055 MCW_strncpy (outfname, argv[nopt], THD_MAX_PREFIX);
00056 if (!THD_filename_ok (outfname))
00057 {
00058 fprintf (stderr, "*** Error - %s is not a valid output name!\n", outfname);
00059 exit (1);
00060 }
00061 if (THD_is_file(outfname))
00062 {
00063 fprintf (stderr, "*** Error - %s already exists!\n", outfname);
00064 exit (1);
00065 }
00066 fout = fopen (outfname, "w") ;
00067 if( fout == NULL ){
00068 fprintf (stderr, "*** Error - can not create %s for some reason!\n", outfname);
00069 exit (1);
00070 }
00071 nopt++; continue;
00072 }
00073 if (strcmp(argv[nopt],"-datum")==0) {
00074 if (strcmp(argv[++nopt],"float")==0)
00075 force_float = 1;
00076 else {
00077 fprintf (stderr, \
00078 "*** Error - can only specify float; otherwise, original format assumed!\n");
00079 exit (1);
00080 }
00081 nopt++; continue;
00082 }
00083 fprintf(stderr, "*** Error - unknown option %s\n", argv[nopt]);
00084 exit(1);
00085 }
00086
00087 old_dset = THD_open_dataset (argv[nopt]);
00088
00089
00090 if (!ISVALID_DSET (old_dset)){
00091 fprintf (stderr, "*** Error - Can not open dataset %s\n", argv[nopt]);
00092 exit (1);
00093 }
00094
00095
00096 if (DSET_NVALS (old_dset) < 2){
00097 fprintf (stderr, "*** Error - Dataset must have at least two sub-briks.\n");
00098 exit (1);
00099 }
00100
00101 nxyz = DSET_NVOX(old_dset) ;
00102
00103 fout = fopen (outfname, "w") ;
00104 if( fout == NULL ){
00105 fprintf (stderr, "*** Error - can not create %s for some reason!\n", outfname);
00106 exit (1);
00107 }
00108
00109 if(force_float==0) {
00110 for(j=0;j<DSET_NVALS(old_dset);j++) {
00111 fac = DSET_BRICK_FACTOR(old_dset, j);
00112 if(fac==0.0) fac=1.0;
00113 if(fac!=1.0) {
00114 force_float = 1;
00115 break;
00116 }
00117 }
00118 }
00119
00120
00121 DSET_mallocize (old_dset);
00122 DSET_load (old_dset);
00123
00124 for(i=0;i<nxyz;i++) {
00125 for(j=0;j<DSET_NVALS(old_dset);j++) {
00126 data_im = DSET_BRICK (old_dset, j);
00127 fac = DSET_BRICK_FACTOR(old_dset, j);
00128 if(fac==0.0) fac=1.0;
00129 switch( data_im->kind ){
00130 case MRI_short:{
00131 short *ar = mri_data_pointer(data_im) ;
00132 voxval = ar[i];
00133 }
00134 break ;
00135
00136 case MRI_byte:{
00137 byte *ar = mri_data_pointer(data_im) ;
00138 voxval = ar[i];
00139 }
00140 break ;
00141
00142 case MRI_float:{
00143 float *ar = mri_data_pointer(data_im) ;
00144 voxval = ar[i];
00145 }
00146 break ;
00147
00148 case MRI_double:{
00149 double *ar = mri_data_pointer(data_im) ;
00150 voxval = ar[i];
00151 }
00152 break ;
00153
00154 case MRI_int:{
00155 int *ar = mri_data_pointer(data_im) ;
00156 voxval = ar[i];
00157 }
00158 break ;
00159
00160 case MRI_complex:{
00161 complex *ar = mri_data_pointer(data_im) ;
00162 voxval = CABS(ar[i]);
00163 }
00164 break ;
00165
00166 case MRI_rgb:{
00167 byte *ar = mri_data_pointer(data_im) ;
00168 voxval = 0.299*ar[3*i]+0.587*ar[3*i+1]+0.114*ar[3*i+2];
00169 }
00170 break ;
00171
00172 default:
00173 voxval = 0.0;
00174 fprintf(stderr,"Unknown type, %s, in sub-brik %d\n", MRI_TYPE_name[data_im->kind], i);
00175 break;
00176 }
00177
00178 voxval = voxval * fac;
00179
00180 if(force_float!=0)
00181 out_kind = MRI_float;
00182 else
00183 out_kind = data_im->kind;
00184
00185 switch(out_kind) {
00186 case MRI_short:{
00187 sval = voxval;
00188 voxptr = &sval;
00189 }
00190 break ;
00191
00192 case MRI_byte:{
00193 bval = voxval;
00194 voxptr = &sval;
00195 }
00196 break ;
00197
00198 case MRI_float:{
00199 voxptr = &voxval;
00200 }
00201 break ;
00202
00203 case MRI_double:{
00204 dval = voxval;
00205 voxptr = &dval;
00206 }
00207 break ;
00208
00209 case MRI_int:{
00210 ival = voxval;
00211 voxptr = &ival;
00212 }
00213 break ;
00214
00215 default:
00216 fprintf(stderr,"Can't handle type, %s, in sub-brik %d\n",\
00217 MRI_TYPE_name[data_im->kind], j);
00218 fclose(fout);
00219 exit(1);
00220 break;
00221 }
00222
00223 statcode = fwrite(voxptr, sizeof(out_kind), 1,fout);
00224 if(statcode!=1) {
00225 fprintf(stderr, "*** Error - writing output file!\n");
00226 fclose(fout);
00227 exit(1);
00228 }
00229 }
00230 }
00231
00232 fprintf(stderr, "File output format is %s\n", MRI_TYPE_name[out_kind]);
00233 fclose(fout);
00234 exit(0);
00235 }