65 char basename[FILENAME_MAX];
67 if(hdrfile!=NULL) strcpy(hdrfile,
"");
68 if(imgfile!=NULL) strcpy(imgfile,
"");
69 if(siffile!=NULL) strcpy(siffile,
"");
70 n=strlen(filename);
if(n<1 || n>=FILENAME_MAX) {
return(1);}
71 strlcpy(basename, filename, FILENAME_MAX);
76 if(hdrfile!=NULL) snprintf(hdrfile, FILENAME_MAX,
"%s.hdr", basename);
77 if(imgfile!=NULL) snprintf(imgfile, FILENAME_MAX,
"%s.img", basename);
79 if(hdrfile!=NULL) snprintf(hdrfile, FILENAME_MAX,
"%s.nii", basename);
80 if(imgfile!=NULL) snprintf(imgfile, FILENAME_MAX,
"%s.nii", basename);
83 if(siffile!=NULL) snprintf(siffile, FILENAME_MAX,
"%s.sif", basename);
110 printf(
"niftiRemove(%s, %d, ...)\n", dbname, fileformat);
114 char imgfile[FILENAME_MAX], hdrfile[FILENAME_MAX], siffile[FILENAME_MAX];
118 if(access(hdrfile, 0)!=-1) {
119 if(verbose>1) {printf(
" removing %s\n", hdrfile); fflush(stdout);}
120 if(remove(hdrfile)!=0) errNr++;
122 if(access(imgfile, 0)!=-1) {
123 if(verbose>1) {printf(
" removing %s\n", imgfile); fflush(stdout);}
124 if(remove(imgfile)!=0) errNr++;
127 if(access(imgfile, 0)!=-1) {
128 if(verbose>1) {printf(
" removing %s\n", imgfile); fflush(stdout);}
129 if(remove(imgfile)!=0) errNr++;
134 if(access(hdrfile, 0)!=-1) {
135 if(verbose>1) {printf(
" removing %s\n", hdrfile); fflush(stdout);}
136 if(remove(hdrfile)!=0) errNr++;
138 if(access(imgfile, 0)!=-1) {
139 if(verbose>1) {printf(
" removing %s\n", imgfile); fflush(stdout);}
140 if(remove(imgfile)!=0) errNr++;
145 if(access(imgfile, 0)!=-1) {
146 if(verbose>1) {printf(
" removing %s\n", imgfile); fflush(stdout);}
147 if(remove(imgfile)!=0) errNr++;
163 const char *filename,
186 char basefile[FILENAME_MAX], temp[FILENAME_MAX], localhdrfile[FILENAME_MAX];
190 if(filename==NULL || strlen(filename)==0)
return(0);
191 if(verbose>0) {printf(
"\nniftiExists(%s, ...)\n", filename); fflush(stdout);}
192 if(status!=NULL) strcpy(status,
"OK");
193 if(header==NULL) dsr=&local_dsr;
else dsr=header;
195 localhdrfile[0]=(char)0;
198 strlcpy(basefile, filename, FILENAME_MAX);
200 if(verbose>1) printf(
"\n basefile := %s\n", basefile);
203 strcpy(temp, basefile); strcat(temp,
".nii");
204 if(access(temp, 0) == -1) {
205 if(verbose>0) printf(
" %s not found or accessible.\n", temp);
208 strcpy(localhdrfile, temp);
209 if(hdrfile!=NULL) strcpy(hdrfile, temp);
210 if(imgfile!=NULL) strcpy(imgfile, temp);
212 if(verbose>1) printf(
" %s is accessible.\n", temp);
217 strcpy(temp, basefile); strcat(temp,
".hdr");
218 if(access(temp, 0) == -1) {
219 strcpy(temp, basefile); strcat(temp,
".img.hdr");
220 if(access(temp, 0) == -1) {
221 if(verbose>0) printf(
" hdr file not found or accessible.\n");
222 if(status!=NULL) strcpy(status,
"file not accessible");
227 strcpy(localhdrfile, temp);
228 if(hdrfile!=NULL) strcpy(hdrfile, temp);
229 if(verbose>1) printf(
" %s is accessible.\n", temp);
234 strcpy(temp, basefile); strcat(temp,
".img");
235 if(access(temp, 0) == -1) {
236 if(verbose>0) printf(
" %s not found or accessible.\n", temp);
237 if(status!=NULL) strcpy(status,
"file not accessible");
241 if(imgfile!=NULL) strcpy(imgfile, temp);
242 if(verbose>1) printf(
" %s is accessible.\n", temp);
247 if(status!=NULL) strcpy(status,
"file is not Nifti");
249 printf(
" %s was not identified as Nifti header file (%d).\n", localhdrfile, ret);
250 printf(
" %s\n", temp);
254 if(verbose>1) printf(
" %s is identified as Nifti.\n", localhdrfile);
258 strcpy(temp, basefile); strcat(temp,
".sif");
259 if(verbose>3) printf(
" checking if %s exists\n", temp);
260 if(access(temp, 0) == -1) {
261 strcpy(temp, basefile); strcat(temp,
".img.sif");
262 if(verbose>3) printf(
" checking if %s exists\n", temp);
263 if(access(temp, 0) == -1) {
264 strcpy(temp, basefile); strcat(temp,
".nii.sif");
265 if(verbose>3) printf(
" checking if %s exists\n", temp);
266 if(access(temp, 0) == -1) {
267 if(verbose>0) printf(
"\n SIF not found or accessible.\n");
268 if(siffile!=NULL) strcpy(siffile,
"");
270 if(combined) strcpy(status,
"combined Nifti file is accessible");
271 else strcpy(status,
"Nifti files are accessible");
278 if(siffile!=NULL) strcpy(siffile, temp);
279 if(verbose>1) printf(
" %s is accessible.\n", temp);
281 if(combined) strcpy(status,
"combined Nifti file and SIF are accessible");
282 else strcpy(status,
"Nifti files and SIF are accessible");
306 int same_order, extender=0;
311 if(filename==NULL || strlen(filename)==0 || dsr==NULL)
return(1);
313 printf(
"\nniftiReadHeader(%s, ...)\n", filename); fflush(stdout);}
314 if(status!=NULL) strcpy(status,
"OK");
315 little=
little_endian();
if(verbose>3) printf(
" little := %d\n", little);
318 fp=fopen(filename,
"rb");
if(fp==NULL) {
319 if(status!=NULL) strcpy(status,
"cannot open file");
320 if(verbose>0) fprintf(stderr,
"Error: cannot open file %s\n", filename);
326 if(status!=NULL) strcpy(status,
"complete Nifti header not found");
328 fprintf(stderr,
"Error: invalid Nifti header file %s\n", filename);
332 for(
int n=0; n<4; n++) dsr->
e.
extension[n]=(
char)0;
334 if(status!=NULL) strcpy(status,
"complete Nifti header not found");
336 fprintf(stdout,
"Nifti header extender not found in %s\n", filename);
345 memcpy(dsr->
h.
magic, buf+344, 4);
346 if(strcasecmp(dsr->
h.
magic,
"ni1")==0) {
347 if(verbose>1) {printf(
" separate hdr and img files.\n"); fflush(stdout);}
348 }
else if(strcasecmp(dsr->
h.
magic,
"n+1")==0) {
349 if(verbose>1) printf(
" combined hdr and img data.\n");
351 if(status!=NULL) strcpy(status,
"Nifti magic number not found");
353 fprintf(stderr,
"Error: not a Nifti header file %s\n", filename);
357 printf(
"magic := {%d, %d, %d, %d}\n", dsr->
h.
magic[0], dsr->
h.
magic[1],
362 if(verbose>1) printf(
" Nifti Magic number := %s\n", dsr->
h.
magic);
364 if(strcasecmp(dsr->
h.
magic,
"n+1")==0 && extender==0) {
365 if(status!=NULL) strcpy(status,
"Nifti header extender not found");
367 fprintf(stderr,
"Error: not valid Nifti n+1 header file %s\n", filename);
374 memcpy(&s, buf+40, 2);
if(verbose>10) printf(
" s := %d\n", s);
379 swabip(&s, 2);
if(verbose>10) printf(
" s := %d\n", s);
384 if(status!=NULL) strcpy(status,
"invalid Nifti byte order");
386 fprintf(stderr,
"Error: not a valid Nifti header file %s\n", filename);
390 if(verbose>1) printf(
" Nifti byte order := %d\n", dsr->
byte_order);
395 memcpy(&n, buf+0, 4);
if(!same_order)
swawbip(&n, 4);
397 if(status!=NULL) strcpy(status,
"invalid Nifti sizeof_hdr");
399 fprintf(stderr,
"Error: not a valid Nifti header file %s\n", filename);
407 memcpy(&dsr->
h.
db_name, buf+14, 18);
408 if(!same_order)
swawbip(buf+32, 4);
410 if(!same_order)
swabip(buf+36, 2);
416 if(!same_order)
swabip(buf+40, 16);
417 memcpy(dsr->
h.
dim, buf+40, 16);
419 if(!same_order)
swawbip(buf+56, 4);
421 if(!same_order)
swawbip(buf+60, 4);
423 if(!same_order)
swawbip(buf+64, 4);
425 if(!same_order)
swabip(buf+68, 2);
429 if(!same_order)
swabip(buf+70, 2);
431 if(!same_order)
swabip(buf+72, 2);
432 memcpy(&dsr->
h.
bitpix, buf+72, 2);
433 if(!same_order)
swabip(buf+74, 2);
435 if(!same_order)
swawbip(buf+76, 32);
436 memcpy(dsr->
h.
pixdim, buf+76, 32);
437 if(!same_order)
swawbip(buf+108, 4);
439 if(!same_order)
swawbip(buf+112, 4);
441 if(!same_order)
swawbip(buf+116, 4);
443 if(!same_order)
swabip(buf+120, 2);
447 if(!same_order)
swawbip(buf+124, 4);
448 memcpy(&dsr->
h.
cal_max, buf+124, 4);
449 if(!same_order)
swawbip(buf+128, 4);
450 memcpy(&dsr->
h.
cal_min, buf+128, 4);
451 if(!same_order)
swawbip(buf+132, 4);
453 if(!same_order)
swawbip(buf+136, 4);
454 memcpy(&dsr->
h.
toffset, buf+136, 4);
455 if(!same_order)
swawbip(buf+140, 4);
456 memcpy(&dsr->
h.
glmax, buf+140, 4);
457 if(!same_order)
swawbip(buf+144, 4);
458 memcpy(&dsr->
h.
glmin, buf+144, 4);
461 memcpy(&dsr->
h.
descrip, buf+148, 80);
466 if(!same_order)
swabip(buf+252, 2);
468 if(!same_order)
swabip(buf+254, 2);
470 if(!same_order)
swawbip(buf+256, 4);
472 if(!same_order)
swawbip(buf+260, 4);
474 if(!same_order)
swawbip(buf+264, 4);
476 if(!same_order)
swawbip(buf+268, 4);
478 if(!same_order)
swawbip(buf+272, 4);
480 if(!same_order)
swawbip(buf+276, 4);
482 if(!same_order)
swawbip(buf+280, 16);
483 memcpy(dsr->
h.
srow_x, buf+280, 16);
484 if(!same_order)
swawbip(buf+296, 16);
485 memcpy(dsr->
h.
srow_y, buf+296, 16);
486 if(!same_order)
swawbip(buf+312, 16);
487 memcpy(dsr->
h.
srow_z, buf+312, 16);
491 if(status!=NULL) strcpy(status,
"complete Nifti header was read");
492 if(verbose>0) fflush(stdout);
508 char *cptr, tmp[256];
511 if(fp==NULL || dsr==NULL)
return(1);
513 fprintf(fp,
"Nifti header:\n");
516 if(dsr->
byte_order==0) strcpy(tmp,
"big");
else strcpy(tmp,
"little");
517 fprintf(fp,
"byte_order := %s endian\n", tmp);
519 fprintf(fp,
"sizeof_hdr := %d\n", dsr->
h.
sizeof_hdr);
521 strncpy(tmp, dsr->
h.
data_type, 10); tmp[10]=(char)0;
522 cptr=tmp;
while(*cptr) {
if(!isprint(cptr[0])) cptr[0]=
' '; cptr++;}
523 fprintf(fp,
"data_type := %s\n", tmp);
525 strncpy(tmp, dsr->
h.
db_name, 18); tmp[18]=(char)0;
526 cptr=tmp;
while(*cptr) {
if(!isprint(*cptr)) *cptr=
' '; cptr++;}
527 fprintf(fp,
"db_name := %s\n", tmp);
529 fprintf(fp,
"extents := %d\n", dsr->
h.
extents);
533 fprintf(fp,
"regular := %d\n", dsr->
h.
regular);
535 fprintf(fp,
"dim_info := %d\n", dsr->
h.
dim_info);
538 i=0; fprintf(fp,
"dim := {%d", dsr->
h.
dim[i]);
539 for(i=1; i<8; i++) fprintf(fp,
", %d", dsr->
h.
dim[i]);
543 fprintf(fp,
"intent_p1 := %g\n", dsr->
h.
intent_p1);
544 fprintf(fp,
"intent_p2 := %g\n", dsr->
h.
intent_p2);
545 fprintf(fp,
"intent_p3 := %g\n", dsr->
h.
intent_p3);
549 fprintf(fp,
"datatype := %d\n", dsr->
h.
datatype);
550 fprintf(fp,
"bitpix := %d\n", dsr->
h.
bitpix);
552 i=0; fprintf(fp,
"pixdim := {%g", dsr->
h.
pixdim[i]);
553 for(i=1; i<8; i++) fprintf(fp,
", %g", dsr->
h.
pixdim[i]);
555 fprintf(fp,
"vox_offset := %g\n", dsr->
h.
vox_offset);
556 fprintf(fp,
"scl_slope := %g\n", dsr->
h.
scl_slope);
557 fprintf(fp,
"scl_inter := %g\n", dsr->
h.
scl_inter);
558 fprintf(fp,
"slice_end := %d\n", dsr->
h.
slice_end);
559 fprintf(fp,
"slice_code := %d\n", dsr->
h.
slice_code);
560 fprintf(fp,
"xyzt_units := %d\n", dsr->
h.
xyzt_units);
561 fprintf(fp,
"cal_max := %g\n", dsr->
h.
cal_max);
562 fprintf(fp,
"cal_min := %g\n", dsr->
h.
cal_min);
564 fprintf(fp,
"toffset := %g\n", dsr->
h.
toffset);
565 fprintf(fp,
"glmax := %d\n", dsr->
h.
glmax);
566 fprintf(fp,
"glmin := %d\n", dsr->
h.
glmin);
569 strncpy(tmp, dsr->
h.
descrip, 80); tmp[80]=(char)0;
570 cptr=tmp;
while(*cptr) {
if(!isprint(*cptr)) *cptr=
' '; cptr++;}
571 fprintf(fp,
"descrip := %s\n", tmp);
572 strncpy(tmp, dsr->
h.
aux_file, 24); tmp[24]=(char)0;
573 cptr=tmp;
while(*cptr) {
if(!isprint(*cptr)) *cptr=
' '; cptr++;}
574 fprintf(fp,
"aux_file := %s\n", tmp);
577 fprintf(fp,
"qform_code := %d\n", dsr->
h.
qform_code);
578 fprintf(fp,
"sform_code := %d\n", dsr->
h.
sform_code);
579 fprintf(fp,
"quatern_b := %g\n", dsr->
h.
quatern_b);
580 fprintf(fp,
"quatern_c := %g\n", dsr->
h.
quatern_c);
581 fprintf(fp,
"quatern_d := %g\n", dsr->
h.
quatern_d);
582 fprintf(fp,
"qoffset_x := %g\n", dsr->
h.
qoffset_x);
583 fprintf(fp,
"qoffset_y := %g\n", dsr->
h.
qoffset_y);
584 fprintf(fp,
"qoffset_z := %g\n", dsr->
h.
qoffset_z);
585 i=0; fprintf(fp,
"srow_x := {%g", dsr->
h.
srow_x[i]);
586 for(i=1; i<4; i++) fprintf(fp,
", %g", dsr->
h.
srow_x[i]);
588 i=0; fprintf(fp,
"srow_y := {%g", dsr->
h.
srow_y[i]);
589 for(i=1; i<4; i++) fprintf(fp,
", %g", dsr->
h.
srow_y[i]);
591 i=0; fprintf(fp,
"srow_z := {%g", dsr->
h.
srow_z[i]);
592 for(i=1; i<4; i++) fprintf(fp,
", %g", dsr->
h.
srow_z[i]);
596 cptr=tmp;
while(*cptr) {
if(!isprint(*cptr)) *cptr=
' '; cptr++;}
597 fprintf(fp,
"intent_name := %s\n", tmp);
600 fprintf(fp,
"magic := %s\n", dsr->
h.
magic);
603 i=0; fprintf(fp,
"extension := {%d", dsr->
e.
extension[i]);
604 for(i=1; i<4; i++) fprintf(fp,
", %d", dsr->
e.
extension[i]);
634 int dimNr, dimx, dimy, dimz=1, dimt=1;
644 printf(
"niftiReadImagedata(fp, h, %d, data, %d)\n", frame, verbose);
648 if(status!=NULL) sprintf(status,
"invalid function input");
649 if(frame<=0 || fp==NULL || dsr==NULL || data==NULL)
return(1);
657 if(s<0) start_pos=-s;
else start_pos=s;
659 if(verbose>2) printf(
" image_start_pos := %llu\n", start_pos);
663 if(status!=NULL) sprintf(status,
"invalid image dimensions");
664 dimNr=dsr->
h.
dim[0];
if(dimNr<2 || dimNr>4)
return(2);
667 if(dimNr>2) dimz=dsr->
h.
dim[3];
668 if(dimNr>3) dimt=dsr->
h.
dim[4];
669 if(frame>dimt)
return(-1);
670 long long pxlNr=dimx*dimy*dimz;
if(pxlNr<1)
return(4);
674 if(verbose>1) printf(
" verifying datatype %d\n", dsr->
h.
datatype);
682 if(verbose>0) printf(
"datatype error %d\n", n);
683 if(status!=NULL) sprintf(status,
"unsupported pixel datatype %d", dsr->
h.
datatype);
689 if(verbose>1) printf(
" allocating memory for binary data\n");
690 if(status!=NULL) sprintf(status,
"invalid pixel data format");
695 long long rawSize=pxlNr*(dsr->
h.
bitpix/8);
if(rawSize<1)
return(6);
696 if(verbose>1) printf(
" pxlNr=%lld rawSize=%lld\n", pxlNr, rawSize);
697 if(status!=NULL) sprintf(status,
"out of memory");
698 mdata=(
char*)malloc(rawSize);
if(mdata==NULL)
return(11);
701 if(verbose>1) printf(
" seeking file position\n");
702 start_pos+=(frame-1)*rawSize;
703 if(verbose>2) printf(
"start_pos=%lld\n", start_pos);
704 fseeko(fp, start_pos, SEEK_SET);
705 if(ftello(fp)!=start_pos) {
706 if(status!=NULL) sprintf(status,
"could not move to start_pos %lld", start_pos);
707 free(mdata);
return(7);
711 if(verbose>1) printf(
" reading binary data\n");
714 size_t n=fread(mptr, rawSize, 1, fp);
717 sprintf(status,
"could read only %zu bytes when request was %lld", n, rawSize);
718 free(mdata);
return(8);
725 if(verbose>0) printf(
"byte conversion\n");
728 case 16:
swabip(mptr, rawSize);
break;
729 case 32:
swawbip(mptr, rawSize);
break;
730 case 64:
swawbip(mptr, rawSize);
break;
732 if(verbose>5) printf(
"unsupported nifti bitpix := %d\n", dsr->
h.
bitpix);
733 sprintf(status,
"unsupported nifti bitpix := %d", dsr->
h.
bitpix);
734 free(mdata);
return(5);
743 if(verbose>1) printf(
" conversion to floating point voxel values\n");
749 sprintf(status,
"invalid combination of datatype and bitpix (%d, %d)",
751 free(mdata);
return(5);
754 for(
long long i=0; i<pxlNr; i++, mptr++, fptr++)
755 *fptr=si+ss*(
float)(*mptr);
760 sprintf(status,
"invalid combination of datatype and bitpix (%d, %d)",
762 free(mdata);
return(5);
765 for(
long long i=0; i<pxlNr; i++, mptr+=2, fptr++) {
766 unsigned short int *uptr=(
unsigned short int*)mptr; *fptr=si+ss*(float)(*uptr);
772 sprintf(status,
"invalid combination of datatype and bitpix (%d, %d)",
774 free(mdata);
return(5);
777 for(
long long i=0; i<pxlNr; i++, mptr+=2, fptr++) {
778 sptr=(
short int*)mptr; *fptr=si+ss*(float)(*sptr);
784 sprintf(status,
"invalid combination of datatype and bitpix (%d, %d)",
786 free(mdata);
return(5);
790 for(
long long i=0; i<pxlNr; i++, mptr+=2, fptr++) {
791 iptr=(
int*)mptr; *fptr=si+ss*(float)(*iptr);
794 for(
long long i=0; i<pxlNr; i++, mptr+=4, fptr++) {
795 iptr=(
int*)mptr; *fptr=si+ss*(float)(*iptr);
801 fptr=data; memcpy(fptr, mptr, pxlNr*4);
802 fptr=data;
for(
long long i=0; i<pxlNr; i++, fptr++) *fptr*=ss;
803 fptr=data;
for(
long long i=0; i<pxlNr; i++, fptr++) *fptr+=si;
806 sprintf(status,
"invalid combination of datatype and bitpix (%d, %d)",
808 free(mdata);
return(5);
814 sprintf(status,
"invalid combination of datatype and bitpix (%d, %d)",
816 free(mdata);
return(5);
819 for(
long long i=0; i<pxlNr; i++, mptr+=8, fptr++) {
820 memcpy(&d, mptr, 8); *fptr=si+ss*d;
825 sprintf(status,
"unsupported pixel datatype %d", dsr->
h.
datatype);
826 free(mdata);
return(5);
829 if(verbose>1) {printf(
" data read successfully.\n"); fflush(stdout);}
831 if(status!=NULL) sprintf(status,
"ok");
864 printf(
"\nniftiWriteHeader(%s, ...)\n", filename); fflush(stdout);
868 if(status!=NULL) strcpy(status,
"invalid function input");
869 if(filename==NULL || strlen(filename)==0 || dsr==NULL)
return(1);
871 if(strcmp(dsr->
h.
magic,
"ni1")!=0 && strcmp(dsr->
h.
magic,
"n+1")!=0)
875 little=
little_endian();
if(verbose>3) printf(
" little := %d\n", little);
876 if(little==dsr->
byte_order) same_order=1;
else same_order=0;
883 if(verbose>2) printf(
" setting write buffer\n");
900 memcpy(bptr, dsr->
h.
dim, 16);
if(!same_order)
swabip(bptr, 16);
912 memcpy(bptr, &dsr->
h.
bitpix, 2);
if(!same_order)
swabip(bptr, 2);
916 memcpy(bptr, dsr->
h.
pixdim, 32);
if(!same_order)
swawbip(bptr, 32);
938 memcpy(bptr, &dsr->
h.
glmax, 4);
if(!same_order)
swawbip(bptr, 4);
940 memcpy(bptr, &dsr->
h.
glmin, 4);
if(!same_order)
swawbip(bptr, 4);
963 memcpy(bptr, dsr->
h.
srow_x, 16);
if(!same_order)
swawbip(bptr, 16);
965 memcpy(bptr, dsr->
h.
srow_y, 16);
if(!same_order)
swawbip(bptr, 16);
967 memcpy(bptr, dsr->
h.
srow_z, 16);
if(!same_order)
swawbip(bptr, 16);
971 memcpy(bptr, dsr->
h.
magic, 4);
975 if(strcmp(dsr->
h.
magic,
"ni1")==0) {
976 if(verbose>2) printf(
" creating NIfTI header %s\n", filename);
977 fp=fopen(filename,
"wb");
978 }
else if(access(filename, 0)==-1) {
979 if(verbose>2) printf(
" creating NIfTI header %s\n", filename);
980 fp=fopen(filename,
"wb");
982 if(verbose>2) printf(
" opening NIfTI header %s\n", filename);
983 fp=fopen(filename,
"r+b");
986 if(status!=NULL) strcpy(status,
"cannot open Nifti header for write");
991 if(verbose>2) printf(
" writing NIfTI header\n");
993 if(status!=NULL) strcpy(status,
"cannot write Nifti header");
994 fclose(fp);
return(3);
998 if(verbose>2) printf(
" writing NIfTI extender\n");
1000 if(status!=NULL) strcpy(status,
"cannot write Nifti header extender");
1001 fclose(fp);
return(3);
1006 if(verbose>2) {printf(
" complete Nifti header was written\n"); fflush(stdout);}
1007 if(status!=NULL) strcpy(status,
"complete Nifti header was written");