39 int verbose=0;
if(status!=NULL) verbose=status->
verbose;
40 if(verbose>0) {printf(
"%s(%s, ...)\n", __func__, filename); fflush(stdout);}
44 if(hdrfile!=NULL) hdrfile[0]=(char)0;
45 if(imgfile!=NULL) imgfile[0]=(char)0;
46 if(siffile!=NULL) siffile[0]=(char)0;
55 char basefile[FILENAME_MAX];
strlcpy(basefile, filename, FILENAME_MAX);
58 if(verbose>1) printf(
" basefile := %s\n", basefile);
62 char temp[FILENAME_MAX+10], localhdrfile[FILENAME_MAX];
63 localhdrfile[0]=(char)0;
64 strcpy(temp, basefile); strcat(temp,
".nii");
66 if(verbose>1) printf(
" %s exists.\n", temp);
68 strcpy(localhdrfile, temp);
69 if(hdrfile!=NULL)
strlcpy(hdrfile, temp, FILENAME_MAX);
70 if(imgfile!=NULL)
strlcpy(imgfile, temp, FILENAME_MAX);
73 if(verbose>1) printf(
" %s does not exist.\n", temp);
75 strcpy(temp, basefile); strcat(temp,
".hdr");
77 strcpy(temp, basefile); strcat(temp,
".img.hdr");
79 if(verbose>1) printf(
" hdr file not found.\n");
85 strlcpy(localhdrfile, temp, FILENAME_MAX);
86 if(hdrfile!=NULL)
strlcpy(hdrfile, temp, FILENAME_MAX);
87 if(verbose>1) printf(
" %s is found.\n", localhdrfile);
89 strcpy(temp, basefile); strcat(temp,
".img");
91 if(verbose>1) printf(
" %s not found.\n", temp);
96 if(imgfile!=NULL)
strlcpy(imgfile, temp, FILENAME_MAX);
97 if(verbose>1) printf(
" %s is found.\n", temp);
99 if(verbose>1) {printf(
" combined := %d\n", combined); fflush(stdout);}
103 NIFTI_DSR *dsr, local_dsr;
if(header==NULL) dsr=&local_dsr;
else dsr=header;
107 printf(
" %s was not identified as NIfTI header file.\n", localhdrfile); fflush(stdout);}
108 statusSet(status, __func__, __FILE__, __LINE__, ret);
111 if(verbose>1) {printf(
" %s is identified as NIfTI.\n", localhdrfile); fflush(stdout);}
115 strcpy(temp, basefile); strcat(temp,
".sif");
116 if(verbose>3) printf(
" checking if %s exists\n", temp);
118 strcpy(temp, basefile); strcat(temp,
".img.sif");
119 if(verbose>3) printf(
" checking if %s exists\n", temp);
121 strcpy(temp, basefile); strcat(temp,
".nii.sif");
122 if(verbose>3) printf(
" checking if %s exists\n", temp);
124 if(verbose>0) printf(
" SIF not found or accessible.\n");
131 if(siffile!=NULL) strcpy(siffile, temp);
132 if(verbose>1) {printf(
" %s is found.\n", temp); fflush(stdout);}
447 const char *filename,
453 if(verbose>0) {printf(
"%s(%s, dsr, %d)\n", __func__, filename, verbose); fflush(stdout);}
461 if(strncmp(dsr->
h1.
magic,
"ni1", 3)!=0 && strncmp(dsr->
h1.
magic,
"n+1", 3)!=0)
463 }
else if(dsr->
n==2) {
464 if(strncmp(dsr->
h2.
magic,
"ni2", 3)!=0 && strncmp(dsr->
h2.
magic,
"n+2", 3)!=0)
472 if(little) printf(
"little endian platform\n");
else printf(
"big endian platform\n");
480 unsigned char buf[hdrSize];
481 memset(buf, 0,
sizeof(hdrSize));
484 if(verbose>2) printf(
" setting write buffer\n");
486 unsigned char *bptr=buf+0; memcpy(bptr, &dsr->
h1.
sizeof_hdr, 4);
if(!same_order)
swawbip(bptr, 4);
488 bptr=buf+14; memcpy(bptr, &dsr->
h1.
db_name, 18);
489 bptr=buf+32; memcpy(bptr, &dsr->
h1.
extents, 4);
if(!same_order)
swawbip(bptr, 4);
491 bptr=buf+38; memcpy(bptr, &dsr->
h1.
regular, 1);
492 bptr=buf+39; memcpy(bptr, &dsr->
h1.
dim_info, 1);
494 bptr=buf+40; memcpy(bptr, dsr->
h1.
dim, 16);
if(!same_order)
swabip(bptr, 16);
499 bptr=buf+70; memcpy(bptr, &dsr->
h1.
datatype, 2);
if(!same_order)
swabip(bptr, 2);
500 bptr=buf+72; memcpy(bptr, &dsr->
h1.
bitpix, 2);
if(!same_order)
swabip(bptr, 2);
502 bptr=buf+76; memcpy(bptr, dsr->
h1.
pixdim, 32);
if(!same_order)
swawbip(bptr, 32);
506 bptr=buf+120; memcpy(bptr, &dsr->
h1.
slice_end, 2);
if(!same_order)
swabip(bptr, 2);
509 bptr=buf+124; memcpy(bptr, &dsr->
h1.
cal_max, 4);
if(!same_order)
swawbip(bptr, 4);
510 bptr=buf+128; memcpy(bptr, &dsr->
h1.
cal_min, 4);
if(!same_order)
swawbip(bptr, 4);
512 bptr=buf+136; memcpy(bptr, &dsr->
h1.
toffset, 4);
if(!same_order)
swawbip(bptr, 4);
513 bptr=buf+140; memcpy(bptr, &dsr->
h1.
glmax, 4);
if(!same_order)
swawbip(bptr, 4);
514 bptr=buf+144; memcpy(bptr, &dsr->
h1.
glmin, 4);
if(!same_order)
swawbip(bptr, 4);
516 bptr=buf+148; memcpy(bptr, dsr->
h1.
descrip, 80);
517 bptr=buf+228; memcpy(bptr, dsr->
h1.
aux_file, 24);
526 bptr=buf+280; memcpy(bptr, dsr->
h1.
srow_x, 16);
if(!same_order)
swawbip(bptr, 16);
527 bptr=buf+296; memcpy(bptr, dsr->
h1.
srow_y, 16);
if(!same_order)
swawbip(bptr, 16);
528 bptr=buf+312; memcpy(bptr, dsr->
h1.
srow_z, 16);
if(!same_order)
swawbip(bptr, 16);
530 bptr=buf+344; memcpy(bptr, dsr->
h1.
magic, 4);
534 unsigned char *bptr=buf+0; memcpy(bptr, &dsr->
h2.
sizeof_hdr, 4);
if(!same_order)
swap32ip(bptr, 1);
535 bptr=buf+4; memcpy(bptr, &dsr->
h2.
magic, 8);
537 bptr=buf+14; memcpy(bptr, &dsr->
h2.
bitpix, 2);
if(!same_order)
swap16ip(bptr, 1);
538 bptr=buf+16; memcpy(bptr, &dsr->
h2.
dim, 64);
if(!same_order)
swap64ip(bptr, 8);
542 bptr=buf+104; memcpy(bptr, &dsr->
h2.
pixdim, 64);
if(!same_order)
swap64ip(bptr, 8);
546 bptr=buf+192; memcpy(bptr, &dsr->
h2.
cal_max, 8);
if(!same_order)
swap64ip(bptr, 1);
547 bptr=buf+200; memcpy(bptr, &dsr->
h2.
cal_min, 8);
if(!same_order)
swap64ip(bptr, 1);
549 bptr=buf+216; memcpy(bptr, &dsr->
h2.
toffset, 8);
if(!same_order)
swap64ip(bptr, 1);
552 bptr=buf+240; memcpy(bptr, dsr->
h2.
descrip, 80);
553 bptr=buf+320; memcpy(bptr, dsr->
h2.
aux_file, 24);
562 bptr=buf+400; memcpy(bptr, dsr->
h2.
srow_x, 8);
if(!same_order)
swap64ip(bptr, 1);
563 bptr+=8; memcpy(bptr, dsr->
h2.
srow_x+1, 8);
if(!same_order)
swap64ip(bptr, 1);
564 bptr+=8; memcpy(bptr, dsr->
h2.
srow_x+2, 8);
if(!same_order)
swap64ip(bptr, 1);
565 bptr+=8; memcpy(bptr, dsr->
h2.
srow_x+3, 8);
if(!same_order)
swap64ip(bptr, 1);
566 bptr=buf+432; memcpy(bptr, dsr->
h2.
srow_y, 8);
if(!same_order)
swap64ip(bptr, 1);
567 bptr+=8; memcpy(bptr, dsr->
h2.
srow_y+1, 8);
if(!same_order)
swap64ip(bptr, 1);
568 bptr+=8; memcpy(bptr, dsr->
h2.
srow_y+2, 8);
if(!same_order)
swap64ip(bptr, 1);
569 bptr+=8; memcpy(bptr, dsr->
h2.
srow_y+3, 8);
if(!same_order)
swap64ip(bptr, 1);
570 bptr=buf+464; memcpy(bptr, dsr->
h2.
srow_z, 8);
if(!same_order)
swap64ip(bptr, 1);
571 bptr+=8; memcpy(bptr, dsr->
h2.
srow_z+1, 8);
if(!same_order)
swap64ip(bptr, 1);
572 bptr+=8; memcpy(bptr, dsr->
h2.
srow_z+2, 8);
if(!same_order)
swap64ip(bptr, 1);
573 bptr+=8; memcpy(bptr, dsr->
h2.
srow_z+3, 8);
if(!same_order)
swap64ip(bptr, 1);
578 bptr=buf+524; memcpy(bptr, &dsr->
h2.
dim_info, 1);
585 if(verbose>2) printf(
" opening file for write in binary mode\n");
587 (strncmp(dsr->
h1.
magic,
"n+1", 3)==0 || strncmp(dsr->
h1.
magic,
"n+2", 3)==0) ) {
589 fp=fopen(filename,
"r+b");
592 fp=fopen(filename,
"wb");
597 if(verbose>2) printf(
" writing NIfTI header\n");
598 if(fwrite(buf, 1, hdrSize, fp) != hdrSize) {
603 if(verbose>2) printf(
" writing NIfTI extender\n");
611 if(verbose>1) printf(
" NIfTI header written.\n");