54 #include "libtpcmisc.h"
78 if(fp==NULL)
return -1;
79 if(parameter==NULL || strlen(parameter)<1)
return -2;
82 if(tmp[0]==
'#')
continue;
83 if(strncasecmp(tmp, parameter, strlen(parameter))!=0)
continue;
85 cptr=tmp+strlen(parameter)+1;
87 if(value!=0) strcpy(value, cptr);
89 if(value!=0) strcpy(value,
"");
111 if(hdrfile==NULL || strlen(hdrfile)<5)
return 0;
113 if((fp=fopen(hdrfile,
"r"))==NULL)
return 0;
116 if(tmp[0]!=
'#') {fclose(fp);
return 0;}
119 if(ret!=0) {fclose(fp);
return 0;}
121 if(ret!=0) {fclose(fp);
return 0;}
123 if(ret!=0) {fclose(fp);
return 0;}
146 char *cptr, basefile[FILENAME_MAX], temp[FILENAME_MAX];
148 if(upetname==NULL || strlen(upetname)==0)
return(0);
149 if(
MICROPET_TEST>1) printf(
"\nupetExists(%s, *str, *str)\n", upetname);
152 strcpy(basefile, upetname);
153 cptr=strrchr(basefile,
'.');
155 if(strncasecmp(cptr,
".HDR", 4)==0 || strncasecmp(cptr,
".IMG", 4)==0 )
158 cptr=strrchr(basefile,
'.');
160 if(strncasecmp(cptr,
".IMG", 4)==0 )
166 strcpy(temp, basefile); strcat(temp,
".hdr");
167 if(access(temp, 0) == -1) {
168 strcpy(temp, basefile); strcat(temp,
".img.hdr");
169 if(access(temp, 0) == -1) {
170 if(
MICROPET_TEST) printf(
"\n hdr file not found or accessible.\n");
177 printf(
"\n %s was not identified as microPET header file.\n", temp);
181 if(hdrfile!=NULL) strcpy(hdrfile, temp);
184 strcpy(temp, basefile); strcat(temp,
".img");
185 if(access(temp, 0) == -1) {
186 if(
MICROPET_TEST) printf(
"\n %s not found or accessible.\n", temp);
190 if(imgfile!=NULL) strcpy(imgfile, temp);
213 if(fp==NULL)
return 1;
214 *z=*x=*y=0;
if(f!=NULL) *f=0;
219 *f=-1; (void)sscanf(tmp,
"%d", f);
222 *x=-1; (void)sscanf(tmp,
"%d", x);
224 *y=-1; (void)sscanf(tmp,
"%d", y);
226 *z=-1; (void)sscanf(tmp,
"%d", z);
227 if(*z<1 || *x<1 || *y<1)
return 2;
228 if(f!=NULL && *f<1)
return 2;
245 struct tm scanstart={0};
247 if(fp==NULL || scant==NULL)
return 1;
251 n=sscanf(tmp,
"%s %s %d %d:%d:%d %d", tmp2, tmp3, &scanstart.tm_mday,
252 &scanstart.tm_hour, &scanstart.tm_min, &scanstart.tm_sec, &i);
254 scanstart.tm_year=i-1900;
255 if(strcasecmp(tmp3,
"Jan")==0) scanstart.tm_mon=0;
256 else if(strcasecmp(tmp3,
"Feb")==0) scanstart.tm_mon=1;
257 else if(strcasecmp(tmp3,
"Mar")==0) scanstart.tm_mon=2;
258 else if(strcasecmp(tmp3,
"Apr")==0) scanstart.tm_mon=3;
259 else if(strcasecmp(tmp3,
"May")==0) scanstart.tm_mon=4;
260 else if(strcasecmp(tmp3,
"Jun")==0) scanstart.tm_mon=5;
261 else if(strcasecmp(tmp3,
"Jul")==0) scanstart.tm_mon=6;
262 else if(strcasecmp(tmp3,
"Aug")==0) scanstart.tm_mon=7;
263 else if(strcasecmp(tmp3,
"Sep")==0) scanstart.tm_mon=8;
264 else if(strcasecmp(tmp3,
"Oct")==0) scanstart.tm_mon=9;
265 else if(strcasecmp(tmp3,
"Nov")==0) scanstart.tm_mon=10;
266 else if(strcasecmp(tmp3,
"Dec")==0) scanstart.tm_mon=11;
267 scanstart.tm_isdst=-1;
268 *scant=mktime(&scanstart);
if(*scant<0)
return 4;
288 char upetheader[FILENAME_MAX], upetimage[FILENAME_MAX];
290 int acquisition_mode, data_type;
295 if(
MICROPET_TEST) printf(
"\nimgMicropetToEcat7(%s, %s, %d)\n",
296 upetname, ecatfile, verbose);
298 if(upetname==NULL || ecatfile==NULL)
return STATUS_FAULT;
299 ret=
upetExists(upetname, upetheader, upetimage);
306 if((fpi=fopen(upetimage,
"rb"))==NULL) {fclose(fph);
return(
STATUS_NOIMGDATA);}
313 if(
MICROPET_TEST>1) printf(
"checking that image format is supported\n");
315 (void)sscanf(tmp,
"%d", &n);
320 (void)sscanf(tmp,
"%d", &acquisition_mode);
321 if(
MICROPET_TEST>2) printf(
"acquisition_mode := %d\n", acquisition_mode);
322 if(acquisition_mode!=2 && acquisition_mode!=3 && acquisition_mode!=9) {
326 (void)sscanf(tmp,
"%d", &data_type);
328 if(data_type!=4 && data_type!=2) {
334 if(acquisition_mode==2 || acquisition_mode==3)
336 else if(acquisition_mode==9)
340 fclose(fph); fclose(fpi);
362 int n, pxlnr, zi, xi, yi, ti, zdim, xdim, ydim, tdim, ret;
363 float *fptr, calibration_factor;
367 if(
MICROPET_TEST>1) printf(
"imgMicropetPETToEcat7(*fph, *fpi, %s, %d)\n",
370 if(fph==NULL || fpi==NULL || ecatfile==NULL)
return STATUS_FAULT;
373 if(access(ecatfile, 0)!=-1 &&
remove(ecatfile)!=0) {
383 printf(
"z_dim := %d\n", zdim);
384 printf(
"x_dim := %d\n", xdim);
385 printf(
"y_dim := %d\n", ydim);
386 printf(
"t_dim := %d\n", tdim);
402 if(
MICROPET_TEST) printf(
"calibration_factor := %g\n", calibration_factor);
405 studynr_from_fname(ecatfile, img.
studyNr);
408 pxlnr=xdim*ydim*zdim;
409 mdata=(
char*)malloc(pxlnr*
sizeof(
float));
if(mdata==NULL) {
413 for(ti=0; ti<tdim; ti++) {
418 if(verbose==0) {fprintf(stdout,
"\n"); fflush(stdout);}
424 if((n=fread(mptr, 4, pxlnr, fpi)) < pxlnr) {
425 if(verbose==0) {fprintf(stdout,
"\n"); fflush(stdout);}
431 for(zi=0; zi<zdim; zi++)
432 for(yi=0; yi<ydim; yi++)
433 for(xi=0; xi<xdim; xi++) {
435 img.
m[zi][yi][xi][0]=(*fptr)*img.
weight[0]*calibration_factor;
442 else if(verbose==0) {fprintf(stdout,
"."); fflush(stdout);}
445 if(verbose==0) {fprintf(stdout,
"\n"); fflush(stdout);}
447 fprintf(stdout,
" %d frame(s) processed.\n", ti);
450 remove(ecatfile);
return ret;
473 int n, pxlnr, zi, xi, yi, zdim, xdim, ydim, ret;
474 float f, scale_factor;
480 if(
MICROPET_TEST>1) printf(
"imgMicropetCTToEcat7(*fph, *fpi, %s, %d)\n",
483 if(fph==NULL || fpi==NULL || ecatfile==NULL)
return STATUS_FAULT;
491 printf(
"z_dim := %d\n", zdim);
492 printf(
"x_dim := %d\n", xdim);
493 printf(
"y_dim := %d\n", ydim);
500 scale_factor=-1; (void)sscanf(tmp,
"%f", &scale_factor);
503 printf(
"scale_factor := %g\n", scale_factor);
507 if(access(ecatfile, 0)!=-1 &&
remove(ecatfile)!=0) {
526 studynr_from_fname(ecatfile, img.
studyNr);
530 mdata=(
char*)malloc(pxlnr*
sizeof(
short int));
if(mdata==NULL) {
534 for(zi=0; zi<zdim; zi++) {
536 if((n=fread(mptr, 2, pxlnr, fpi)) < pxlnr) {
537 if(verbose==0) {fprintf(stdout,
"\n"); fflush(stdout);}
543 for(yi=0; yi<ydim; yi++)
544 for(xi=0; xi<xdim; xi++) {
546 f=(float)*si*scale_factor;
547 if(f>=0.0) img.
m[zi][yi][xi][0]=f;
else img.
m[zi][yi][xi][0]=0.0;
551 else if(verbose==0) {fprintf(stdout,
"."); fflush(stdout);}
554 if(verbose==0) {fprintf(stdout,
"\n"); fflush(stdout);}
573 float *calibration_factor
577 float f, branching_fraction=1.0;
580 if(
MICROPET_TEST>1) printf(
"imgGetMicropetMainHeader(*fp, *img, *f)\n");
581 if(fp==NULL)
return 1;
582 if(img==NULL)
return 2;
588 n=-1; (void)sscanf(tmp,
"%d", &n);
if(n<0)
return 11;
595 f=-1; (void)sscanf(tmp,
"%f", &f);
if(f<0)
return 11;
603 f=-1; (void)sscanf(tmp,
"%f", &f);
if(f<0)
return 12;
608 f=-1; (void)sscanf(tmp,
"%f", &f);
if(f<0)
return 12;
617 f=-1; (void)sscanf(tmp,
"%f", &f);
if(f<0)
return 13;
622 f=-1; (void)sscanf(tmp,
"%f", &f);
if(f<0)
return 12;
631 f=-1; (void)sscanf(tmp,
"%f", &f);
if(f<0)
return 14;
639 f=-1; (void)sscanf(tmp,
"%f", &f);
if(f<0)
return 14;
647 if(
MICROPET_TEST>9) printf(
" reading 'transaxial_bin_size'\n");
649 f=-1; (void)sscanf(tmp,
"%f", &f);
if(f>0) img->
sizez=10.0*f;
654 if(
MICROPET_TEST>9) printf(
" reading 'isotope_half_life'\n");
656 f=-1; (void)sscanf(tmp,
"%f", &f);
if(f<0)
return 15;
662 if(
MICROPET_TEST>9) printf(
" reading 'isotope_branching_fraction'\n");
664 f=-1; (void)sscanf(tmp,
"%f", &f);
if(f<0)
return 16;
665 branching_fraction=f;
670 if(
MICROPET_TEST>9) printf(
" reading 'decay_correction_applied'\n");
672 n=-1; (void)sscanf(tmp,
"%d", &n);
if(n<0)
return 17;
678 if(
MICROPET_TEST>9) printf(
" reading 'calibration_units'\n");
680 n=-1; (void)sscanf(tmp,
"%d", &n);
if(n<0)
return 18;
691 if(
MICROPET_TEST>9) printf(
" reading 'calibration_factor'\n");
692 if(calibration_factor!=NULL &&
695 f=-1; (void)sscanf(tmp,
"%f", &f);
if(f<=0.0)
return 19;
696 *calibration_factor=f;
697 if(branching_fraction>0.0) *calibration_factor/=branching_fraction;
704 f=-1; (void)sscanf(tmp,
"%f", &f);
if(f<0)
return 20;
729 if(fp==NULL)
return 1;
730 if(img==NULL)
return 2;
731 if(frame_index<0)
return 3;
737 sprintf(tmp,
"frame %d", frame_index);
742 f=-1.0; (void)sscanf(tmp,
"%f", &f);
if(f<0.0)
return 11;
747 f=-1.0; (void)sscanf(tmp,
"%f", &f);
if(f<=0.0)
return 12;
753 f=-1; (void)sscanf(tmp,
"%f", &f);
if(f<0)
return 13;
758 f=-1; (void)sscanf(tmp,
"%f", &f);
if(f<0)
return 14;
780 if(fp==NULL)
return 1;
781 if(sif==NULL)
return 2;
787 n=-1; (void)sscanf(tmp,
"%d", &n);
if(n<1)
return 11;
790 ret=
sifSetmem(sif, n);
if(ret!=0)
return 4;
804 for(i=0; i<sif->
frameNr; i++) {
806 sprintf(tmp,
"frame %d", i);
810 n=-1; (void)sscanf(tmp,
"%d", &n);
if(n<0)
return 22;
814 n=-1; (void)sscanf(tmp,
"%d", &n);
if(n<0)
return 23;
815 sif->
x2[i]=sif->
x1[i]+n;
818 n=-1; (void)sscanf(tmp,
"%s %s %d", tmp2, tmp3, &n);
if(n<0)
return 24;
822 n=-1; (void)sscanf(tmp,
"%s %s %d", tmp2, tmp3, &n);
if(n<0)
return 25;
int imgWrite(const char *fname, IMG *img)
int upetScanStart(FILE *fp, time_t *scant)
int imgAllocate(IMG *image, int planes, int rows, int columns, int frames)
void imgEmpty(IMG *image)
int imgGetMicropetFrameHeader(FILE *fp, IMG *img, int frame_index)
int imgGetMicropetSIF(FILE *fp, SIF *sif)
int upetHeaderReadParameter(FILE *fp, char *parameter, char *value)
int sifSetmem(SIF *data, int frameNr)
int imgWriteFrame(const char *fname, int frame_to_write, IMG *img, int frame_index)
int upetIsHeader(char *hdrfile)
int upetGetImageDimensions(FILE *fp, int *z, int *x, int *y, int *f)
int upetExists(char *upetname, char *hdrfile, char *imgfile)
#define MAX_MICROPET_LINE_LEN
int imgMicropetPETToEcat7(FILE *fph, FILE *fpi, char *ecatfile, int verbose)
char studyNr[MAX_STUDYNR_LEN+1]
int imgMicropetToEcat7(char *upetname, char *ecatfile, int verbose)
int imgGetMicropetMainHeader(FILE *fp, IMG *img, float *calibration_factor)
int imgMicropetCTToEcat7(FILE *fph, FILE *fpi, char *ecatfile, int verbose)