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 char fext[FILENAME_MAX]; fext[0]=(char)0;
199 strcpy(temp, filename);
201 if(cptr!=NULL) strcpy(fext, cptr);
203 if(strcasecmp(fext,
".v")==0)
return(0);
206 strlcpy(basefile, filename, FILENAME_MAX);
208 if(verbose>1) printf(
"\n basefile := %s\n", basefile);
211 if(strcasecmp(fext,
".nii")==0) {
212 if(verbose>1) printf(
" we have filename with .nii extension %s\n", temp);
215 if(access(filename, 0) == -1) {
216 if(verbose>0) printf(
" %s not found or accessible.\n", filename);
217 if(status!=NULL) strcpy(status,
"file not accessible");
221 strcpy(localhdrfile, temp);
222 if(hdrfile!=NULL) strcpy(hdrfile, temp);
223 if(imgfile!=NULL) strcpy(imgfile, temp);
225 if(verbose>1) printf(
" %s is accessible.\n", temp);
229 if(!localhdrfile[0] && (strcasecmp(fext,
".hdr")==0 || strcasecmp(fext,
".img")==0)) {
231 if(access(filename, 0) == -1) {
232 if(verbose>0) printf(
" %s not found or accessible.\n", filename);
233 if(status!=NULL) strcpy(status,
"file not accessible");
237 if(strcasecmp(fext,
".hdr")==0) {
238 strcpy(temp, basefile); strcat(temp,
".img");
239 if(access(temp, 0) == -1) {
240 if(verbose>0) printf(
" img file not found or accessible.\n");
241 if(status!=NULL) strcpy(status,
"file not accessible");
245 strcpy(localhdrfile, filename);
246 if(hdrfile!=NULL) strcpy(hdrfile, filename);
247 if(imgfile!=NULL) strcpy(imgfile, temp);
248 if(verbose>1) printf(
" %s and %s are accessible.\n", filename, temp);
250 strcpy(temp, basefile); strcat(temp,
".hdr");
251 if(access(temp, 0) == -1) {
252 strcpy(temp, basefile); strcat(temp,
".img.hdr");
253 if(access(temp, 0) == -1) {
254 if(verbose>0) printf(
" hdr file not found or accessible.\n");
255 if(status!=NULL) strcpy(status,
"file not accessible");
260 strcpy(localhdrfile, temp);
261 if(hdrfile!=NULL) strcpy(hdrfile, temp);
262 if(imgfile!=NULL) strcpy(imgfile, filename);
263 if(verbose>1) printf(
" %s and %s are accessible.\n", filename, temp);
268 if(!localhdrfile[0]) {
269 if(verbose>1) printf(
" we may have got basename %s\n", filename);
270 strcpy(temp, filename); strcat(temp,
".nii");
271 if(access(temp, 0) == 0) {
273 strcpy(localhdrfile, temp);
274 if(hdrfile!=NULL) strcpy(hdrfile, temp);
275 if(imgfile!=NULL) strcpy(imgfile, temp);
277 if(verbose>1) printf(
" %s is accessible.\n", temp);
279 strcpy(temp, basefile); strcat(temp,
".hdr");
280 if(access(temp, 0) == -1) {
281 strcpy(temp, basefile); strcat(temp,
".img.hdr");
282 if(access(temp, 0) == -1) {
283 if(verbose>0) printf(
" hdr file not found or accessible.\n");
284 if(status!=NULL) strcpy(status,
"file not accessible");
288 strcpy(localhdrfile, temp);
289 strcpy(temp, basefile); strcat(temp,
".img");
290 if(access(temp, 0) == -1) {
291 if(verbose>0) printf(
" img file not found or accessible.\n");
292 if(status!=NULL) strcpy(status,
"file not accessible");
296 if(hdrfile!=NULL) strcpy(hdrfile, localhdrfile);
297 if(imgfile!=NULL) strcpy(imgfile, temp);
298 if(verbose>1) printf(
" %s and %s are accessible.\n", filename, temp);
304 if(status!=NULL) strcpy(status,
"file is not Nifti");
306 printf(
" %s was not identified as Nifti header file (%d).\n", localhdrfile, ret);
307 printf(
" %s\n", temp);
311 if(verbose>1) printf(
" %s is identified as Nifti.\n", localhdrfile);
317 strcpy(temp, basefile); strcat(temp,
".nii");
318 if(access(temp, 0) == -1) {
319 if(verbose>0) printf(
" %s not found or accessible.\n", temp);
322 strcpy(localhdrfile, temp);
323 if(hdrfile!=NULL) strcpy(hdrfile, temp);
324 if(imgfile!=NULL) strcpy(imgfile, temp);
326 if(verbose>1) printf(
" %s is accessible.\n", temp);
331 strcpy(temp, basefile); strcat(temp,
".hdr");
332 if(access(temp, 0) == -1) {
333 strcpy(temp, basefile); strcat(temp,
".img.hdr");
334 if(access(temp, 0) == -1) {
335 if(verbose>0) printf(
" hdr file not found or accessible.\n");
336 if(status!=NULL) strcpy(status,
"file not accessible");
341 strcpy(localhdrfile, temp);
342 if(hdrfile!=NULL) strcpy(hdrfile, temp);
343 if(verbose>1) printf(
" %s is accessible.\n", temp);
348 strcpy(temp, basefile); strcat(temp,
".img");
349 if(access(temp, 0) == -1) {
350 if(verbose>0) printf(
" %s not found or accessible.\n", temp);
351 if(status!=NULL) strcpy(status,
"file not accessible");
355 if(imgfile!=NULL) strcpy(imgfile, temp);
356 if(verbose>1) printf(
" %s is accessible.\n", temp);
361 if(status!=NULL) strcpy(status,
"file is not Nifti");
363 printf(
" %s was not identified as Nifti header file (%d).\n", localhdrfile, ret);
364 printf(
" %s\n", temp);
368 if(verbose>1) printf(
" %s is identified as Nifti.\n", localhdrfile);
373 strcpy(temp, basefile); strcat(temp,
".sif");
374 if(verbose>3) printf(
" checking if %s exists\n", temp);
375 if(access(temp, 0) == -1) {
376 strcpy(temp, basefile); strcat(temp,
".img.sif");
377 if(verbose>3) printf(
" checking if %s exists\n", temp);
378 if(access(temp, 0) == -1) {
379 strcpy(temp, basefile); strcat(temp,
".nii.sif");
380 if(verbose>3) printf(
" checking if %s exists\n", temp);
381 if(access(temp, 0) == -1) {
382 if(verbose>0) printf(
"\n SIF not found or accessible.\n");
383 if(siffile!=NULL) strcpy(siffile,
"");
385 if(combined) strcpy(status,
"combined Nifti file is accessible");
386 else strcpy(status,
"Nifti files are accessible");
393 if(siffile!=NULL) strcpy(siffile, temp);
394 if(verbose>1) printf(
" %s is accessible.\n", temp);
396 if(combined) strcpy(status,
"combined Nifti file and SIF are accessible");
397 else strcpy(status,
"Nifti files and SIF are accessible");
749 int dimNr, dimx, dimy, dimz=1, dimt=1;
759 printf(
"niftiReadImagedata(fp, h, %d, data, %d)\n", frame, verbose);
763 if(status!=NULL) sprintf(status,
"invalid function input");
764 if(frame<=0 || fp==NULL || dsr==NULL || data==NULL)
return(1);
772 if(s<0) start_pos=-s;
else start_pos=s;
774 if(verbose>2) printf(
" image_start_pos := %llu\n", start_pos);
778 if(status!=NULL) sprintf(status,
"invalid image dimensions");
779 dimNr=dsr->
h.
dim[0];
if(dimNr<2 || dimNr>4)
return(2);
782 if(dimNr>2) dimz=dsr->
h.
dim[3];
783 if(dimNr>3) dimt=dsr->
h.
dim[4];
784 if(frame>dimt)
return(-1);
785 long long pxlNr=dimx*dimy*dimz;
if(pxlNr<1)
return(4);
789 if(verbose>1) printf(
" verifying datatype %d\n", dsr->
h.
datatype);
797 if(verbose>0) printf(
"datatype error %d\n", n);
798 if(status!=NULL) sprintf(status,
"unsupported pixel datatype %d", dsr->
h.
datatype);
804 if(verbose>1) printf(
" allocating memory for binary data\n");
805 if(status!=NULL) sprintf(status,
"invalid pixel data format");
810 long long rawSize=pxlNr*(dsr->
h.
bitpix/8);
if(rawSize<1)
return(6);
811 if(verbose>1) printf(
" pxlNr=%lld rawSize=%lld\n", pxlNr, rawSize);
812 if(status!=NULL) sprintf(status,
"out of memory");
813 mdata=(
char*)malloc(rawSize);
if(mdata==NULL)
return(11);
816 if(verbose>1) printf(
" seeking file position\n");
817 start_pos+=(frame-1)*rawSize;
818 if(verbose>2) printf(
"start_pos=%lld\n", start_pos);
819 fseeko(fp, start_pos, SEEK_SET);
820 if(ftello(fp)!=start_pos) {
821 if(status!=NULL) sprintf(status,
"could not move to start_pos %lld", start_pos);
822 free(mdata);
return(7);
826 if(verbose>1) printf(
" reading binary data\n");
829 size_t n=fread(mptr, rawSize, 1, fp);
832 sprintf(status,
"could read only %zu bytes when request was %lld", n, rawSize);
833 free(mdata);
return(8);
840 if(verbose>0) printf(
"byte conversion\n");
843 case 16:
swabip(mptr, rawSize);
break;
844 case 32:
swawbip(mptr, rawSize);
break;
845 case 64:
swawbip(mptr, rawSize);
break;
847 if(verbose>5) printf(
"unsupported nifti bitpix := %d\n", dsr->
h.
bitpix);
848 sprintf(status,
"unsupported nifti bitpix := %d", dsr->
h.
bitpix);
849 free(mdata);
return(5);
858 if(verbose>1) printf(
" conversion to floating point voxel values\n");
864 sprintf(status,
"invalid combination of datatype and bitpix (%d, %d)",
866 free(mdata);
return(5);
869 for(
long long i=0; i<pxlNr; i++, mptr++, fptr++)
870 *fptr=si+ss*(
float)(*mptr);
875 sprintf(status,
"invalid combination of datatype and bitpix (%d, %d)",
877 free(mdata);
return(5);
880 for(
long long i=0; i<pxlNr; i++, mptr+=2, fptr++) {
881 unsigned short int *uptr=(
unsigned short int*)mptr; *fptr=si+ss*(float)(*uptr);
887 sprintf(status,
"invalid combination of datatype and bitpix (%d, %d)",
889 free(mdata);
return(5);
892 for(
long long i=0; i<pxlNr; i++, mptr+=2, fptr++) {
893 sptr=(
short int*)mptr; *fptr=si+ss*(float)(*sptr);
899 sprintf(status,
"invalid combination of datatype and bitpix (%d, %d)",
901 free(mdata);
return(5);
905 for(
long long i=0; i<pxlNr; i++, mptr+=2, fptr++) {
906 iptr=(
int*)mptr; *fptr=si+ss*(float)(*iptr);
909 for(
long long i=0; i<pxlNr; i++, mptr+=4, fptr++) {
910 iptr=(
int*)mptr; *fptr=si+ss*(float)(*iptr);
916 fptr=data; memcpy(fptr, mptr, pxlNr*4);
917 fptr=data;
for(
long long i=0; i<pxlNr; i++, fptr++) *fptr*=ss;
918 fptr=data;
for(
long long i=0; i<pxlNr; i++, fptr++) *fptr+=si;
921 sprintf(status,
"invalid combination of datatype and bitpix (%d, %d)",
923 free(mdata);
return(5);
929 sprintf(status,
"invalid combination of datatype and bitpix (%d, %d)",
931 free(mdata);
return(5);
934 for(
long long i=0; i<pxlNr; i++, mptr+=8, fptr++) {
935 memcpy(&d, mptr, 8); *fptr=si+ss*d;
940 sprintf(status,
"unsupported pixel datatype %d", dsr->
h.
datatype);
941 free(mdata);
return(5);
944 if(verbose>1) {printf(
" data read successfully.\n"); fflush(stdout);}
946 if(status!=NULL) sprintf(status,
"ok");