48 #include "libtpcmisc.h"
72 if(fp==NULL)
return -1;
73 if(parameter==NULL || strlen(parameter)<1)
return -2;
76 if(tmp[0]==
'#')
continue;
77 if(strncasecmp(tmp, parameter, strlen(parameter))!=0)
continue;
79 cptr=tmp+strlen(parameter)+1;
81 if(value!=0) strcpy(value, cptr);
83 if(value!=0) strcpy(value,
"");
105 if(hdrfile==NULL || strlen(hdrfile)<5)
return 0;
106 if((fp=fopen(hdrfile,
"r"))==NULL)
return 0;
109 if(tmp[0]!=
'#') {fclose(fp);
return 0;}
112 if(ret!=0) {fclose(fp);
return 0;}
114 if(ret!=0) {fclose(fp);
return 0;}
116 if(ret!=0) {fclose(fp);
return 0;}
139 char *cptr, basefile[FILENAME_MAX], temp[FILENAME_MAX];
141 if(upetname==NULL || strlen(upetname)==0)
return(0);
144 strcpy(basefile, upetname);
145 cptr=strrchr(basefile,
'.');
147 if(strncasecmp(cptr,
".HDR", 4)==0 || strncasecmp(cptr,
".IMG", 4)==0 )
150 cptr=strrchr(basefile,
'.');
152 if(strncasecmp(cptr,
".IMG", 4)==0 )
157 strcpy(temp, basefile); strcat(temp,
".hdr");
158 if(access(temp, 0) == -1) {
159 strcpy(temp, basefile); strcat(temp,
".img.hdr");
160 if(access(temp, 0) == -1)
return(0);
165 if(hdrfile!=NULL) strcpy(hdrfile, temp);
168 strcpy(temp, basefile); strcat(temp,
".img");
169 if(access(temp, 0) == -1)
return(0);
171 if(imgfile!=NULL) strcpy(imgfile, temp);
194 if(fp==NULL)
return 1;
195 *z=*x=*y=0;
if(f!=NULL) *f=0;
200 *f=-1; (void)sscanf(tmp,
"%d", f);
203 *x=-1; (void)sscanf(tmp,
"%d", x);
205 *y=-1; (void)sscanf(tmp,
"%d", y);
207 *z=-1; (void)sscanf(tmp,
"%d", z);
208 if(*z<1 || *x<1 || *y<1)
return 2;
209 if(f!=NULL && *f<1)
return 2;
226 struct tm scanstart={0};
228 if(fp==NULL || scant==NULL)
return 1;
232 n=sscanf(tmp,
"%s %s %d %d:%d:%d %d", tmp2, tmp3, &scanstart.tm_mday,
233 &scanstart.tm_hour, &scanstart.tm_min, &scanstart.tm_sec, &i);
235 scanstart.tm_year=i-1900;
236 if(strcasecmp(tmp3,
"Jan")==0) scanstart.tm_mon=0;
237 else if(strcasecmp(tmp3,
"Feb")==0) scanstart.tm_mon=1;
238 else if(strcasecmp(tmp3,
"Mar")==0) scanstart.tm_mon=2;
239 else if(strcasecmp(tmp3,
"Apr")==0) scanstart.tm_mon=3;
240 else if(strcasecmp(tmp3,
"May")==0) scanstart.tm_mon=4;
241 else if(strcasecmp(tmp3,
"Jun")==0) scanstart.tm_mon=5;
242 else if(strcasecmp(tmp3,
"Jul")==0) scanstart.tm_mon=6;
243 else if(strcasecmp(tmp3,
"Aug")==0) scanstart.tm_mon=7;
244 else if(strcasecmp(tmp3,
"Sep")==0) scanstart.tm_mon=8;
245 else if(strcasecmp(tmp3,
"Oct")==0) scanstart.tm_mon=9;
246 else if(strcasecmp(tmp3,
"Nov")==0) scanstart.tm_mon=10;
247 else if(strcasecmp(tmp3,
"Dec")==0) scanstart.tm_mon=11;
248 scanstart.tm_isdst=-1;
249 *scant=mktime(&scanstart);
if(*scant<0)
return 4;
269 char upetheader[FILENAME_MAX], upetimage[FILENAME_MAX];
271 int acquisition_mode, data_type;
276 if(
MICROPET_TEST) printf(
"\nimgMicropetToEcat7(%s, %s, %d)\n",
277 upetname, ecatfile, verbose);
279 if(upetname==NULL || ecatfile==NULL)
return STATUS_FAULT;
280 ret=
upetExists(upetname, upetheader, upetimage);
287 if((fpi=fopen(upetimage,
"rb"))==NULL) {fclose(fph);
return(
STATUS_NOIMGDATA);}
294 if(
MICROPET_TEST>1) printf(
"checking that image format is supported\n");
296 (void)sscanf(tmp,
"%d", &n);
301 (void)sscanf(tmp,
"%d", &acquisition_mode);
302 if(
MICROPET_TEST>2) printf(
"acquisition_mode := %d\n", acquisition_mode);
303 if(acquisition_mode!=2 && acquisition_mode!=3 && acquisition_mode!=9) {
307 (void)sscanf(tmp,
"%d", &data_type);
309 if(data_type!=4 && data_type!=2) {
315 if(acquisition_mode==2 || acquisition_mode==3)
317 else if(acquisition_mode==9)
321 fclose(fph); fclose(fpi);
343 int n, pxlnr, zi, xi, yi, ti, zdim, xdim, ydim, tdim, ret;
344 float *fptr, calibration_factor;
349 if(fph==NULL || fpi==NULL || ecatfile==NULL)
return STATUS_FAULT;
352 if(access(ecatfile, 0)!=-1 &&
remove(ecatfile)!=0) {
362 printf(
"z_dim := %d\n", zdim);
363 printf(
"x_dim := %d\n", xdim);
364 printf(
"y_dim := %d\n", ydim);
365 printf(
"t_dim := %d\n", tdim);
381 if(
MICROPET_TEST) printf(
"calibration_factor := %g\n", calibration_factor);
384 studynr_from_fname(ecatfile, img.
studyNr);
387 pxlnr=xdim*ydim*zdim;
388 mdata=(
char*)malloc(pxlnr*
sizeof(
float));
if(mdata==NULL) {
392 for(ti=0; ti<tdim; ti++) {
397 if(verbose==0) {fprintf(stdout,
"\n"); fflush(stdout);}
403 if((n=fread(mptr, 4, pxlnr, fpi)) < pxlnr) {
404 if(verbose==0) {fprintf(stdout,
"\n"); fflush(stdout);}
410 for(zi=0; zi<zdim; zi++)
411 for(yi=0; yi<ydim; yi++)
412 for(xi=0; xi<xdim; xi++) {
414 img.
m[zi][yi][xi][0]=(*fptr)*img.
weight[0]*calibration_factor;
421 else if(verbose==0) {fprintf(stdout,
"."); fflush(stdout);}
424 if(verbose==0) {fprintf(stdout,
"\n"); fflush(stdout);}
426 fprintf(stdout,
" %d frame(s) processed.\n", ti);
429 remove(ecatfile);
return ret;
452 int n, pxlnr, zi, xi, yi, zdim, xdim, ydim, ret;
453 float f, scale_factor;
460 if(fph==NULL || fpi==NULL || ecatfile==NULL)
return STATUS_FAULT;
468 printf(
"z_dim := %d\n", zdim);
469 printf(
"x_dim := %d\n", xdim);
470 printf(
"y_dim := %d\n", ydim);
477 scale_factor=-1; (void)sscanf(tmp,
"%f", &scale_factor);
480 printf(
"scale_factor := %g\n", scale_factor);
484 if(access(ecatfile, 0)!=-1 &&
remove(ecatfile)!=0) {
503 studynr_from_fname(ecatfile, img.
studyNr);
507 mdata=(
char*)malloc(pxlnr*
sizeof(
short int));
if(mdata==NULL) {
511 for(zi=0; zi<zdim; zi++) {
513 if((n=fread(mptr, 2, pxlnr, fpi)) < pxlnr) {
514 if(verbose==0) {fprintf(stdout,
"\n"); fflush(stdout);}
520 for(yi=0; yi<ydim; yi++)
521 for(xi=0; xi<xdim; xi++) {
523 f=(float)*si*scale_factor;
524 if(f>=0.0) img.
m[zi][yi][xi][0]=f;
else img.
m[zi][yi][xi][0]=0.0;
528 else if(verbose==0) {fprintf(stdout,
"."); fflush(stdout);}
531 if(verbose==0) {fprintf(stdout,
"\n"); fflush(stdout);}
550 float *calibration_factor
554 float f, branching_fraction=1.0;
557 if(fp==NULL)
return 1;
558 if(img==NULL)
return 2;
563 n=-1; (void)sscanf(tmp,
"%d", &n);
if(n<0)
return 11;
569 f=-1; (void)sscanf(tmp,
"%f", &f);
if(f<0)
return 11;
575 f=-1; (void)sscanf(tmp,
"%f", &f);
if(f<0)
return 12;
581 f=-1; (void)sscanf(tmp,
"%f", &f);
if(f<0)
return 13;
587 f=-1; (void)sscanf(tmp,
"%f", &f);
if(f<0)
return 14;
591 f=-1; (void)sscanf(tmp,
"%f", &f);
if(f>0) img->
sizez=10.0*f;
597 f=-1; (void)sscanf(tmp,
"%f", &f);
if(f<0)
return 15;
604 f=-1; (void)sscanf(tmp,
"%f", &f);
if(f<0)
return 16;
605 branching_fraction=f;
611 n=-1; (void)sscanf(tmp,
"%d", &n);
if(n<0)
return 17;
618 n=-1; (void)sscanf(tmp,
"%d", &n);
if(n<0)
return 18;
629 if(calibration_factor!=NULL &&
632 f=-1; (void)sscanf(tmp,
"%f", &f);
if(f<=0.0)
return 19;
633 *calibration_factor=f;
634 if(branching_fraction>0.0) *calibration_factor/=branching_fraction;
640 f=-1; (void)sscanf(tmp,
"%f", &f);
if(f<0)
return 20;
666 if(fp==NULL)
return 1;
667 if(img==NULL)
return 2;
668 if(frame_index<0)
return 3;
674 sprintf(tmp,
"frame %d", frame_index);
679 n=-1; (void)sscanf(tmp,
"%d", &n);
if(n<0)
return 11;
684 n=-1; (void)sscanf(tmp,
"%d", &n);
if(n<0)
return 12;
690 f=-1; (void)sscanf(tmp,
"%f", &f);
if(f<0)
return 13;
695 f=-1; (void)sscanf(tmp,
"%f", &f);
if(f<0)
return 14;
717 if(fp==NULL)
return 1;
718 if(sif==NULL)
return 2;
724 n=-1; (void)sscanf(tmp,
"%d", &n);
if(n<1)
return 11;
727 ret=
sifSetmem(sif, n);
if(ret!=0)
return 4;
741 for(i=0; i<sif->
frameNr; i++) {
743 sprintf(tmp,
"frame %d", i);
747 n=-1; (void)sscanf(tmp,
"%d", &n);
if(n<0)
return 22;
751 n=-1; (void)sscanf(tmp,
"%d", &n);
if(n<0)
return 23;
752 sif->
x2[i]=sif->
x1[i]+n;
755 n=-1; (void)sscanf(tmp,
"%s %s %d", tmp2, tmp3, &n);
if(n<0)
return 24;
759 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)