Doxygen Source Code Documentation
ge_header.c File Reference
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
Go to the source code of this file.
Functions | |
void | swap_4 (void *ppp) |
void | swap_8 (void *ppp) |
void | swap_2 (void *ppp) |
int | main (int argc, char *argv[]) |
Function Documentation
|
\** File : SUMA.c
Input paramters :
Definition at line 51 of file ge_header.c. References abs, argc, L, skip, swap, swap_2(), and swap_4().
00052 { 00053 FILE *imfile ; 00054 int length , skip , swap=0 , gg ; 00055 char orients[8] , str[8] ; 00056 int nx , ny , bpp , cflag , hdroff , verb=0 , iarg=1 ; 00057 00058 /*------ help? ------*/ 00059 00060 if( argc < 2 || strcmp(argv[1],"-help") == 0 ){ 00061 printf("Usage: ge_header [-verb] file ...\n" 00062 "Prints out information from the GE image header of each file.\n" 00063 "Options:\n" 00064 " -verb: Print out some probably useless extra stuff.\n" 00065 ) ; 00066 exit(0) ; 00067 } 00068 00069 if( strcmp(argv[iarg],"-verb") == 0 ){ verb++ ; iarg++ ; } 00070 if( strcmp(argv[iarg],"-verb") == 0 ){ verb++ ; iarg++ ; } 00071 00072 /*----- loop over input files -----*/ 00073 00074 for( gg=iarg ; gg < argc ; gg++ ){ 00075 00076 imfile = fopen( argv[gg] , "r" ) ; 00077 if( imfile == NULL ){ 00078 printf("-----------------------------------\n" 00079 "Can't open file %s\n" , 00080 argv[gg] ) ; 00081 continue ; /* skip to next file */ 00082 } 00083 00084 fseek( imfile , 0L , SEEK_END ) ; /* get the length of the file */ 00085 length = ftell( imfile ) ; /* (the AJ way) */ 00086 00087 if( length < 1024 ){ 00088 printf("-----------------------------------\n" 00089 "File %s is too short to be an image\n" , 00090 argv[gg] ) ; 00091 fclose(imfile) ; continue ; 00092 } 00093 00094 /*--- 03 Dec 2001: check for GEMS format file "IMGF" ---*/ 00095 /*[[[ Information herein from Medical Image Format FAQ ]]]*/ 00096 00097 strcpy(str,"JUNK") ; /* initialize string */ 00098 rewind(imfile) ; 00099 fread(str,1,4,imfile) ; /* check for "IMGF" at start of file */ 00100 00101 if( str[0]=='I' && str[1]=='M' && str[2]=='G' && str[3]=='F' ){ /* good */ 00102 00103 /*-- read next 5 ints (after the "IMGF" string) --*/ 00104 00105 fread( &skip , 4,1, imfile ) ; /* offset into file of image data */ 00106 fread( &nx , 4,1, imfile ) ; /* x-size */ 00107 fread( &ny , 4,1, imfile ) ; /* y-size */ 00108 fread( &bpp , 4,1, imfile ) ; /* bits per pixel (should be 16) */ 00109 fread( &cflag, 4,1, imfile ) ; /* compression flag (1=uncompressed)*/ 00110 00111 /*-- check if nx is funny --*/ 00112 00113 if( nx < 0 || nx > 8192 ){ /* have to byte swap these 5 ints */ 00114 swap = 1 ; /* flag to swap data, too */ 00115 swap_4(&skip); swap_4(&nx); swap_4(&ny); swap_4(&bpp); swap_4(&cflag); 00116 if( nx < 0 || nx > 8192 || ny < 0 || ny > 8192 ){ 00117 printf("-----------------------------------\n" 00118 "File %s: illegal nx hand/or ny in header\n" , 00119 argv[gg] ) ; 00120 fclose(imfile) ; continue ; 00121 } 00122 } else { 00123 swap = 0 ; /* data is ordered for this CPU */ 00124 } 00125 00126 printf("-----------------------------------\n" 00127 "File %s\n" 00128 " nx=%d ny=%d skip=%d swap=%s compressed=%s bits-per-pixel=%d\n", 00129 argv[gg] , nx , ny , skip , 00130 (swap) ? "YES" : "NO" , 00131 (cflag==1) ? "NO" : "YES" , 00132 bpp 00133 ) ; 00134 00135 /*-- try to read image header data as well --*/ 00136 00137 length = fseek( imfile , 148L , SEEK_SET ) ; /* magic GEMS offset */ 00138 if( length == 0 ){ /* seek was good */ 00139 fread( &hdroff , 4,1 , imfile ) ; /* location of image header */ 00140 if( swap ) swap_4(&hdroff) ; 00141 if( hdroff > 0 ){ /* read from image header */ 00142 float dx,dy,dz, xyz[9], xx,yy,zz, tr ; int itr, ii,jj,kk ; 00143 00144 /*-- get voxel grid sizes --*/ 00145 00146 fseek( imfile , hdroff+26 , SEEK_SET ) ; /* dz */ 00147 fread( &dz , 4,1 , imfile ) ; 00148 00149 fseek( imfile , hdroff+50 , SEEK_SET ) ; /* dx and dy */ 00150 fread( &dx , 4,1 , imfile ) ; 00151 fread( &dy , 4,1 , imfile ) ; 00152 00153 if( swap ){ swap_4(&dx); swap_4(&dy); swap_4(&dz); } 00154 00155 printf(" dx=%g dy=%g dz=%g" , dx,dy,dz ) ; 00156 00157 /* grid orientation: from 3 sets of LPI corner coordinates: */ 00158 /* xyz[0..2] = top left hand corner of image (TLHC) */ 00159 /* xyz[3..5] = top right hand corner of image (TRHC) */ 00160 /* xyz[6..8] = bottom right hand corner of image (BRHC) */ 00161 /* GEMS coordinate orientation here is LPI */ 00162 00163 fseek( imfile , hdroff+154 , SEEK_SET ) ; /* another magic number */ 00164 fread( xyz , 4,9 , imfile ) ; 00165 if( swap ){ 00166 swap_4(xyz+0); swap_4(xyz+1); swap_4(xyz+2); 00167 swap_4(xyz+3); swap_4(xyz+4); swap_4(xyz+5); 00168 swap_4(xyz+6); swap_4(xyz+7); swap_4(xyz+8); 00169 } 00170 00171 /* x-axis orientation */ 00172 /* ii determines which spatial direction is x-axis */ 00173 /* and is the direction that has the biggest change */ 00174 /* between the TLHC and TRHC */ 00175 00176 dx = fabs(xyz[3]-xyz[0]) ; ii = 1 ; 00177 dy = fabs(xyz[4]-xyz[1]) ; if( dy > dx ){ ii=2; dx=dy; } 00178 dz = fabs(xyz[5]-xyz[2]) ; if( dz > dx ){ ii=3; } 00179 dx = xyz[ii+2]-xyz[ii-1] ; if( dx < 0. ){ ii = -ii; } 00180 switch( ii ){ 00181 case 1: orients[0]= 'L'; orients[1]= 'R'; break; /* Left to Right */ 00182 case -1: orients[0]= 'R'; orients[1]= 'L'; break; /* Right to Left */ 00183 case 2: orients[0]= 'P'; orients[1]= 'A'; break; /* Posterior to Anterior */ 00184 case -2: orients[0]= 'A'; orients[1]= 'P'; break; /* Anterior to Posterior */ 00185 case 3: orients[0]= 'I'; orients[1]= 'S'; break; /* Inferior to Superior */ 00186 case -3: orients[0]= 'S'; orients[1]= 'I'; break; /* Superior to Inferior */ 00187 default: orients[0]='\0'; orients[1]='\0'; break; /* should never happen */ 00188 } 00189 00190 /* y-axis orientation */ 00191 /* jj determines which spatial direction is y-axis */ 00192 /* and is the direction that has the biggest change */ 00193 /* between the BRHC and TRHC */ 00194 00195 dx = fabs(xyz[6]-xyz[3]) ; jj = 1 ; 00196 dy = fabs(xyz[7]-xyz[4]) ; if( dy > dx ){ jj=2; dx=dy; } 00197 dz = fabs(xyz[8]-xyz[5]) ; if( dz > dx ){ jj=3; } 00198 dx = xyz[jj+5]-xyz[jj+2] ; if( dx < 0. ){ jj = -jj; } 00199 switch( jj ){ 00200 case 1: orients[2] = 'L'; orients[3] = 'R'; break; 00201 case -1: orients[2] = 'R'; orients[3] = 'L'; break; 00202 case 2: orients[2] = 'P'; orients[3] = 'A'; break; 00203 case -2: orients[2] = 'A'; orients[3] = 'P'; break; 00204 case 3: orients[2] = 'I'; orients[3] = 'S'; break; 00205 case -3: orients[2] = 'S'; orients[3] = 'I'; break; 00206 default: orients[2] ='\0'; orients[3] ='\0'; break; 00207 } 00208 00209 orients[4] = '\0' ; /* terminate orientation string */ 00210 00211 kk = 6 - abs(ii)-abs(jj) ; /* which spatial direction is z-axis */ 00212 /* where 1=LR, 2=PA, 3=IS */ 00213 /* (can't tell orientation from 1 slice) */ 00214 00215 zz = xyz[kk-1] ; /* z-coordinate of this slice */ 00216 xx = xyz[abs(ii)-1] ; 00217 yy = xyz[abs(jj)-1] ; 00218 00219 printf(" zoff=%g orient=%s xoff=%g yoff=%g", 00220 zz,orients,xx,yy ) ; 00221 00222 /*-- get TR in seconds --*/ 00223 00224 fseek( imfile , hdroff+194 , SEEK_SET ) ; 00225 fread( &itr , 4,1 , imfile ) ; /* note itr is an int */ 00226 if( swap ) swap_4(&itr) ; 00227 tr = 1.0e-6 * itr ; /* itr is in microsec */ 00228 printf(" TR=%gs",tr) ; 00229 00230 /*-- get TE in milliseconds --*/ 00231 00232 fseek( imfile , hdroff+202 , SEEK_SET ) ; 00233 fread( &itr , 4,1 , imfile ) ; /* itr is an int, in microsec */ 00234 if( swap ) swap_4(&itr) ; 00235 printf(" TE=%gms",1.0e-3*itr) ; 00236 00237 /*-- end of image header printouts --*/ 00238 00239 printf("\n") ; 00240 00241 /*-- verbosity? --*/ 00242 00243 if( verb ) 00244 printf(" TLHC= %g %g %g\n" 00245 " TRHC= %g %g %g\n" 00246 " BRHC= %g %g %g\n" , 00247 xyz[0],xyz[1],xyz[2],xyz[3],xyz[4],xyz[5],xyz[6],xyz[7],xyz[8] ) ; 00248 00249 /*-- date/time stamp */ 00250 00251 if( verb ){ 00252 int dt1 , dt2 ; short in1,in2 ; 00253 fseek( imfile , hdroff+10 , SEEK_SET ) ; 00254 fread( &in1 , 2,1 , imfile ) ; 00255 fread( &in2 , 2,1 , imfile ) ; 00256 fread( &dt1 , 4,1 , imfile ) ; 00257 fread( &dt2 , 4,1 , imfile ) ; 00258 if( swap ){ swap_4(&dt1);swap_4(&dt2);swap_2(&in1);swap_2(&in2); } 00259 printf(" image date/time stamps=%d,%d series#=%d im#=%d\n",dt1,dt2,in1,in2) ; 00260 } /* end of philately */ 00261 00262 } /* end of actually reading image header */ 00263 00264 } /* end of trying to read image header */ 00265 00266 /* maybe read series header */ 00267 00268 if( verb > 1 ){ 00269 int dt1 , dt2 ; short in1,in2 ; 00270 fseek( imfile , 140L , SEEK_SET ) ; 00271 fread( &hdroff , 4,1 , imfile ) ; /* location of series header */ 00272 if( swap ) swap_4(&hdroff) ; 00273 if( hdroff > 0 ){ 00274 fseek( imfile , hdroff+10 , SEEK_SET ) ; 00275 fread( &in1 , 2,1 , imfile ) ; 00276 fread( &dt1 , 4,1 , imfile ) ; 00277 fread( &dt2 , 4,1 , imfile ) ; 00278 if( swap ){ swap_4(&dt1);swap_4(&dt2);swap_2(&in1); } 00279 printf(" series date/time stamps=%d,%d series#=%d\n",dt1,dt2,in1) ; 00280 } 00281 } /* end of series header */ 00282 00283 /***************************************************************** 00284 To actually read image data, do something like this: 00285 int npix=nx*ny , nb=bpp/8 ; 00286 data = malloc(nb*npix) ; 00287 fseek( imfile , skip , SEEK_SET ) ; 00288 fread( data , 1 , nb*npix , imfile ) ; 00289 if( swap ){ switch( nb ){ 00290 case 2: swap_twobytes ( npix , data ); break; 00291 case 4: swap_fourbytes( npix , data ); break; 00292 } } 00293 The latter 2 swap functions are in mri_swapbytes.c 00294 ******************************************************************/ 00295 00296 } else { /*-------------- not a GEMS file --------------*/ 00297 00298 printf("-----------------------------------\n" 00299 "File %s is not a GE image file [doesn't start with IMGF]\n" , 00300 argv[gg] ) ; 00301 } 00302 00303 fclose(imfile) ; /* done with this file */ 00304 00305 } /* end of loop over input files */ 00306 00307 exit(0) ; 00308 } |
|
Swap the 2 bytes pointed to by ppp: ab -> ba. Definition at line 40 of file ge_header.c.
00041 { 00042 unsigned char *pntr = (unsigned char *) ppp ; 00043 unsigned char b0, b1; 00044 00045 b0 = *pntr; b1 = *(pntr+1); 00046 *pntr = b1; *(pntr+1) = b0; 00047 } |
|
Swap the 4 bytes pointed to by ppp: abcd -> dcba. Definition at line 14 of file ge_header.c.
00015 { 00016 unsigned char *pntr = (unsigned char *) ppp ; 00017 unsigned char b0, b1, b2, b3; 00018 00019 b0 = *pntr; b1 = *(pntr+1); b2 = *(pntr+2); b3 = *(pntr+3); 00020 *pntr = b3; *(pntr+1) = b2; *(pntr+2) = b1; *(pntr+3) = b0; 00021 } |
|
Swap the 8 bytes pointed to by ppp: abcdefgh -> hgfedcba. Definition at line 25 of file ge_header.c.
00026 { 00027 unsigned char *pntr = (unsigned char *) ppp ; 00028 unsigned char b0, b1, b2, b3; 00029 unsigned char b4, b5, b6, b7; 00030 00031 b0 = *pntr ; b1 = *(pntr+1); b2 = *(pntr+2); b3 = *(pntr+3); 00032 b4 = *(pntr+4); b5 = *(pntr+5); b6 = *(pntr+6); b7 = *(pntr+7); 00033 00034 *pntr = b7; *(pntr+1) = b6; *(pntr+2) = b5; *(pntr+3) = b4; 00035 *(pntr+4) = b3; *(pntr+5) = b2; *(pntr+6) = b1; *(pntr+7) = b0; 00036 } |