| 1 |
/* {{{ Copyright etc. */ |
| 2 |
|
| 3 |
/**********************************************\ |
| 4 |
|
| 5 |
analyze2minc 0.3 by Stephen Smith |
| 6 |
|
| 7 |
Copyright 1997-1999 Stephen Smith |
| 8 |
|
| 9 |
Some elements are Copyright 1986-1995 |
| 10 |
Biomedical Imaging Resource Mayo Foundation |
| 11 |
|
| 12 |
Oxford Centre for Functional Magnetic Resonance Imaging of the Brain, |
| 13 |
Department of Clinical Neurology, Oxford University, Oxford, UK |
| 14 |
Email: [email protected] |
| 15 |
WWW: http://www.fmrib.ox.ac.uk/~steve |
| 16 |
|
| 17 |
This program should be considered a beta test version and must not |
| 18 |
be used for any clinical purposes. |
| 19 |
|
| 20 |
This code is written using an emacs folding mode, making moving |
| 21 |
around the different sections very easy. This is why there are |
| 22 |
various marks within comments and why comments are indented. |
| 23 |
|
| 24 |
Requires: |
| 25 |
rawtominc (setup the macro RAWTOMINC below) |
| 26 |
|
| 27 |
Compile with: |
| 28 |
cc -o analyze2minc analyze2minc.c |
| 29 |
|
| 30 |
History: |
| 31 |
0.3 January 20 1999 fixed L-R error |
| 32 |
0.2 July 17 1998 added 4D functionality |
| 33 |
0.1 June 1 1998 |
| 34 |
|
| 35 |
\**********************************************/ |
| 36 |
|
| 37 |
/* }}} */ |
| 38 |
|
| 39 |
#define RAWTOMINC "/usr/local/mni/bin/rawtominc" |
| 40 |
|
| 41 |
/* {{{ includes and defines */ |
| 42 |
|
| 43 |
#include <stdlib.h> |
| 44 |
#include <stdio.h> |
| 45 |
#include <string.h> |
| 46 |
#include <math.h> |
| 47 |
#include <sys/file.h> |
| 48 |
#include <malloc.h> |
| 49 |
#include "dbh.h" |
| 50 |
typedef unsigned char uchar; |
| 51 |
|
| 52 |
/* }}} */ |
| 53 |
/* {{{ routines from avw_read */ |
| 54 |
|
| 55 |
/* {{{ swap_long */ |
| 56 |
|
| 57 |
void swap_long(pntr) |
| 58 |
unsigned char *pntr; |
| 59 |
{ |
| 60 |
unsigned char b0, b1, b2, b3; |
| 61 |
|
| 62 |
b0 = *pntr; |
| 63 |
b1 = *(pntr+1); |
| 64 |
b2 = *(pntr+2); |
| 65 |
b3 = *(pntr+3); |
| 66 |
|
| 67 |
*pntr = b3; |
| 68 |
*(pntr+1) = b2; |
| 69 |
*(pntr+2) = b1; |
| 70 |
*(pntr+3) = b0; |
| 71 |
} |
| 72 |
|
| 73 |
/* }}} */ |
| 74 |
/* {{{ swap_short */ |
| 75 |
|
| 76 |
void swap_short(pntr) |
| 77 |
unsigned char *pntr; |
| 78 |
{ |
| 79 |
unsigned char b0, b1; |
| 80 |
|
| 81 |
b0 = *pntr; |
| 82 |
b1 = *(pntr+1); |
| 83 |
|
| 84 |
*pntr = b1; |
| 85 |
*(pntr+1) = b0; |
| 86 |
} |
| 87 |
|
| 88 |
/* }}} */ |
| 89 |
/* {{{ swap_hdr */ |
| 90 |
|
| 91 |
void swap_hdr(pntr) |
| 92 |
struct dsr *pntr; |
| 93 |
{ |
| 94 |
swap_short(&pntr->hist.originator[0]) ; |
| 95 |
swap_short(&pntr->hist.originator[1]) ; |
| 96 |
swap_short(&pntr->hist.originator[2]) ; |
| 97 |
swap_short(&pntr->hist.originator[3]) ; |
| 98 |
swap_short(&pntr->hist.originator[4]) ; |
| 99 |
swap_long(&pntr->hk.sizeof_hdr) ; |
| 100 |
swap_long(&pntr->hk.extents) ; |
| 101 |
swap_short(&pntr->hk.session_error) ; |
| 102 |
swap_short(&pntr->dime.dim[0]) ; |
| 103 |
swap_short(&pntr->dime.dim[1]) ; |
| 104 |
swap_short(&pntr->dime.dim[2]) ; |
| 105 |
swap_short(&pntr->dime.dim[3]) ; |
| 106 |
swap_short(&pntr->dime.dim[4]) ; |
| 107 |
swap_short(&pntr->dime.dim[5]) ; |
| 108 |
swap_short(&pntr->dime.dim[6]) ; |
| 109 |
swap_short(&pntr->dime.dim[7]) ; |
| 110 |
swap_short(&pntr->dime.unused1) ; |
| 111 |
swap_short(&pntr->dime.datatype) ; |
| 112 |
swap_short(&pntr->dime.bitpix) ; |
| 113 |
swap_long(&pntr->dime.pixdim[0]) ; |
| 114 |
swap_long(&pntr->dime.pixdim[1]) ; |
| 115 |
swap_long(&pntr->dime.pixdim[2]) ; |
| 116 |
swap_long(&pntr->dime.pixdim[3]) ; |
| 117 |
swap_long(&pntr->dime.pixdim[4]) ; |
| 118 |
swap_long(&pntr->dime.pixdim[5]) ; |
| 119 |
swap_long(&pntr->dime.pixdim[6]) ; |
| 120 |
swap_long(&pntr->dime.pixdim[7]) ; |
| 121 |
swap_long(&pntr->dime.vox_offset) ; |
| 122 |
swap_long(&pntr->dime.funused1) ; |
| 123 |
swap_long(&pntr->dime.funused2) ; |
| 124 |
swap_long(&pntr->dime.cal_max) ; |
| 125 |
swap_long(&pntr->dime.cal_min) ; |
| 126 |
swap_long(&pntr->dime.compressed) ; |
| 127 |
swap_long(&pntr->dime.verified) ; |
| 128 |
swap_short(&pntr->dime.dim_un0) ; |
| 129 |
swap_long(&pntr->dime.glmax) ; |
| 130 |
swap_long(&pntr->dime.glmin) ; |
| 131 |
} |
| 132 |
|
| 133 |
/* }}} */ |
| 134 |
/* {{{ ShowHdr */ |
| 135 |
|
| 136 |
void ShowHdr(fileName,hdr) |
| 137 |
struct dsr *hdr; |
| 138 |
char *fileName; |
| 139 |
{ |
| 140 |
int i; |
| 141 |
char string[128]; |
| 142 |
printf("Analyze Header Dump of: <%s> \n", fileName); |
| 143 |
/* Header Key */ |
| 144 |
printf("sizeof_hdr: <%d> \n", hdr->hk.sizeof_hdr); |
| 145 |
printf("data_type: <%s> \n", hdr->hk.data_type); |
| 146 |
printf("db_name: <%s> \n", hdr->hk.db_name); |
| 147 |
printf("extents: <%d> \n", hdr->hk.extents); |
| 148 |
printf("session_error: <%d> \n", hdr->hk.session_error); |
| 149 |
printf("regular: <%c> \n", hdr->hk.regular); |
| 150 |
printf("hkey_un0: <%c> \n", hdr->hk.hkey_un0); |
| 151 |
|
| 152 |
/* Image Dimension */ |
| 153 |
for(i=0;i<8;i++) |
| 154 |
printf("dim[%d]: <%d> \n", i, hdr->dime.dim[i]); |
| 155 |
|
| 156 |
strncpy(string,hdr->dime.vox_units,4); |
| 157 |
printf("vox_units: <%s> \n", string); |
| 158 |
|
| 159 |
strncpy(string,hdr->dime.cal_units,8); |
| 160 |
printf("cal_units: <%s> \n", string); |
| 161 |
printf("unused1: <%d> \n", hdr->dime.unused1); |
| 162 |
printf("datatype: <%d> \n", hdr->dime.datatype); |
| 163 |
printf("bitpix: <%d> \n", hdr->dime.bitpix); |
| 164 |
|
| 165 |
for(i=0;i<8;i++) |
| 166 |
printf("pixdim[%d]: <%6.4f> \n",i, hdr->dime.pixdim[i]); |
| 167 |
|
| 168 |
printf("vox_offset: <%6.4> \n", hdr->dime.vox_offset); |
| 169 |
printf("funused1: <%6.4f> \n", hdr->dime.funused1); |
| 170 |
printf("funused2: <%6.4f> \n", hdr->dime.funused2); |
| 171 |
printf("funused3: <%6.4f> \n", hdr->dime.funused3); |
| 172 |
printf("cal_max: <%6.4f> \n", hdr->dime.cal_max); |
| 173 |
printf("cal_min: <%6.4f> \n", hdr->dime.cal_min); |
| 174 |
printf("compressed: <%d> \n", hdr->dime.compressed); |
| 175 |
printf("verified: <%d> \n", hdr->dime.verified); |
| 176 |
printf("glmax: <%d> \n", hdr->dime.glmax); |
| 177 |
printf("glmin: <%d> \n", hdr->dime.glmin); |
| 178 |
|
| 179 |
/* Data History */ |
| 180 |
strncpy(string,hdr->hist.descrip,80); |
| 181 |
printf("descrip: <%s> \n", string); |
| 182 |
strncpy(string,hdr->hist.aux_file,24); |
| 183 |
printf("aux_file: <%s> \n", string); |
| 184 |
printf("orient: <%d> \n", hdr->hist.orient); |
| 185 |
|
| 186 |
strncpy(string,hdr->hist.originator,10); |
| 187 |
printf("originator: <%s> \n", string); |
| 188 |
|
| 189 |
strncpy(string,hdr->hist.generated,10); |
| 190 |
printf("generated: <%s> \n", string); |
| 191 |
|
| 192 |
|
| 193 |
strncpy(string,hdr->hist.scannum,10); |
| 194 |
printf("scannum: <%s> \n", string); |
| 195 |
|
| 196 |
strncpy(string,hdr->hist.patient_id,10); |
| 197 |
printf("patient_id: <%s> \n", string); |
| 198 |
|
| 199 |
strncpy(string,hdr->hist.exp_date,10); |
| 200 |
printf("exp_date: <%s> \n", string); |
| 201 |
|
| 202 |
strncpy(string,hdr->hist.exp_time,10); |
| 203 |
printf("exp_time: <%s> \n", string); |
| 204 |
|
| 205 |
strncpy(string,hdr->hist.hist_un0,10); |
| 206 |
printf("hist_un0: <%s> \n", string); |
| 207 |
|
| 208 |
printf("views: <%d> \n", hdr->hist.views); |
| 209 |
printf("vols_added: <%d> \n", hdr->hist.vols_added); |
| 210 |
printf("start_field:<%d> \n", hdr->hist.start_field); |
| 211 |
printf("field_skip: <%d> \n", hdr->hist.field_skip); |
| 212 |
printf("omax: <%d> \n", hdr->hist.omax); |
| 213 |
printf("omin: <%d> \n", hdr->hist.omin); |
| 214 |
printf("smin: <%d> \n", hdr->hist.smax); |
| 215 |
printf("smin: <%d> \n", hdr->hist.smin); |
| 216 |
|
| 217 |
} |
| 218 |
|
| 219 |
/* }}} */ |
| 220 |
|
| 221 |
/* }}} */ |
| 222 |
|
| 223 |
main(argc,argv) |
| 224 |
int argc; |
| 225 |
char *argv[]; |
| 226 |
{ |
| 227 |
/* {{{ vars */ |
| 228 |
|
| 229 |
FILE *fd, *fd2; |
| 230 |
struct dsr hdr; |
| 231 |
int int_a, size, rev=0, volumes=1, swap=0, bytepix, i, x, y, z, revxdata=1; |
| 232 |
double cmax, cmin; |
| 233 |
short datatype, short_a, short_array_a[1000], x_dim_short, y_dim_short, z_dim_short; |
| 234 |
uchar uchar_a, *buffera, *bufferb; |
| 235 |
float float_a, float_array_a[1000], x_pix_dim_float, y_pix_dim_float, z_pix_dim_float, x_origin_float, y_origin_float, z_origin_float; |
| 236 |
char data_file[1000], string_a[1000], string_a2[1000], string_array_a[100][1000]; |
| 237 |
static char type_string[100]; |
| 238 |
static int DataTypeSizes[32] = {0,1,8,0,16,0,0,0,32,0,0,0,0,0,0,0,32}; |
| 239 |
|
| 240 |
/* }}} */ |
| 241 |
|
| 242 |
/* {{{ COMMENT usage */ |
| 243 |
|
| 244 |
#ifdef FoldingComment |
| 245 |
|
| 246 |
if (argc==1) |
| 247 |
{ |
| 248 |
printf("Usage: analyze2minc <analyze_input_root> [-rev]\n"); |
| 249 |
printf("E.g.: if input file is image.hdr and image.img, use \"analyze2minc image\"\n"); |
| 250 |
printf("(-rev causes reversal in the x direction.)\n"); |
| 251 |
exit(0); |
| 252 |
} |
| 253 |
|
| 254 |
if (argc==3) |
| 255 |
rev=1; |
| 256 |
|
| 257 |
#endif |
| 258 |
|
| 259 |
/* }}} */ |
| 260 |
/* {{{ usage */ |
| 261 |
|
| 262 |
if (argc==1) |
| 263 |
{ |
| 264 |
printf("Usage: analyze2minc <analyze_input_root>\n"); |
| 265 |
printf("E.g.: if input file is image.hdr and image.img, use \"analyze2minc image\"\n"); |
| 266 |
exit(0); |
| 267 |
} |
| 268 |
|
| 269 |
/* }}} */ |
| 270 |
/* {{{ open input file and read header */ |
| 271 |
|
| 272 |
sprintf(string_a,"%s.hdr",argv[1]); |
| 273 |
|
| 274 |
if((fd=fopen(string_a,"r"))==NULL) |
| 275 |
{ |
| 276 |
fprintf(stderr,"Can't open:<%s>\n", argv[1]); |
| 277 |
exit(0); |
| 278 |
} |
| 279 |
|
| 280 |
fread(&hdr,1,sizeof(struct dsr),fd); |
| 281 |
fclose(fd); |
| 282 |
|
| 283 |
if(hdr.dime.dim[0] < 0 || hdr.dime.dim[0] > 15) |
| 284 |
{ |
| 285 |
printf("Byte swapping\n"); |
| 286 |
swap=1; |
| 287 |
swap_hdr(&hdr); |
| 288 |
} |
| 289 |
|
| 290 |
/* }}} */ |
| 291 |
/* {{{ read image_dimension */ |
| 292 |
|
| 293 |
x_dim_short=hdr.dime.dim[1]; |
| 294 |
y_dim_short=hdr.dime.dim[2]; |
| 295 |
z_dim_short=hdr.dime.dim[3]; |
| 296 |
volumes=hdr.dime.dim[4]; |
| 297 |
|
| 298 |
x_pix_dim_float=hdr.dime.pixdim[1]; |
| 299 |
if (x_pix_dim_float<0) |
| 300 |
{ |
| 301 |
x_pix_dim_float = -x_pix_dim_float; |
| 302 |
/* revxdata=0;*/ /* don't need this */ |
| 303 |
} |
| 304 |
y_pix_dim_float=hdr.dime.pixdim[2]; |
| 305 |
z_pix_dim_float=hdr.dime.pixdim[3]; |
| 306 |
|
| 307 |
datatype=hdr.dime.datatype; |
| 308 |
sprintf(type_string,""); |
| 309 |
if (datatype==DT_UNSIGNED_CHAR) sprintf(type_string,"-byte "); |
| 310 |
if (datatype==DT_SIGNED_SHORT) sprintf(type_string,"-short -signed "); |
| 311 |
if (datatype==DT_SIGNED_INT) sprintf(type_string,"-long -signed "); |
| 312 |
if (datatype==DT_FLOAT) sprintf(type_string,"-float "); |
| 313 |
if (datatype==DT_DOUBLE) sprintf(type_string,"-double "); |
| 314 |
|
| 315 |
bytepix=DataTypeSizes[datatype]/8; |
| 316 |
|
| 317 |
buffera = (uchar*)malloc(volumes*x_dim_short*y_dim_short*z_dim_short*bytepix); |
| 318 |
bufferb = (uchar*)malloc(volumes*x_dim_short*y_dim_short*z_dim_short*bytepix); |
| 319 |
|
| 320 |
/* }}} */ |
| 321 |
/* {{{ read data_history */ |
| 322 |
|
| 323 |
/* origin */ |
| 324 |
memcpy(short_array_a,hdr.hist.originator,5*sizeof(short)); |
| 325 |
x_origin_float=((float)-short_array_a[0])*x_pix_dim_float; |
| 326 |
y_origin_float=((float)-short_array_a[1])*y_pix_dim_float; |
| 327 |
z_origin_float=((float)-short_array_a[2])*z_pix_dim_float; |
| 328 |
|
| 329 |
/* }}} */ |
| 330 |
/* {{{ debug prints */ |
| 331 |
|
| 332 |
printf("%d %d %d\n",x_dim_short,y_dim_short,z_dim_short); |
| 333 |
printf("%f %f %f\n",x_pix_dim_float,y_pix_dim_float,z_pix_dim_float); |
| 334 |
printf("%f %f %f\n",x_origin_float,y_origin_float,z_origin_float); |
| 335 |
|
| 336 |
/* }}} */ |
| 337 |
/* {{{ reverse x */ |
| 338 |
|
| 339 |
if (revxdata) |
| 340 |
{ |
| 341 |
sprintf(string_a,"%s.img",argv[1]); |
| 342 |
fd=fopen(string_a,"r"); |
| 343 |
fread(buffera,bytepix,volumes*x_dim_short*y_dim_short*z_dim_short,fd); |
| 344 |
fclose(fd); |
| 345 |
|
| 346 |
for(i=0; i<volumes; i++) |
| 347 |
for(z=0; z<z_dim_short; z++) |
| 348 |
for(y=0; y<y_dim_short; y++) |
| 349 |
for(x=0; x<x_dim_short; x++) |
| 350 |
memcpy(bufferb+( ( i*z_dim_short*y_dim_short*x_dim_short + |
| 351 |
z*y_dim_short*x_dim_short + |
| 352 |
y*x_dim_short + |
| 353 |
x) * bytepix ), |
| 354 |
buffera +( ( i*z_dim_short*y_dim_short*x_dim_short + |
| 355 |
z*y_dim_short*x_dim_short + |
| 356 |
y*x_dim_short + |
| 357 |
(x_dim_short-x-1) ) * bytepix ), |
| 358 |
bytepix); |
| 359 |
|
| 360 |
sprintf(data_file,"%s.tmp.img",argv[1]); |
| 361 |
fd=fopen(data_file,"w"); |
| 362 |
fwrite(bufferb,bytepix,volumes*x_dim_short*y_dim_short*z_dim_short,fd); |
| 363 |
fclose(fd); |
| 364 |
} |
| 365 |
else |
| 366 |
sprintf(data_file,"%s.img",argv[1]); |
| 367 |
|
| 368 |
/* }}} */ |
| 369 |
/* {{{ swap bytes on image files if necessary */ |
| 370 |
|
| 371 |
if ( swap ) |
| 372 |
{ |
| 373 |
if((fd=fopen(data_file,"r"))==NULL) |
| 374 |
{ |
| 375 |
fprintf(stderr,"Can't open:<%s>\n", argv[1]); |
| 376 |
exit(0); |
| 377 |
} |
| 378 |
|
| 379 |
sprintf(string_a,"%s.tmp.tmp.img",argv[1]); |
| 380 |
if((fd2=fopen(string_a,"w"))==NULL) |
| 381 |
{ |
| 382 |
fprintf(stderr,"Can't open:<%s> for writing\n", argv[1]); |
| 383 |
exit(0); |
| 384 |
} |
| 385 |
|
| 386 |
for(i=0; i<z_dim_short*y_dim_short*x_dim_short; i++) |
| 387 |
{ |
| 388 |
fread(buffera,bytepix,1,fd); |
| 389 |
|
| 390 |
if (bytepix==2) |
| 391 |
swap_short(buffera); |
| 392 |
|
| 393 |
if (bytepix==4) |
| 394 |
swap_long(buffera); |
| 395 |
|
| 396 |
fwrite(buffera,bytepix,1,fd2); |
| 397 |
} |
| 398 |
|
| 399 |
fclose(fd); |
| 400 |
fclose(fd2); |
| 401 |
|
| 402 |
sprintf(string_a,"/bin/mv %s.tmp.tmp.img %s.tmp.img",argv[1],argv[1]); |
| 403 |
system(string_a); |
| 404 |
} |
| 405 |
|
| 406 |
/* }}} */ |
| 407 |
/* {{{ write minc file */ |
| 408 |
|
| 409 |
if ( rev == 1 ) |
| 410 |
{ |
| 411 |
printf("Reversing x\n"); |
| 412 |
x_origin_float = x_origin_float+(x_pix_dim_float*((float)x_dim_short)); |
| 413 |
x_pix_dim_float = -x_pix_dim_float; |
| 414 |
} |
| 415 |
|
| 416 |
/* several MNI programs won't work if volumes is set to 1 rather than not set at all */ |
| 417 |
if ( volumes > 1 ) |
| 418 |
sprintf(string_a2,"%d",volumes); |
| 419 |
else |
| 420 |
sprintf(string_a2,""); |
| 421 |
|
| 422 |
sprintf(string_a, |
| 423 |
"%s %s -transverse -scan_range -input %s -xstep %f -ystep %f -zstep %f -xstart %f -ystart %f -zstart %f %s.mnc %s %d %d %d", |
| 424 |
RAWTOMINC, type_string, data_file, |
| 425 |
x_pix_dim_float,y_pix_dim_float,z_pix_dim_float, |
| 426 |
x_origin_float,y_origin_float,z_origin_float, |
| 427 |
argv[1], |
| 428 |
string_a2,z_dim_short,y_dim_short,x_dim_short); |
| 429 |
|
| 430 |
system(string_a); |
| 431 |
|
| 432 |
/* }}} */ |
| 433 |
/* {{{ remove temp data file */ |
| 434 |
|
| 435 |
if (revxdata) |
| 436 |
{ |
| 437 |
sprintf(string_a,"/bin/rm %s",data_file); |
| 438 |
system(string_a); |
| 439 |
} |
| 440 |
|
| 441 |
/* }}} */ |
| 442 |
} |