36 if(hdrfile!=NULL) strcpy(hdrfile,
"");
37 if(imgfile!=NULL) strcpy(imgfile,
"");
38 if(siffile!=NULL) strcpy(siffile,
"");
41 char basename[FILENAME_MAX];
44 char basenamewopath[FILENAME_MAX];
50 if(hdrfile!=NULL) snprintf(hdrfile, FILENAME_MAX,
"%s.hdr", basename);
51 if(imgfile!=NULL) snprintf(imgfile, FILENAME_MAX,
"%s.img", basename);
53 if(hdrfile!=NULL) snprintf(hdrfile, FILENAME_MAX,
"%s.nii", basename);
54 if(imgfile!=NULL) snprintf(imgfile, FILENAME_MAX,
"%s.nii", basename);
58 if(siffile!=NULL) snprintf(siffile, FILENAME_MAX,
"%s.sif", basename);
80 int verbose=0;
if(status!=NULL) verbose=status->
verbose;
81 if(verbose>0) {printf(
"%s(..., %s, ...)\n", __func__, fname); fflush(stdout);}
83 if(img==NULL ||
strnlen(fname, 2)<1) {
91 if(verbose>1) printf(
"checking that %s exists and is NIfTI\n", fname);
92 char hdrfile[FILENAME_MAX], imgfile[FILENAME_MAX], siffile[FILENAME_MAX];
94 if(!
niftiExists(fname, hdrfile, imgfile, siffile, &dsr, status)) {
95 if(status!=NULL)
return(status->
error);
98 if(verbose>1) printf(
"NIfTI header read from %s\n", hdrfile);
101 if(verbose>2) printf(
" verifying datatype\n");
126 if(verbose>0) printf(
"Error: currently unsupported NIfTI datatype %d\n", datatype);
132 if(verbose>2) printf(
" bits per pixel := %d\n", bitpix);
136 if(bitpix<8 || (bitpix%8)!=0) {
144 int bytepix=bitpix/8;
if(verbose>2) printf(
" bytes per pixel := %d\n", bytepix);
151 statusSet(status, __func__, __FILE__, __LINE__, ret);
154 if(verbose>1) printf(
"allocating memory for 4D image\n");
163 statusSet(status, __func__, __FILE__, __LINE__, ret);
170 if(verbose>1) printf(
"reading pixel data in %s\n", imgfile);
171 FILE *fp=fopen(imgfile,
"rb");
181 long long start_pos=0;
185 if(s<0) start_pos=-s;
else start_pos=s;
191 if(verbose>2) printf(
" image_start_pos := %llu\n", start_pos);
194 fseeko(fp, start_pos, SEEK_SET);
195 if(ftello(fp)!=start_pos) {
204 if(verbose>2) printf(
" pixels/frame := %llu\n", pxlNr);
205 long long rawSize=pxlNr*(bitpix/8);
206 if(verbose>1) printf(
" raw bytes per frame := %lld\n", rawSize);
207 if(verbose>1) printf(
" allocating memory for raw binary data\n");
208 unsigned char *buf=(
unsigned char*)malloc(rawSize);
216 if(verbose>1) printf(
" reading binary data\n");
221 if(verbose>1) printf(
" byte conversion needed\n");
224 float scl_slope, scl_inter;
227 if(scl_slope==0.0) scl_slope=1.0;
229 for(
int ti=0; ti<img->
dimt; ti++) {
230 if(verbose>3) printf(
" frame %d\n", 1+ti);
231 if(fread(buf, rawSize, 1, fp) < 1) {
232 fclose(fp);
imgFree(img); free(buf);
237 if(byteconv)
switch(bitpix) {
239 case 16:
swap16ip(buf, pxlNr);
break;
240 case 32:
swap32ip(buf, pxlNr);
break;
241 case 64:
swap64ip(buf, pxlNr);
break;
246 unsigned char *bptr=buf;
250 if(bitpix!=8) {ret=1;
break;}
251 for(
int zi=0; zi<img->
dimz; zi++)
252 for(
int yi=0; yi<img->
dimy; yi++)
253 for(
int xi=0; xi<img->
dimx; xi++) {
254 img->
m[zi][yi][xi][ti]=scl_inter+scl_slope*(float)(*bptr);
259 if(bitpix!=16) {ret=1;
break;}
260 for(
int zi=0; zi<img->
dimz; zi++)
261 for(
int yi=0; yi<img->
dimy; yi++)
262 for(
int xi=0; xi<img->
dimx; xi++) {
265 img->
m[zi][yi][xi][ti]=scl_inter+scl_slope*(float)a;
270 if(bitpix!=16) {ret=1;
break;}
271 for(
int zi=0; zi<img->
dimz; zi++)
272 for(
int yi=0; yi<img->
dimy; yi++)
273 for(
int xi=0; xi<img->
dimx; xi++) {
276 img->
m[zi][yi][xi][ti]=scl_inter+scl_slope*(float)a;
281 if(bitpix!=32) {ret=1;
break;}
282 for(
int zi=0; zi<img->
dimz; zi++)
283 for(
int yi=0; yi<img->
dimy; yi++)
284 for(
int xi=0; xi<img->
dimx; xi++) {
287 img->
m[zi][yi][xi][ti]=scl_inter+scl_slope*(float)a;
292 if(bitpix!=32) {ret=1;
break;}
293 for(
int zi=0; zi<img->
dimz; zi++)
294 for(
int yi=0; yi<img->
dimy; yi++)
295 for(
int xi=0; xi<img->
dimx; xi++) {
298 img->
m[zi][yi][xi][ti]=scl_inter+scl_slope*(float)a;
303 if(bitpix!=64) {ret=1;
break;}
304 for(
int zi=0; zi<img->
dimz; zi++)
305 for(
int yi=0; yi<img->
dimy; yi++)
306 for(
int xi=0; xi<img->
dimx; xi++) {
309 img->
m[zi][yi][xi][ti]=scl_inter+scl_slope*(float)a;
314 if(bitpix!=64) {ret=1;
break;}
315 for(
int zi=0; zi<img->
dimz; zi++)
316 for(
int yi=0; yi<img->
dimy; yi++)
317 for(
int xi=0; xi<img->
dimx; xi++) {
320 img->
m[zi][yi][xi][ti]=scl_inter+scl_slope*(float)a;
325 if(bitpix!=32) {ret=1;
break;}
326 for(
int zi=0; zi<img->
dimz; zi++)
327 for(
int yi=0; yi<img->
dimy; yi++)
328 for(
int xi=0; xi<img->
dimx; xi++) {
331 img->
m[zi][yi][xi][ti]=scl_inter+scl_slope*a;
336 if(bitpix!=64) {ret=1;
break;}
337 for(
int zi=0; zi<img->
dimz; zi++)
338 for(
int yi=0; yi<img->
dimy; yi++)
339 for(
int xi=0; xi<img->
dimx; xi++) {
342 img->
m[zi][yi][xi][ti]=scl_inter+scl_slope*(float)a;
350 fclose(fp);
imgFree(img); free(buf);
355 fclose(fp); free(buf);
360 if(verbose>0) {printf(
"reading SIF %s\n", siffile); fflush(stdout);}
362 int ret=
tacRead(&sif, siffile, status);
364 ret=
imgFromSIF(img, &sif, 1, 1, 0, verbose-1);
368 statusSet(status, __func__, __FILE__, __LINE__, ret);
398 int verbose=0;
if(status!=NULL) verbose=status->
verbose;
399 if(verbose>0) {printf(
"%s(..., %s, ...)\n", __func__, fname); fflush(stdout);}
401 if(img==NULL ||
strnlen(fname, 2)<1) {
411 if(verbose>1) {printf(
"determine output format\n"); fflush(stdout);}
422 if(verbose>1) {printf(
"get dbname\n"); fflush(stdout);}
423 char basename[FILENAME_MAX];
426 char basenamewopath[FILENAME_MAX];
428 if(strlen(basenamewopath)<1) {
433 if(verbose>1) {printf(
"determine output file names\n"); fflush(stdout);}
434 char hdrfile[FILENAME_MAX], imgfile[FILENAME_MAX], siffile[FILENAME_MAX];
435 hdrfile[0]=imgfile[0]=siffile[0]=(char)0;
441 char filepath[FILENAME_MAX];
453 statusSet(status, __func__, __FILE__, __LINE__, ret);
458 if(verbose>1) {printf(
"removing any previous files\n"); fflush(stdout);}
464 if(verbose>1) {printf(
"writing the header\n"); fflush(stdout);}
471 statusSet(status, __func__, __FILE__, __LINE__, ret);
475 if(verbose>1) {printf(
"opening file for pixel data\n"); fflush(stdout);}
478 fp=fopen(imgfile,
"wb");
480 fp=fopen(imgfile,
"r+b");
489 if(fseek(fp, pos, SEEK_SET)!=0) {
496 fdata=(
float*)calloc(voxNr,
sizeof(
float));
503 for(
unsigned int fi=0; fi<img->
dimt; fi++)
504 for(
unsigned int zi=0; zi<img->
dimz; zi++)
505 for(
unsigned int yi=0; yi<img->
dimy; yi++)
506 for(
unsigned int xi=0; xi<img->
dimx; xi++, fptr++)
507 *fptr=img->
m[zi][yi][xi][fi];
508 if(verbose>1) {printf(
"writing pixel data\n"); fflush(stdout);}
510 if(fwrite(fptr,
sizeof(
float), voxNr, fp) != voxNr) {
520 if(verbose>1) printf(
" no frame times to save into SIF\n");
525 if(verbose>1) {printf(
"making SIF\n"); fflush(stdout);}
532 ret=
imgToSIF(img, &sif, 1, 1, 0, verbose-3);
535 ret=
imgToSIF(img, &sif, 1, 1, 2, verbose-3);
538 if(verbose>0) fprintf(stderr,
" Error: cannot create SIF contents.\n");
543 if(verbose>1) {printf(
"writing SIF\n"); fflush(stdout);}
544 fp=fopen(siffile,
"w");