00001 #include <stdio.h>
00002 #include <string.h>
00003 #include <sys/stat.h>
00004 #include <stdlib.h>
00005 #include <math.h>
00006 #include "siemens_vision.h"
00007
00008 static void byteswap8 (double *x) {
00009 char *p1,*p2; double d; int i;
00010 p1=(char *)x; p2=(char *)&d;
00011 for(i=0;i<8;i++) p2[7-i]=p1[i];
00012 (*x)=d;
00013 }
00014
00015 static void byteswap4( u_int * x ) {
00016 char *p1,*p2 ; u_int d ; int i ;
00017 p1=(char *)x; p2=(char *)&d;
00018 for(i=0;i<4;i++) p2[3-i]=p1[i];
00019 (*x)=d;
00020 }
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032 int main( int argc , char * argv[] )
00033 {
00034 struct Siemens_vision_header head ;
00035 FILE * fp ;
00036 char orients[7] ;
00037 int i,j,xx,yy , matrix , swap , slices ;
00038 double dd , qq ;
00039 struct stat file_stat ;
00040 int imagesize=64 ;
00041 short *imar=NULL ;
00042 char *prefix=NULL ; int iarg=1 , nfiles=0 ;
00043
00044 if( argc < 2 || strcmp(argv[1],"-help") == 0 ){
00045 printf("Usage: siemens_vision [options] filename ...\n"
00046 "Prints out information from the Siemens .ima file header(s).\n"
00047 "\n"
00048 "The only option is to rename the file according to the\n"
00049 "TextImageNumber field stored in the header. The option is:\n"
00050 "\n"
00051 " -rename ppp\n"
00052 "\n"
00053 "which will rename each file to the form 'ppp.nnnn.ima',\n"
00054 "where 'nnnn' is the image number expressed with 4 digits.\n"
00055 "\n"
00056 "When '-rename' is used, the header info from the input files\n"
00057 "will not be printed.\n"
00058 ) ;
00059 exit(0) ;
00060 }
00061
00062 if( strcmp(argv[iarg],"-rename") == 0 ){
00063 if( iarg+2 >= argc ){
00064 fprintf(stderr,"** ERROR: not enough arguments after '-rename'\n") ;
00065 exit(1) ;
00066 }
00067 prefix = strdup(argv[++iarg]) ;
00068 if( !THD_filename_ok(prefix) ){
00069 fprintf(stderr,"** ERROR: prefix after '-rename' is illegal\n") ;
00070 exit(1) ;
00071 }
00072 iarg++ ;
00073 }
00074
00075
00076
00077 for( ; iarg < argc ; iarg++ ){
00078
00079 nfiles++ ;
00080
00081 i = stat( argv[iarg] , &file_stat ) ;
00082 if( i < 0 ){
00083 fprintf(stderr,"** Can't access file %s\n",argv[iarg]) ; exit(1) ;
00084 }
00085
00086
00087
00088 fp = fopen( argv[iarg] , "r" ) ;
00089 if( fp == NULL ) exit(1) ;
00090 fread( &head , sizeof(struct Siemens_vision_header) , 1 , fp ) ;
00091
00092
00093
00094 swap = ( head.SiemensStudyDateMM < 0 || head.SiemensStudyDateMM > 12 ) ;
00095 if( swap ){
00096 byteswap4( &(head.SiemensStudyDateMM) ) ;
00097 if( head.SiemensStudyDateMM < 0 || head.SiemensStudyDateMM > 12 ){
00098 printf("** Can't determine byte swap status of file %s\n",argv[iarg]) ;
00099 byteswap4( &(head.SiemensStudyDateMM) ) ;
00100 swap = 0 ;
00101 }
00102 }
00103
00104
00105
00106 if( prefix != NULL ){
00107 char *cpt , fname[2048] ;
00108 int num = (int)strtol( head.TextImageNumber , &cpt , 10 ) ;
00109 if( *cpt != '\0' && num == 0 ){
00110 printf("** Can't get TextImageNumber from file %s\n",argv[iarg]) ;
00111 num = iarg ;
00112 }
00113 sprintf(fname,"%s.%04d.ima",prefix,num) ;
00114 if( THD_is_ondisk(fname) ){
00115 printf("** Can't rename %s to pre-existing filename %s\n",
00116 argv[iarg],fname ) ;
00117 } else {
00118 printf("++ Renaming %s to %s\n",argv[iarg],fname) ;
00119 rename(argv[iarg],fname) ;
00120 }
00121 fclose(fp) ; continue ;
00122 }
00123
00124
00125
00126
00127
00128 if( swap ) byteswap4( &(head.DisplayMatrixSize) ) ;
00129 imagesize = head.DisplayMatrixSize ;
00130
00131
00132
00133 if( file_stat.st_size==(imagesize * imagesize * 64 * 2 + SIEMENS_HEADERSIZE) ){
00134 matrix = 8 ;
00135 } else if( file_stat.st_size==(imagesize * imagesize * 16 * 2 + SIEMENS_HEADERSIZE) ){
00136 matrix = 4;
00137 } else {
00138 matrix = 0 ;
00139 printf("Don't recognize file size. Is this a Siemens Magnetom Vision .ima file?\n") ;
00140 }
00141
00142
00143
00144 if( matrix > 0 ){
00145 imar = (short *) calloc(sizeof(short),matrix*matrix*imagesize*imagesize) ;
00146 fseek( fp , SIEMENS_HEADERSIZE , SEEK_SET ) ;
00147 fread( imar , sizeof(short) , matrix*matrix*imagesize*imagesize , fp ) ;
00148 }
00149
00150 fclose(fp) ;
00151
00152
00153
00154 if (swap) {
00155 #if 0
00156 byteswap4(&(head.SiemensStudyDateYYYY)) ;
00157 byteswap4(&(head.SiemensStudyDateMM)) ;
00158 byteswap4(&(head.SiemensStudyDateDD)) ;
00159 byteswap4(&(head.AcquisitionDateYYYY)) ;
00160 byteswap4(&(head.AcquisitionDateMM)) ;
00161 byteswap4(&(head.AcquisitionDateDD)) ;
00162 byteswap4(&(head.ImageDateYYYY)) ;
00163 byteswap4(&(head.ImageDateMM)) ;
00164 byteswap4(&(head.ImageDateDD)) ;
00165 byteswap4(&(head.SiemensStudyTimeHH)) ;
00166 byteswap4(&(head.SiemensStudyTimeMM)) ;
00167 byteswap4(&(head.SiemensStudyTimeSS)) ;
00168 byteswap4(&(head.AcquisitionTimeHH)) ;
00169 byteswap4(&(head.AcquisitionTimeMM)) ;
00170 byteswap4(&(head.AcquisitionTimeSS)) ;
00171 byteswap4(&(head.ImageTimeHH)) ;
00172 byteswap4(&(head.ImageTimeMM)) ;
00173 byteswap4(&(head.ImageTimeSS)) ;
00174 #endif
00175
00176 byteswap8(&(head.FOVRow));
00177 byteswap8(&(head.FOVColumn));
00178 byteswap8(&(head.SliceThickness));
00179 byteswap8(&(head.RepetitionTime)) ;
00180 byteswap8(&(head.FrequencyMHz)) ;
00181 byteswap8(&(head.EchoTime)) ;
00182 byteswap8(&(head.CenterPointX)) ;
00183 byteswap8(&(head.CenterPointY)) ;
00184 byteswap8(&(head.CenterPointZ)) ;
00185 byteswap8(&(head.NormalVectorX));
00186 byteswap8(&(head.NormalVectorY));
00187 byteswap8(&(head.NormalVectorZ));
00188 byteswap8(&(head.PixelSizeRow)) ;
00189 byteswap8(&(head.PixelSizeColumn)) ;
00190 byteswap8(&(head.RowVectorX)) ;
00191 byteswap8(&(head.RowVectorY)) ;
00192 byteswap8(&(head.RowVectorZ)) ;
00193 byteswap8(&(head.ColumnVectorX)) ;
00194 byteswap8(&(head.ColumnVectorY)) ;
00195 byteswap8(&(head.ColumnVectorZ)) ;
00196 byteswap8(&(head.DistanceFromIsocenter)) ;
00197 }
00198
00199
00200
00201 printf("Manufact = %s\n",head.Manufacturer) ;
00202 printf("Institut = %s\n",head.InstitutionName) ;
00203
00204 printf("Date = %s\n"
00205 "Time = %s\n"
00206 "Sequence = %s\n"
00207 "Patient = %s\n"
00208 "ImageNum = %s\n" ,
00209 head.TextDate , head.TextTime ,
00210 head.SequenceType , head.PatientName , head.TextImageNumber ) ;
00211 printf("SubjectID= %s\n",head.PatientID) ;
00212 printf("RF Coil = %s\n",head.ReceivingCoil) ;
00213
00214 orients[0]= head.OrientationSet1Left[0]; orients[1]=head.OrientationSet2Right[0];
00215 orients[2]= head.OrientationSet1Top[0] ; orients[3]=head.OrientationSet2Down[0] ;
00216 orients[4]= head.OrientationSet1Back[0]; orients[5]=head.OrientationSet2Front[0];
00217 for (i=0; i<6; i++) {
00218 if (orients[i]=='H') orients[i]='S';
00219 if (orients[i]=='F') orients[i]='I';
00220 }
00221 orients[6] = '\0' ;
00222 printf("Orient = %s\n",orients) ;
00223
00224 printf("FOV Row = %g\n"
00225 "FOV Col = %g\n"
00226 "SliceThk = %g\n" , head.FOVRow , head.FOVColumn , head.SliceThickness ) ;
00227
00228 printf("TR = %g\n",head.RepetitionTime) ;
00229 printf("TE = %g\n",head.EchoTime) ;
00230 printf("Cen X = %g\n",head.CenterPointX) ;
00231 printf("Cen Y = %g\n",head.CenterPointY) ;
00232 printf("Cen Z = %g\n",head.CenterPointZ) ;
00233 printf("Delta X = %g\n",head.PixelSizeRow) ;
00234 printf("Delta Y = %g\n",head.PixelSizeColumn) ;
00235 printf("RowVec X = %g\n",head.RowVectorX) ;
00236 printf("RowVec Y = %g\n",head.RowVectorY) ;
00237 printf("RowVec Z = %g\n",head.RowVectorZ) ;
00238 printf("ColVec X = %g\n",head.ColumnVectorX) ;
00239 printf("ColVec Y = %g\n",head.ColumnVectorY) ;
00240 printf("ColVec Z = %g\n",head.ColumnVectorZ) ;
00241 printf("Normal X = %g\n",head.NormalVectorX);
00242 printf("Normal Y = %g\n",head.NormalVectorY);
00243 printf("Normal Z = %g\n",head.NormalVectorZ);
00244
00245 #if 0
00246 dd = head.RowVectorX * head.ColumnVectorX
00247 +head.RowVectorY * head.ColumnVectorY
00248 +head.RowVectorZ * head.ColumnVectorZ ;
00249 printf("Row*Col = %g\n",dd) ;
00250
00251 dd = head.RowVectorX * head.NormalVectorX
00252 +head.RowVectorY * head.NormalVectorY
00253 +head.RowVectorZ * head.NormalVectorZ ;
00254 printf("Row*Nor = %g\n",dd) ;
00255
00256 dd = head.ColumnVectorX * head.NormalVectorX
00257 +head.ColumnVectorY * head.NormalVectorY
00258 +head.ColumnVectorZ * head.NormalVectorZ ;
00259 printf("Col*Nor = %g\n",dd) ;
00260 #endif
00261
00262 printf("D-IsoCen = %g\n",head.DistanceFromIsocenter) ;
00263 printf("SlicePos = %s\n",head.TextSlicePosition) ;
00264
00265
00266
00267 slices = matrix*matrix ;
00268
00269 if( imar != NULL ){
00270 int nxx = matrix*imagesize , blank ;
00271 printf("Recognized %dx%d mosaic of %dx%d images:\n",matrix,matrix,imagesize,imagesize) ;
00272 for( yy=0 ; yy < matrix ; yy++ ){
00273 printf(" ") ;
00274 for( xx=0 ; xx < matrix ; xx++ ){
00275 blank = 1 ;
00276 for( j=0 ; j < imagesize ; j++ ){
00277 for( i=0 ; i < imagesize ; i++ ){
00278 if( imar[i+xx*imagesize+(j+yy*imagesize)*nxx] ) blank = 0 ;
00279 }
00280 }
00281 printf(" %s" , (blank) ? "blank" : "full ") ;
00282 if( !blank ) slices = 1 + xx + yy*matrix ;
00283 }
00284 printf("\n") ;
00285 }
00286 }
00287
00288 dd = fabs(strtod(head.TextSlicePosition,NULL)) ;
00289 qq = dd - (slices-1)*head.SliceThickness ;
00290 if( qq > 0.0 ){
00291 orients[5] = orients[4] ;
00292 } else {
00293 qq = -qq ;
00294 }
00295
00296 }
00297
00298 if( prefix == NULL ){
00299 printf("\nto3d -epan"
00300 " -time:zt %d %d %0.3fs alt+z"
00301 " -xFOV %0.2f%c-%c"
00302 " -yFOV %0.2f%c-%c"
00303 " -zSLAB %0.2f%c-%0.2f%c \n" ,
00304
00305 slices , nfiles , 0.001*head.RepetitionTime ,
00306 0.5*head.FOVRow , orients[0] , orients[1] ,
00307 0.5*head.FOVColumn , orients[2] , orients[3] ,
00308 dd , orients[4] , qq , orients[5] ) ;
00309 }
00310
00311 exit(0) ;
00312 }